|
@@ -5,6 +5,7 @@ import ( |
|
@@ -5,6 +5,7 @@ import ( |
5
|
"github.com/linmadan/egglib-go/core/application"
|
5
|
"github.com/linmadan/egglib-go/core/application"
|
6
|
coreDomain "github.com/linmadan/egglib-go/core/domain"
|
6
|
coreDomain "github.com/linmadan/egglib-go/core/domain"
|
7
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
7
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
8
|
+ "github.com/shopspring/decimal"
|
8
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
|
9
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
|
9
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
|
10
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
|
10
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
|
11
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
|
|
@@ -18,37 +19,62 @@ type ConfirmDividendsIncentivesEstimateService struct { |
|
@@ -18,37 +19,62 @@ type ConfirmDividendsIncentivesEstimateService struct { |
18
|
// Confirm 确认业绩分红预算
|
19
|
// Confirm 确认业绩分红预算
|
19
|
func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoods []*domain.OrderGood) ([]*service.DividendsEstimateDetail, error) {
|
20
|
func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoods []*domain.OrderGood) ([]*service.DividendsEstimateDetail, error) {
|
20
|
var cooperationContractRepository domain.CooperationContractRepository // 共创合约仓储
|
21
|
var cooperationContractRepository domain.CooperationContractRepository // 共创合约仓储
|
|
|
22
|
+ var cooperationProjectRepository domain.CooperationProjectRepository // 共创项目仓储
|
21
|
var dividendsOrderRepository domain.DividendsOrderRepository // 分红订单仓储
|
23
|
var dividendsOrderRepository domain.DividendsOrderRepository // 分红订单仓储
|
22
|
var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository // 分红退货单仓储
|
24
|
var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository // 分红退货单仓储
|
|
|
25
|
+
|
23
|
// 共创合约仓储初始化
|
26
|
// 共创合约仓储初始化
|
24
|
if repo, err := repository.NewCooperationContractRepository(domainService.transactionContext); err != nil {
|
27
|
if repo, err := repository.NewCooperationContractRepository(domainService.transactionContext); err != nil {
|
25
|
return nil, err
|
28
|
return nil, err
|
26
|
} else {
|
29
|
} else {
|
27
|
cooperationContractRepository = repo
|
30
|
cooperationContractRepository = repo
|
28
|
}
|
31
|
}
|
|
|
32
|
+
|
|
|
33
|
+ // 共创项目仓储初始化
|
|
|
34
|
+ if repo, err := repository.NewCooperationProjectRepository(domainService.transactionContext); err != nil {
|
|
|
35
|
+ return nil, err
|
|
|
36
|
+ } else {
|
|
|
37
|
+ cooperationProjectRepository = repo
|
|
|
38
|
+ }
|
|
|
39
|
+
|
29
|
// 分红订单仓储初始化
|
40
|
// 分红订单仓储初始化
|
30
|
if repo, err := repository.NewDividendsOrderRepository(domainService.transactionContext); err != nil {
|
41
|
if repo, err := repository.NewDividendsOrderRepository(domainService.transactionContext); err != nil {
|
31
|
return nil, err
|
42
|
return nil, err
|
32
|
} else {
|
43
|
} else {
|
33
|
dividendsOrderRepository = repo
|
44
|
dividendsOrderRepository = repo
|
34
|
}
|
45
|
}
|
|
|
46
|
+
|
35
|
// 分红退货单仓储初始化
|
47
|
// 分红退货单仓储初始化
|
36
|
if repo, err := repository.NewDividendsReturnedOrderRepository(domainService.transactionContext); err != nil {
|
48
|
if repo, err := repository.NewDividendsReturnedOrderRepository(domainService.transactionContext); err != nil {
|
37
|
return nil, err
|
49
|
return nil, err
|
38
|
} else {
|
50
|
} else {
|
39
|
dividendsReturnedOrderRepository = repo
|
51
|
dividendsReturnedOrderRepository = repo
|
40
|
}
|
52
|
}
|
|
|
53
|
+
|
41
|
// 确认业绩分红预算
|
54
|
// 确认业绩分红预算
|
42
|
var dividendsEstimateDetails []*service.DividendsEstimateDetail
|
55
|
var dividendsEstimateDetails []*service.DividendsEstimateDetail
|
43
|
for _, orderGood := range orderGoods {
|
56
|
for _, orderGood := range orderGoods {
|
44
|
// 获取合约
|
57
|
// 获取合约
|
45
|
- cooperationContract, err := cooperationContractRepository.FindOne(map[string]interface{}{"cooperationContractNumber": orderGood.CooperationContractNumber})
|
58
|
+ cooperationContract, err := cooperationContractRepository.FindOne(map[string]interface{}{
|
|
|
59
|
+ "cooperationContractNumber": orderGood.CooperationContractNumber,
|
|
|
60
|
+ })
|
46
|
if err != nil {
|
61
|
if err != nil {
|
47
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
62
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
48
|
}
|
63
|
}
|
49
|
if cooperationContract == nil {
|
64
|
if cooperationContract == nil {
|
50
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("共创合约%s不存在", orderGood.CooperationContractNumber))
|
65
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("共创合约%s不存在", orderGood.CooperationContractNumber))
|
51
|
}
|
66
|
}
|
|
|
67
|
+ // TODO 校验合约关联的项目是否已结束
|
|
|
68
|
+ cooperationProject, err := cooperationProjectRepository.FindOne(map[string]interface{}{
|
|
|
69
|
+ "cooperationProjectNumber": cooperationContract.CooperationProjectNumber,
|
|
|
70
|
+ })
|
|
|
71
|
+ if err != nil {
|
|
|
72
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
73
|
+ }
|
|
|
74
|
+ if cooperationProject == nil {
|
|
|
75
|
+ return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("共创项目%s不存在", cooperationContract.CooperationProjectNumber))
|
|
|
76
|
+ }
|
|
|
77
|
+
|
52
|
if orderGood.DividendsOrderNumber != "" {
|
78
|
if orderGood.DividendsOrderNumber != "" {
|
53
|
// 获取分红订单
|
79
|
// 获取分红订单
|
54
|
dividendsOrder, err2 := dividendsOrderRepository.FindOne(map[string]interface{}{
|
80
|
dividendsOrder, err2 := dividendsOrderRepository.FindOne(map[string]interface{}{
|
|
@@ -78,8 +104,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
|
@@ -78,8 +104,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
78
|
if dividendsIncentivesRuleMatched != nil {
|
104
|
if dividendsIncentivesRuleMatched != nil {
|
79
|
for _, undertaker := range cooperationContract.Undertakers {
|
105
|
for _, undertaker := range cooperationContract.Undertakers {
|
80
|
// 添加承接人分红预算信息详情
|
106
|
// 添加承接人分红预算信息详情
|
81
|
- // TODO 使用decimal提高精度
|
|
|
82
|
- undertakerDividendsAmount := orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.DividendsIncentivesPercentage / 100
|
107
|
+ undertakerDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.DividendsIncentivesPercentage).Div(decimal.NewFromFloat(100))).Float64()
|
83
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
108
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
84
|
DividendsUser: &domain.User{
|
109
|
DividendsUser: &domain.User{
|
85
|
UserId: undertaker.UserId,
|
110
|
UserId: undertaker.UserId,
|
|
@@ -101,7 +126,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
|
@@ -101,7 +126,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
101
|
})
|
126
|
})
|
102
|
// 添加推荐人分红预算信息详情
|
127
|
// 添加推荐人分红预算信息详情
|
103
|
if undertaker.Referrer != nil {
|
128
|
if undertaker.Referrer != nil {
|
104
|
- referrerDividendsAmount := orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.ReferrerPercentage / 100
|
129
|
+ referrerDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.ReferrerPercentage).Div(decimal.NewFromFloat(100))).Float64()
|
105
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
130
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
106
|
DividendsUser: &domain.User{
|
131
|
DividendsUser: &domain.User{
|
107
|
UserId: undertaker.Referrer.UserId,
|
132
|
UserId: undertaker.Referrer.UserId,
|
|
@@ -123,7 +148,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
|
@@ -123,7 +148,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
123
|
}
|
148
|
}
|
124
|
// 添加关联业务员分红预算信息详情
|
149
|
// 添加关联业务员分红预算信息详情
|
125
|
if undertaker.Salesman != nil {
|
150
|
if undertaker.Salesman != nil {
|
126
|
- salesmanDividendsAmount := orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.SalesmanPercentage / 100
|
151
|
+ salesmanDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.SalesmanPercentage).Div(decimal.NewFromFloat(100))).Float64()
|
127
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
152
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
128
|
DividendsUser: &domain.User{
|
153
|
DividendsUser: &domain.User{
|
129
|
UserId: undertaker.Salesman.UserId,
|
154
|
UserId: undertaker.Salesman.UserId,
|
|
@@ -166,7 +191,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
|
@@ -166,7 +191,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
166
|
// 计算分红
|
191
|
// 计算分红
|
167
|
for _, undertaker := range cooperationContract.Undertakers {
|
192
|
for _, undertaker := range cooperationContract.Undertakers {
|
168
|
// 添加承接人分红退货预算信息详情
|
193
|
// 添加承接人分红退货预算信息详情
|
169
|
- undertakerDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.DividendsIncentivesPercentage / 100
|
194
|
+ undertakerDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.DividendsIncentivesPercentage).Div(decimal.NewFromFloat(100))).Float64()
|
170
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
195
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
171
|
DividendsUser: &domain.User{
|
196
|
DividendsUser: &domain.User{
|
172
|
UserId: undertaker.UserId,
|
197
|
UserId: undertaker.UserId,
|
|
@@ -184,11 +209,11 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
|
@@ -184,11 +209,11 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
184
|
},
|
209
|
},
|
185
|
DividendsParticipateType: domain.UNDERTAKER,
|
210
|
DividendsParticipateType: domain.UNDERTAKER,
|
186
|
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
|
211
|
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
|
187
|
- DividendsAmount: undertakerDividendsAmount,
|
212
|
+ DividendsAmount: -undertakerDividendsAmount,
|
188
|
})
|
213
|
})
|
189
|
// 添加推荐人分红退货预算信息详情
|
214
|
// 添加推荐人分红退货预算信息详情
|
190
|
if undertaker.Referrer != nil {
|
215
|
if undertaker.Referrer != nil {
|
191
|
- referrerDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.ReferrerPercentage / 100
|
216
|
+ referrerDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.ReferrerPercentage).Div(decimal.NewFromFloat(100))).Float64()
|
192
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
217
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
193
|
DividendsUser: &domain.User{
|
218
|
DividendsUser: &domain.User{
|
194
|
UserId: undertaker.Referrer.UserId,
|
219
|
UserId: undertaker.Referrer.UserId,
|
|
@@ -205,12 +230,12 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
|
@@ -205,12 +230,12 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
205
|
},
|
230
|
},
|
206
|
DividendsParticipateType: domain.REFERRER,
|
231
|
DividendsParticipateType: domain.REFERRER,
|
207
|
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
|
232
|
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
|
208
|
- DividendsAmount: referrerDividendsAmount,
|
233
|
+ DividendsAmount: -referrerDividendsAmount,
|
209
|
})
|
234
|
})
|
210
|
}
|
235
|
}
|
211
|
// 添加关联业务员分红退货预算信息详情
|
236
|
// 添加关联业务员分红退货预算信息详情
|
212
|
if undertaker.Salesman != nil {
|
237
|
if undertaker.Salesman != nil {
|
213
|
- salesmanDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.SalesmanPercentage / 100
|
238
|
+ salesmanDividendsAmount, _ := decimal.NewFromFloat(orderGood.OrderGoodAmount).Mul(decimal.NewFromFloat(dividendsIncentivesRuleMatched.SalesmanPercentage).Div(decimal.NewFromFloat(100))).Float64()
|
214
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
239
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
215
|
DividendsUser: &domain.User{
|
240
|
DividendsUser: &domain.User{
|
216
|
UserId: undertaker.Salesman.UserId,
|
241
|
UserId: undertaker.Salesman.UserId,
|
|
@@ -227,7 +252,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
|
@@ -227,7 +252,7 @@ func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoo |
227
|
},
|
252
|
},
|
228
|
DividendsParticipateType: domain.SALESMAN,
|
253
|
DividendsParticipateType: domain.SALESMAN,
|
229
|
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
|
254
|
DividendsStage: dividendsIncentivesRuleMatched.DividendsIncentivesStage,
|
230
|
- DividendsAmount: salesmanDividendsAmount,
|
255
|
+ DividendsAmount: -salesmanDividendsAmount,
|
231
|
})
|
256
|
})
|
232
|
}
|
257
|
}
|
233
|
}
|
258
|
}
|