作者 yangfu

共创模式统计

... ... @@ -14,7 +14,7 @@ type CooperationGoodsStatisticsDto struct {
type CooperationModeStatisticsDto struct {
// 共创人数
CooperationPeople float64 `json:"cooperationPeople"`
CooperationPeople int `json:"cooperationPeople"`
// 分红预算
DividendsEstimate float64 `json:"dividendsEstimate"`
// 订单金额
... ... @@ -22,7 +22,7 @@ type CooperationModeStatisticsDto struct {
// 共创模式编号
CooperationModeNumber string `json:"cooperationModeNumber"`
// 结算金额
SettlementAmount float64 `json:"settlementAmount"`
SettlementAmount float64 `json:"settlementAmount,omitempty"`
}
type DividendStatisticsDto struct {
... ...
... ... @@ -188,6 +188,9 @@ func (dao *CooperationContractDao) Find(queryOptions map[string]interface{}) (in
if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
}
if cooperationModeNumber, ok := queryOptions["cooperationModeNumber"]; ok && cooperationModeNumber.(string) != "" {
query.Where("cooperation_mode_number = ?", cooperationModeNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query.Where("company->>'companyId' = '?'", companyId)
}
... ...
... ... @@ -2,6 +2,7 @@ package dao
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/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
... ... @@ -125,6 +126,9 @@ func (dao *DividendsEstimateDao) CountDividendsEstimateDividendsAmount(queryOpti
if _, ok := queryOptions["beginTime"]; ok {
query.Where(fmt.Sprintf("created_at>='%s' and created_at<'%s'", (queryOptions["beginTime"].(time.Time)).Format(time.RFC3339), (queryOptions["endTime"].(time.Time)).Format(time.RFC3339)))
}
if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
}
query.AllWithDeleted()
query.Where("is_canceled is null")
var dividendsAmount float64
... ...
... ... @@ -2,6 +2,7 @@ package dao
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/pg/models"
... ... @@ -86,6 +87,34 @@ func (dao *OrderGoodDao) CooperationUserModeStatistics(queryOptions map[string]i
return goods, err
}
// CalculateDividendsOrderAmount 计算分红订单金额
func (dao *DividendsOrderDao) CalculateGoodOrderAmount(queryOptions map[string]interface{}) (float64, error) {
tx := dao.transactionContext.PgTx
var orderGood = new(models.OrderGood)
query := tx.Model(orderGood)
query.ColumnExpr("sum(case when dividends_order_number is null then -order_good_amount else order_good_amount end) as order_good_amount")
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query.Where("company_id = '?'", companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query.Where("org_id = '?'", orgId)
}
if beginTime, ok := queryOptions["beginTime"]; ok {
query.Where("order_time>= ?", beginTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
query.Where("order_time< ?", endTime)
}
if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
}
err := query.Select()
if err != nil {
return 0, err
}
return orderGood.OrderGoodAmount, nil
}
func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -80,7 +80,7 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(queryOptions
// CooperationModeStatistics 企业-共创模式统计
//
// p1 p1_desc
func (ptr *CooperationStatisticsService) CooperationModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) {
func (ptr *CooperationStatisticsService) CooperationModeStatistics(queryOptions map[string]interface{}) (interface{}, error) {
orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
// 参数验证
var request = struct {
... ... @@ -100,7 +100,101 @@ func (ptr *CooperationStatisticsService) CooperationModeStatistics(queryOptions
modeStatistics = make([]*domain.CooperationModeStatisticsDto, 0)
}
return modeStatistics, nil
modeStatistics = make([]*domain.CooperationModeStatisticsDto, 0)
// 1.当前企业有效的模式(不包含禁用、删除)
cooperationModeRepository, _ := repository.NewCooperationModeRepository(ptr.transactionContext)
_, models, err := cooperationModeRepository.Find(map[string]interface{}{"companyId": request.CompanyId, "orgId": request.OrgId, "status": int32(1)})
if err != nil {
return nil, err
}
if len(models) == 0 {
return nil, nil
}
// 2.遍历模式 当前模式包含的合约列表
// 2.1 根据合约查询订单金额,已分红预算的订单
// 2.2 根据合约查询分红预算的金额
// 2.3 根据合约去重参与的共创人数
cooperationContractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext)
var totalContracts []*domain.CooperationContract
var cooperationModes = make([]map[string]interface{}, 0)
for i := range models {
_, contracts, err := cooperationContractRepository.Find(map[string]interface{}{"companyId": request.CompanyId, "orgId": request.OrgId, "cooperationModeNumber": models[i].CooperationModeNumber})
if err != nil {
return nil, err
}
result, err := ptr.cooperationModeStatistics(models[i], contracts, request.CompanyId, request.OrgId)
if err != nil {
return nil, err
}
modeStatistics = append(modeStatistics, result)
totalContracts = append(totalContracts, contracts...)
cooperationModes = append(cooperationModes, map[string]interface{}{
"cooperationModeId": models[i].CooperationModeId,
"cooperationModeName": models[i].CooperationModeName,
"cooperationModeNumber": models[i].CooperationModeNumber,
})
}
totalModeStatistics, err := ptr.cooperationModeStatistics(&domain.CooperationMode{}, totalContracts, request.CompanyId, request.OrgId)
if err != nil {
return nil, err
}
return map[string]interface{}{
"cooperationModeStatistics": modeStatistics,
"totalCooperationModeStatistics": totalModeStatistics,
"cooperationModes": cooperationModes,
}, nil
}
func (ptr *CooperationStatisticsService) cooperationModeStatistics(mode *domain.CooperationMode, contracts []*domain.CooperationContract, companyId, orgId int64) (*domain.CooperationModeStatisticsDto, error) {
initStatistic := &domain.CooperationModeStatisticsDto{
CooperationPeople: 0,
DividendsEstimate: 0,
OrderAmount: 0,
CooperationModeNumber: mode.CooperationModeNumber,
SettlementAmount: 0,
}
var err error
var undertakers []*domain.CooperationContractUndertaker
var mapUndertakers = make(map[int64]int64)
if len(contracts) == 0 {
return initStatistic, nil
}
var contractNumbers = make([]string, 0)
for i := range contracts {
contractNumbers = append(contractNumbers, contracts[i].CooperationContractNumber)
}
dividendsOrderDao, _ := dao.NewDividendsOrderDao(ptr.transactionContext)
initStatistic.OrderAmount, err = dividendsOrderDao.CalculateGoodOrderAmount(map[string]interface{}{"companyId": companyId, "orgId": orgId, "cooperationContractNumbers": contractNumbers})
if err != nil {
return initStatistic, err
}
dividendsEstimateDao, _ := dao.NewDividendsEstimateDao(ptr.transactionContext)
initStatistic.DividendsEstimate, err = dividendsEstimateDao.CountDividendsEstimateDividendsAmount(map[string]interface{}{"companyId": companyId, "orgId": orgId, "cooperationContractNumbers": contractNumbers})
if err != nil {
return initStatistic, err
}
undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)
if _, undertakers, err = undertakerRepository.Find(map[string]interface{}{}); err != nil {
return initStatistic, err
}
for i := range undertakers {
undertaker := undertakers[i]
if undertaker.Undertaker != nil && undertaker.Undertaker.UserId != 0 {
u1 := undertaker.Undertaker
mapUndertakers[u1.UserId] = u1.UserId
if u1.Referrer != nil && u1.Referrer.UserId != 0 {
mapUndertakers[u1.Referrer.UserId] = u1.Referrer.UserId
}
if u1.Salesman != nil && u1.Salesman.UserId != 0 {
mapUndertakers[u1.Salesman.UserId] = u1.Salesman.UserId
}
}
}
initStatistic.OrderAmount = utils.Round(initStatistic.OrderAmount, 1)
initStatistic.DividendsEstimate = utils.Round(initStatistic.DividendsEstimate, 1)
initStatistic.CooperationPeople = len(mapUndertakers)
return initStatistic, nil
}
// DividendsStatistics 分红统计
... ...