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
package cn.wisenergy.service.Manager;
import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.mapper.*;
import cn.wisenergy.model.app.*;
import cn.wisenergy.model.enums.RebateStatusEnum;
import cn.wisenergy.model.enums.TradeRecordEnum;
import cn.wisenergy.model.enums.TradeStatusEnum;
import cn.wisenergy.model.vo.TeamPerformanceSortVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author 86187
* @ Description: 账户管理公共类
* @ Author : 86187
* @ Date : 2021/2/23 10:43
*/
@Component
@Slf4j
public class AccountManager {
@Autowired
private OrderMapper orderMapper;
@Autowired
private AccountMapper accountMapper;
@Autowired
private TradeRecordMapper recordMapper;
@Autowired
private TeamPerformanceMapper teamPerformanceMapper;
@Autowired
private ProgressPrizeMapper progressPrizeMapper;
@Autowired
private TradeRecordMapper tradeRecordMapper;
@Autowired
private MonthManureMapper monthManureMapper;
private static final String PATTERN = "yyyy-MM";
/**
* 保存用户佣金
*
* @param orderInfo 订单信息
* @param accountInfo 账户信息
* @param memberPercent 会员等级百比分
*/
@Transactional(rollbackFor = Exception.class)
public void updateOrderAddMoney(OrderInfo orderInfo, AccountInfo accountInfo, MemberPercent memberPercent) {
//1、计算返佣金额
BigDecimal bigDecimal = orderInfo.getPayment().multiply(memberPercent.getPercent());
BigDecimal extractMoney = accountInfo.getExtractMoney().add(bigDecimal);
accountInfo.setExtractMoney(extractMoney.setScale(2, RoundingMode.HALF_UP));
BigDecimal performanceMonth = accountInfo.getEarningsMonth().add(bigDecimal);
accountInfo.setEarningsMonth(performanceMonth.setScale(2, RoundingMode.HALF_UP));
BigDecimal performanceTotal = accountInfo.getEarningsTotal().add(bigDecimal);
accountInfo.setEarningsTotal(performanceTotal.setScale(2, RoundingMode.HALF_UP));
//2、修改订单返佣状态:已返佣 1
orderInfo.setRebateStatus(RebateStatusEnum.ALREADY_REBATE.getCode());
orderMapper.updateById(orderInfo);
//3、更新账户可用金额
accountMapper.edit(accountInfo);
//4、添加交易流水记录
TradeRecord tradeRecord = new TradeRecord();
tradeRecord.setUserId(orderInfo.getUserId());
tradeRecord.setTradeType(TradeRecordEnum.ORDER_REBATE.getCode());
tradeRecord.setTradeNo(orderInfo.getTid());
tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
recordMapper.add(tradeRecord);
}
@Transactional(rollbackFor = Exception.class)
public Boolean updateAccountPerformanceMonth(List<TeamPerformance> addList, List<TeamPerformance> updateList) {
//1、新增
if (!CollectionUtils.isEmpty(addList)) {
for (TeamPerformance teamPerformance : addList) {
int count = teamPerformanceMapper.add(teamPerformance);
if (count == 0) {
return false;
}
}
}
//2、更新
if (!CollectionUtils.isEmpty(updateList)) {
for (TeamPerformance teamPerformance : updateList) {
int count = teamPerformanceMapper.edit(teamPerformance);
if (count == 0) {
return false;
}
}
}
return true;
}
/**
* 修改 或新增 删除 最大进步奖实体类
*
* @param listVo 最新的进步奖 对象
* @param accountInfoList 要更新的最大进步奖账户收益
* @param prizes 上一次统计的进步奖 对象
*/
@Transactional(rollbackFor = Exception.class)
public Boolean updateOrSavePrize(List<TeamPerformanceSortVo> listVo, List<AccountInfo> accountInfoList,
List<ProgressPrize> prizes, int flag) {
Date date = new Date();
String yearMonth = DateUtil.convertDateToStr(date, PATTERN);
//构造一个map key:userId value progressPrize
Map<String, ProgressPrize> map = new HashMap<>(32);
for (ProgressPrize progressPrize : prizes) {
map.put(progressPrize.getUserId(), progressPrize);
}
//1、判断 prizes 是否为空集合,是新增
//新增
if (CollectionUtils.isEmpty(prizes)) {
for (TeamPerformanceSortVo sortVo : listVo) {
String userId = sortVo.getTeamPerformance().getUserId();
ProgressPrize progressPrize = new ProgressPrize();
progressPrize.setUserId(userId);
progressPrize.setGrowthRate(sortVo.getGrowthRate());
progressPrize.setAwardMoney(sortVo.getMonthPerformance());
progressPrize.setYearMonth(yearMonth);
int count = progressPrizeMapper.add(progressPrize);
if (count == 0) {
return false;
}
}
} else {
//2、不为空。存在的修改,不存在的新增,多余的,删除
List<ProgressPrize> updateList = new ArrayList<>();
List<ProgressPrize> addList = new ArrayList<>();
List<String> userIds = prizes.stream().map(ProgressPrize::getUserId).collect(Collectors.toList());
//遍历 筛选出要修改、新增、删除 对象
for (TeamPerformanceSortVo sortVo : listVo) {
if (userIds.contains(sortVo.getUserId())) {
//修改
ProgressPrize prize = map.get(sortVo.getUserId());
prize.setGrowthRate(sortVo.getGrowthRate());
prize.setAwardMoney(sortVo.getMonthPerformance());
updateList.add(prize);
//移除要修改的
prizes.remove(prize);
} else {
//新增
String userId = sortVo.getTeamPerformance().getUserId();
ProgressPrize progress = new ProgressPrize();
progress.setUserId(userId);
progress.setGrowthRate(sortVo.getGrowthRate());
progress.setAwardMoney(sortVo.getMonthPerformance());
progress.setYearMonth(yearMonth);
addList.add(progress);
}
}
//剩余的是要删除的
List<ProgressPrize> deleteList = new ArrayList<>(prizes);
for (ProgressPrize updatePrize : updateList) {
int count = progressPrizeMapper.edit(updatePrize);
if (count == 0) {
return false;
}
}
for (ProgressPrize addPrize : addList) {
int count = progressPrizeMapper.add(addPrize);
if (count == 0) {
return false;
}
}
for (ProgressPrize deletePrize : deleteList) {
int count = progressPrizeMapper.delById(deletePrize.getId());
if (count == 0) {
return false;
}
}
}
//3、添加账户获得的收益
for (AccountInfo accountInfo : accountInfoList) {
accountMapper.edit(accountInfo);
//添加交易流水记录
TradeRecord tradeRecord = new TradeRecord();
tradeRecord.setUserId(accountInfo.getUserId());
tradeRecord.setTradeType(TradeRecordEnum.PROGRESS_PRIZE.getCode());
tradeRecord.setTradeNo(null);
if (flag == 1) {
tradeRecord.setStatus(TradeStatusEnum.NO_SETTLE_ACCOUNTS.getCode());
} else {
tradeRecord.setStatus(TradeStatusEnum.ALREADY_SETTLE_ACCOUNTS.getCode());
}
int count = recordMapper.add(tradeRecord);
if (count == 0) {
return false;
}
}
return true;
}
/**
* 更新账户信息和保存交易流水记录
*
* @param accountInfoList 账户列表
* @param recordList 交易流水信息
*/
@Transactional(rollbackFor = Exception.class)
public Boolean updateAccountAddRecord(List<AccountInfo> accountInfoList, List<TradeRecord> recordList) {
//更新月收益
if (!CollectionUtils.isEmpty(accountInfoList)) {
for (AccountInfo accountInfo : accountInfoList) {
int count = accountMapper.edit(accountInfo);
if (count == 0) {
return false;
}
}
}
//新增交易流水记录
if (!CollectionUtils.isEmpty(recordList)) {
for (TradeRecord tradeRecord : recordList) {
int count = tradeRecordMapper.add(tradeRecord);
if (count == 0) {
return false;
}
}
}
return true;
}
/**
* 更新账户信息和保存交易流水记录保存最大进步奖信息
*
* @param accountInfoList 账户列表
* @param recordList 交易流水信息
*/
@Transactional(rollbackFor = Exception.class)
public Boolean updateAccountAddRecordAddPrize(List<AccountInfo> accountInfoList, List<TradeRecord> recordList,
List<ProgressPrize> prizeList) {
//更新月收益
if (!CollectionUtils.isEmpty(accountInfoList)) {
for (AccountInfo accountInfo : accountInfoList) {
int count = accountMapper.edit(accountInfo);
if (count == 0) {
return false;
}
}
}
//新增交易流水记录
if (!CollectionUtils.isEmpty(recordList)) {
for (TradeRecord tradeRecord : recordList) {
int count = tradeRecordMapper.add(tradeRecord);
if (count == 0) {
return false;
}
}
}
//新增最大进步奖信息
if (!CollectionUtils.isEmpty(prizeList)) {
for (ProgressPrize progressPrize : prizeList) {
int count = progressPrizeMapper.add(progressPrize);
if (count == 0) {
return false;
}
}
}
return true;
}
/**
* 更新账户信息和保存交易流水记录、保存上月剩余月度肥料奖金
*
* @param accountInfoList 账户列表
* @param recordList 交易流水信息
*/
@Transactional(rollbackFor = Exception.class)
public Boolean updateAccountAddRecordAndManure(List<AccountInfo> accountInfoList,
List<TradeRecord> recordList,
MonthManure manure, MonthManure monthManure) {
//更新月收益
if (!CollectionUtils.isEmpty(accountInfoList)) {
for (AccountInfo accountInfo : accountInfoList) {
int count = accountMapper.edit(accountInfo);
if (count == 0) {
return false;
}
}
}
//新增交易流水记录
if (!CollectionUtils.isEmpty(recordList)) {
for (TradeRecord tradeRecord : recordList) {
int count = tradeRecordMapper.add(tradeRecord);
if (count == 0) {
return false;
}
}
}
//判断是否存在月度肥料奖金信息
if (null == monthManure) {
//新增上月剩余月度肥料奖金
int count = monthManureMapper.add(manure);
return count != 0;
} else {
//编辑
monthManure.setManureAward(manure.getManureAward());
monthManure.setYearMonth(manure.getYearMonth());
int count = monthManureMapper.edit(monthManure);
return count != 0;
}
}
}