pg_cooperation_person_statistics_service.go
20.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package domain_service
import (
"fmt"
"github.com/go-pg/pg/v10"
"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/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/service_gateway"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
)
// 共创公司统计
func (ptr *CooperationStatisticsService) CooperationCompanyStatistics(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
UserBaseId int64 `json:"userBaseId" valid:"Required"`
CompanyList []int64 `json:"companyList" valid:"Required"` //公司列表 待统计的公司列表
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
var response = make([]*cooperationCompanyStatisticsResponse, 0)
var totalDividend float64
for i := range request.CompanyList {
orgId := request.CompanyList[i]
item, err := ptr.cooperationCompanyStatistics(request.UserBaseId, orgId)
if err != nil {
return response, err
}
totalDividend += item.DividendsIncome
response = append(response, item)
}
if totalDividend > 0 {
for i := range response {
response[i].DividendsRatio = utils.Round(response[i].DividendsIncome/totalDividend*100, 2)
}
}
return response, nil
}
func (ptr *CooperationStatisticsService) cooperationCompanyStatistics(userBaseId int64, orgId int64) (*cooperationCompanyStatisticsResponse, error) {
// 1.相关项目统计
cooperationApplicationRepository, _ := repository.NewCooperationApplicationRepository(ptr.transactionContext)
cooperationProjectCount, _, err := cooperationApplicationRepository.Find(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId,
"limit": 1, "cooperationApplicationStatus": int32(2)})
if err != nil {
return nil, err
}
// 2.相关合约统计
contractInfo, err := ptr.PersonCooperationContractStatistics(map[string]interface{}{
"orgId": orgId,
"userBaseId": userBaseId,
})
if err != nil {
return nil, err
}
//cooperationContractRelevantRepository, _ := dao.NewCooperationContractUndertakerDao(ptr.transactionContext)
//cooperationContractCount, _, err := cooperationContractRelevantRepository.Find(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId,
// "limit": 100,"distinctColumnContractNumber":1})
//if err != nil {
// return nil, err
//}
// 3.个人分红统计
creditAccountDao, _ := dao.NewDividendsEstimateDao(ptr.transactionContext)
var allDividends float64
if allDividends, err = creditAccountDao.CountDividendsEstimateDividendsAmount(map[string]interface{}{"userBaseId": userBaseId, "orgId": orgId}); err != nil {
return nil, err
}
var ret = &cooperationCompanyStatisticsResponse{
OrgId: orgId,
CooperationProjectCount: cooperationProjectCount,
CooperationContractCount: int64(contractInfo.ContractSum),
DividendsIncome: utils.Round(allDividends, 2),
}
return ret, nil
}
type cooperationCompanyStatisticsResponse struct {
// 当天统计的企业id
OrgId int64 `json:"orgId"`
// 共创项目数
CooperationProjectCount int64 `json:"cooperationProjectCount"`
// 共创合约数
CooperationContractCount int64 `json:"cooperationContractCount"`
// 分红占比
DividendsRatio float64 `json:"dividendsRatio"`
// 分红支出
DividendsIncome float64 `json:"dividendsIncome"`
}
// 个人-用户合约统计
func (ptr *CooperationStatisticsService) PersonCooperationContractStatistics(queryOptions map[string]interface{}) (*ContractSum, error) {
// 参数验证
var request = struct {
UserBaseId int64 `json:"userBaseId" valid:"Required"`
OrgId int64 `json:"orgId"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
queryOptions["limit"] = 10000 //TODO:合约数大于10000时?
cooperationContractUndertaker, _ := dao.NewCooperationContractUndertakerDao(ptr.transactionContext)
_, contractUndertakers, err := cooperationContractUndertaker.Find(queryOptions)
if err != nil {
return nil, nil
}
var response = &ContractSum{}
if len(contractUndertakers) == 0 {
return response, nil
}
var contractIds []int64
for i := range contractUndertakers {
contractIds = append(contractIds, contractUndertakers[i].CooperationContractId)
}
response.ContractSum = ptr.cooperationContractCount(contractIds, 0)
response.ContractStoppedSum = ptr.cooperationContractCount(contractIds, 2)
return response, nil
}
type ContractSum struct {
ContractSum int `json:"contractSum"`
ContractStoppedSum int `json:"contractStoppedSum"`
}
func (ptr *CooperationStatisticsService) cooperationContractCount(contractIds []int64, status int) int {
var total int
var contract = new(models.CooperationContract)
query := ptr.transactionContext.PgDd.Model(contract)
query.ColumnExpr("count(*) total")
query.Where("cooperation_contract_id in (?)", pg.In(contractIds))
if status > 0 {
query.Where("status =? ", status)
}
query.Select(&total)
return total
}
// 个人 - 企业支付统计
func (ptr *CooperationStatisticsService) PersonCompanyPaymentHistoryStatistics(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
Limit int `json:"limit" valid:"Required"`
Offset int `json:"offset"`
UserBaseId int64 `json:"userBaseId" valid:"Required"`
OrgId int64 `json:"orgId"`
SortByActuallyPaidAmount int `json:"sortByActuallyPaidAmount" valid:"Required"`
PaymentBeginTime time.Time `json:"paymentBeginTime"`
PaymentEndTime time.Time `json:"paymentEndTime"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
type companyStatisticsResponse struct {
CooperationTime int64 `json:"cooperationTime"`
DividendsOrderAmount float64 `json:"dividendsOrderAmount"`
DividesAmount float64 `json:"dividesAmount"` // 分红金额
ActuallyPaidAmount float64 `json:"actuallyPaidAmount"` // 已支付金额
UnPaidAmount float64 `json:"unPaidAmount"` // 未支付金额
OrgId int64 `json:"orgId,string"`
OrgName string `json:"orgName"`
Participator interface{} `json:"participator"`
}
var responses []companyStatisticsResponse
creditAccountDao, _ := dao.NewCreditAccountDao(ptr.transactionContext)
if err := creditAccountDao.CooperationCompanyDividendsStatistics(queryOptions, &responses); err != nil {
return nil, err
}
var retMap = make([]interface{}, 0)
for i := range responses {
retMap = append(retMap, map[string]interface{}{
"paymentAmount": responses[i].ActuallyPaidAmount,
"org": map[string]interface{}{
"orgId": responses[i].OrgId,
"orgName": responses[i].OrgName,
},
})
}
return retMap, nil
}
// 个人 - 共创浏览 详情页
func (ptr *CooperationStatisticsService) PersonCooperationProjectSharedInfo(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
UserBaseId int64 `json:"userBaseId"`
// 项目ID
ProjectId int64 `json:"projectId" valid:"Required"`
// 敏感数据
Sensitive bool `json:"sensitive" valid:"Required"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
queryOptions = tool_funs.SimpleStructToMap(&request)
var response = domain.CooperationProjectSharedInfoDto{
ContractParticipant: make([]*domain.ContractParticipant, 0),
}
projectRepository, _ := repository.NewCooperationProjectRepository(ptr.transactionContext)
project, err := projectRepository.FindOne(map[string]interface{}{"cooperationProjectId": request.ProjectId})
if err != nil {
return nil, fmt.Errorf("共创项目不存在")
}
// 0.共创项目数据
updateAt := project.UpdatedAt
if project.UpdatedAt.IsZero() {
updateAt = project.CreatedAt
}
response.Project = map[string]interface{}{
"cooperationProjectId": fmt.Sprintf("%v", project.CooperationProjectId),
"attachment": project.Attachment,
"cooperationProjectDescription": project.CooperationProjectDescription,
"cooperationProjectName": project.CooperationProjectName,
//"cooperationProjectDescription":project.CooperationProjectDescription,
"updatedAt": updateAt.Unix() * 1000,
}
// 0.1 组织数据
response.Org = project.Org
if request.UserBaseId != 0 {
// 当前用户对于项目组织的关注状态
userServiceGateway := service_gateway.NewHttplibUserServiceGateway()
userDetail, e := userServiceGateway.GetUserInfo(0, 0, request.UserBaseId)
if e == nil && userDetail != nil {
response.OrgStarred = userDetail.CheckOrgStarred(project.Org.OrgId)
}
}
// 0.2 合约数据
contractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext) // repository.NewCooperationContractRepository(ptr.transactionContext)
_, contracts, _ := contractRepository.Find(map[string]interface{}{"cooperationProjectNumber": project.CooperationProjectNumber, "companyId": project.Company.CompanyId, "orgId": project.Org.OrgId, "offsetLimit": false})
if len(contracts) == 0 {
return response, nil
}
contractNumbers := make([]string, 0)
mapContract := make(map[string]*domain.CooperationContract)
for i := range contracts {
contractNumbers = append(contractNumbers, contracts[i].CooperationContractNumber)
mapContract[contracts[i].CooperationContractNumber] = contracts[i]
}
keyfun := func(num string, userBaseId int64) string {
return num + "-" + fmt.Sprintf("%v", userBaseId)
}
// 1.项目的承接人
undertakerRepository, _ := repository.NewCooperationContractUndertakerRepository(ptr.transactionContext)
_, undertakers, err := undertakerRepository.Find(map[string]interface{}{"cooperationContractNumbers": contractNumbers, "companyId": project.Company.CompanyId, "orgId": project.Org.OrgId, "offsetLimitFlag": false})
userSorted := make([]string, 0)
mapUser := make(map[string]*domain.ContractParticipant)
for i := range undertakers {
u := undertakers[i]
key := keyfun(u.CooperationContractNumber, u.Undertaker.UserBaseId)
userSorted = append(userSorted, key)
mapUser[keyfun(u.CooperationContractNumber, u.Undertaker.UserBaseId)] = domain.NewContractParticipant(u.Undertaker.ToUser(), key, u.Undertaker.ContractAttachment)
if u.Undertaker.Referrer != nil {
key = keyfun(u.CooperationContractNumber, u.Undertaker.Referrer.UserBaseId)
userSorted = append(userSorted, key)
mapUser[keyfun(u.CooperationContractNumber, u.Undertaker.Referrer.UserBaseId)] = domain.NewContractParticipant(u.Undertaker.Referrer.ToUser(), key, u.Undertaker.ContractAttachment)
}
if u.Undertaker.Salesman != nil {
userSorted = append(userSorted, keyfun(u.CooperationContractNumber, u.Undertaker.Salesman.UserBaseId))
mapUser[keyfun(u.CooperationContractNumber, u.Undertaker.Salesman.UserBaseId)] = domain.NewContractParticipant(u.Undertaker.Salesman.ToUser(), key, u.Undertaker.ContractAttachment)
}
}
// 2.合约的订单金额
mapContractOrderAmount := make(map[string]float64)
dividendsOrderDao, _ := dao.NewDividendsOrderDao(ptr.transactionContext)
mapContractOrderAmount, _ = dividendsOrderDao.CalculateGoodOrderAmountByGroup(map[string]interface{}{
"cooperationContractNumbers": contractNumbers, "companyId": project.Company.CompanyId, "orgId": project.Org.OrgId,
})
// 3.计算合约用户的分红预算金额
var mapCreditAccount = make(map[string]*domain.CreditAccount)
var creditAccountIds = make([]int64, 0)
dividendsEstimateRepository, _ := repository.NewDividendsEstimateRepository(ptr.transactionContext)
_, dividends, err := dividendsEstimateRepository.Find(map[string]interface{}{"cooperationContractNumbers": contractNumbers, "companyId": project.Company.CompanyId, "orgId": project.Org.OrgId, "isCanceled": false, "offsetLimit": false})
if err != nil {
return nil, err
}
for i := range dividends {
d := dividends[i]
key := keyfun(d.CooperationContractNumber, d.DividendsUser.UserBaseId)
if v, ok := mapUser[key]; ok {
v.DividendsAmount += d.DividendsAmount
}
if d.PaymentStatus == 2 && d.CreditAccountId != 0 {
creditAccountIds = append(creditAccountIds, d.CreditAccountId)
}
}
// 3.1 用户账期结算记录 - 查询是否有支付凭证
if len(creditAccountIds) > 0 {
creditAccountRepository, _ := repository.NewCreditAccountRepository(ptr.transactionContext)
_, creditAccounts, err := creditAccountRepository.Find(map[string]interface{}{
"creditAccountIds": creditAccountIds,
})
if err != nil {
return nil, err
}
for i := range creditAccounts {
if len(creditAccounts[i].PaymentDocumentAttachments) == 0 {
continue
}
for j := range creditAccounts[i].AccountDetail {
detail := creditAccounts[i].AccountDetail[j]
key := keyfun(detail.CooperationContractNumber, creditAccounts[i].Participator.UserBaseId)
mapCreditAccount[key] = creditAccounts[i]
if v, ok := mapUser[key]; ok && len(v.PaymentDocument) == 0 {
if len(mapCreditAccount[key].PaymentDocumentAttachments) > 0 {
v.PaymentDocument = "查看"
}
}
}
}
}
for _, v := range mapUser {
if v1, ok := mapContractOrderAmount[v.CooperationContractNumber()]; ok {
v.OrderAmount = v1
}
if v1, ok := mapContract[v.CooperationContractNumber()]; ok {
v.Contract = map[string]interface{}{
"cooperationContractId": fmt.Sprintf("%v", v1.CooperationContractId),
"cooperationContractName": v1.CooperationContractName,
"status": v1.Status,
}
}
}
for i := range userSorted {
if v, ok := mapUser[userSorted[i]]; ok {
response.ContractParticipant = append(response.ContractParticipant, v.Complete(request.UserBaseId, request.Sensitive))
}
}
return response, nil
}
// 个人 - 共创浏览 合约列表
func (ptr *CooperationStatisticsService) CompanyCooperationProjectContracts(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
Offset int `json:"offset"`
Limit int `json:"limit"`
// 项目ID
ProjectId int64 `json:"projectId" valid:"Required"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
projectRepository, _ := repository.NewCooperationProjectRepository(ptr.transactionContext)
project, err := projectRepository.FindOne(map[string]interface{}{"cooperationProjectId": request.ProjectId})
if err != nil {
return nil, fmt.Errorf("共创项目不存在")
}
// 0.2 合约数据
contractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext) // repository.NewCooperationContractRepository(ptr.transactionContext)
_, contracts, _ := contractRepository.Find(map[string]interface{}{"cooperationProjectNumber": project.CooperationProjectNumber,
"companyId": project.Company.CompanyId,
"orgId": project.Org.OrgId,
"limit": request.Limit,
"offset": request.Offset,
})
if len(contracts) == 0 {
return nil, nil
}
contractNumbers := make([]string, 0)
mapContract := make(map[string]*domain.CooperationContract)
for i := range contracts {
contractNumbers = append(contractNumbers, contracts[i].CooperationContractNumber)
mapContract[contracts[i].CooperationContractNumber] = contracts[i]
}
// 2.合约的订单金额
mapContractOrderAmount := make(map[string]float64)
dividendsOrderDao, _ := dao.NewDividendsOrderDao(ptr.transactionContext)
mapContractOrderAmount, _ = dividendsOrderDao.CalculateGoodOrderAmountByGroup(map[string]interface{}{
"cooperationContractNumbers": contractNumbers, "companyId": project.Company.CompanyId, "orgId": project.Org.OrgId,
})
// 3.计算合约用户的分红预算金额
dividendsEstimateRepository, _ := dao.NewDividendsEstimateDao(ptr.transactionContext) //repository.NewDividendsEstimateRepository(ptr.transactionContext)
mapDividends, err := dividendsEstimateRepository.DividendsEstimateStatisticsGroup(map[string]interface{}{"cooperationContractNumbers": contractNumbers, "companyId": project.Company.CompanyId, "orgId": project.Org.OrgId})
if err != nil {
return nil, err
}
results := make([]interface{}, 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,
}
if v, ok := mapContractOrderAmount[contracts[i].CooperationContractNumber]; ok {
resultItem.DividendsOrderAmount = v
}
if v, ok := mapDividends[contracts[i].CooperationContractNumber]; ok {
resultItem.DividendsAmount = v
}
results = append(results, resultItem)
}
return results, nil
}
// func(ptr *CooperationStatisticsService)
// 个人 - 共创浏览 详情页 - 附件数据
// response,error 有可能同时为空,返回需要判断
func (ptr *CooperationStatisticsService) PersonCooperationProjectSharedInfoAttachment(queryOptions map[string]interface{}) (interface{}, error) {
// 参数验证
var request = struct {
UserBaseId int64 `json:"userBaseId"`
// 用户
UserId int64 `json:"userId" valid:"Required"`
// 项目ID
ProjectId int64 `json:"cooperationProjectId" valid:"Required"`
// 合约ID
ContractId int64 `json:"cooperationContractId" valid:"Required"`
// 附件类型 1:合约附件 2:支付凭证
AttachmentType int `json:"attachmentType" valid:"Required"`
}{}
if err := LoadQueryObject(queryOptions, &request); err != nil {
return nil, err
}
var response = struct {
Attachment *domain.Attachment `json:"attachment"`
UserBaseId int64 `json:"userBaseId"`
}{}
userServiceGateway := service_gateway.NewHttplibUserServiceGateway()
userDetail, e := userServiceGateway.GetUser(0, 0, request.UserId)
if e != nil {
return nil, e
}
request.UserBaseId = userDetail.UserBaseId
response.UserBaseId = userDetail.UserBaseId
switch request.AttachmentType {
case 1:
contractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext) // repository.NewCooperationContractRepository(ptr.transactionContext)
contracts, err := contractRepository.FindOne(map[string]interface{}{"cooperationContractId": request.ContractId})
if err != nil {
return nil, fmt.Errorf("合约不存在")
}
undertakerRepository, _ := dao.NewCooperationContractUndertakerDao(ptr.transactionContext)
_, undertakers, err := undertakerRepository.Find(map[string]interface{}{
"userBaseId": request.UserBaseId,
"cooperationContractIds": []int64{contracts.CooperationContractId},
})
if err != nil {
return nil, err
}
for i := range undertakers {
if len(undertakers[i].Undertaker.ContractAttachment) == 0 {
continue
}
response.Attachment = undertakers[i].Undertaker.ContractAttachment[0]
break
}
case 2:
// 0.2 合约数据
contractRepository, _ := dao.NewCooperationContractDao(ptr.transactionContext) // repository.NewCooperationContractRepository(ptr.transactionContext)
contracts, err := contractRepository.FindOne(map[string]interface{}{"cooperationContractId": request.ContractId})
if err != nil {
return nil, fmt.Errorf("合约不存在")
}
dividendsEstimateRepository, _ := repository.NewDividendsEstimateRepository(ptr.transactionContext)
_, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{
"orgId": contracts.Org.OrgId,
"companyId": contracts.Company.CompanyId,
"cooperationContractNumber": contracts.CooperationContractNumber,
"isCanceled": false,
"paymentStatus": int32(2),
})
if err != nil {
return nil, err
}
var estimateIds []int64
if len(dividendsEstimates) == 0 {
return response, nil
}
for i := range dividendsEstimates {
estimateIds = append(estimateIds, dividendsEstimates[i].DividendsEstimateId)
}
creditAccountRepository, _ := repository.NewCreditAccountRepository(ptr.transactionContext)
_, creditAccount, err := creditAccountRepository.Find(map[string]interface{}{
"userBaseId": request.UserBaseId,
"orgId": contracts.Org.OrgId,
"companyId": contracts.Company.CompanyId,
"dividendsEstimateOrderIds": estimateIds,
})
if err != nil {
return nil, err
}
if len(creditAccount) == 0 {
return response, nil
}
for i := range creditAccount {
if len(creditAccount[i].PaymentDocumentAttachments) == 0 {
continue
}
response.Attachment = creditAccount[i].PaymentDocumentAttachments[0] // 获取第一个
break
}
default:
return nil, fmt.Errorf("unkown attachment type %v", request.AttachmentType)
}
return response, nil
}