pg_cooperation_statistics_service.go
13.2 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package domain_service
import (
"encoding/json"
"fmt"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
"time"
)
const (
// 合约分红列表查询
SearchContractDividends = "SearchContractDividends"
// 获取分红合约详情
GetContractDividends = "GetContractDividends"
// 分红统计(分红明细)
DividendsStatistics = "DividendsStatistics"
// 企业-商品统计
CooperationGoodsStatistics = "cooperationGoodsStatistics"
// 企业-共创模式统计
CooperationModeStatistics = "CooperationModeStatistics"
// 企业-分红统计
CompanyDividendsStatistics = "CompanyDividendsStatistics"
// 企业、个人 - 分红预算列表
SearchDividendsEstimates = "SearchDividendsEstimates"
)
// CooperationStatisticsService 共创统计服务
type CooperationStatisticsService struct {
transactionContext *pgTransaction.TransactionContext
}
func NewCooperationStatisticsService(transactionContext *pgTransaction.TransactionContext) (*CooperationStatisticsService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationStatisticsService{
transactionContext: transactionContext,
}, nil
}
}
/***** 1.合约分红模块 *****/
/*1.1 分红合约搜索*/
// 分红合约搜索
//
// queryOptions 查询参数
func (ptr *CooperationStatisticsService) SearchContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
// 1.根据个人、企业查询合约列表
var contracts []*domain.CooperationContract
var err error
if _, ok := queryOptions["userBaseId"]; ok {
contracts, err = ptr.getUserContracts(queryOptions)
} else if v, ok := queryOptions["orgId"]; ok {
queryOptions["orgId"], _ = (v.(json.Number)).Int64()
contracts, err = ptr.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.Unix() * 1000,
}
results = append(results, resultItem)
numbers = append(numbers, item.CooperationContractNumber)
}
mapEstimate, err := ptr.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.根据合约查询订单金额
//for i:=range results{
// queryOptions["cooperationContractNumber"] =results[i].CooperationContractNumber
// results[i].DividendsOrderAmount = ptr.CalculateOrderAmount(queryOptions)
//}
return results, nil
}
// getUserContracts 获取用户的合约列表
//
// p1 p1_desc
func (ptr *CooperationStatisticsService) getUserContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.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(ptr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
}
// getCompanyContracts 获取组织合约列表
//
// p1 p1_desc
func (ptr *CooperationStatisticsService) getCompanyContracts(queryOptions map[string]interface{}) ([]*domain.CooperationContract, error) {
contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
// TODO: 参数查询条件
_, contracts, err := contractRepository.Find(queryOptions)
return contracts, err
}
// getContractsDividendsEstimate 合约分红预算
func (ptr *CooperationStatisticsService) 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 := ptr.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 int64 `json:"createdAt"`
}
/*1.2 分红合约详情*/
//GetContractDividends 分红合约详情
//
// queryOptions 查询参数
func (ptr *CooperationStatisticsService) GetContractDividends(queryOptions map[string]interface{}) (interface{}, error) {
// 1.合约详情
contractRepository, _ := repository.NewCooperationContractRepository(ptr.transactionContext)
if _, ok := queryOptions["contractId"]; !ok {
return nil, fmt.Errorf("合约ID(contractId)不能为空")
}
var res = make(map[string]interface{})
contract, err := contractRepository.FindOne(map[string]interface{}{"cooperationContractId": queryOptions["contractId"]})
if err != nil {
return nil, err
}
res["cooperationContract"] = map[string]interface{}{
"cooperationContractDescription": contract.CooperationContractDescription,
"cooperationContractId": contract.CooperationContractId,
"cooperationContractName": contract.CooperationContractName,
"cooperationContractNumber": contract.CooperationContractNumber,
"createdAt": contract.CreatedAt.Unix() * 1000,
"status": contract.Status,
}
// 2.合约分红列表
creditAccountRepository, _ := repository.NewCreditAccountRepository(ptr.transactionContext)
queryOptions["cooperationContractNumber"] = contract.CooperationContractNumber
_, creditAccounts, err := creditAccountRepository.Find(queryOptions)
if err != nil {
return res, err
}
var dividends = make([]interface{}, 0)
for i := range creditAccounts {
a := creditAccounts[i]
dividends = append(dividends, map[string]interface{}{
"creditAccountId": a.CreditAccountId,
"goodName": "",
"dividendsType": a.AccountDetail.DividendsType,
"dividendsRatio": 0,
"dividendsAmount": a.AccountDetail.DividendsAmount,
"dividendsUser": map[string]interface{}{
"userInfo": a.Participator.UserInfo,
"userId": a.Participator.UserId,
},
"dividendsParticipateType": a.ParticipateType,
"paymentStatus": a.PaymentStatus,
"dividendsEstimateTime": a.CreatedAt.Unix() * 1000,
"orderOrReturnedOrderNum": a.CreditAccountOrderNum,
})
}
res["dividends"] = dividends
return res, nil
}
// 分红统计(分红明细)
func (ptr *CooperationStatisticsService) DividendsStatistics(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
//企业
CompanyId int64 `json:"companyId"`
OrgId int64 `json:"orgId"`
//个人
UserBaseId int64 `json:"userBaseId"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
type response struct {
Total float64 `json:"total"`
Accounting float64 `json:"accounting"`
Accounted float64 `json:"accounted"`
Paid float64 `json:"paid"`
}
creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
var allDividends = &response{}
if err := creditAccountDao.DividendsStatistics(queryOptions, allDividends); err != nil {
return nil, err
}
allDividends.Accounting = allDividends.Total - allDividends.Accounted
var annualDividends = &response{}
queryOptions["beginTime"] = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local)
queryOptions["endTime"] = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Local).AddDate(1, 0, 0)
if err := creditAccountDao.DividendsStatistics(queryOptions, annualDividends); err != nil {
return nil, err
}
annualDividends.Accounting = annualDividends.Total - annualDividends.Accounted
var quarterDividends = &response{}
queryOptions["beginTime"], queryOptions["endTime"] = quarterBeginEnd()
if err := creditAccountDao.DividendsStatistics(queryOptions, quarterDividends); err != nil {
return nil, err
}
quarterDividends.Accounting = quarterDividends.Total - quarterDividends.Accounted
ret := map[string]interface{}{
"allDividends": allDividends,
"annualDividends": annualDividends,
"quarterDividends": quarterDividends,
}
return ret, nil
}
func quarterBeginEnd() (time.Time, time.Time) {
y := time.Now().Year()
m := time.Now().Month()
var mBegin, mEnd int
var begin, end time.Time
switch m {
case 4, 5, 6:
mBegin = 4
mEnd = 6
case 7, 8, 9:
mBegin = 7
mEnd = 9
case 10, 11, 12:
mBegin = 10
mEnd = 12
case 1, 2, 3:
mBegin = 1
mEnd = 3
}
begin = time.Date(y, time.Month(mBegin), 1, 0, 0, 0, 0, time.Local)
end = time.Date(y, time.Month(mEnd), 1, 0, 0, 0, 0, time.Local)
return begin, end
}
// 分红预算列表
func (ptr *CooperationStatisticsService) SearchDividendsEstimates(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
CompanyId int64 `json:"companyId"`
OrgId int64 `json:"orgId"`
UserBaseId int64 `json:"userBaseId"`
DividendsAccountStatus int32 `json:"dividendsAccountStatus"` // 分红结算状态
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
dividendsEstimateRepository, _ := repository.NewDividendsEstimateRepository(ptr.transactionContext)
_, estimates, err := dividendsEstimateRepository.Find(queryOptions)
if err != nil {
return nil, err
}
type response struct {
// 承接人分红预算记录ID
DividendsEstimateId int64 `json:"dividendsEstimateId,string"`
// 分红结算状态,1待结算,2已结算
DividendsAccountStatus int32 `json:"dividendsAccountStatus"`
// 分红金额
DividendsAmount float64 `json:"dividendsAmount"`
// 参与分红类型,1承接人,2推荐人,3关联业务员
DividendsParticipateType int32 `json:"dividendsParticipateType"`
// 分红类型,1订单分红,2退货冲销,3金额激励
DividendsType int32 `json:"dividendsType"`
// 分红用户(共创参与)
DividendsUser *domain.User `json:"dividendsUser"`
// 分红订单金额
DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
//OrderGoodName string `json:"orderGoodName"`
CustomerName string `json:"customerName"`
}
var results = make([]response, 0)
dividendsOrderRepository, _ := repository.NewDividendsOrderRepository(ptr.transactionContext)
dividendsReturnedOrderRepository, _ := repository.NewDividendsReturnedOrderRepository(ptr.transactionContext)
for i := range estimates {
item := estimates[i]
result := response{
DividendsEstimateId: item.DividendsEstimateId,
DividendsAccountStatus: item.DividendsAccountStatus,
DividendsAmount: item.DividendsAmount,
DividendsParticipateType: item.DividendsParticipateType,
DividendsType: item.DividendsType,
DividendsUser: item.DividendsUser,
}
if item.DividendsType == 1 {
order, err := dividendsOrderRepository.FindOne(map[string]interface{}{"dividendsOrderNumber": item.OrderOrReturnedOrderNum})
if err != nil {
return nil, err
}
result.DividendsOrderAmount = order.DividendsOrderAmount
result.CustomerName = order.CustomerName
} else if item.DividendsType == 2 {
order, err := dividendsReturnedOrderRepository.FindOne(map[string]interface{}{"dividendsReturnedOrderNumber": item.OrderOrReturnedOrderNum})
if err != nil {
return nil, err
}
result.DividendsOrderAmount = order.DividendsReturnedOrderRefund
result.CustomerName = order.DividendsReturnedCustomerName
}
results = append(results, result)
}
return results, nil
}