pg_confirm_money_incentives_estimate_service.go
8.1 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
package domain_service
import (
"fmt"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/shopspring/decimal"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"time"
)
type ConfirmMoneyIncentivesEstimateService struct {
coreDomain.BaseEventPublisher
transactionContext *pgTransaction.TransactionContext
}
func (domainService *ConfirmMoneyIncentivesEstimateService) Confirm(contract *domain.CooperationContract, stage int32, undertakerUIDs []int64) ([]*service.DividendsEstimateDetail, error) {
var dividendsEstimateDao *dao.DividendsEstimateDao // 分红预算DAO
// 分红预算DAO初始化
if estimateDao, err := dao.NewDividendsEstimateDao(domainService.transactionContext); err != nil {
return nil, err
} else {
dividendsEstimateDao = estimateDao
}
// 金额激励详情
var dividendsEstimateDetails []*service.DividendsEstimateDetail
// 匹配金额激励规则
var moneyIncentivesRuleMatched *domain.MoneyIncentivesRule
for _, moneyIncentivesRule := range contract.MoneyIncentivesRules {
if moneyIncentivesRule.MoneyIncentivesStage == stage {
moneyIncentivesRuleMatched = moneyIncentivesRule
break
}
}
if moneyIncentivesRuleMatched == nil {
return nil, fmt.Errorf("未匹配到金额激励规则")
}
// 判断金额激励阶段是否合法
currentTime := time.Now()
if currentTime.Before(moneyIncentivesRuleMatched.MoneyIncentivesTime) {
return nil, fmt.Errorf("还未到分红时间")
}
// 金额激励预算
for _, undertaker := range contract.Undertakers {
if utils.IsContain64(undertakerUIDs, undertaker.UndertakerId) {
// 判断承接人在当前阶段是否已经分红
undertakerEstimated, err := dividendsEstimateDao.UserEstimated(map[string]interface{}{
"cooperationContractUndertakerId": undertaker.UndertakerId,
"dividendsParticipateType": int32(1), // 承接人类型
"cooperationContractNumber": contract.CooperationContractNumber,
"dividendsStage": moneyIncentivesRuleMatched.MoneyIncentivesStage,
"undertakerUid": undertaker.UserId,
"companyId": contract.Company.CompanyId,
"orgId": contract.Org.OrgId,
})
if err != nil {
return nil, err
}
if undertakerEstimated {
return nil, fmt.Errorf("承接人 " + undertaker.UserName + " 已分红")
} else {
undertakerDividendsAmount, _ := decimal.NewFromFloat(moneyIncentivesRuleMatched.MoneyIncentivesAmount).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.UserId,
UserBaseId: undertaker.UserBaseId,
Org: undertaker.Org,
Orgs: undertaker.Orgs,
Department: undertaker.Department,
Roles: undertaker.Roles,
UserInfo: undertaker.UserInfo,
UserType: undertaker.UserType,
UserName: undertaker.UserName,
UserPhone: undertaker.UserPhone,
Status: undertaker.Status,
Company: undertaker.Company,
},
DividendsParticipateType: 1,
DividendsStage: stage,
DividendsAmount: undertakerDividendsAmount,
CooperationContractUndertakerId: undertaker.UndertakerId,
})
}
// 判断业务员在当前阶段是否已经分红
if undertaker.Salesman != nil {
salesmanEstimated, err := dividendsEstimateDao.UserEstimated(map[string]interface{}{
"cooperationContractUndertakerId": undertaker.UndertakerId,
"dividendsParticipateType": int32(3), // 承接人类型
"cooperationContractNumber": contract.CooperationContractNumber,
"dividendsStage": moneyIncentivesRuleMatched.MoneyIncentivesStage,
"undertakerUid": undertaker.Salesman.UserId,
"companyId": contract.Company.CompanyId,
"orgId": contract.Org.OrgId,
})
if err != nil {
return nil, err
}
if salesmanEstimated {
return nil, fmt.Errorf("业务员 " + undertaker.Salesman.UserName + " 已分红")
} else {
undertakerDividendsAmount, _ := decimal.NewFromFloat(moneyIncentivesRuleMatched.MoneyIncentivesAmount).Mul(decimal.NewFromFloat(moneyIncentivesRuleMatched.SalesmanPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.Salesman.UserId,
UserBaseId: undertaker.Salesman.UserBaseId,
Org: undertaker.Salesman.Org,
Orgs: undertaker.Salesman.Orgs,
Department: undertaker.Salesman.Department,
Roles: undertaker.Salesman.Roles,
UserInfo: undertaker.Salesman.UserInfo,
UserType: undertaker.Salesman.UserType,
UserName: undertaker.Salesman.UserName,
UserPhone: undertaker.Salesman.UserPhone,
Company: undertaker.Salesman.Company,
},
DividendsParticipateType: 3,
DividendsStage: stage,
DividendsAmount: undertakerDividendsAmount,
CooperationContractUndertakerId: undertaker.UndertakerId,
DividendsPercentage: moneyIncentivesRuleMatched.SalesmanPercentage,
})
}
}
// 判断推荐人在当前阶段是否已经分红
if undertaker.Referrer != nil {
referrerEstimated, err := dividendsEstimateDao.UserEstimated(map[string]interface{}{
"cooperationContractUndertakerId": undertaker.UndertakerId,
"dividendsParticipateType": int32(2), // 承接人类型
"cooperationContractNumber": contract.CooperationContractNumber,
"dividendsStage": moneyIncentivesRuleMatched.MoneyIncentivesStage,
"undertakerUid": undertaker.Referrer.UserId,
"companyId": contract.Company.CompanyId,
"orgId": contract.Org.OrgId,
})
if err != nil {
return nil, err
}
if referrerEstimated {
return nil, fmt.Errorf("推荐人 " + undertaker.Referrer.UserName + " 已分红")
} else {
undertakerDividendsAmount, _ := decimal.NewFromFloat(moneyIncentivesRuleMatched.MoneyIncentivesAmount).Mul(decimal.NewFromFloat(moneyIncentivesRuleMatched.ReferrerPercentage).Div(decimal.NewFromFloat(100))).Float64()
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
DividendsUser: &domain.User{
UserId: undertaker.Referrer.UserId,
UserBaseId: undertaker.Referrer.UserBaseId,
Org: undertaker.Referrer.Org,
Orgs: undertaker.Referrer.Orgs,
Department: undertaker.Referrer.Department,
Roles: undertaker.Referrer.Roles,
UserInfo: undertaker.Referrer.UserInfo,
UserType: undertaker.Referrer.UserType,
UserName: undertaker.Referrer.UserName,
UserPhone: undertaker.Referrer.UserPhone,
Company: undertaker.Referrer.Company,
},
DividendsParticipateType: 2,
DividendsStage: stage,
DividendsAmount: undertakerDividendsAmount,
CooperationContractUndertakerId: undertaker.UndertakerId,
DividendsPercentage: moneyIncentivesRuleMatched.ReferrerPercentage,
})
}
}
}
}
return dividendsEstimateDetails, nil
}
func NewConfirmMoneyIncentivesEstimateService(transactionContext *pgTransaction.TransactionContext) (*ConfirmMoneyIncentivesEstimateService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为空")
} else {
return &ConfirmMoneyIncentivesEstimateService{
transactionContext: transactionContext,
}, nil
}
}