coperation_statistics_service.go
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package domain_service
import (
"fmt"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
"time"
)
const (
ContractDividends = "ContractDividends"
)
// ContractStatisticsService 共创统计服务
type ContractStatisticsService struct {
transactionContext *pgTransaction.TransactionContext
}
/***** 1.合约分红模块 *****/
// 分红合约搜索
//
// queryOptions 查询参数
// - pageNumber
// - pageSize
// 按组织
// - companyId
// - orgId
// 按个人
// - userBaseId
func (svr *ContractStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
// 1.根据个人、企业查询合约列表
var contracts []*domain.CooperationContract
var err error
if _, ok := queryOptions["userBaseId"]; ok {
contracts, err = svr.getUserContracts(queryOptions)
} else if _, ok := queryOptions["orgId"]; ok {
contracts, err = svr.getCompanyContracts(queryOptions)
}
if err != nil {
return nil, err
}
// 2.根据合约查询分红预算
var numbers []string
var results = make([]*searchContractDividendsResult, 0)
for i := range contracts {
item := contracts[i]
resultItem := &searchContractDividendsResult{
CooperationContractId: item.CooperationContractId,
CooperationContractName: item.CooperationContractName,
CooperationContractNumber: item.CooperationContractNumber,
Status: item.Status,
CreatedAt: item.CreatedAt,
}
results = append(results, resultItem)
numbers = append(numbers, item.CooperationContractNumber)
}
mapEstimate, err := svr.getContractsDividendsEstimate(numbers)
if err != nil {
return nil, err
}
for i := range results {
if v, ok := mapEstimate[results[i].CooperationContractNumber]; ok {
results[i].DividendsAmount = v.DividendsAmount
}
}
// TODO: 3.根据合约查询订单金额
return results, nil
}
// getUserContracts 获取用户的合约列表
//
// p1 p1_desc
func (svr *ContractStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(svr.transactionContext)
_, undertakers, err := undertakerRepository.Find(queryOptions)
var numbers []string
for i := range undertakers {
numbers = append(numbers, undertakers[i].CooperationContractNumber)
}
if len(numbers) == 0 {
return []*domain.CooperationContract{}, nil
}
queryOptions["inCooperationContractNumber"] = numbers
contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
}
// getCompanyContracts 获取组织合约列表
//
// p1 p1_desc
func (svr *ContractStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
contractRepository, _ := repository.NewCooperationContractRepository(svr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
}
// getContractsDividendsEstimate 合约分红预算
func (svr *ContractStatisticsService) getContractsDividendsEstimate(numbers []string) (map[string]*domain.DividendsEstimate, error) {
var estimates []*domain.DividendsEstimate
var resMap = make(map[string]*domain.DividendsEstimate)
if len(numbers) == 0 {
return resMap, nil
}
_, err := svr.transactionContext.PgDd.Query(estimates, `select cooperation_contract_number,sum(dividends_amount) dividends_amount from dividends_estimates
where cooperation_contract_number in (?)
group by cooperation_contract_number
`, pg.In(numbers))
if err != nil {
return nil, err
}
for i := range estimates {
resMap[estimates[i].CooperationContractNumber] = estimates[i]
}
return resMap, nil
}
type searchContractDividendsResult struct {
// 共创合约ID
CooperationContractId int64 `json:"cooperationContractId,string"`
// 共创合约名称
CooperationContractName string `json:"cooperationContractName"`
// 共创合约编号
CooperationContractNumber string `json:"cooperationContractNumber"`
// 合约状态,1恢复,2暂停
Status int32 `json:"cooperationContractStatus"`
// 分红预算
DividendsAmount float64 `json:"dividendsAmount"`
// 分红订单金额
DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
}
func NewContractStatisticsService(transactionContext *pgTransaction.TransactionContext) (*ContractStatisticsService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &ContractStatisticsService{
transactionContext: transactionContext,
}, nil
}
}