pg_cooperation_person_statistics_service.go 3.4 KB
package domain_service

import (
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
)

// 共创公司统计
func (ptr *CooperationStatisticsService) CooperationCompanyStatistics(queryOptions map[string]interface{}) (interface{}, error) {
	// 参数验证
	var request = struct {
		UserBaseId  int64   `json:"userBaseId" valid:"Required"`
		CompanyList []int64 `json:"companyList" valid:"Required"` //公司列表 待统计的公司列表
	}{}
	if err := LoadQueryObject(queryOptions, &request); err != nil {
		return nil, err
	}
	queryOptions = tool_funs.SimpleStructToMap(&request)

	var response = make([]*cooperationCompanyStatisticsResponse, 0)
	var totalDividend float64
	for i := range request.CompanyList {
		orgId := request.CompanyList[i]
		item, err := ptr.cooperationCompanyStatistics(request.UserBaseId, orgId)
		if err != nil {
			return response, err
		}
		totalDividend += item.DividendsIncome
		response = append(response, item)
	}
	if totalDividend > 0 {
		for i := range response {
			response[i].DividendsRatio = utils.Round(response[i].DividendsIncome/totalDividend*100, 2)
		}
	}
	return response, nil
}

func (ptr *CooperationStatisticsService) cooperationCompanyStatistics(userBaseId int64, orgId int64) (*cooperationCompanyStatisticsResponse, error) {
	// 1.相关项目统计
	cooperationApplicationRepository, _ := repository.NewCooperationApplicationRepository(ptr.transactionContext)
	cooperationProjectCount, _, err := cooperationApplicationRepository.Find(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId,
		"limit": 1, "cooperationApplicationStatus": int32(2)})
	if err != nil {
		return nil, err
	}

	// 2.相关合约统计
	cooperationContractRelevantRepository, _ := repository.NewCooperationContractRelevantRepository(ptr.transactionContext)
	cooperationContractCount, _, err := cooperationContractRelevantRepository.Find(map[string]interface{}{"userBaseId": userBaseId,
		"limit": 1})
	if err != nil {
		return nil, err
	}

	// 3.个人分红统计
	type response struct {
		Total      float64 `json:"total"`
		Accounting float64 `json:"accounting"`
		Accounted  float64 `json:"accounted"`
		Paid       float64 `json:"paid"`
	}
	creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
	var allDividends = &response{}
	if err := creditAccountDao.DividendsStatistics(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId}, allDividends); err != nil {
		return nil, err
	}
	allDividends.Accounting = allDividends.Total - allDividends.Accounted

	var ret = &cooperationCompanyStatisticsResponse{
		OrgId:                    orgId,
		CooperationProjectCount:  cooperationProjectCount,
		CooperationContractCount: cooperationContractCount,
		DividendsIncome:          utils.Round(allDividends.Total, 2),
	}
	return ret, nil
}

type cooperationCompanyStatisticsResponse struct {
	// 当天统计的企业id
	OrgId int64 `json:"orgId"`
	// 共创项目数
	CooperationProjectCount int64 `json:"cooperationProjectCount"`
	// 共创合约数
	CooperationContractCount int64 `json:"cooperationContractCount"`
	// 分红占比
	DividendsRatio float64 `json:"dividendsRatio"`
	// 分红支出
	DividendsIncome float64 `json:"dividendsIncome"`
}