作者 yangfu

企业统计

@@ -2,48 +2,17 @@ package domain_service @@ -2,48 +2,17 @@ package domain_service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 - pgTransaction "github.com/linmadan/egglib-go/transaction/pg"  
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
9 "time" 8 "time"
10 ) 9 )
11 10
12 -// PgCommonStatisticsService 通用统计服务  
13 -type PgCommonStatisticsService struct {  
14 - transactionContext *pgTransaction.TransactionContext  
15 -}  
16 -  
17 -const (  
18 - // 共创产品统计数据  
19 - CooperationGoodsStatistics = iota + 1  
20 -)  
21 -  
22 -var (  
23 - MapKey = map[int]string{  
24 - CooperationGoodsStatistics: "cooperationGoodsStatistics",  
25 - }  
26 -)  
27 -  
28 -// Scan 扫描需要统计的项  
29 -//  
30 -// keyFlags 统计项标识符号  
31 -// queryOption 查询参数  
32 -func (ptr *PgCommonStatisticsService) Scan(keyFlags []int, queryOption map[string]interface{}) (interface{}, error) {  
33 - var res = make(map[string]interface{})  
34 - for i := range keyFlags {  
35 - switch keyFlags[i] {  
36 - case CooperationGoodsStatistics:  
37 - }  
38 - }  
39 - return res, nil  
40 -}  
41 -  
42 // totalOrganizationUser 统计组织用户 11 // totalOrganizationUser 统计组织用户
43 12
44 // rankType 排行榜类型,1月榜,2年榜 3总榜,默认展示年榜 13 // rankType 排行榜类型,1月榜,2年榜 3总榜,默认展示年榜
45 // top 排名前n个 14 // top 排名前n个
46 -func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int, top int) ([]*domain.CooperationGoodsStatisticsDto, error) { 15 +func (ptr *CooperationStatisticsService) CooperationGoodsStatistics(companyId, orgId int64, rankType int, top int) ([]*domain.CooperationGoodsStatisticsDto, error) {
47 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) 16 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
48 queryOptions := make(map[string]interface{}) 17 queryOptions := make(map[string]interface{})
49 queryOptions["companyId"] = companyId 18 queryOptions["companyId"] = companyId
@@ -62,38 +31,38 @@ func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgI @@ -62,38 +31,38 @@ func (ptr *PgCommonStatisticsService) CooperationGoodsStatistics(companyId, orgI
62 queryOptions["beginTime"] = beginTime 31 queryOptions["beginTime"] = beginTime
63 queryOptions["endTime"] = endTime 32 queryOptions["endTime"] = endTime
64 } 33 }
65 - if top >0{ 34 + if top > 0 {
66 queryOptions["limit"] = top 35 queryOptions["limit"] = top
67 } 36 }
68 - goods,err:= orderGoodDao.CooperationGoodsStatistics(queryOptions)  
69 - if err!=nil{  
70 - return nil,err 37 + goods, err := orderGoodDao.CooperationGoodsStatistics(queryOptions)
  38 + if err != nil {
  39 + return nil, err
71 } 40 }
72 41
73 // 2.计算百分比 42 // 2.计算百分比
74 var totalAmount float64 43 var totalAmount float64
75 - for i:=range goods{ 44 + for i := range goods {
76 goods[i].Rank = int32(i) 45 goods[i].Rank = int32(i)
77 - totalAmount +=goods[i].GoodAmount 46 + totalAmount += goods[i].GoodAmount
78 } 47 }
79 - for i:=range goods{  
80 - goods[i].GoodRatio = utils.Round((goods[i].GoodAmount / totalAmount)*100,0) 48 + for i := range goods {
  49 + goods[i].GoodRatio = utils.Round((goods[i].GoodAmount/totalAmount)*100, 0)
81 } 50 }
82 - return goods,nil 51 + return goods, nil
83 } 52 }
84 53
85 // CooperationModeStatistics 企业-共创模式统计 54 // CooperationModeStatistics 企业-共创模式统计
86 // 55 //
87 // p1 p1_desc 56 // p1 p1_desc
88 -func (ptr *PgCommonStatisticsService) CooperationModeStatistics(companyId, orgId int64) ([]*domain.CooperationModeStatisticsDto, error){ 57 +func (ptr *CooperationStatisticsService) CooperationModeStatistics(companyId, orgId int64) ([]*domain.CooperationModeStatisticsDto, error) {
89 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) 58 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
90 queryOptions := make(map[string]interface{}) 59 queryOptions := make(map[string]interface{})
91 queryOptions["companyId"] = companyId 60 queryOptions["companyId"] = companyId
92 queryOptions["orgId"] = orgId 61 queryOptions["orgId"] = orgId
93 62
94 - modeStatistics,err:= orderGoodDao.CooperationModeStatistics(queryOptions)  
95 - if err!=nil{  
96 - return nil,err 63 + modeStatistics, err := orderGoodDao.CooperationModeStatistics(queryOptions)
  64 + if err != nil {
  65 + return nil, err
97 } 66 }
98 67
99 return modeStatistics, nil 68 return modeStatistics, nil
@@ -102,14 +71,14 @@ func (ptr *PgCommonStatisticsService) CooperationModeStatistics(companyId, orgId @@ -102,14 +71,14 @@ func (ptr *PgCommonStatisticsService) CooperationModeStatistics(companyId, orgId
102 // DividendsStatistics 分红统计 71 // DividendsStatistics 分红统计
103 // 72 //
104 // action 1:当前月 73 // action 1:当前月
105 -func (ptr *PgCommonStatisticsService) DividendsStatistics(companyId, orgId int64,action int) (interface{}, error){ 74 +func (ptr *CooperationStatisticsService) CompanyDividendsStatistics(companyId, orgId int64, action int) (interface{}, error) {
106 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext) 75 orderGoodDao, _ := dao.NewOrderGoodDao(ptr.transactionContext)
107 queryOptions := make(map[string]interface{}) 76 queryOptions := make(map[string]interface{})
108 queryOptions["companyId"] = companyId 77 queryOptions["companyId"] = companyId
109 queryOptions["orgId"] = orgId 78 queryOptions["orgId"] = orgId
110 var beginTime, endTime time.Time 79 var beginTime, endTime time.Time
111 var res = make(map[string]interface{}) 80 var res = make(map[string]interface{})
112 - if action==1{ 81 + if action == 1 {
113 y := time.Now().Year() 82 y := time.Now().Year()
114 m := time.Now().Month() 83 m := time.Now().Month()
115 beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local) 84 beginTime = time.Date(y, m, 1, 0, 0, 0, 0, time.Local)
@@ -117,23 +86,23 @@ func (ptr *PgCommonStatisticsService) DividendsStatistics(companyId, orgId int64 @@ -117,23 +86,23 @@ func (ptr *PgCommonStatisticsService) DividendsStatistics(companyId, orgId int64
117 queryOptions["beginTime"] = beginTime 86 queryOptions["beginTime"] = beginTime
118 queryOptions["endTime"] = endTime 87 queryOptions["endTime"] = endTime
119 } 88 }
120 - totalDividends,err:= orderGoodDao.DividendsStatistics(queryOptions)  
121 - if err!=nil{  
122 - return nil,err 89 + totalDividends, err := orderGoodDao.DividendsStatistics(queryOptions)
  90 + if err != nil {
  91 + return nil, err
123 } 92 }
124 res["creditAccount"] = totalDividends.DividendsEstimate 93 res["creditAccount"] = totalDividends.DividendsEstimate
125 res["orderAmount"] = totalDividends.OrderAmount 94 res["orderAmount"] = totalDividends.OrderAmount
126 95
127 queryOptions["paymentStatus"] = 2 96 queryOptions["paymentStatus"] = 2
128 - dividendsEstimate,err:= orderGoodDao.DividendsStatistics(queryOptions)  
129 - if err!=nil{  
130 - return nil,err 97 + dividendsEstimate, err := orderGoodDao.DividendsStatistics(queryOptions)
  98 + if err != nil {
  99 + return nil, err
131 } 100 }
132 res["dividendsEstimate"] = dividendsEstimate.DividendsEstimate 101 res["dividendsEstimate"] = dividendsEstimate.DividendsEstimate
133 return res, nil 102 return res, nil
134 } 103 }
135 104
136 -func (ptr *PgCommonStatisticsService) loadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[string]interface{}, error) { 105 +func LoadQueryOptions(queryOption map[string]interface{}, keys ...string) (map[string]interface{}, error) {
137 var res = make(map[string]interface{}) 106 var res = make(map[string]interface{})
138 for i := 0; i < len(keys); i++ { 107 for i := 0; i < len(keys); i++ {
139 k := keys[i] 108 k := keys[i]
1 package domain_service 1 package domain_service
2 2
3 import ( 3 import (
  4 + "encoding/json"
4 "fmt" 5 "fmt"
5 "github.com/go-pg/pg/v10" 6 "github.com/go-pg/pg/v10"
6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 7 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
@@ -13,6 +14,13 @@ const ( @@ -13,6 +14,13 @@ const (
13 SearchContractDividends = "SearchContractDividends" 14 SearchContractDividends = "SearchContractDividends"
14 // 获取分红合约详情 15 // 获取分红合约详情
15 GetContractDividends = "GetContractDividends" 16 GetContractDividends = "GetContractDividends"
  17 +
  18 + // 企业-商品统计
  19 + CooperationGoodsStatistics = "cooperationGoodsStatistics"
  20 + // 企业-共创模式统计
  21 + CooperationModeStatistics = "CooperationModeStatistics"
  22 + // 企业-分红统计
  23 + CompanyDividendsStatistics = "CompanyDividendsStatistics"
16 ) 24 )
17 25
18 // CooperationStatisticsService 共创统计服务 26 // CooperationStatisticsService 共创统计服务
@@ -20,6 +28,16 @@ type CooperationStatisticsService struct { @@ -20,6 +28,16 @@ type CooperationStatisticsService struct {
20 transactionContext *pgTransaction.TransactionContext 28 transactionContext *pgTransaction.TransactionContext
21 } 29 }
22 30
  31 +func NewCooperationStatisticsService(transactionContext *pgTransaction.TransactionContext) (*CooperationStatisticsService, error) {
  32 + if transactionContext == nil {
  33 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  34 + } else {
  35 + return &CooperationStatisticsService{
  36 + transactionContext: transactionContext,
  37 + }, nil
  38 + }
  39 +}
  40 +
23 /***** 1.合约分红模块 *****/ 41 /***** 1.合约分红模块 *****/
24 42
25 /*1.1 分红合约搜索*/ 43 /*1.1 分红合约搜索*/
@@ -34,14 +52,15 @@ type CooperationStatisticsService struct { @@ -34,14 +52,15 @@ type CooperationStatisticsService struct {
34 // - orgId 52 // - orgId
35 // 按个人 53 // 按个人
36 // - userBaseId 54 // - userBaseId
37 -func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) { 55 +func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
38 // 1.根据个人、企业查询合约列表 56 // 1.根据个人、企业查询合约列表
39 var contracts []*domain.CooperationContract 57 var contracts []*domain.CooperationContract
40 var err error 58 var err error
41 if _, ok := queryOptions["userBaseId"]; ok { 59 if _, ok := queryOptions["userBaseId"]; ok {
42 - contracts, err = svr.getUserContracts(queryOptions)  
43 - } else if _, ok := queryOptions["orgId"]; ok {  
44 - contracts, err = svr.getCompanyContracts(queryOptions) 60 + contracts, err = ptr.getUserContracts(queryOptions)
  61 + } else if v, ok := queryOptions["orgId"]; ok {
  62 + queryOptions["orgId"], _ = (v.(json.Number)).Int64()
  63 + contracts, err = ptr.getCompanyContracts(queryOptions)
45 } 64 }
46 if err != nil { 65 if err != nil {
47 return nil, err 66 return nil, err
@@ -62,7 +81,7 @@ func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -62,7 +81,7 @@ func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
62 results = append(results, resultItem) 81 results = append(results, resultItem)
63 numbers = append(numbers, item.CooperationContractNumber) 82 numbers = append(numbers, item.CooperationContractNumber)
64 } 83 }
65 - mapEstimate, err := svr.getContractsDividendsEstimate(numbers) 84 + mapEstimate, err := ptr.getContractsDividendsEstimate(numbers)
66 if err != nil { 85 if err != nil {
67 return nil, err 86 return nil, err
68 } 87 }
@@ -79,8 +98,8 @@ func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions ma @@ -79,8 +98,8 @@ func (svr *CooperationStatisticsService) SearchContractDividends(queryOptions ma
79 // getUserContracts 获取用户的合约列表 98 // getUserContracts 获取用户的合约列表
80 // 99 //
81 // p1 p1_desc 100 // p1 p1_desc
82 -func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {  
83 - undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(svr.transactionContext) 101 +func (ptr *CooperationStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
  102 + undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)
84 _, undertakers, err := undertakerRepository.Find(queryOptions) 103 _, undertakers, err := undertakerRepository.Find(queryOptions)
85 var numbers []string 104 var numbers []string
86 for i := range undertakers { 105 for i := range undertakers {
@@ -90,7 +109,7 @@ func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[strin @@ -90,7 +109,7 @@ func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[strin
90 return []*domain.CooperationContract{}, nil 109 return []*domain.CooperationContract{}, nil
91 } 110 }
92 queryOptions["inCooperationContractNumber"] = numbers 111 queryOptions["inCooperationContractNumber"] = numbers
93 - contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext) 112 + contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
94 // TODO: 参数查询条件 113 // TODO: 参数查询条件
95 _, contracts, err := contractRepository.Find(queryOptions) 114 _, contracts, err := contractRepository.Find(queryOptions)
96 return contracts, err 115 return contracts, err
@@ -99,21 +118,21 @@ func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[strin @@ -99,21 +118,21 @@ func (svr *CooperationStatisticsService) getUserContracts(queryOptions map[strin
99 // getCompanyContracts 获取组织合约列表 118 // getCompanyContracts 获取组织合约列表
100 // 119 //
101 // p1 p1_desc 120 // p1 p1_desc
102 -func (svr *CooperationStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {  
103 - contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext) 121 +func (ptr *CooperationStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
  122 + contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
104 // TODO: 参数查询条件 123 // TODO: 参数查询条件
105 _, contracts, err := contractRepository.Find(queryOptions) 124 _, contracts, err := contractRepository.Find(queryOptions)
106 return contracts, err 125 return contracts, err
107 } 126 }
108 127
109 // getContractsDividendsEstimate 合约分红预算 128 // getContractsDividendsEstimate 合约分红预算
110 -func (svr *CooperationStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) { 129 +func (ptr *CooperationStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
111 var estimates []*domain.DividendsEstimate 130 var estimates []*domain.DividendsEstimate
112 var resMap = make(map[string]*domain.DividendsEstimate) 131 var resMap = make(map[string]*domain.DividendsEstimate)
113 if len(numbers) == 0 { 132 if len(numbers) == 0 {
114 return resMap, nil 133 return resMap, nil
115 } 134 }
116 - _, err := svr.transactionContext.PgDd.Query(&estimates, `select cooperation_contract_number,sum(dividends_amount) dividends_amount from dividends_estimates 135 + _, err := ptr.transactionContext.PgDd.Query(&estimates, `select cooperation_contract_number,sum(dividends_amount) dividends_amount from dividends_estimates
117 where cooperation_contract_number in (?) 136 where cooperation_contract_number in (?)
118 group by cooperation_contract_number 137 group by cooperation_contract_number
119 `, pg.In(numbers)) 138 `, pg.In(numbers))
@@ -144,16 +163,6 @@ type searchContractDividendsResult struct { @@ -144,16 +163,6 @@ type searchContractDividendsResult struct {
144 CreatedAt int64 `json:"createdAt"` 163 CreatedAt int64 `json:"createdAt"`
145 } 164 }
146 165
147 -func NewCooperationStatisticsService(transactionContext *pgTransaction.TransactionContext) (*CooperationStatisticsService, error) {  
148 - if transactionContext == nil {  
149 - return nil, fmt.Errorf("transactionContext参数不能为nil")  
150 - } else {  
151 - return &CooperationStatisticsService{  
152 - transactionContext: transactionContext,  
153 - }, nil  
154 - }  
155 -}  
156 -  
157 /*1.2 分红合约详情*/ 166 /*1.2 分红合约详情*/
158 167
159 // 分红合约搜索 168 // 分红合约搜索
@@ -164,9 +173,9 @@ func NewCooperationStatisticsService(transactionContext *pgTransaction.Transacti @@ -164,9 +173,9 @@ func NewCooperationStatisticsService(transactionContext *pgTransaction.Transacti
164 // - orgId 173 // - orgId
165 // 按个人 174 // 按个人
166 // - userBaseId 175 // - userBaseId
167 -func (svr *CooperationStatisticsService) GetContractDividends(queryOptions map[string]interface{}) (interface{}, error) { 176 +func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
168 // 1.合约详情 177 // 1.合约详情
169 - contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext) 178 + contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
170 if _, ok := queryOptions["contractId"]; !ok { 179 if _, ok := queryOptions["contractId"]; !ok {
171 return nil, fmt.Errorf("合约ID(contractId)不能为空") 180 return nil, fmt.Errorf("合约ID(contractId)不能为空")
172 } 181 }