作者 yangfu

分红预算统计修改

@@ -61,13 +61,16 @@ sum(settlement_amount) total, @@ -61,13 +61,16 @@ sum(settlement_amount) total,
61 sum((case when payment_status = 1 then actually_paid_amount else 0 end)) paid, 61 sum((case when payment_status = 1 then actually_paid_amount else 0 end)) paid,
62 sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted 62 sum((case when settlement_time is not null then settlement_amount else 0 end)) accounted
63 from credit_accounts 63 from credit_accounts
64 -where created_at>? and created_at<? 64 +where
65 ` 65 `
66 - if v, ok := queryOptions["userBaseId"]; ok {  
67 - sql += fmt.Sprintf(`and participator->>'userBaseId'='%v'`, v) 66 + if _, ok := queryOptions["beginTime"]; ok {
  67 + sql += `created_at>? and created_at<? and `
68 } 68 }
69 - if v, ok := queryOptions["orgId"]; ok {  
70 - sql += fmt.Sprintf(`and org->>'orgId'= '%v'`, v) 69 + if v, ok := queryOptions["userBaseId"]; ok && v.(int64) > 0 {
  70 + sql += fmt.Sprintf(`participator->>'userBaseId'='%v'`, v)
  71 + }
  72 + if v, ok := queryOptions["orgId"]; ok && v.(int64) > 0 {
  73 + sql += fmt.Sprintf(`org->>'orgId'= '%v'`, v)
71 } 74 }
72 _, err := dao.transactionContext.PgDd.Query(v, sql, queryOptions["beginTime"], queryOptions["endTime"]) 75 _, err := dao.transactionContext.PgDd.Query(v, sql, queryOptions["beginTime"], queryOptions["endTime"])
73 return err 76 return err
@@ -55,7 +55,27 @@ func (dao *DividendsOrderDao) CheckDividendsOrderNumberAvailable(queryOptions ma @@ -55,7 +55,27 @@ func (dao *DividendsOrderDao) CheckDividendsOrderNumberAvailable(queryOptions ma
55 55
56 // CalculateDividendsOrderAmount 计算分红订单金额 56 // CalculateDividendsOrderAmount 计算分红订单金额
57 func (dao *DividendsOrderDao) CalculateDividendsOrderAmount(queryOptions map[string]interface{}) (float64, error) { 57 func (dao *DividendsOrderDao) CalculateDividendsOrderAmount(queryOptions map[string]interface{}) (float64, error) {
58 - return 0, nil 58 + tx := dao.transactionContext.PgTx
  59 + var dividendsOrderModel = new(models.DividendsOrder)
  60 + query := tx.Model(dividendsOrderModel)
  61 + query.ColumnExpr("sum(dividends_order_amount) dividends_order_amount")
  62 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  63 + query.Where("company->>'companyId' = '?'", companyId)
  64 + }
  65 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  66 + query.Where("org->>'orgId' = '?'", orgId)
  67 + }
  68 + if beginTime, ok := queryOptions["beginTime"]; ok {
  69 + query.Where("order_time>= ?", beginTime)
  70 + }
  71 + if endTime, ok := queryOptions["endTime"]; ok {
  72 + query.Where("order_time< ?", endTime)
  73 + }
  74 + err := query.Select()
  75 + if err != nil {
  76 + return 0, err
  77 + }
  78 + return dividendsOrderModel.DividendsOrderAmount, nil
59 } 79 }
60 80
61 func NewDividendsOrderDao(transactionContext *pgTransaction.TransactionContext) (*DividendsOrderDao, error) { 81 func NewDividendsOrderDao(transactionContext *pgTransaction.TransactionContext) (*DividendsOrderDao, error) {
@@ -35,6 +35,31 @@ func (dao *DividendsReturnedOrderDao) GenerateDividendsReturnedOrderNumber() (st @@ -35,6 +35,31 @@ func (dao *DividendsReturnedOrderDao) GenerateDividendsReturnedOrderNumber() (st
35 } 35 }
36 } 36 }
37 37
  38 +// CalculateDividendsOrderAmount 计算分红订单金额
  39 +func (dao *DividendsReturnedOrderDao) CalculateDividendsReturnedOrderAmount(queryOptions map[string]interface{}) (float64, error) {
  40 + tx := dao.transactionContext.PgTx
  41 + var dividendsOrderModel = new(models.DividendsReturnedOrder)
  42 + query := tx.Model(dividendsOrderModel)
  43 + query.ColumnExpr("sum(dividends_returned_order_refund) dividends_returned_order_refund")
  44 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  45 + query.Where("company->>'companyId' = '?'", companyId)
  46 + }
  47 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  48 + query.Where("org->>'orgId' = '?'", orgId)
  49 + }
  50 + if beginTime, ok := queryOptions["beginTime"]; ok {
  51 + query.Where("order_time>= ?", beginTime)
  52 + }
  53 + if endTime, ok := queryOptions["endTime"]; ok {
  54 + query.Where("order_time< ?", endTime)
  55 + }
  56 + err := query.Select()
  57 + if err != nil {
  58 + return 0, err
  59 + }
  60 + return dividendsOrderModel.DividendsReturnedOrderRefund, nil
  61 +}
  62 +
