作者 yangfu

统计修改

1 package domain 1 package domain
2 2
3 import ( 3 import (
  4 + "bytes"
4 "fmt" 5 "fmt"
5 - "github.com/linmadan/egglib-go/utils/json"  
6 "time" 6 "time"
7 ) 7 )
8 8
@@ -89,11 +89,20 @@ func (creditAccount *CreditAccount) Update(data map[string]interface{}) error { @@ -89,11 +89,20 @@ func (creditAccount *CreditAccount) Update(data map[string]interface{}) error {
89 } 89 }
90 90
91 func ConditionInContractNumbers(numbers []string) string { 91 func ConditionInContractNumbers(numbers []string) string {
92 - var response = make([]map[string]string, 0) 92 + var sqlBuilder = bytes.NewBuffer(nil)
  93 + l := len(numbers)
93 for i := range numbers { 94 for i := range numbers {
94 - response = append(response, map[string]string{  
95 - "cooperationContractNumber": numbers[i],  
96 - }) 95 + sqlBuilder.WriteString(fmt.Sprintf(`account_detail @> '[{"cooperationContractNumber":"%v"}]' `, numbers[i]))
  96 + if i < (l - 1) {
  97 + sqlBuilder.WriteString(" OR ")
97 } 98 }
98 - return fmt.Sprintf("account_detail <@'%v'", json.MarshalToString(response)) 99 + }
  100 + return sqlBuilder.String()
  101 + //var response = make([]map[string]string, 0)
  102 + //for i := range numbers {
  103 + // response = append(response, map[string]string{
  104 + // "cooperationContractNumber": numbers[i],
  105 + // })
  106 + //}
  107 + //return fmt.Sprintf("account_detail <@'%v'", json.MarshalToString(response))
99 } 108 }
@@ -84,6 +84,9 @@ func (dao *DividendsOrderDao) CalculateDividendsOrderAmount(queryOptions map[str @@ -84,6 +84,9 @@ func (dao *DividendsOrderDao) CalculateDividendsOrderAmount(queryOptions map[str
84 if endTime, ok := queryOptions["endTime"]; ok { 84 if endTime, ok := queryOptions["endTime"]; ok {
85 query.Where("order_time< ?", endTime) 85 query.Where("order_time< ?", endTime)
86 } 86 }
  87 + if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
  88 + query.Where("cooperation_contract_number = ?", cooperationContractNumber)
  89 + }
87 err := query.Select() 90 err := query.Select()
88 if err != nil { 91 if err != nil {
89 return 0, err 92 return 0, err
@@ -65,23 +65,24 @@ group by b.cooperation_mode_number @@ -65,23 +65,24 @@ group by b.cooperation_mode_number
65 func (dao *OrderGoodDao) CooperationUserModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) { 65 func (dao *OrderGoodDao) CooperationUserModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) {
66 tx := dao.transactionContext.PgTx 66 tx := dao.transactionContext.PgTx
67 var goods []*domain.CooperationModeStatisticsDto 67 var goods []*domain.CooperationModeStatisticsDto
68 - creditAccount := new(models.CreditAccount) 68 + creditAccount := new(models.DividendsEstimate)
69 query := tx.Model(creditAccount) 69 query := tx.Model(creditAccount)
70 query.ColumnExpr("count(0) cooperation_people") 70 query.ColumnExpr("count(0) cooperation_people")
71 - query.ColumnExpr("sum(actually_paid_amount) dividends_estimate")  
72 - query.ColumnExpr("sum(good_amount_count) order_amount")  
73 - query.ColumnExpr("sum(actually_paid_amount) settlement_amount") 71 + query.ColumnExpr("sum(dividends_amount) dividends_estimate")
  72 + //query.ColumnExpr("sum(good_amount_count) order_amount")
  73 + //query.ColumnExpr("sum(actually_paid_amount) settlement_amount")
74 query.ColumnExpr("a.cooperation_mode_number cooperation_mode_number") 74 query.ColumnExpr("a.cooperation_mode_number cooperation_mode_number")
75 - query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = credit_account.cooperation_contract_number") 75 + query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = dividends_estimate.cooperation_contract_number")
76 //query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = credit_account.cooperation_contract_number") 76 //query.Join("inner join cooperation_contracts as a").JoinOn("a.cooperation_contract_number = credit_account.cooperation_contract_number")
77 if v, ok := queryOptions["userId"]; ok && v.(int64) > 0 { 77 if v, ok := queryOptions["userId"]; ok && v.(int64) > 0 {
78 - query.Where(fmt.Sprintf(`credit_account.participator->>'userId'='%v' `, v)) 78 + query.Where(fmt.Sprintf(`dividends_estimate.dividends_user->>'userId'='%v' `, v))
79 } 79 }
80 if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 { 80 if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
81 - query.Where(fmt.Sprintf(` "credit_account".org->>'orgId'= '%v'`, v)) 81 + query.Where(fmt.Sprintf(` "dividends_estimate".org->>'orgId'= '%v'`, v))
82 query.Where(fmt.Sprintf(` a.org->>'orgId'= '%v'`, v)) 82 query.Where(fmt.Sprintf(` a.org->>'orgId'= '%v'`, v))
83 } 83 }
84 - query.Where("credit_account.deleted_at is null") 84 + query.Where("dividends_estimate.is_canceled is null")
  85 + query.Where("dividends_estimate.deleted_at is null")
85 query.Group("cooperation_mode_number") 86 query.Group("cooperation_mode_number")
86 err := query.Select(&goods) 87 err := query.Select(&goods)
87 return goods, err 88 return goods, err
@@ -392,10 +392,10 @@ func (ptr *CooperationStatisticsService) CooperationUserModeStatistics(queryOpti @@ -392,10 +392,10 @@ func (ptr *CooperationStatisticsService) CooperationUserModeStatistics(queryOpti
392 m := cooperModes[i] 392 m := cooperModes[i]
393 if modeStatistics, ok := mapModeStatistics[m.CooperationModeNumber]; ok { 393 if modeStatistics, ok := mapModeStatistics[m.CooperationModeNumber]; ok {
394 totalOrderAmount += modeStatistics.OrderAmount 394 totalOrderAmount += modeStatistics.OrderAmount
395 - totalDividendAmount += modeStatistics.SettlementAmount 395 + totalDividendAmount += modeStatistics.DividendsEstimate
396 dividendsExpenseByTypes = append(dividendsExpenseByTypes, map[string]interface{}{ 396 dividendsExpenseByTypes = append(dividendsExpenseByTypes, map[string]interface{}{
397 "dividendsTypeName": m.CooperationModeName + "分红支出", 397 "dividendsTypeName": m.CooperationModeName + "分红支出",
398 - "dividendsExpense": modeStatistics.SettlementAmount, //实际已支付金额 398 + "dividendsExpense": modeStatistics.DividendsEstimate, //实际已支付金额
399 }) 399 })
400 orderAmountByTypes = append(orderAmountByTypes, map[string]interface{}{ 400 orderAmountByTypes = append(orderAmountByTypes, map[string]interface{}{
401 "orderAmount": modeStatistics.OrderAmount, 401 "orderAmount": modeStatistics.OrderAmount,
@@ -93,7 +93,7 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -93,7 +93,7 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
93 var contracts []*domain.CooperationContract 93 var contracts []*domain.CooperationContract
94 var err error 94 var err error
95 if request.UserBaseId > 0 { 95 if request.UserBaseId > 0 {
96 - contracts, err = ptr.getUserContracts(request.UserBaseId, queryOptions) 96 + contracts, err = ptr.getUserContracts(request.OrgId, request.UserBaseId, queryOptions)
97 } else if request.OrgId > 0 { 97 } else if request.OrgId > 0 {
98 queryOptions["orgId"] = request.OrgId 98 queryOptions["orgId"] = request.OrgId
99 contracts, err = ptr.getCompanyContracts(request.OrgId, request.UserId, queryOptions) 99 contracts, err = ptr.getCompanyContracts(request.OrgId, request.UserId, queryOptions)
@@ -103,6 +103,8 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -103,6 +103,8 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
103 } 103 }
104 104
105 // 2.根据合约查询分红预算 105 // 2.根据合约查询分红预算
  106 + orderGoodDao, _ := dao.NewDividendsOrderDao(ptr.transactionContext)
  107 + dividendsEstimateDao, _ := dao.NewDividendsEstimateDao(ptr.transactionContext)
106 var numbers []string 108 var numbers []string
107 var results = make([]*searchContractDividendsResult, 0) 109 var results = make([]*searchContractDividendsResult, 0)
108 for i := range contracts { 110 for i := range contracts {
@@ -114,31 +116,34 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -114,31 +116,34 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
114 Status: item.Status, 116 Status: item.Status,
115 CreatedAt: item.CreatedAt.Unix() * 1000, 117 CreatedAt: item.CreatedAt.Unix() * 1000,
116 } 118 }
  119 + resultItem.DividendsOrderAmount, _ = orderGoodDao.CalculateGoodOrderAmount(map[string]interface{}{"orgId": request.OrgId, "cooperationContractNumbers": []string{item.CooperationContractNumber}})
  120 + resultItem.DividendsAmount, _ = dividendsEstimateDao.CountDividendsEstimateDividendsAmount(map[string]interface{}{"orgId": request.OrgId, "userBaseId": request.UserBaseId, "cooperationContractNumbers": []string{item.CooperationContractNumber}})
  121 +
117 results = append(results, resultItem) 122 results = append(results, resultItem)
118 numbers = append(numbers, item.CooperationContractNumber) 123 numbers = append(numbers, item.CooperationContractNumber)
119 } 124 }
120 - mapCreditAccount, err := ptr.getContractsCreditAccount(numbers)  
121 - if err != nil {  
122 - return nil, err  
123 - }  
124 - for i := range results {  
125 - if v, ok := mapCreditAccount[results[i].CooperationContractNumber]; ok {  
126 - results[i].DividendsAmount = v.SettlementAmount  
127 - results[i].DividendsOrderAmount = v.GoodAmountCount  
128 - }  
129 - } 125 + //mapCreditAccount, err := ptr.getContractsCreditAccount(numbers)
  126 + //if err != nil {
  127 + // return nil, err
  128 + //}
  129 + //for i := range results {
  130 + // if v, ok := mapCreditAccount[results[i].CooperationContractNumber]; ok {
  131 + // results[i].DividendsAmount = v.SettlementAmount
  132 + // results[i].DividendsOrderAmount = v.GoodAmountCount
  133 + // }
  134 + //}
130 return results, nil 135 return results, nil
131 } 136 }
132 137
133 // getUserContracts 获取用户的合约列表 138 // getUserContracts 获取用户的合约列表
134 // 139 //
135 // p1 p1_desc 140 // p1 p1_desc
136 -func (ptr *CooperationStatisticsService) getUserContracts(userBaseId int64, queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) { 141 +func (ptr *CooperationStatisticsService) getUserContracts(orgId, userBaseId int64, queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
137 var contracts = make([]*domain.CooperationContract, 0) 142 var contracts = make([]*domain.CooperationContract, 0)
138 if userBaseId == 0 { 143 if userBaseId == 0 {
139 return contracts, nil 144 return contracts, nil
140 } 145 }
141 - contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"userBaseId": userBaseId}) 146 + contractNumbers, err := ptr.getRelevantContracts(map[string]interface{}{"orgId": orgId, "userBaseId": userBaseId})
142 if len(contractNumbers) == 0 { 147 if len(contractNumbers) == 0 {
143 return contracts, nil 148 return contracts, nil
144 } 149 }
@@ -206,7 +206,7 @@ func (repository *CreditAccountRepository) Find(queryOptions map[string]interfac @@ -206,7 +206,7 @@ func (repository *CreditAccountRepository) Find(queryOptions map[string]interfac
206 query.Where(`(credit_account.participator->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", participatorName)) 206 query.Where(`(credit_account.participator->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", participatorName))
207 } 207 }
208 if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" { 208 if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
209 - query.Where(`cooperation_contract_number = ?`, fmt.Sprintf("%s", cooperationContractNumber)) 209 + query.Where(fmt.Sprintf(`account_detail @>'[{"cooperationContractNumber":"%v"}]'`, cooperationContractNumber))
210 } 210 }
211 if v, ok := queryOptions["cooperationContractNumbers"]; ok && len(v.([]string)) > 0 { 211 if v, ok := queryOptions["cooperationContractNumbers"]; ok && len(v.([]string)) > 0 {
212 //query.Where("cooperation_contract_number in (?)", pg.In(v)) 212 //query.Where("cooperation_contract_number in (?)", pg.In(v))