coperation_statistics_service.go 6.4 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"
)

const (
	// 合约分红列表查询
	SearchContractDividends = "SearchContractDividends"
	// 获取分红合约详情
	GetContractDividends = "GetContractDividends"
)

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

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

/*1.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.Unix() * 1000,
		}
		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 int64 `json:"createdAt"`
}

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

/*1.2 分红合约详情*/

// 分红合约搜索
//
// queryOptions 查询参数
// 按组织
// - companyId
// - orgId
// 按个人
// - userBaseId
func (svr *CooperationStatisticsService) GetContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
	// 1.合约详情
	contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
	if _, ok := queryOptions["contractId"]; !ok {
		return nil, fmt.Errorf("合约ID(contractId)不能为空")
	}
	var res = make(map[string]interface{})
	contract, err := contractRepository.FindOne(map[string]interface{}{"cooperationContractId": queryOptions["contractId"]})
	if err != nil {
		return nil, err
	}
	res["cooperationContract"] = map[string]interface{}{
		"cooperationContractDescription": contract.CooperationContractDescription,
		"cooperationContractId":          contract.CooperationContractId,
		"cooperationContractName":        contract.CooperationContractName,
		"cooperationContractNumber":      contract.CooperationContractNumber,
		"createdAt":                      contract.CreatedAt.Unix() * 1000,
		"status":                         contract.Status,
	}

	// 2.合约分红列表
	if _, ok := queryOptions["userBaseId"]; ok {

	} else if _, ok := queryOptions["orgId"]; ok {

	}
	res["dividends"] = []struct{}{}
	return res, nil
}