作者 yangfu

共创订单金额统计

@@ -79,7 +79,7 @@ func (dao *CreditAccountDao) CooperationUsersDividendsStatistics(queryOptions ma @@ -79,7 +79,7 @@ func (dao *CreditAccountDao) CooperationUsersDividendsStatistics(queryOptions ma
79 creditAccount := new(models.CreditAccount) 79 creditAccount := new(models.CreditAccount)
80 query := dao.transactionContext.PgTx.Model(creditAccount) 80 query := dao.transactionContext.PgTx.Model(creditAccount)
81 query.ColumnExpr(`0 cooperation_time`) 81 query.ColumnExpr(`0 cooperation_time`)
82 - query.ColumnExpr(`0 dividends_order_amount`) 82 + query.ColumnExpr(`sum(good_amount_count) dividends_order_amount`)
83 query.ColumnExpr(`sum(settlement_amount) divides_amount`) 83 query.ColumnExpr(`sum(settlement_amount) divides_amount`)
84 query.ColumnExpr(`sum((case when payment_status = 1 then actually_paid_amount else 0 end)) actually_paid_amount`) 84 query.ColumnExpr(`sum((case when payment_status = 1 then actually_paid_amount else 0 end)) actually_paid_amount`)
85 query.ColumnExpr(`max(participator->>'userId') user_id`) 85 query.ColumnExpr(`max(participator->>'userId') user_id`)
@@ -46,7 +46,7 @@ order by good_amount desc @@ -46,7 +46,7 @@ order by good_amount desc
46 func (dao *OrderGoodDao) CooperationModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) { 46 func (dao *OrderGoodDao) CooperationModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) {
47 tx := dao.transactionContext.PgTx 47 tx := dao.transactionContext.PgTx
48 var goods []*domain.CooperationModeStatisticsDto 48 var goods []*domain.CooperationModeStatisticsDto
49 - sql := fmt.Sprintf(`select count(0) cooperation_people,sum(a.actually_paid_amount) dividends_estimate,b.cooperation_mode_number,0 order_amount 49 + sql := fmt.Sprintf(`select count(0) cooperation_people,sum(a.actually_paid_amount) dividends_estimate,b.cooperation_mode_number,sum(good_amount_count) order_amount
50 from credit_accounts a inner join cooperation_contracts b on a.cooperation_contract_number = b.cooperation_contract_number 50 from credit_accounts a inner join cooperation_contracts b on a.cooperation_contract_number = b.cooperation_contract_number
51 where a.company->>'companyId' = '?' and a.org->>'orgId' = '?' and b.status = 1 51 where a.company->>'companyId' = '?' and a.org->>'orgId' = '?' and b.status = 1
52 group by b.cooperation_mode_number 52 group by b.cooperation_mode_number
@@ -222,14 +222,8 @@ func (ptr *CooperationStatisticsService) CompanyCooperationUsersStatistics(query @@ -222,14 +222,8 @@ func (ptr *CooperationStatisticsService) CompanyCooperationUsersStatistics(query
222 for i := range responses { 222 for i := range responses {
223 responses[i].CooperationTime = time.Now().Unix() * 1000 223 responses[i].CooperationTime = time.Now().Unix() * 1000
224 unPaidAmount := responses[i].DividesAmount - responses[i].ActuallyPaidAmount 224 unPaidAmount := responses[i].DividesAmount - responses[i].ActuallyPaidAmount
225 - responses[i].Participator = map[string]interface{}{  
226 - "userId": responses[i].UserId,  
227 - "userInfo": map[string]interface{}{  
228 - "userName": responses[i].UserName,  
229 - },  
230 - }  
231 retMap = append(retMap, map[string]interface{}{ 225 retMap = append(retMap, map[string]interface{}{
232 - "dividendsOrderAmount": 0, 226 + "dividendsOrderAmount": responses[i].DividendsOrderAmount,
233 "dividesAmount": responses[i].DividesAmount, 227 "dividesAmount": responses[i].DividesAmount,
234 "unPaidAmount": unPaidAmount, 228 "unPaidAmount": unPaidAmount,
235 "cooperationTime": time.Now().Unix() * 1000, 229 "cooperationTime": time.Now().Unix() * 1000,
@@ -9,6 +9,7 @@ import ( @@ -9,6 +9,7 @@ import (
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
12 "strconv" 13 "strconv"
13 "time" 14 "time"
14 ) 15 )
@@ -103,21 +104,16 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -103,21 +104,16 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
103 results = append(results, resultItem) 104 results = append(results, resultItem)
104 numbers = append(numbers, item.CooperationContractNumber) 105 numbers = append(numbers, item.CooperationContractNumber)
105 } 106 }
106 - mapEstimate, err := ptr.getContractsDividendsEstimate(numbers) 107 + mapCreditAccount, err := ptr.getContractsCreditAccount(numbers)
107 if err != nil { 108 if err != nil {
108 return nil, err 109 return nil, err
109 } 110 }
110 for i := range results { 111 for i := range results {
111 - if v, ok := mapEstimate[results[i].CooperationContractNumber]; ok {  
112 - results[i].DividendsAmount = v.DividendsAmount 112 + if v, ok := mapCreditAccount[results[i].CooperationContractNumber]; ok {
  113 + results[i].DividendsAmount = v.SettlementAmount
  114 + results[i].DividendsOrderAmount = v.GoodAmountCount
113 } 115 }
114 } 116 }
115 -  
116 - // TODO: 3.根据合约查询订单金额  
117 - //for i:=range results{  
118 - // queryOptions["cooperationContractNumber"] =results[i].CooperationContractNumber  
119 - // results[i].DividendsOrderAmount = ptr.CalculateOrderAmount(queryOptions)  
120 - //}  
121 return results, nil 117 return results, nil
122 } 118 }
123 119
@@ -171,6 +167,33 @@ group by cooperation_contract_number @@ -171,6 +167,33 @@ group by cooperation_contract_number
171 return resMap, nil 167 return resMap, nil
172 } 168 }
173 169
  170 +// getContractsDividendsEstimate 合约分红预算
  171 +func (ptr *CooperationStatisticsService) getContractsCreditAccount(numbers []string) (map[string]*domain.CreditAccount, error) {
  172 + var creditAccounts []*domain.CreditAccount
  173 + var resMap = make(map[string]*domain.CreditAccount)
  174 + if len(numbers) == 0 {
  175 + return resMap, nil
  176 + }
  177 + var creditAccount = new(domain.CreditAccount)
  178 +
  179 + query := ptr.transactionContext.PgDd.Model(creditAccount)
  180 + query.Column("cooperation_contract_number")
  181 + query.ColumnExpr("sum(good_amount_count) good_amount_count")
  182 + query.ColumnExpr("sum(settlement_amount) settlement_amount")
  183 + query.Where("cooperation_contract_number in (?)", pg.In(numbers))
  184 + query.Where("deleted_at is null")
  185 + query.Group("cooperation_contract_number")
  186 + err := query.Select(&creditAccounts)
  187 + if err != nil {
  188 + return nil, err
  189 + }
  190 +
  191 + for i := range creditAccounts {
  192 + resMap[creditAccounts[i].CooperationContractNumber] = creditAccounts[i]
  193 + }
  194 + return resMap, nil
  195 +}
  196 +
174 type searchContractDividendsResult struct { 197 type searchContractDividendsResult struct {
175 // 共创合约ID 198 // 共创合约ID
176 CooperationContractId int64 `json:"cooperationContractId,string"` 199 CooperationContractId int64 `json:"cooperationContractId,string"`
@@ -220,17 +243,40 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s @@ -220,17 +243,40 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s
220 if err != nil { 243 if err != nil {
221 return res, err 244 return res, err
222 } 245 }
  246 +
  247 + // 3.取订单商品列表
  248 + var orderGoodIds []int64
  249 + for i := range creditAccounts {
  250 + a := creditAccounts[i]
  251 + for j := range a.AccountDetail {
  252 + if a.AccountDetail[j].OrderGoodId > 0 {
  253 + orderGoodIds = append(orderGoodIds, a.AccountDetail[j].OrderGoodId)
  254 + }
  255 + }
  256 + }
  257 +
  258 + orderGoodRepository, _ := repository.NewOrderGoodRepository(ptr.transactionContext)
  259 + _, orderGoods, err := orderGoodRepository.Find(map[string]interface{}{"orderGoodIds": orderGoodIds})
  260 + if err != nil {
  261 + return res, err
  262 + }
  263 + var mapOrderGoods = make(map[int64]*domain.OrderGood)
  264 + for i := range orderGoods {
  265 + mapOrderGoods[orderGoods[i].OrderGoodId] = orderGoods[i]
  266 + }
  267 +
223 var dividends = make([]interface{}, 0) 268 var dividends = make([]interface{}, 0)
224 for i := range creditAccounts { 269 for i := range creditAccounts {
225 a := creditAccounts[i] 270 a := creditAccounts[i]
226 for j := range a.AccountDetail { 271 for j := range a.AccountDetail {
227 participateType, _ := strconv.Atoi(a.ParticipateType) 272 participateType, _ := strconv.Atoi(a.ParticipateType)
  273 + detail := a.AccountDetail[j]
228 item := map[string]interface{}{ 274 item := map[string]interface{}{
229 "creditAccountId": a.CreditAccountId, 275 "creditAccountId": a.CreditAccountId,
230 "orderGoodName": "", 276 "orderGoodName": "",
231 "dividendsType": 0, 277 "dividendsType": 0,
232 "dividendsRatio": 0, 278 "dividendsRatio": 0,
233 - "dividendsAmount": a.AccountDetail[j].DividendsAmount, 279 + "dividendsAmount": detail.DividendsAmount,
234 "dividendsUser": map[string]interface{}{ 280 "dividendsUser": map[string]interface{}{
235 "userInfo": a.Participator.UserInfo, 281 "userInfo": a.Participator.UserInfo,
236 "userId": a.Participator.UserId, 282 "userId": a.Participator.UserId,
@@ -240,7 +286,14 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s @@ -240,7 +286,14 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s
240 "dividendsEstimateTime": a.CreatedAt.Unix() * 1000, 286 "dividendsEstimateTime": a.CreatedAt.Unix() * 1000,
241 "orderOrReturnedOrderNum": a.CreditAccountOrderNum, 287 "orderOrReturnedOrderNum": a.CreditAccountOrderNum,
242 } 288 }
  289 + if detail.OrderGoodAmount > 0 {
  290 + item["dividendsRatio"] = utils.Round(detail.DividendsAmount/detail.OrderGoodAmount*100, 2)
  291 + }
243 item["dividendsType"] = a.AccountDetail[j].DividendsType 292 item["dividendsType"] = a.AccountDetail[j].DividendsType
  293 + if v, ok := mapOrderGoods[detail.OrderGoodId]; ok {
  294 + item["orderGoodName"] = v.OrderGoodName
  295 + }
  296 +
244 dividends = append(dividends, item) 297 dividends = append(dividends, item)
245 } 298 }
246 299