38 func NewDividendsReturnedOrderDao(transactionContext *pgTransaction.TransactionContext) (*DividendsReturnedOrderDao, error) { 63 func NewDividendsReturnedOrderDao(transactionContext *pgTransaction.TransactionContext) (*DividendsReturnedOrderDao, error) {
39 if transactionContext == nil { 64 if transactionContext == nil {
40 return nil, fmt.Errorf("transactionContext参数不能未") 65 return nil, fmt.Errorf("transactionContext参数不能未")
@@ -74,7 +74,7 @@ func (dao *OrderGoodDao) CompanyDividendsStatistics(queryOptions map[string]inte @@ -74,7 +74,7 @@ func (dao *OrderGoodDao) CompanyDividendsStatistics(queryOptions map[string]inte
74 } 74 }
75 sql := fmt.Sprintf(`select sum(actually_paid_amount) dividends_estimate,0 order_amount 75 sql := fmt.Sprintf(`select sum(actually_paid_amount) dividends_estimate,0 order_amount
76 from credit_accounts 76 from credit_accounts
77 -where company->>'companyId' = '?' and org->>'orgId' = '?' %v %v 77 +where company->>'companyId' = '?' and org->>'orgId' = '?' and deleted_at is null %v %v
78 `, queryTime, queryPaymentStatus) 78 `, queryTime, queryPaymentStatus)
79 var s = &domain.DividendStatisticsDto{} 79 var s = &domain.DividendStatisticsDto{}
80 _, err := tx.Query(s, sql, queryOptions["companyId"], queryOptions["orgId"]) 80 _, err := tx.Query(s, sql, queryOptions["companyId"], queryOptions["orgId"])
@@ -8,6 +8,8 @@ import ( @@ -8,6 +8,8 @@ import (
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/log"
  12 + "math"
11 "time" 13 "time"
12 ) 14 )
13 15
@@ -23,6 +25,7 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(queryOptions @@ -23,6 +25,7 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(queryOptions
23 OrgId int64 `json:"orgId" valid:"Required"` 25 OrgId int64 `json:"orgId" valid:"Required"`
24 RankType int `json:"rankType" valid:"Required"` 26 RankType int `json:"rankType" valid:"Required"`
25 Top int `json:"top" valid:"Required"` 27 Top int `json:"top" valid:"Required"`
  28 + Offset int `json:"offset"`
26 }{} 29 }{}
27 if err := LoadQueryObject(queryOptions, &request); err != nil { 30 if err := LoadQueryObject(queryOptions, &request); err != nil {
28 return nil, err 31 return nil, err
@@ -46,6 +49,9 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(queryOptions @@ -46,6 +49,9 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(queryOptions
46 if request.Top > 0 { 49 if request.Top > 0 {
47 queryOptions["limit"] = request.Top 50 queryOptions["limit"] = request.Top
48 } 51 }
  52 + if request.Offset > 0 {
  53 + queryOptions["offset"] = request.Offset
  54 + }
49 goods, err := orderGoodDao.CooperationGoodsStatistics(queryOptions) 55 goods, err := orderGoodDao.CooperationGoodsStatistics(queryOptions)
50 if err != nil { 56 if err != nil {
51 return nil, err 57 return nil, err
@@ -131,13 +137,31 @@ func (ptr *CooperationStatisticsService) CompanyDividendsStatistics(queryOptions @@ -131,13 +137,31 @@ func (ptr *CooperationStatisticsService) CompanyDividendsStatistics(queryOptions
131 if err != nil { 137 if err != nil {
132 return nil, err 138 return nil, err
133 } 139 }
  140 + orderAmount := ptr.CalculateOrderAmount(queryOptions)
134 141
135 - res["creditAccount"] = totalDividends.DividendsEstimate  
136 - res["orderAmount"] = totalDividends.OrderAmount  
137 - res["dividendsEstimate"] = dividendsEstimate.DividendsEstimate 142 + res["creditAccount"] = dividendsEstimate.DividendsEstimate // 本月账期结算统计
  143 + res["orderAmount"] = orderAmount // 订单金额统计
  144 + res["dividendsEstimate"] = totalDividends.DividendsEstimate // 分红预算统计
138 return res, nil 145 return res, nil
139 } 146 }
140 147
  148 +func (ptr *CooperationStatisticsService) CalculateOrderAmount(queryOption map[string]interface{}) float64 {
  149 + orderDao, err := dao.NewDividendsOrderDao(ptr.transactionContext)
  150 + if err != nil {
  151 + log.Logger.Error(err.Error())
  152 + }
  153 + amount, err := orderDao.CalculateDividendsOrderAmount(queryOption)
  154 + if err != nil {
  155 + log.Logger.Error(err.Error())
  156 + }
  157 + returnedOrderDao, _ := dao.NewDividendsReturnedOrderDao(ptr.transactionContext)
  158 + returnedAmount, _ := returnedOrderDao.CalculateDividendsReturnedOrderAmount(queryOption)
  159 + if math.Abs(returnedAmount) > math.Abs(amount) {
  160 + return 0
  161 + }
  162 + return utils.Round(math.Abs(amount)-math.Abs(returnedAmount), 1)
  163 +}
  164 +
141 func LoadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[string]interface{}, error) { 165 func LoadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[string]interface{}, error) {
142 var res = make(map[string]interface{}) 166 var res = make(map[string]interface{})
143 for i := 0; i < len(keys); i++ { 167 for i := 0; i < len(keys); i++ {
@@ -92,6 +92,10 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -92,6 +92,10 @@ func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
92 } 92 }
93 93
94 // TODO: 3.根据合约查询订单金额 94 // TODO: 3.根据合约查询订单金额
  95 + //for i:=range results{
  96 + // queryOptions["cooperationContractNumber"] =results[i].CooperationContractNumber
  97 + // results[i].DividendsOrderAmount = ptr.CalculateOrderAmount(queryOptions)
  98 + //}
95 return results, nil 99 return results, nil
96 } 100 }
97 101
@@ -189,12 +193,32 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s @@ -189,12 +193,32 @@ func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[s
189 } 193 }
190 194
191 // 2.合约分红列表 195 // 2.合约分红列表
192 - if _, ok := queryOptions["userBaseId"]; ok {  
193 -  
194 - } else if _, ok := queryOptions["orgId"]; ok {  
195 - 196 + creditAccountRepository, _ := repository.NewCreditAccountRepository(ptr.transactionContext)
  197 + queryOptions["cooperationContractNumber"] = contract.CooperationContractNumber
  198 + _, creditAccounts, err := creditAccountRepository.Find(queryOptions)
  199 + if err != nil {
  200 + return res, err
  201 + }
  202 + var dividends = make([]interface{}, 0)
  203 + for i := range creditAccounts {
  204 + a := creditAccounts[i]
  205 + dividends = append(dividends, map[string]interface{}{
  206 + "creditAccountId": a.CreditAccountId,
  207 + "goodName": "",
  208 + "dividendsType": a.AccountDetail.DividendsType,
  209 + "dividendsRatio": 0,
  210 + "dividendsAmount": a.AccountDetail.DividendsAmount,
  211 + "dividendsUser": map[string]interface{}{
  212 + "userInfo": a.Participator.UserInfo,
  213 + "userId": a.Participator.UserId,
  214 + },
  215 + "dividendsParticipateType": a.ParticipateType,
  216 + "paymentStatus": a.PaymentStatus,
  217 + "dividendsEstimateTime": a.CreatedAt.Unix() * 1000,
  218 + "orderOrReturnedOrderNum": a.CreditAccountOrderNum,
  219 + })
196 } 220 }
197 - res["dividends"] = []struct{}{} 221 + res["dividends"] = dividends
198 return res, nil 222 return res, nil
199 } 223 }
200 224
@@ -12,6 +12,7 @@ func TransformToContractUndertakerFeedbackDomainModelFromPgModels( @@ -12,6 +12,7 @@ func TransformToContractUndertakerFeedbackDomainModelFromPgModels(
12 FeedbackAttachment: contractUndertakerFeedbackModel.FeedbackAttachment, 12 FeedbackAttachment: contractUndertakerFeedbackModel.FeedbackAttachment,
13 FeedbackContent: contractUndertakerFeedbackModel.FeedbackContent, 13 FeedbackContent: contractUndertakerFeedbackModel.FeedbackContent,
14 CooperationContractNumber: contractUndertakerFeedbackModel.CooperationContractNumber, 14 CooperationContractNumber: contractUndertakerFeedbackModel.CooperationContractNumber,
  15 + CooperationContractName: contractUndertakerFeedbackModel.CooperationContractName,
15 ContractUndertaker: contractUndertakerFeedbackModel.ContractUndertaker, 16 ContractUndertaker: contractUndertakerFeedbackModel.ContractUndertaker,
16 Org: contractUndertakerFeedbackModel.Org, 17 Org: contractUndertakerFeedbackModel.Org,
17 Company: contractUndertakerFeedbackModel.Company, 18 Company: contractUndertakerFeedbackModel.Company,
@@ -156,6 +156,15 @@ func (repository *ContractUndertakerFeedbackRepository) Find(queryOptions map[st @@ -156,6 +156,15 @@ func (repository *ContractUndertakerFeedbackRepository) Find(queryOptions map[st
156 if undertakerName, ok := queryOptions["undertakerName"]; ok && undertakerName != "" { 156 if undertakerName, ok := queryOptions["undertakerName"]; ok && undertakerName != "" {
157 query.Where("contract_undertaker->>'userName' like ?", fmt.Sprintf("%%%s%%", undertakerName)) 157 query.Where("contract_undertaker->>'userName' like ?", fmt.Sprintf("%%%s%%", undertakerName))
158 } 158 }
  159 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  160 + query.Where("company->>'companyId' = '?'", companyId)
  161 + }
  162 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  163 + query.Where("org->>'orgId' = '?'", orgId)
  164 + }
  165 + if orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
  166 + query.Where("org->>'orgId' in (?)", pg.In(orgIds))
  167 + }
159 // 搜索内容包括项目合约名称、承接人姓名、承接内容关键字 168 // 搜索内容包括项目合约名称、承接人姓名、承接内容关键字
160 if matchWord, ok := queryOptions["matchWord"]; ok && matchWord != "" { 169 if matchWord, ok := queryOptions["matchWord"]; ok && matchWord != "" {
161 query.WhereGroup(func(q *orm.Query) (*orm.Query, error) { 170 query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
@@ -201,6 +201,9 @@ func (repository *CreditAccountRepository) Find(queryOptions map[string]interfac @@ -201,6 +201,9 @@ func (repository *CreditAccountRepository) Find(queryOptions map[string]interfac
201 if participatorName, ok := queryOptions["participatorName"]; ok && participatorName != "" { 201 if participatorName, ok := queryOptions["participatorName"]; ok && participatorName != "" {
202 query.Where(`(credit_account.participator->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", participatorName)) 202 query.Where(`(credit_account.participator->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", participatorName))
203 } 203 }
  204 + if cooperationContractNumber, ok := queryOptions["cooperation_contract_number"]; ok && cooperationContractNumber != "" {
  205 + query.Where(`cooperation_contract_number = ?`, fmt.Sprintf("%%%s%%", cooperationContractNumber))
  206 + }
204 if paymentStatus, ok := queryOptions["paymentStatus"]; ok && paymentStatus.(int32) != 0 { 207 if paymentStatus, ok := queryOptions["paymentStatus"]; ok && paymentStatus.(int32) != 0 {
205 query.Where("payment_status = ?", paymentStatus) 208 query.Where("payment_status = ?", paymentStatus)
206 } 209 }
@@ -292,6 +292,9 @@ func (repository *DividendsEstimateRepository) Find(queryOptions map[string]inte @@ -292,6 +292,9 @@ func (repository *DividendsEstimateRepository) Find(queryOptions map[string]inte
292 if orderOrReturnedOrderNum, ok := queryOptions["orderOrReturnedOrderNum"]; ok && orderOrReturnedOrderNum != "" { 292 if orderOrReturnedOrderNum, ok := queryOptions["orderOrReturnedOrderNum"]; ok && orderOrReturnedOrderNum != "" {
293 query.Where("order_or_returned_order_num = ?", orderOrReturnedOrderNum) 293 query.Where("order_or_returned_order_num = ?", orderOrReturnedOrderNum)
294 } 294 }
  295 + if dividendsAccountStatus, ok := queryOptions["dividendsAccountStatus"]; ok && (dividendsAccountStatus.(int32)) > 0 {
  296 + query.Where("dividends_account_status = ?", dividendsAccountStatus)
  297 + }
295 if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 { 298 if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
296 query.Where("company->>'companyId' = '?'", companyId) 299 query.Where("company->>'companyId' = '?'", companyId)
297 } 300 }