coperation_statistics_service.go 4.9 KB
package domain_service

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
	"time"
)

const (
	ContractDividends = "ContractDividends"
)

//  CooperationStatisticsService 共创统计服务
type CooperationStatisticsService struct {
	transactionContext *pgTransaction.TransactionContext
}

/*****   1.合约分红模块  *****/

// 分红合约搜索
//
// queryOptions 查询参数
// - pageNumber
// - pageSize
// 按组织
// - companyId
// - orgId
// 按个人
// - userBaseId
func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
	// 1.根据个人、企业查询合约列表
	var contracts []*domain.CooperationContract
	var err error
	if _, ok := queryOptions["userBaseId"]; ok {
		contracts, err = svr.getUserContracts(queryOptions)
	} else if _, ok := queryOptions["orgId"]; ok {
		contracts, err = svr.getCompanyContracts(queryOptions)
	}
	if err != nil {
		return nil, err
	}

	// 2.根据合约查询分红预算
	var numbers []string
	var results = make([]*searchContractDividendsResult, 0)
	for i := range contracts {
		item := contracts[i]
		resultItem := &searchContractDividendsResult{
			CooperationContractId:     item.CooperationContractId,
			CooperationContractName:   item.CooperationContractName,
			CooperationContractNumber: item.CooperationContractNumber,
			Status:                    item.Status,
			CreatedAt:                 item.CreatedAt,
		}
		results = append(results, resultItem)
		numbers = append(numbers, item.CooperationContractNumber)
	}
	mapEstimate, err := svr.getContractsDividendsEstimate(numbers)
	if err != nil {
		return nil, err
	}
	for i := range results {
		if v, ok := mapEstimate[results[i].CooperationContractNumber]; ok {
			results[i].DividendsAmount = v.DividendsAmount
		}
	}

	// TODO: 3.根据合约查询订单金额
	return results, nil
}

// getUserContracts  获取用户的合约列表
//
// p1 p1_desc
func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
	undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(svr.transactionContext)
	_, undertakers, err := undertakerRepository.Find(queryOptions)
	var numbers []string
	for i := range undertakers {
		numbers = append(numbers, undertakers[i].CooperationContractNumber)
	}
	if len(numbers) == 0 {
		return []*domain.CooperationContract{}, nil
	}
	queryOptions["inCooperationContractNumber"] = numbers
	contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
	// TODO:  参数查询条件
	_, contracts, err := contractRepository.Find(queryOptions)
	return contracts, err
}

// getCompanyContracts  获取组织合约列表
//
// p1 p1_desc
func (svr *CooperationStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
	contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
	// TODO:  参数查询条件
	_, contracts, err := contractRepository.Find(queryOptions)
	return contracts, err
}

// getContractsDividendsEstimate 合约分红预算
func (svr *CooperationStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
	var estimates []*domain.DividendsEstimate
	var resMap = make(map[string]*domain.DividendsEstimate)
	if len(numbers) == 0 {
		return resMap, nil
	}
	_, err := svr.transactionContext.PgDd.Query(estimates, `select cooperation_contract_number,sum(dividends_amount) dividends_amount from dividends_estimates 
where cooperation_contract_number in (?)
group by cooperation_contract_number
`, pg.In(numbers))
	if err != nil {
		return nil, err
	}

	for i := range estimates {
		resMap[estimates[i].CooperationContractNumber] = estimates[i]
	}
	return resMap, nil
}

type searchContractDividendsResult struct {
	// 共创合约ID
	CooperationContractId int64 `json:"cooperationContractId,string"`
	// 共创合约名称
	CooperationContractName string `json:"cooperationContractName"`
	// 共创合约编号
	CooperationContractNumber string `json:"cooperationContractNumber"`
	// 合约状态,1恢复,2暂停
	Status int32 `json:"cooperationContractStatus"`
	// 分红预算
	DividendsAmount float64 `json:"dividendsAmount"`
	// 分红订单金额
	DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt"`
}

func NewCooperationStatisticsService(transactionContext *pgTransaction.TransactionContext) (*CooperationStatisticsService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &CooperationStatisticsService{
			transactionContext: transactionContext,
		}, nil
	}
}