RightsOperationServiceImpl.java
11.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
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
package com.topdraw.business.process.service.impl;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.topdraw.business.module.coupon.service.CouponService;
import com.topdraw.business.module.coupon.service.dto.CouponDTO;
import com.topdraw.business.module.rights.history.domain.RightsHistory;
import com.topdraw.business.module.rights.history.service.RightsHistoryService;
import com.topdraw.business.module.rights.service.RightsService;
import com.topdraw.business.module.rights.service.dto.RightsDTO;
import com.topdraw.business.process.domian.*;
import com.topdraw.business.process.service.CouponOperationService;
import com.topdraw.business.process.service.ExpOperationService;
import com.topdraw.business.process.service.PointsOperationService;
import com.topdraw.business.process.service.RightsOperationService;
import com.topdraw.util.TimestampUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 权益处理
*
* @author XiangHan
* @date 2021.10.23
*/
@Service
@Slf4j
public class RightsOperationServiceImpl implements RightsOperationService {
private static final Logger LOG = LoggerFactory.getLogger(RightsOperationServiceImpl.class);
@Autowired
RightsHistoryService rightsHistoryService;
@Autowired
CouponOperationService couponOperationService;
@Autowired
RightsService rightsService;
@Autowired
ExpOperationService expOperationService;
@Autowired
PointsOperationService pointsOperationService;
@Autowired
CouponService couponService;
// @Autowired
// ThreadPoolTaskExecutor threadPoolTaskExecutor;
private ExecutorService threadPoolTaskExecutor = Executors.newFixedThreadPool(10);
/**
* 系统手动发放
* 实现步骤:
* 1.当前权益只有实体券
* @param rightsList
*/
@Override
public void grantRightsByManual(List<RightsHistory> rightsList) {
// 1.权益区分
Map<RightType,Object> tempRightsMap = this.distinguishRight(rightsList);
// 2.权益下发
this.refresh(tempRightsMap);
// 3.保存权益历史
this.doInsertTrRightHistory(rightsList);
}
/**
* 权益发放
* @param tempRightsMap 权益类型
*/
@Override
public void grantRights(Map<RightType, Object> tempRightsMap) {
// this.threadPoolTaskExecutor.execute(()-> {
// 2.创建权益历史对象
List<RightsHistory> rightsList = this.getRightHistory(tempRightsMap);
// 3.保存权益历史
this.doInsertTrRightHistory(rightsList);
// });
// 1.权益下发
this.refresh(tempRightsMap);
}
/**
*
* @param tempRightsMap
* @return
*/
private List<RightsHistory> getRightHistory(Map<RightType, Object> tempRightsMap) {
List<TempRights> values = (List<TempRights>)tempRightsMap.get(RightType.RIGHTS);
List<RightsHistory> rightsHistoryList = new ArrayList<>();
values.forEach(value -> {
RightsHistory rightsHistory = new RightsHistory();
rightsHistory.setSendTime(LocalDateTime.now());
rightsHistory.setRightsId(value.getId());
rightsHistory.setMemberId(value.getMemberId());
rightsHistory.setExpireTime(value.getExpireTime());
String memberCode = value.getMemberCode();
rightsHistory.setMemberCode(memberCode);
rightsHistoryList.add(rightsHistory);
});
return rightsHistoryList;
}
/**
* 成长值发放,基于已获得的权益
* @param tempExpList 权益列表
*/
private void grantExp(List<TempExp> tempExpList) {
if (!CollectionUtils.isEmpty(tempExpList))
this.expOperationService.grantPointsThroughTempExp(tempExpList);
}
/**
* 发放积分,基于已获得的权益
*
* @param tempPointsList 权益列表
*/
private void grantPoint(List<TempPoints> tempPointsList) {
if (!CollectionUtils.isEmpty(tempPointsList))
this.pointsOperationService.grantPointsThroughTempRightsList(tempPointsList);
}
/**
* 发放优惠券,基于已获得的权益
*
* @param tempCouponList 优惠券
*/
private void grantCoupon(List<TempCoupon> tempCouponList) {
// 发放优惠券
if (!CollectionUtils.isEmpty(tempCouponList))
this.couponOperationService.grantCouponThroughTempCoupon(tempCouponList);
}
/**
* 权益发放
* @param tempRightsMap
*/
private void refresh(Map<RightType, Object> tempRightsMap) {
/*FutureTask<Map<Long,Long>> futureTask1 = new FutureTask(()->{
log.info(Thread.currentThread().getName() + "=========>> start");
// 积分
this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
log.info(Thread.currentThread().getName() + "=========>>grantPoint end");
// 成长值
// this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
// 优惠券
// this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
return null;
});
FutureTask<Map<Long,Long>> futureTask2 = new FutureTask(()->{
// 积分
// this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
// 成长值
this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
// 优惠券
// this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
return null;
});
FutureTask<Map<Long,Long>> futureTask3 = new FutureTask(()->{
// 积分
// this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
// 成长值
// this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
// 优惠券
this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
return null;
});
this.threadPoolTaskExecutor.execute(futureTask1);
this.threadPoolTaskExecutor.execute(futureTask2);
this.threadPoolTaskExecutor.execute(futureTask3);*/
/*this.threadPoolTaskExecutor.execute(() -> {
// 积分
this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
// 成长值
this.grantExp((List<TempExp>) tempRightsMap.get(RightType.EXP));
// 优惠券
this.grantCoupon((List<TempCoupon>) tempRightsMap.get(RightType.COUPON));
});*/
/*this.threadPoolTaskExecutor.execute(() -> {
log.info(Thread.currentThread().getName() + "=========>> start");
// 积分
this.grantPoint((List<TempPoints>) tempRightsMap.get(RightType.POINTS));
log.info(Thread.currentThread().getName() + "=========>> end");
});*/
List<TempPoints> tempPointsList = (List<TempPoints>) tempRightsMap.get(RightType.POINTS);
if (!CollectionUtils.isEmpty(tempPointsList)) {
// 积分
this.grantPoint(tempPointsList);
}
List<TempExp> tempExpList = (List<TempExp>) tempRightsMap.get(RightType.EXP);
if (!CollectionUtils.isEmpty(tempExpList)) {
// 成长值
this.grantExp(tempExpList);
}
List<TempCoupon> tempCouponList = (List<TempCoupon>) tempRightsMap.get(RightType.COUPON);
if (!CollectionUtils.isEmpty(tempCouponList)) {
// 优惠券
this.grantCoupon(tempCouponList);
}
}
/**
* 区分权益
* @param rightsList
* @return
*/
private Map<RightType, Object> distinguishRight(List<RightsHistory> rightsList) {
Map<RightType,Object> map = new HashMap<>();
// 优惠券
List<TempCoupon> tempCouponList = new ArrayList<>();
for (RightsHistory right : rightsList) {
Long rightId = right.getRightsId();
Long memberId = right.getMemberId();
Long userId = right.getUserId();
// 权益类型
RightsDTO rightsDTO = this.getRights(rightId);
// 权益的实体类型 1:积分;2成长值;3优惠券
String type = rightsDTO.getEntityType();
Long expireTime = rightsDTO.getExpireTime();
switch (type) {
// 优惠券
case "1":
Long entityId = rightsDTO.getEntityId();
CouponDTO couponDTO = this.findCouponById(entityId);
if (Objects.nonNull(couponDTO)) {
TempCoupon tempCoupon = new TempCoupon();
tempCoupon.setId(couponDTO.getId());
tempCoupon.setMemberId(memberId);
tempCoupon.setUserId(userId);
tempCoupon.setRightsAmount(1);
tempCoupon.setRightsSendStrategy(0);
tempCoupon.setCode(couponDTO.getCode());
if (Objects.nonNull(expireTime))
tempCoupon.setExpireTime(LocalDateTimeUtil.of(expireTime));
tempCouponList.add(tempCoupon);
}
break;
// 观影券
case "2":
break;
// 活动参与机会
case "3":
break;
default:
break;
}
}
// 优惠券
if (!CollectionUtils.isEmpty(tempCouponList))
map.put(RightType.COUPON,tempCouponList);
return map;
}
/**
* 获取优惠券信息
* @param id
* @return
*/
private CouponDTO findCouponById(Long id) {
CouponDTO couponDTO = this.couponService.findById(id);
return couponDTO;
}
/**
* 权益详情
* @param rightsId
* @return
*/
private RightsDTO getRights(Long rightsId) {
RightsDTO rightsDTO = this.rightsService.findById(rightsId);
return rightsDTO;
}
/**
* 添加权益领取记录
* @param rightsHistories
*/
private void doInsertTrRightHistory(List<RightsHistory> rightsHistories) {
if (!CollectionUtils.isEmpty(rightsHistories)) {
for (RightsHistory rightsHistory : rightsHistories) {
Long operatorId = rightsHistory.getOperatorId();
String operatorName = rightsHistory.getOperatorName();
rightsHistory.setSendTime(LocalDateTime.now());
rightsHistory.setOperatorId(Objects.nonNull(operatorId)?operatorId:0);
rightsHistory.setOperatorName(!StringUtils.isEmpty(operatorName)?operatorName:"系统发放");
this.rightsHistoryService.create(rightsHistory);
}
}
}
}