pg_cooperation_person_statistics_service.go 7.2 KB
package domain_service

import (
	"github.com/go-pg/pg/v10"
	"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/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
	"time"
)

// 共创公司统计
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, _ := dao.NewCooperationContractUndertakerDao(ptr.transactionContext)
	cooperationContractCount, _, err := cooperationContractRelevantRepository.Find(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId,
		"limit": 1})
	if err != nil {
		return nil, err
	}

	// 3.个人分红统计
	//creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
	//var allDividends = &CreditAccountStatisticsResponse{}
	//if err := creditAccountDao.DividendsStatistics(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId}, allDividends); err != nil {
	//	return nil, err
	//}
	//allDividends.Accounting = allDividends.Total - allDividends.Accounted

	creditAccountDao, _ := dao.NewDividendsEstimateDao(ptr.transactionContext)
	var allDividends float64
	if allDividends, err = creditAccountDao.CountDividendsEstimateDividendsAmount(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId}); err != nil {
		return nil, err
	}
	//allDividends.Accounting = allDividends.Total - allDividends.Accounted

	var ret = &cooperationCompanyStatisticsResponse{
		OrgId:                    orgId,
		CooperationProjectCount:  cooperationProjectCount,
		CooperationContractCount: cooperationContractCount,
		DividendsIncome:          utils.Round(allDividends, 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"`
}

// 个人-用户合约统计
func (ptr *CooperationStatisticsService) PersonCooperationContractStatistics(queryOptions map[string]interface{}) (interface{}, error) {
	// 参数验证
	var request = struct {
		UserBaseId int64 `json:"userBaseId" valid:"Required"`
	}{}
	if err := LoadQueryObject(queryOptions, &request); err != nil {
		return nil, err
	}
	queryOptions = tool_funs.SimpleStructToMap(&request)
	queryOptions["limit"] = 10000 //TODO:合约数大于10000时?
	cooperationContractUndertaker, _ := dao.NewCooperationContractUndertakerDao(ptr.transactionContext)
	_, contractUndertakers, err := cooperationContractUndertaker.Find(queryOptions)
	if err != nil {
		return nil, nil
	}

	var response = struct {
		ContractSum        int `json:"contractSum"`
		ContractStoppedSum int `json:"contractStoppedSum"`
	}{}
	if len(contractUndertakers) == 0 {
		return response, nil
	}
	var contractIds []int64
	for i := range contractUndertakers {
		contractIds = append(contractIds, contractUndertakers[i].CooperationContractId)
	}
	response.ContractSum = ptr.cooperationContractCount(contractIds, 0)
	response.ContractStoppedSum = ptr.cooperationContractCount(contractIds, 2)
	return response, nil
}

func (ptr *CooperationStatisticsService) cooperationContractCount(contractIds []int64, status int) int {
	var total int
	var contract = new(models.CooperationContract)
	query := ptr.transactionContext.PgDd.Model(contract)
	query.ColumnExpr("count(*) total")
	query.Where("cooperation_contract_id in (?)", pg.In(contractIds))
	if status > 0 {
		query.Where("status =? ", status)
	}
	query.Select(&total)
	return total
}

// 个人 - 企业支付统计
func (ptr *CooperationStatisticsService) PersonCompanyPaymentHistoryStatistics(queryOptions map[string]interface{}) (interface{}, error) {
	// 参数验证
	var request = struct {
		Limit                    int       `json:"limit" valid:"Required"`
		Offset                   int       `json:"offset"`
		UserBaseId               int64     `json:"userBaseId" valid:"Required"`
		OrgId                    int64     `json:"orgId" valid:"Required"`
		SortByActuallyPaidAmount int       `json:"sortByActuallyPaidAmount" valid:"Required"`
		BeginTime                time.Time `json:"beginTime"`
		EndTime                  time.Time `json:"endTime"`
	}{}
	if err := LoadQueryObject(queryOptions, &request); err != nil {
		return nil, err
	}
	queryOptions = tool_funs.SimpleStructToMap(&request)

	type companyStatisticsResponse struct {
		CooperationTime      int64   `json:"cooperationTime"`
		DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
		DividesAmount        float64 `json:"dividesAmount"`      // 分红金额
		ActuallyPaidAmount   float64 `json:"actuallyPaidAmount"` // 已支付金额
		UnPaidAmount         float64 `json:"unPaidAmount"`       // 未支付金额
		OrgId                int64   `json:"orgId,string"`
		OrgName              string  `json:"orgName"`

		Participator interface{} `json:"participator"`
	}

	var responses []companyStatisticsResponse
	creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
	if err := creditAccountDao.CooperationCompanyDividendsStatistics(queryOptions, &responses); err != nil {
		return nil, err
	}

	var retMap = make([]interface{}, 0)
	for i := range responses {
		retMap = append(retMap, map[string]interface{}{
			"paymentAmount": responses[i].ActuallyPaidAmount,
			"org": map[string]interface{}{
				"orgId":   responses[i].OrgId,
				"orgName": responses[i].OrgName,
			},
		})
	}

	return retMap, nil
}