作者 yangfu

共创模式统计

@@ -14,7 +14,7 @@ type CooperationGoodsStatisticsDto struct { @@ -14,7 +14,7 @@ type CooperationGoodsStatisticsDto struct {
14 14
15 type CooperationModeStatisticsDto struct { 15 type CooperationModeStatisticsDto struct {
16 // 共创人数 16 // 共创人数
17 - CooperationPeople float64 `json:"cooperationPeople"` 17 + CooperationPeople int `json:"cooperationPeople"`
18 // 分红预算 18 // 分红预算
19 DividendsEstimate float64 `json:"dividendsEstimate"` 19 DividendsEstimate float64 `json:"dividendsEstimate"`
20 // 订单金额 20 // 订单金额
@@ -22,7 +22,7 @@ type CooperationModeStatisticsDto struct { @@ -22,7 +22,7 @@ type CooperationModeStatisticsDto struct {
22 // 共创模式编号 22 // 共创模式编号
23 CooperationModeNumber string `json:"cooperationModeNumber"` 23 CooperationModeNumber string `json:"cooperationModeNumber"`
24 // 结算金额 24 // 结算金额
25 - SettlementAmount float64 `json:"settlementAmount"` 25 + SettlementAmount float64 `json:"settlementAmount,omitempty"`
26 } 26 }
27 27
28 type DividendStatisticsDto struct { 28 type DividendStatisticsDto struct {
@@ -188,6 +188,9 @@ func (dao *CooperationContractDao) Find(queryOptions map[string]interface{}) (in @@ -188,6 +188,9 @@ func (dao *CooperationContractDao) Find(queryOptions map[string]interface{}) (in
188 if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 { 188 if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
189 query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers)) 189 query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
190 } 190 }
  191 + if cooperationModeNumber, ok := queryOptions["cooperationModeNumber"]; ok && cooperationModeNumber.(string) != "" {
  192 + query.Where("cooperation_mode_number = ?", cooperationModeNumber)
  193 + }
191 if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 { 194 if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
192 query.Where("company->>'companyId' = '?'", companyId) 195 query.Where("company->>'companyId' = '?'", companyId)
193 } 196 }
@@ -2,6 +2,7 @@ package dao @@ -2,6 +2,7 @@ package dao
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/go-pg/pg/v10"
5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
@@ -125,6 +126,9 @@ func (dao *DividendsEstimateDao) CountDividendsEstimateDividendsAmount(queryOpti @@ -125,6 +126,9 @@ func (dao *DividendsEstimateDao) CountDividendsEstimateDividendsAmount(queryOpti
125 if _, ok := queryOptions["beginTime"]; ok { 126 if _, ok := queryOptions["beginTime"]; ok {
126 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))) 127 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)))
127 } 128 }
  129 + if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
  130 + query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
  131 + }
128 query.AllWithDeleted() 132 query.AllWithDeleted()
129 query.Where("is_canceled is null") 133 query.Where("is_canceled is null")
130 var dividendsAmount float64 134 var dividendsAmount float64
@@ -2,6 +2,7 @@ package dao @@ -2,6 +2,7 @@ package dao
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/go-pg/pg/v10"
5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
@@ -86,6 +87,34 @@ func (dao *OrderGoodDao) CooperationUserModeStatistics(queryOptions map[string]i @@ -86,6 +87,34 @@ func (dao *OrderGoodDao) CooperationUserModeStatistics(queryOptions map[string]i
86 return goods, err 87 return goods, err
87 } 88 }
88 89
  90 +// CalculateDividendsOrderAmount 计算分红订单金额
  91 +func (dao *DividendsOrderDao) CalculateGoodOrderAmount(queryOptions map[string]interface{}) (float64, error) {
  92 + tx := dao.transactionContext.PgTx
  93 + var orderGood = new(models.OrderGood)
  94 + query := tx.Model(orderGood)
  95 + query.ColumnExpr("sum(case when dividends_order_number is null then -order_good_amount else order_good_amount end) as order_good_amount")
  96 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  97 + query.Where("company_id = '?'", companyId)
  98 + }
  99 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  100 + query.Where("org_id = '?'", orgId)
  101 + }
  102 + if beginTime, ok := queryOptions["beginTime"]; ok {
  103 + query.Where("order_time>= ?", beginTime)
  104 + }
  105 + if endTime, ok := queryOptions["endTime"]; ok {
  106 + query.Where("order_time< ?", endTime)
  107 + }
  108 + if cooperationContractNumbers, ok := queryOptions["cooperationContractNumbers"]; ok && len(cooperationContractNumbers.([]string)) != 0 {
  109 + query.Where("cooperation_contract_number in (?)", pg.In(cooperationContractNumbers))
  110 + }
  111 + err := query.Select()
  112 + if err != nil {
  113 + return 0, err
  114 + }
  115 + return orderGood.OrderGoodAmount, nil
  116 +}
  117 +
