作者 yangfu

企业端共创统计修改

@@ -37,6 +37,12 @@ func (svr *CooperationStatisticsService) CooperationContractStatistics(contractS @@ -37,6 +37,12 @@ func (svr *CooperationStatisticsService) CooperationContractStatistics(contractS
37 res, err = statisticsService.SearchContractDividends(contractStatisticsQuery.QueryOptions) 37 res, err = statisticsService.SearchContractDividends(contractStatisticsQuery.QueryOptions)
38 case domain_service.GetContractDividends: 38 case domain_service.GetContractDividends:
39 res, err = statisticsService.GetContractDividends(contractStatisticsQuery.QueryOptions) 39 res, err = statisticsService.GetContractDividends(contractStatisticsQuery.QueryOptions)
  40 + case domain_service.CooperationGoodsStatistics:
  41 + res, err = statisticsService.CooperationGoodsStatistics(contractStatisticsQuery.QueryOptions)
  42 + case domain_service.CooperationModeStatistics:
  43 + res, err = statisticsService.CooperationModeStatistics(contractStatisticsQuery.QueryOptions)
  44 + case domain_service.CompanyDividendsStatistics:
  45 + res, err = statisticsService.CompanyDividendsStatistics(contractStatisticsQuery.QueryOptions)
40 } 46 }
41 if err != nil { 47 if err != nil {
42 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 48 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
@@ -27,7 +27,7 @@ type CooperationModeStatisticsDto struct { @@ -27,7 +27,7 @@ type CooperationModeStatisticsDto struct {
27 27
28 type DividendStatisticsDto struct { 28 type DividendStatisticsDto struct {
29 // 分红预算 29 // 分红预算
30 - DividendsEstimate string `json:"dividendsEstimate"` 30 + DividendsEstimate float64 `json:"dividendsEstimate"`
31 // 订单金额 31 // 订单金额
32 OrderAmount float64 `json:"orderAmount"` 32 OrderAmount float64 `json:"orderAmount"`
33 -}  
  33 +}
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "fmt" 4 "fmt"
5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
  7 + "time"
7 ) 8 )
8 9
9 type OrderGoodDao struct { 10 type OrderGoodDao struct {
@@ -20,7 +21,7 @@ type OrderGoodDao struct { @@ -20,7 +21,7 @@ type OrderGoodDao struct {
20 func (dao *OrderGoodDao) CooperationGoodsStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationGoodsStatisticsDto, error) { 21 func (dao *OrderGoodDao) CooperationGoodsStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationGoodsStatisticsDto, error) {
21 tx := dao.transactionContext.PgTx 22 tx := dao.transactionContext.PgTx
22 var goods []*domain.CooperationGoodsStatisticsDto 23 var goods []*domain.CooperationGoodsStatisticsDto
23 - var queryTime,queryLimit string 24 + var queryTime, queryLimit string
24 if _, ok := queryOptions["beginTime"]; ok { 25 if _, ok := queryOptions["beginTime"]; ok {
25 queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"]) 26 queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"])
26 } 27 }
@@ -32,7 +33,7 @@ where company_id=? and org_id = ? %v @@ -32,7 +33,7 @@ where company_id=? and org_id = ? %v
32 GROUP BY order_good_name 33 GROUP BY order_good_name
33 order by goodAmount desc 34 order by goodAmount desc
34 %v 35 %v
35 -`, queryTime,queryLimit) 36 +`, queryTime, queryLimit)
36 _, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"]) 37 _, err := tx.Query(&goods, sql, queryOptions["companyId"], queryOptions["orgId"])
37 return goods, err 38 return goods, err
38 } 39 }
@@ -62,26 +63,24 @@ group by b.cooperation_mode_number @@ -62,26 +63,24 @@ group by b.cooperation_mode_number
62 // - companyId 企业Id 63 // - companyId 企业Id
63 // - orgId 组织Id 64 // - orgId 组织Id
64 // - paymentStatus 支付状态 65 // - paymentStatus 支付状态
65 -func (dao *OrderGoodDao) DividendsStatistics(queryOptions map[string]interface{}) (*domain.DividendStatisticsDto, error) { 66 +func (dao *OrderGoodDao) CompanyDividendsStatistics(queryOptions map[string]interface{}) (*domain.DividendStatisticsDto, error) {
66 tx := dao.transactionContext.PgTx 67 tx := dao.transactionContext.PgTx
67 var queryTime, queryPaymentStatus string 68 var queryTime, queryPaymentStatus string
68 if _, ok := queryOptions["beginTime"]; ok { 69 if _, ok := queryOptions["beginTime"]; ok {
69 - queryTime = fmt.Sprintf("and created_at>='%v' and created_at<'%v'", queryOptions["beginTime"], queryOptions["endTime"]) 70 + queryTime = fmt.Sprintf("and created_at>='%s' and created_at<'%s'", (queryOptions["beginTime"].(time.Time)).Format(time.RFC3339), (queryOptions["endTime"].(time.Time)).Format(time.RFC3339))
70 } 71 }
71 if _, ok := queryOptions["paymentStatus"]; ok { 72 if _, ok := queryOptions["paymentStatus"]; ok {
72 queryPaymentStatus = fmt.Sprintf(" and payment_status=%v", queryOptions["paymentStatus"]) 73 queryPaymentStatus = fmt.Sprintf(" and payment_status=%v", queryOptions["paymentStatus"])
73 } 74 }
74 - sql := fmt.Sprintf(`select sum(actually_paid_amount) dividendsEstimate,0 orderAmount 75 + sql := fmt.Sprintf(`select sum(actually_paid_amount) dividends_estimate,0 order_amount
75 from credit_accounts 76 from credit_accounts
76 where company->>'companyId' = '?' and org->>'orgId' = '?' %v %v 77 where company->>'companyId' = '?' and org->>'orgId' = '?' %v %v
77 `, queryTime, queryPaymentStatus) 78 `, queryTime, queryPaymentStatus)
78 - var s *domain.DividendStatisticsDto  
79 - _, err := tx.Query(&s, sql, queryOptions["companyId"], queryOptions["orgId"]) 79 + var s = &domain.DividendStatisticsDto{}
  80 + _, err := tx.Query(s, sql, queryOptions["companyId"], queryOptions["orgId"])
80 return s, err 81 return s, err
81 } 82 }
82 83
83 -  
84 -  
85 func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) { 84 func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*OrderGoodDao, error) {
86 if transactionContext == nil { 85 if transactionContext == nil {
87 return nil, fmt.Errorf("transactionContext参数不能为nil") 86 return nil, fmt.Errorf("transactionContext参数不能为nil")
@@ -90,4 +89,4 @@ func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*Ord @@ -90,4 +89,4 @@ func NewOrderGoodDao(transactionContext *pgTransaction.TransactionContext) (*Ord
90 transactionContext: transactionContext, 89 transactionContext: transactionContext,
91 }, nil 90 }, nil
92 } 91 }
93 -}  
  92 +}
