PointsOperationServiceImpl.java
21.7 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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package com.topdraw.business.process.service.impl;
import com.topdraw.business.module.member.domain.Member;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.points.available.domain.PointsAvailable;
import com.topdraw.business.module.points.available.service.PointsAvailableService;
import com.topdraw.business.module.points.available.service.dto.PointsAvailableDTO;
import com.topdraw.business.module.points.detail.detailhistory.service.PointsDetailHistoryService;
import com.topdraw.business.module.points.detail.domain.PointsDetail;
import com.topdraw.business.module.points.detail.service.PointsDetailService;
import com.topdraw.business.module.points.service.PointsService;
import com.topdraw.business.process.domian.TempPoints;
import com.topdraw.business.process.service.PointsOperationService;
import com.topdraw.business.process.service.member.MemberOperationService;
import com.topdraw.util.IdWorker;
import com.topdraw.util.TimestampUtil;
import com.topdraw.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
*
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
@Slf4j
public class PointsOperationServiceImpl implements PointsOperationService {
@Autowired
PointsService pointsService;
@Autowired
PointsDetailService pointsDetailService;
@Autowired
PointsAvailableService pointsAvailableService;
@Autowired
PointsDetailHistoryService pointsDetailHistoryService;
@Autowired
MemberOperationService memberOperationService;
@Autowired
MemberService memberService;
// 过期阈值 30天
private static final Integer EXPIRE_FACTOR = 30;
private static final String DELETE_AVAILABLE_POINTS = "delete";
private static final String INSERT_AVAILABLE_POINTS = "insert";
@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Override
@Transactional(rollbackFor = Exception.class)
public void grantPointsByManual(Long memberId,TempPoints tempPoints){
if (Objects.nonNull(tempPoints) && Objects.nonNull(tempPoints.getPoints())) {
this.refresh(tempPoints);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void grantPointsByManualByTempPoints(TempPoints tempPoints) {
if (Objects.nonNull(tempPoints) && Objects.nonNull(tempPoints.getPoints())) {
String memberCode = tempPoints.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
if (Objects.nonNull(memberDTO)) {
Long memberId = memberDTO.getId();
tempPoints.setMemberId(memberId);
this.refresh(tempPoints);
}
}
}
/**
* 积分消耗
* @param tempPoints 任务对象
* @return true: 满足 false:不满足
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean customPoints(TempPoints tempPoints) {
String memberCode = tempPoints.getMemberCode();
MemberDTO memberDTO = this.memberService.findByCode(memberCode);
if (Objects.nonNull(memberDTO)) {
Long memberId = memberDTO.getId();
tempPoints.setMemberId(memberId);
try {
//1.删除过期的积分
this.cleanInvalidAvailablePointsByMemberId(memberId);
// 1.判断可用积分是否够用
boolean b = this.checkAvailablePoints(tempPoints);
log.info("customPoints-->>> 当前可用总积分大于待兑换的积分--->>" + b);
if (b) {
// 2.可用积分表,按照过期时间进行升序排列
List<PointsAvailableDTO> pointsAvailableDTOS = this.findByMemberIdOrderByExpireTime(tempPoints);
// 3.当前可用总积分
long currentPoints = this.findAvailablePointsByMemberId(memberId);
// 2.优先使用即将过期的积分,累加到超过需兑换积分时,需要进行拆分
Map<String, List<PointsAvailableDTO>> customAvailablePointsMap = this.customAvailablePoints(tempPoints, pointsAvailableDTOS);
// 3.添加积分明细
this.doInsertTrPointsDetailByAvailablePointsMap(tempPoints, customAvailablePointsMap, currentPoints);
// 4.更新可用积分表,超过的删除,剩余的新增
long totalPoints = this.doFreshTrPointsAvailableByAvailablePointsMap(customAvailablePointsMap, currentPoints);
// 5.即将过期的积分
long soonExpirePoints = this.getSoonExpirePoints(memberId, tempPoints);
// 6.更新会员积分信息
this.freshMemberCurrentPoints(memberId, totalPoints,soonExpirePoints,tempPoints);
return true;
}
}catch (Exception e) {
e.printStackTrace();
throw e;
}
}
return false;
}
/**
* 更新可用积分表
* @param customAvailablePointsMap
*/
private long doFreshTrPointsAvailableByAvailablePointsMap(
Map<String, List<PointsAvailableDTO>> customAvailablePointsMap,long currentPoints) {
long totalCustomAvailablePoints = 0L;
if (customAvailablePointsMap != null) {
// 需要删除的
List<PointsAvailableDTO> pointsAvailableDTOS1 = customAvailablePointsMap.get(DELETE_AVAILABLE_POINTS);
if (!CollectionUtils.isEmpty(pointsAvailableDTOS1)) {
for (PointsAvailableDTO pointsAvailableDTO : pointsAvailableDTOS1) {
totalCustomAvailablePoints = (totalCustomAvailablePoints+pointsAvailableDTO.getPoints());
this.pointsAvailableService.delete(pointsAvailableDTO.getId());
}
}
// 需要添加的
List<PointsAvailableDTO> pointsAvailableDTOS2 = customAvailablePointsMap.get(INSERT_AVAILABLE_POINTS);
if (!CollectionUtils.isEmpty(pointsAvailableDTOS2)) {
PointsAvailableDTO pointsAvailableDTO = pointsAvailableDTOS2.get(0);
PointsAvailable pointsAvailable = new PointsAvailable();
BeanUtils.copyProperties(pointsAvailableDTO,pointsAvailable);
pointsAvailable.setId(null);
pointsAvailable.setCode(String.valueOf(IdWorker.generator()));
this.pointsAvailableService.create4Custom(pointsAvailable);
}
}
return currentPoints - totalCustomAvailablePoints;
}
/**
* 优先使用即将过期的积分,注意拆分
* @param customAvailablePointsMap
*/
private void doInsertTrPointsDetailByAvailablePointsMap(TempPoints tempPoints,
Map<String, List<PointsAvailableDTO>> customAvailablePointsMap,
long currentPoints) {
// 兑换的积分
Long points = tempPoints.getPoints();
List<PointsAvailableDTO> pointsAvailableDTOS = customAvailablePointsMap.get(DELETE_AVAILABLE_POINTS);
if (!CollectionUtils.isEmpty(pointsAvailableDTOS)) {
PointsAvailableDTO pointsAvailableDTO = pointsAvailableDTOS.get(0);
Long memberId = pointsAvailableDTO.getMemberId();
TempPoints tempPoints1 = new TempPoints();
BeanUtils.copyProperties(pointsAvailableDTO,tempPoints1);
BeanUtils.copyProperties(tempPoints,tempPoints1);
tempPoints1.setPoints(-(Math.abs(points)));
Long totalPoints = this.calculateTotalPoints(tempPoints1, currentPoints);
this.doInsertTrPointsDetail(memberId,tempPoints1,currentPoints,totalPoints);
}
}
/**
* 消耗过期的积分
* @param tempPoints 需要消耗的积分
* @param pointsAvailableDTOS 可用积分列表
* @return List<PointsAvailableDTO> 已消耗的可用积分列表
*/
private Map<String,List<PointsAvailableDTO>> customAvailablePoints(TempPoints tempPoints, List<PointsAvailableDTO> pointsAvailableDTOS) {
// 兑换的积分
Long points = tempPoints.getPoints();
Long points1_ = 0L;
List<PointsAvailableDTO> pointsAvailableDTOS1 = new ArrayList<>();
List<PointsAvailableDTO> pointsAvailableDTOS2 = new ArrayList<>();
Map<String,List<PointsAvailableDTO>> map = new HashMap<>();
for (PointsAvailableDTO pointsAvailableDTO : pointsAvailableDTOS) {
Long points1 = pointsAvailableDTO.getPoints();
points1_ = (points1_ + points1);
// 不够
if (points1_ < points) {
pointsAvailableDTOS1.add(pointsAvailableDTO);
continue;
}
// 刚好
if (points1_ == points) {
pointsAvailableDTOS1.add(pointsAvailableDTO);
break;
}
// 超了,拆分
if (points1_ > points) {
// 超过的
long beyond = points1_ - points;
// 新增
PointsAvailableDTO pointsAvailableDTO2 = new PointsAvailableDTO();
BeanUtils.copyProperties(pointsAvailableDTO,pointsAvailableDTO2);
pointsAvailableDTO2.setPoints(beyond);
pointsAvailableDTOS2.add(pointsAvailableDTO2);
// 剩余的
long suit = points1 - beyond;
// 扣除
PointsAvailableDTO pointsAvailableDTO1 = new PointsAvailableDTO();
BeanUtils.copyProperties(pointsAvailableDTO,pointsAvailableDTO1);
pointsAvailableDTO1.setPoints(suit);
pointsAvailableDTOS1.add(pointsAvailableDTO1);
break;
}
}
map.put(DELETE_AVAILABLE_POINTS,pointsAvailableDTOS1);
map.put(INSERT_AVAILABLE_POINTS,pointsAvailableDTOS2);
return map;
}
/**
* 可用积分表,按照过期时间进行升序排列
* @param tempPoints
* @return
*/
private List<PointsAvailableDTO> findByMemberIdOrderByExpireTime(TempPoints tempPoints) {
return this.pointsAvailableService.findByMemberIdOrderByExpireTime(tempPoints.getMemberId());
}
/**
* 检查当前用户可用积分是否足够
* @param tempPoints
* @return true : 满足 false: 不满足
*/
private boolean checkAvailablePoints(TempPoints tempPoints) {
Long memberId = tempPoints.getMemberId();
// 可用积分
long currentPoints = this.findAvailablePointsByMemberId(memberId);
long points1 = tempPoints.getPoints();
if (currentPoints > 0 && points1 > 0 && (currentPoints - Math.abs(points1) >= 0)) {
return true;
}
return false;
}
/**
* 积分发放,基于已获得的权益
*
* @param tempPointsList 已获得的权益
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void grantPointsThroughTempRightsList(List<TempPoints> tempPointsList){
log.info("------->>grantPointsThroughTempRightsList start1");
for (TempPoints tempPoints : tempPointsList){
log.info("------->>grantPointsThroughTempRightsList start");
this.refresh(tempPoints);
}
}
/**
* 清理过期积分
* @param memberId
*/
private void cleanInvalidAvailablePointsByMemberId(Long memberId) {
List<PointsAvailableDTO> pointsAvailableDTOS =
pointsAvailableService.findByMemberIdAndExpireTimeBefore(memberId,LocalDateTime.now());
if (!CollectionUtils.isEmpty(pointsAvailableDTOS)) {
//1.获取原始积分
for (PointsAvailableDTO pointsAvailableDTO : pointsAvailableDTOS) {
// 添加积分明细 uc_points_detail
this.doCreatePointsDetail(pointsAvailableDTO);
// 删除已过期的积分
this.doDeleteInvalidAvailablePoints(pointsAvailableDTO);
}
}
}
@Override
public Long cleanInvalidPointsAndCalculateCurrentPoints(Long memberId) {
// 清理当前用户的过期积分
this.cleanInvalidAvailablePointsByMemberId(memberId);
// 获取当前用户的可用总积分
long currentPoints = this.findAvailablePointsByMemberId(memberId);
// 即将过期的积分
long soonExpirePoints = this.getSoonExpirePoints(memberId, null);
// 更新会员信息
this.doUpdateMemberPoints(memberId,currentPoints,soonExpirePoints);
return currentPoints;
}
/**
* 获取可用总积分
* @param memberId
* @return
*/
private long findAvailablePointsByMemberId(Long memberId){
return this.pointsAvailableService.findAvailablePointsByMemberId(memberId);
}
/**
* 修改会员信息
* @param memberId
* @param currentPoints
*/
private void doUpdateMemberPoints(Long memberId, long currentPoints,long soonExpirePoints) {
Member member = new Member();
member.setId(memberId);
member.setPoints(currentPoints);
member.setDuePoints(soonExpirePoints);
this.memberOperationService.doUpdateMemberPoints(member);
}
/**
*
* @param pointsAvailableDTOS
*/
private void doDeleteBatchInvalidAvailablePoints(List<PointsAvailableDTO> pointsAvailableDTOS) {
List<Long> collect = pointsAvailableDTOS.stream().map(pointsAvailableDTO -> pointsAvailableDTO.getId()).collect(Collectors.toList());
this.pointsAvailableService.deleteBatchByIds(collect);
}
/**
*
* @param pointsAvailableDTO
*/
private void doDeleteInvalidAvailablePoints(PointsAvailableDTO pointsAvailableDTO) {
this.pointsAvailableService.delete(pointsAvailableDTO.getId());
}
/**
* 可用积分
* @param pointsAvailableDTO
*/
private void doCreatePointsDetail(PointsAvailableDTO pointsAvailableDTO) {
Long memberId = pointsAvailableDTO.getMemberId();
// 原始积分
long availablePoints = this.pointsAvailableService.findTotalPointsByMemberId(memberId);//this.findAvailablePointsByMemberId(memberId);
// 过期积分
long l = pointsAvailableDTO.getPoints();
// 结果积分
long resultPoints = availablePoints - l;
PointsDetail pointsDetail = new PointsDetail();
BeanUtils.copyProperties(pointsAvailableDTO,pointsDetail);
pointsDetail.setId(null);
pointsDetail.setPoints(-Math.abs(l));
pointsDetail.setCode(String.valueOf(IdWorker.generator()));
pointsDetail.setOriginalPoints(availablePoints);
pointsDetail.setResultPoints(resultPoints);
pointsDetail.setDescription("过期积分");
pointsDetail.setEvtType(99);
pointsDetail.setCreateTime(TimestampUtil.now());
pointsDetail.setUpdateTime(TimestampUtil.now());
this.doInsertPointsDetail(pointsDetail);
}
/**
* 模板方法,提供更新积分的总体流程
*
* @param tempPoints 积分
*/
private void refresh(TempPoints tempPoints) {
Long memberId = tempPoints.getMemberId();
log.info("----------->> points refresh start");
log.info("----------->> rLock --->> start" );
try {
log.info("----------->> refresh findAvailablePointsByMemberId start");
// 1.可用总积分
Long currentPoints = this.findAvailablePointsByMemberId(memberId);
log.info("----------->> refresh findAvailablePointsByMemberId currentPoints " + currentPoints);
// 2.计算总积分
Long totalPoints = this.calculateTotalPoints(tempPoints, currentPoints);
log.info("----------->> refresh findAvailablePointsByMemberId totalPoints " + totalPoints);
// 3.添加积分明细,并计算总积分
this.doInsertTrPointsDetail(memberId, tempPoints, currentPoints, totalPoints);
log.info(Thread.currentThread().getName() + "----------->> refresh doInsertTrPointsDetail end ");
// 4.添加可用积分
log.info("----------->> refresh doInsertTrPointsAvailable start ");
this.doInsertTrPointsAvailable(tempPoints);
log.info("----------->> refresh doInsertTrPointsAvailable end ");
// 即将过期的积分
long soonExpirePoints = this.getSoonExpirePoints(memberId, tempPoints);
// 6.更新会员的总积分
log.info("----------->> refresh freshMemberCurrentPoints start ");
this.freshMemberCurrentPoints(memberId, totalPoints,soonExpirePoints,tempPoints);
log.info("----------->> refresh freshMemberCurrentPoints end ");
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 获取总积分
* @param tempPoints
* @param currentPoints
* @return
*/
private Long calculateTotalPoints(TempPoints tempPoints, Long currentPoints) {
// 获取的积分
Long rewardPoints = tempPoints.getPoints();
// 总积分
Long totalPoints = currentPoints + rewardPoints;
return totalPoints;
}
/**
* 获取即将过期的积分
* @param memberId
* @return
*/
private long getSoonExpirePoints(Long memberId,TempPoints tempPoints) {
// 计算即将过期的天数
Integer factor = this.calculateExpireFactor(tempPoints);
if (Objects.isNull(factor)) {
factor = EXPIRE_FACTOR;
}
Long soonExpireTime = this.pointsAvailableService.findSoonExpireTime(memberId, factor);
return Objects.nonNull(soonExpireTime) ? soonExpireTime : 0L;
}
/**
* 如果过期时间和过期天数同时存在,则以过期时间为准
* @param tempPoints
* @return
*/
private Integer calculateExpireFactor(TempPoints tempPoints) {
// TODO 计算过期的相对时间
return null;
}
/**
* 更新会员总积分
* @param memberId 会员Id
* @param currentPoints 当前总积分
*/
private void freshMemberCurrentPoints(Long memberId, Long currentPoints,long duePoints,TempPoints tempPoints) {
Member member = new Member();
member.setId(memberId);
member.setPoints(Objects.nonNull(currentPoints)?currentPoints:0);
member.setDuePoints(duePoints);
member.setUpdateTime(LocalDateTime.now());
member.setCode(tempPoints.getMemberCode());
try {
this.memberOperationService.doUpdateMemberPoints(member);
} catch (Exception e){
throw e;
}
}
/**
* 计算当前总积分
* @param memberId 会员id
* @return
*/
private Long findAvailablePointsByMemberId(long memberId){
Long availablePoints = this.pointsAvailableService.findAvailablePointsByMemberId(memberId);
return Objects.nonNull(availablePoints) ? availablePoints : 0L;
}
/**
* 更新可用积分表
* @param tempPoints
*/
private void doInsertTrPointsAvailable(TempPoints tempPoints){
PointsAvailable pointsAvailable = new PointsAvailable();
BeanUtils.copyProperties(tempPoints,pointsAvailable);
String description = pointsAvailable.getDescription();
pointsAvailable.setCode(String.valueOf(IdWorker.generator()));
pointsAvailable.setDescription(StringUtils.isEmpty(description)?"#":description);
LocalDateTime timestamp = tempPoints.getExpireTime();
if (Objects.nonNull(timestamp)) {
pointsAvailable.setExpireTime(timestamp);
}
// 添加可用积分记录
this.doInsertTrPointsAvailable(pointsAvailable);
}
/**
* 添加可用积分记录
* @param pointsAvailable 可用积分
*/
private void doInsertTrPointsAvailable(PointsAvailable pointsAvailable) {
this.pointsAvailableService.create(pointsAvailable);
}
/**
* 添加积分明细
* @param memberId 会员Id
* @param tempPoints 积分
* @return Integer 总积分
*/
private void doInsertTrPointsDetail(Long memberId, TempPoints tempPoints,Long currentPoints,Long totalPoints){
PointsDetail pointsDetail = new PointsDetail();
BeanUtils.copyProperties(tempPoints,pointsDetail);
pointsDetail.setId(null);
pointsDetail.setMemberId(memberId);
pointsDetail.setCode(String.valueOf(IdWorker.generator()));
pointsDetail.setPoints(tempPoints.getPoints());
pointsDetail.setOriginalPoints(currentPoints);
pointsDetail.setResultPoints(totalPoints);
pointsDetail.setCreateTime(null);
pointsDetail.setUpdateTime(null);
String description = pointsDetail.getDescription();
if (StringUtils.isEmpty(description)) {
pointsDetail.setDescription("#");
}
// 保存积分流水
this.doInsertPointsDetail(pointsDetail);
}
/**
*
* @param pointsDetail
*/
private void doInsertPointsDetail(PointsDetail pointsDetail){
this.pointsDetailService.create4Custom(pointsDetail);
}
}