89 func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) { 118 func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) {
90 if transactionContext == nil { 119 if transactionContext == nil {
91 return nil, fmt.Errorf("transactionContext参数不能为nil") 120 return nil, fmt.Errorf("transactionContext参数不能为nil")
@@ -80,7 +80,7 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(queryOptions @@ -80,7 +80,7 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(queryOptions
80 // CooperationModeStatistics 企业-共创模式统计 80 // CooperationModeStatistics 企业-共创模式统计
81 // 81 //
82 // p1 p1_desc 82 // p1 p1_desc
83 -func (ptr *CooperationStatisticsService) CooperationModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) { 83 +func (ptr *CooperationStatisticsService) CooperationModeStatistics(queryOptions map[string]interface{}) (interface{}, error) {
84 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) 84 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
85 // 参数验证 85 // 参数验证
86 var request = struct { 86 var request = struct {
@@ -100,7 +100,101 @@ func (ptr *CooperationStatisticsService) CooperationModeStatistics(queryOptions @@ -100,7 +100,101 @@ func (ptr *CooperationStatisticsService) CooperationModeStatistics(queryOptions
100 modeStatistics = make([]*domain.CooperationModeStatisticsDto, 0) 100 modeStatistics = make([]*domain.CooperationModeStatisticsDto, 0)
101 } 101 }
102 102
103 - return modeStatistics, nil 103 + modeStatistics = make([]*domain.CooperationModeStatisticsDto, 0)
  104 + // 1.当前企业有效的模式(不包含禁用、删除)
  105 + cooperationModeRepository, _ := repository.NewCooperationModeRepository(ptr.transactionContext)
  106 + _, models, err := cooperationModeRepository.Find(map[string]interface{}{"companyId": request.CompanyId, "orgId": request.OrgId, "status": int32(1)})
  107 + if err != nil {
  108 + return nil, err
  109 + }
  110 + if len(models) == 0 {
  111 + return nil, nil
  112 + }
  113 +
  114 + // 2.遍历模式 当前模式包含的合约列表
  115 + // 2.1 根据合约查询订单金额,已分红预算的订单
  116 + // 2.2 根据合约查询分红预算的金额
  117 + // 2.3 根据合约去重参与的共创人数
  118 + cooperationContractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext)
  119 + var totalContracts []*domain.CooperationContract
  120 + var cooperationModes = make([]map[string]interface{}, 0)
  121 + for i := range models {
  122 + _, contracts, err := cooperationContractRepository.Find(map[string]interface{}{"companyId": request.CompanyId, "orgId": request.OrgId, "cooperationModeNumber": models[i].CooperationModeNumber})
  123 + if err != nil {
  124 + return nil, err
  125 + }
  126 + result, err := ptr.cooperationModeStatistics(models[i], contracts, request.CompanyId, request.OrgId)
  127 + if err != nil {
  128 + return nil, err
  129 + }
  130 + modeStatistics = append(modeStatistics, result)
  131 + totalContracts = append(totalContracts, contracts...)
  132 + cooperationModes = append(cooperationModes, map[string]interface{}{
  133 + "cooperationModeId": models[i].CooperationModeId,
  134 + "cooperationModeName": models[i].CooperationModeName,
  135 + "cooperationModeNumber": models[i].CooperationModeNumber,
  136 + })
  137 + }
  138 + totalModeStatistics, err := ptr.cooperationModeStatistics(&domain.CooperationMode{}, totalContracts, request.CompanyId, request.OrgId)
  139 + if err != nil {
  140 + return nil, err
  141 + }
  142 + return map[string]interface{}{
  143 + "cooperationModeStatistics": modeStatistics,
  144 + "totalCooperationModeStatistics": totalModeStatistics,
  145 + "cooperationModes": cooperationModes,
  146 + }, nil
  147 +}
  148 +
  149 +func (ptr *CooperationStatisticsService) cooperationModeStatistics(mode *domain.CooperationMode, contracts []*domain.CooperationContract, companyId, orgId int64) (*domain.CooperationModeStatisticsDto, error) {
  150 + initStatistic := &domain.CooperationModeStatisticsDto{
  151 + CooperationPeople: 0,
  152 + DividendsEstimate: 0,
  153 + OrderAmount: 0,
  154 + CooperationModeNumber: mode.CooperationModeNumber,
  155 + SettlementAmount: 0,
  156 + }
  157 + var err error
  158 + var undertakers []*domain.CooperationContractUndertaker
  159 + var mapUndertakers = make(map[int64]int64)
  160 + if len(contracts) == 0 {
  161 + return initStatistic, nil
  162 + }
  163 + var contractNumbers = make([]string, 0)
  164 + for i := range contracts {
  165 + contractNumbers = append(contractNumbers, contracts[i].CooperationContractNumber)
  166 + }
  167 + dividendsOrderDao, _ := dao.NewDividendsOrderDao(ptr.transactionContext)
  168 + initStatistic.OrderAmount, err = dividendsOrderDao.CalculateGoodOrderAmount(map[string]interface{}{"companyId": companyId, "orgId": orgId, "cooperationContractNumbers": contractNumbers})
  169 + if err != nil {
  170 + return initStatistic, err
  171 + }
  172 + dividendsEstimateDao, _ := dao.NewDividendsEstimateDao(ptr.transactionContext)
  173 + initStatistic.DividendsEstimate, err = dividendsEstimateDao.CountDividendsEstimateDividendsAmount(map[string]interface{}{"companyId": companyId, "orgId": orgId, "cooperationContractNumbers": contractNumbers})
  174 + if err != nil {
  175 + return initStatistic, err
  176 + }
  177 + undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)
  178 + if _, undertakers, err = undertakerRepository.Find(map[string]interface{}{}); err != nil {
  179 + return initStatistic, err
  180 + }
  181 + for i := range undertakers {
  182 + undertaker := undertakers[i]
  183 + if undertaker.Undertaker != nil && undertaker.Undertaker.UserId != 0 {
  184 + u1 := undertaker.Undertaker
  185 + mapUndertakers[u1.UserId] = u1.UserId
  186 + if u1.Referrer != nil && u1.Referrer.UserId != 0 {
  187 + mapUndertakers[u1.Referrer.UserId] = u1.Referrer.UserId
  188 + }
  189 + if u1.Salesman != nil && u1.Salesman.UserId != 0 {
  190 + mapUndertakers[u1.Salesman.UserId] = u1.Salesman.UserId
  191 + }
  192 + }
  193 + }
  194 + initStatistic.OrderAmount = utils.Round(initStatistic.OrderAmount, 1)
  195 + initStatistic.DividendsEstimate = utils.Round(initStatistic.DividendsEstimate, 1)
  196 + initStatistic.CooperationPeople = len(mapUndertakers)
  197 + return initStatistic, nil
104 } 198 }
105 199
106 // DividendsStatistics 分红统计 200 // DividendsStatistics 分红统计