@@ -2,6 +2,9 @@ package domain_service @@ -2,6 +2,9 @@ package domain_service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/beego/beego/v2/core/validation"
  6 + "github.com/linmadan/egglib-go/utils/json"
  7 + "github.com/linmadan/egglib-go/utils/tool_funs"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
@@ -12,32 +15,44 @@ import ( @@ -12,32 +15,44 @@ import (
12 15
13 // rankType 排行榜类型,1月榜,2年榜 3总榜,默认展示年榜 16 // rankType 排行榜类型,1月榜,2年榜 3总榜,默认展示年榜
14 // top 排名前n个 17 // top 排名前n个
15 -func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int, top int) ([]*domain.CooperationGoodsStatisticsDto, error) { 18 +func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationGoodsStatisticsDto, error) {
16 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) 19 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
17 - queryOptions := make(map[string]interface{})  
18 - queryOptions["companyId"] = companyId  
19 - queryOptions["orgId"] = orgId 20 + // 参数验证
  21 + var request = struct {
  22 + CompanyId int64 `json:"companyId" valid:"Required"`
  23 + OrgId int64 `json:"orgId" valid:"Required"`
  24 + RankType int `json:"rankType" valid:"Required"`
  25 + Top int `json:"top" valid:"Required"`
  26 + }{}
  27 + if err := LoadQueryObject(queryOptions, &request); err != nil {
  28 + return nil, err
  29 + }
  30 + queryOptions = tool_funs.SimpleStructToMap(&request)
  31 +
20 y := time.Now().Year() 32 y := time.Now().Year()
21 m := time.Now().Month() 33 m := time.Now().Month()
22 var beginTime, endTime time.Time 34 var beginTime, endTime time.Time
23 - if rankType == 1 { //1月榜 35 + if request.RankType == 1 { //1月榜
24 beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local) 36 beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local)
25 endTime = beginTime.AddDate(0, 1, 0) 37 endTime = beginTime.AddDate(0, 1, 0)
26 - queryOptions["beginTime"] = beginTime  
27 - queryOptions["endTime"] = endTime  
28 - } else if rankType == 2 { //2年榜 38 + queryOptions["beginTime"] = beginTime.Format(time.RFC3339)
  39 + queryOptions["endTime"] = endTime.Format(time.RFC3339)
  40 + } else if request.RankType == 2 { //2年榜
29 beginTime = time.Date(y, 1, 1, 0, 0, 0, 0, time.Local) 41 beginTime = time.Date(y, 1, 1, 0, 0, 0, 0, time.Local)
30 endTime = beginTime.AddDate(1, 0, 0) 42 endTime = beginTime.AddDate(1, 0, 0)
31 - queryOptions["beginTime"] = beginTime  
32 - queryOptions["endTime"] = endTime 43 + queryOptions["beginTime"] = beginTime.Format(time.RFC3339)
  44 + queryOptions["endTime"] = endTime.Format(time.RFC3339)
33 } 45 }
34 - if top > 0 {  
35 - queryOptions["limit"] = top 46 + if request.Top > 0 {
  47 + queryOptions["limit"] = request.Top
36 } 48 }
37 goods, err := orderGoodDao.CooperationGoodsStatistics(queryOptions) 49 goods, err := orderGoodDao.CooperationGoodsStatistics(queryOptions)
38 if err != nil { 50 if err != nil {
39 return nil, err 51 return nil, err
40 } 52 }
  53 + if len(goods) == 0 {
  54 + goods = make([]*domain.CooperationGoodsStatisticsDto, 0)
  55 + }
41 56
42 // 2.计算百分比 57 // 2.计算百分比
43 var totalAmount float64 58 var totalAmount float64
@@ -54,16 +69,25 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(companyId, o @@ -54,16 +69,25 @@ func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(companyId, o
54 // CooperationModeStatistics 企业-共创模式统计 69 // CooperationModeStatistics 企业-共创模式统计
55 // 70 //
56 // p1 p1_desc 71 // p1 p1_desc
57 -func (ptr *CooperationStatisticsService) CooperationModeStatistics(companyId, orgId int64) ([]*domain.CooperationModeStatisticsDto, error) { 72 +func (ptr *CooperationStatisticsService) CooperationModeStatistics(queryOptions map[string]interface{}) ([]*domain.CooperationModeStatisticsDto, error) {
58 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) 73 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
59 - queryOptions := make(map[string]interface{})  
60 - queryOptions["companyId"] = companyId  
61 - queryOptions["orgId"] = orgId 74 + // 参数验证
  75 + var request = struct {
  76 + CompanyId int64 `json:"companyId" valid:"Required"`
  77 + OrgId int64 `json:"orgId" valid:"Required"`
  78 + }{}
  79 + if err := LoadQueryObject(queryOptions, &request); err != nil {
  80 + return nil, err
  81 + }
  82 + queryOptions = tool_funs.SimpleStructToMap(&request)
62 83
63 modeStatistics, err := orderGoodDao.CooperationModeStatistics(queryOptions) 84 modeStatistics, err := orderGoodDao.CooperationModeStatistics(queryOptions)
64 if err != nil { 85 if err != nil {
65 return nil, err 86 return nil, err
66 } 87 }
  88 + if len(modeStatistics) == 0 {
  89 + modeStatistics = make([]*domain.CooperationModeStatisticsDto, 0)
  90 + }
67 91
68 return modeStatistics, nil 92 return modeStatistics, nil
69 } 93 }
@@ -71,14 +95,22 @@ func (ptr *CooperationStatisticsService) CooperationModeStatistics(companyId, or @@ -71,14 +95,22 @@ func (ptr *CooperationStatisticsService) CooperationModeStatistics(companyId, or
71 // DividendsStatistics 分红统计 95 // DividendsStatistics 分红统计
72 // 96 //
73 // action 1:当前月 97 // action 1:当前月
74 -func (ptr *CooperationStatisticsService) CompanyDividendsStatistics(companyId, orgId int64, action int) (interface{}, error) { 98 +func (ptr *CooperationStatisticsService) CompanyDividendsStatistics(queryOptions map[string]interface{}) (interface{}, error) {
  99 + // 参数验证
  100 + var request = struct {
  101 + CompanyId int64 `json:"companyId" valid:"Required"`
  102 + OrgId int64 `json:"orgId" valid:"Required"`
  103 + Action int `json:"action" valid:"Required"`
  104 + }{}
  105 + if err := LoadQueryObject(queryOptions, &request); err != nil {
  106 + return nil, err
  107 + }
  108 + queryOptions = tool_funs.SimpleStructToMap(&request)
  109 +
75 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) 110 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
76 - queryOptions := make(map[string]interface{})  
77 - queryOptions["companyId"] = companyId  
78 - queryOptions["orgId"] = orgId  
79 var beginTime, endTime time.Time 111 var beginTime, endTime time.Time
80 var res = make(map[string]interface{}) 112 var res = make(map[string]interface{})
81 - if action == 1 { 113 + if request.Action == 1 {
82 y := time.Now().Year() 114 y := time.Now().Year()
83 m := time.Now().Month() 115 m := time.Now().Month()
84 beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local) 116 beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local)
@@ -86,18 +118,18 @@ func (ptr *CooperationStatisticsService) CompanyDividendsStatistics(companyId, o @@ -86,18 +118,18 @@ func (ptr *CooperationStatisticsService) CompanyDividendsStatistics(companyId, o
86 queryOptions["beginTime"] = beginTime 118 queryOptions["beginTime"] = beginTime
87 queryOptions["endTime"] = endTime 119 queryOptions["endTime"] = endTime
88 } 120 }
89 - totalDividends, err := orderGoodDao.DividendsStatistics(queryOptions) 121 + totalDividends, err := orderGoodDao.CompanyDividendsStatistics(queryOptions)
90 if err != nil { 122 if err != nil {
91 return nil, err 123 return nil, err
92 } 124 }
93 - res["creditAccount"] = totalDividends.DividendsEstimate  
94 - res["orderAmount"] = totalDividends.OrderAmount  
95 -  
96 queryOptions["paymentStatus"] = 2 125 queryOptions["paymentStatus"] = 2
97 - dividendsEstimate, err := orderGoodDao.DividendsStatistics(queryOptions) 126 + dividendsEstimate, err := orderGoodDao.CompanyDividendsStatistics(queryOptions)
98 if err != nil { 127 if err != nil {
99 return nil, err 128 return nil, err
100 } 129 }
  130 +
  131 + res["creditAccount"] = totalDividends.DividendsEstimate
  132 + res["orderAmount"] = totalDividends.OrderAmount
101 res["dividendsEstimate"] = dividendsEstimate.DividendsEstimate 133 res["dividendsEstimate"] = dividendsEstimate.DividendsEstimate
102 return res, nil 134 return res, nil
103 } 135 }
@@ -115,6 +147,13 @@ func LoadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[s @@ -115,6 +147,13 @@ func LoadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[s
115 return res, nil 147 return res, nil
116 } 148 }
117 149
  150 +func LoadQueryObject(queryOption map[string]interface{}, obj interface{}) error {
  151 + json.UnmarshalFromString(json.MarshalToString(queryOption), obj)
  152 + validation := validation.Validation{}
  153 + _, err := validation.Valid(obj)
  154 + return err
  155 +}
  156 +
118 type item struct { 157 type item struct {
119 key string 158 key string
120 val interface{} 159 val interface{}