|
@@ -32,10 +32,7 @@ import java.time.LocalDate; |
|
@@ -32,10 +32,7 @@ import java.time.LocalDate; |
32
|
import java.time.LocalDateTime;
|
32
|
import java.time.LocalDateTime;
|
33
|
import java.time.LocalTime;
|
33
|
import java.time.LocalTime;
|
34
|
import java.time.ZoneOffset;
|
34
|
import java.time.ZoneOffset;
|
35
|
-import java.util.ArrayList;
|
|
|
36
|
-import java.util.List;
|
|
|
37
|
-import java.util.Map;
|
|
|
38
|
-import java.util.Objects;
|
35
|
+import java.util.*;
|
39
|
import java.util.function.Function;
|
36
|
import java.util.function.Function;
|
40
|
import java.util.stream.Collectors;
|
37
|
import java.util.stream.Collectors;
|
41
|
|
38
|
|
|
@@ -279,9 +276,10 @@ public class CouponServiceImpl implements ICouponService { |
|
@@ -279,9 +276,10 @@ public class CouponServiceImpl implements ICouponService { |
279
|
}
|
276
|
}
|
280
|
|
277
|
|
281
|
// 批量获取coupon
|
278
|
// 批量获取coupon
|
282
|
- List<String> couponIdList = list.stream().map(UserCoupon::getCouponToken).collect(Collectors.toList());
|
|
|
283
|
- List<Coupon> couponList = couponCacheService.getCouponsWithCache(couponIdList);
|
|
|
284
|
- Map<Integer, Coupon> couponMap = couponList.stream().filter(coupon -> {
|
279
|
+ List<String> couponTokens = list.stream().map(UserCoupon::getCouponToken).collect(Collectors.toList());
|
|
|
280
|
+ List<Coupon> couponList = couponCacheService.getCouponsWithCache(couponTokens);
|
|
|
281
|
+
|
|
|
282
|
+ final Map<Integer, Coupon> couponMap = couponList.stream().filter(coupon -> {
|
285
|
if (coupon.getStatus() != null && coupon.getStatus().intValue() == CouponsStatusEnum.VALID.getCode()) {
|
283
|
if (coupon.getStatus() != null && coupon.getStatus().intValue() == CouponsStatusEnum.VALID.getCode()) {
|
286
|
return true;
|
284
|
return true;
|
287
|
} else {
|
285
|
} else {
|
|
@@ -290,34 +288,28 @@ public class CouponServiceImpl implements ICouponService { |
|
@@ -290,34 +288,28 @@ public class CouponServiceImpl implements ICouponService { |
290
|
}).collect(Collectors.toMap(Coupon::getId, Function.identity()));
|
288
|
}).collect(Collectors.toMap(Coupon::getId, Function.identity()));
|
291
|
|
289
|
|
292
|
// 优惠券商品限制 key=couponId, value=ProductId集合
|
290
|
// 优惠券商品限制 key=couponId, value=ProductId集合
|
293
|
- Map<Integer, List<Integer>> couponProductIdMap = getProductIdListMap(couponList);
|
|
|
294
|
-
|
|
|
295
|
- List<UserCouponsBo> couponBoList = list.stream().filter(userCoupon -> {
|
|
|
296
|
- return couponMap.containsKey(userCoupon.getCouponId());
|
|
|
297
|
- }).map(userCoupon -> {
|
|
|
298
|
-
|
|
|
299
|
- Coupon coupon = couponMap.get(userCoupon.getCouponId());
|
|
|
300
|
- List<Integer> productIds = couponProductIdMap.get(userCoupon.getCouponId());
|
|
|
301
|
-
|
|
|
302
|
- // 单个获取couponType
|
|
|
303
|
- CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType() != null ? userCoupon.getCouponType().intValue() : null);
|
|
|
304
|
-
|
|
|
305
|
- UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon, couponType);
|
|
|
306
|
- couponBo.setProductIdInclude(productIds);
|
|
|
307
|
-
|
|
|
308
|
- return couponBo;
|
|
|
309
|
- }).collect(Collectors.toList());
|
291
|
+ final Map<Integer, List<Integer>> couponProductIdMap = getAssociatedProductIdListMap(couponList);
|
310
|
|
292
|
|
|
|
293
|
+ //包装返回结果
|
|
|
294
|
+ List<UserCouponsBo> couponBoList = list.stream()
|
|
|
295
|
+ .filter(userCoupon -> couponMap.containsKey(userCoupon.getCouponId()))
|
|
|
296
|
+ .map(userCoupon -> wrapperUserCoupon(userCoupon, couponMap.get(userCoupon.getCouponId()), couponProductIdMap.get(userCoupon.getCouponId())))
|
|
|
297
|
+ .collect(Collectors.toList());
|
311
|
|
298
|
|
312
|
return UserCouponsListBo.builder().coupons(couponBoList).build();
|
299
|
return UserCouponsListBo.builder().coupons(couponBoList).build();
|
313
|
}
|
300
|
}
|
314
|
|
301
|
|
315
|
- private Map<Integer, List<Integer>> getProductIdListMap(List<Coupon> couponList) {
|
|
|
316
|
- // 批量获取 coupon_product_limt 过滤出商品限制的优惠券id
|
302
|
+ /**
|
|
|
303
|
+ * 关联的商品id
|
|
|
304
|
+ * @param couponList
|
|
|
305
|
+ * @return
|
|
|
306
|
+ */
|
|
|
307
|
+ private Map<Integer, List<Integer>> getAssociatedProductIdListMap(List<Coupon> couponList) {
|
|
|
308
|
+ // 批量获取 coupon_product_limit 过滤出商品限制的优惠券id
|
317
|
List<Integer> productLimitCouponIds = couponList.stream().filter(coupon -> {
|
309
|
List<Integer> productLimitCouponIds = couponList.stream().filter(coupon -> {
|
318
|
- if (coupon.getProductLimitType()!=null && CouponProductLimitTypeEnum.SPECIFIC_PRODUCT.getLimitType().equals(String.valueOf(coupon.getProductLimitType()))){
|
310
|
+ if (coupon.getProductLimitType() != null && !StringUtils.equals(CouponProductLimitTypeEnum.NON.getLimitType(),String.valueOf(coupon.getProductLimitType()))) {
|
319
|
return true;
|
311
|
return true;
|
320
|
- }else{
|
312
|
+ } else {
|
321
|
return false;
|
313
|
return false;
|
322
|
}
|
314
|
}
|
323
|
}).map(Coupon::getId).collect(Collectors.toList());
|
315
|
}).map(Coupon::getId).collect(Collectors.toList());
|
|
@@ -326,6 +318,84 @@ public class CouponServiceImpl implements ICouponService { |
|
@@ -326,6 +318,84 @@ public class CouponServiceImpl implements ICouponService { |
326
|
|
318
|
|
327
|
}
|
319
|
}
|
328
|
|
320
|
|
|
|
321
|
+ @Override
|
|
|
322
|
+ public UserCouponsListBo checkAndGetCoupons(int uid, List<String> couponCodes) {
|
|
|
323
|
+ //数据校验
|
|
|
324
|
+ if (uid <= 0 || CollectionUtils.isEmpty(couponCodes)) {
|
|
|
325
|
+ logger.warn("check coupon use param error:{}, {}", uid, couponCodes);
|
|
|
326
|
+ throw new ServiceException(ServiceError.PROMOTION_PARAM_IS_ERROR);
|
|
|
327
|
+ }
|
|
|
328
|
+
|
|
|
329
|
+ // 获取用户券记录
|
|
|
330
|
+ List<UserCoupon> userCoupons = userCouponMapper.selectByUidAndCouponCodes(uid, couponCodes);
|
|
|
331
|
+
|
|
|
332
|
+ // 批量获取coupon
|
|
|
333
|
+ List<String> couponTokens = userCoupons.stream().map(UserCoupon::getCouponToken).collect(Collectors.toList());
|
|
|
334
|
+
|
|
|
335
|
+ List<Coupon> couponList = couponCacheService.getCouponsWithCache(couponTokens);
|
|
|
336
|
+
|
|
|
337
|
+ final Map<Integer, Coupon> couponMap = couponList.stream().filter(coupon -> {
|
|
|
338
|
+ if (coupon.getStatus() != null && coupon.getStatus().intValue() == CouponsStatusEnum.VALID.getCode()) {
|
|
|
339
|
+ return true;
|
|
|
340
|
+ } else {
|
|
|
341
|
+ return false;
|
|
|
342
|
+ }
|
|
|
343
|
+ }).collect(Collectors.toMap(Coupon::getId, Function.identity()));
|
|
|
344
|
+
|
|
|
345
|
+ // 优惠券商品限制 key=couponId, value=ProductId集合
|
|
|
346
|
+ Map<Integer, List<Integer>> couponProductIdMap = getAssociatedProductIdListMap(couponList);
|
|
|
347
|
+
|
|
|
348
|
+
|
|
|
349
|
+ int time = DateUtil.getCurrentTimeSecond();
|
|
|
350
|
+ List<UserCouponsBo> couponBoList = couponCodes.stream().map(couponCode -> {
|
|
|
351
|
+
|
|
|
352
|
+ UserCoupon userCoupon = userCoupons.stream().filter(e -> couponCode.equals(e.getCouponCode())).findAny().orElse(null);
|
|
|
353
|
+ logger.info("user coupon {},{},{}", uid, couponCode, userCoupon);
|
|
|
354
|
+ if (userCoupon == null) {
|
|
|
355
|
+ logger.warn("not find user coupon by{},{}", uid, couponCode);
|
|
|
356
|
+ throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_YOUS);
|
|
|
357
|
+ }
|
|
|
358
|
+ //判断券状态
|
|
|
359
|
+ if (userCoupon.getStatus().intValue() != CouponUseStatusEnum.NOT_USED.getCode()) {
|
|
|
360
|
+ logger.warn("user this coupon is used:{},{}", userCoupon.getUid(), userCoupon.getCouponCode());
|
|
|
361
|
+ throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_USE_DOUBLE);
|
|
|
362
|
+ }
|
|
|
363
|
+
|
|
|
364
|
+ //判断生失效时间
|
|
|
365
|
+ if (userCoupon.getStartTime() > time || userCoupon.getEndTime() < time) {
|
|
|
366
|
+ logger.warn("coupon has expire or not arrive time:{},{},{}", userCoupon.getStartTime(), userCoupon.getEndTime(), time);
|
|
|
367
|
+ throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_ALLOW_OR_EXPIRE);
|
|
|
368
|
+ }
|
|
|
369
|
+
|
|
|
370
|
+ //券模板的状态
|
|
|
371
|
+ Coupon coupon = couponMap.get(userCoupon.getCouponId());
|
|
|
372
|
+ if (coupon == null) {
|
|
|
373
|
+ logger.warn("not find coupon by couponId:{}",userCoupon.getCouponId());
|
|
|
374
|
+ throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_EXISTS);
|
|
|
375
|
+ }
|
|
|
376
|
+ if (coupon.getStatus() != CouponsStatusEnum.VALID.getCode()) {
|
|
|
377
|
+ logger.warn("coupon status can't be use:{},{}", coupon.getId(), coupon.getStatus());
|
|
|
378
|
+ throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
379
|
+ }
|
|
|
380
|
+
|
|
|
381
|
+ //所有的校验都通过了,生成bo
|
|
|
382
|
+ return wrapperUserCoupon(userCoupon, coupon, couponProductIdMap.get(userCoupon.getCouponId()));
|
|
|
383
|
+
|
|
|
384
|
+ }).collect(Collectors.toList());
|
|
|
385
|
+
|
|
|
386
|
+ return UserCouponsListBo.builder().coupons(couponBoList).build();
|
|
|
387
|
+ }
|
|
|
388
|
+
|
|
|
389
|
+ private UserCouponsBo wrapperUserCoupon(final UserCoupon userCoupon, final Coupon coupon, final List<Integer> productIds) {
|
|
|
390
|
+
|
|
|
391
|
+ // 单个获取couponType
|
|
|
392
|
+ CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType() != null ? userCoupon.getCouponType().intValue() : null);
|
|
|
393
|
+
|
|
|
394
|
+ UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon, couponType, productIds);
|
|
|
395
|
+
|
|
|
396
|
+ return couponBo;
|
|
|
397
|
+ }
|
|
|
398
|
+
|
329
|
|
399
|
|
330
|
|
400
|
|
331
|
@Override
|
401
|
@Override
|
|
@@ -429,63 +499,4 @@ public class CouponServiceImpl implements ICouponService { |
|
@@ -429,63 +499,4 @@ public class CouponServiceImpl implements ICouponService { |
429
|
.size(req.getLimit())
|
499
|
.size(req.getLimit())
|
430
|
.build();
|
500
|
.build();
|
431
|
}
|
501
|
}
|
432
|
-
|
|
|
433
|
- @Override
|
|
|
434
|
- public UserCouponsListBo checkAndGetCoupons(int uid, List<String> couponCodes) {
|
|
|
435
|
- //数据校验
|
|
|
436
|
- if (uid <= 0 || CollectionUtils.isEmpty(couponCodes)) {
|
|
|
437
|
- logger.warn("check coupon use param error:{}, {}", uid, couponCodes);
|
|
|
438
|
- throw new ServiceException(ServiceError.PROMOTION_PARAM_IS_ERROR);
|
|
|
439
|
- }
|
|
|
440
|
-
|
|
|
441
|
- // 获取用户券记录
|
|
|
442
|
- List<UserCoupon> userCoupons = userCouponMapper.selectByUidAndCouponCodes(uid, couponCodes);
|
|
|
443
|
-
|
|
|
444
|
- int time = DateUtil.getCurrentTimeSecond();
|
|
|
445
|
-
|
|
|
446
|
- List<UserCouponsBo> couponBoList = couponCodes.stream().map(couponCode -> {
|
|
|
447
|
-
|
|
|
448
|
- UserCoupon userCoupon = userCoupons.stream().filter(e -> couponCode.equals(e.getCouponCode())).findAny().orElse(null);
|
|
|
449
|
- logger.info("user coupon {},{},{}", uid, couponCode, userCoupon);
|
|
|
450
|
- if (userCoupon == null) {
|
|
|
451
|
- logger.warn("not find user coupon by{},{}", uid, couponCode);
|
|
|
452
|
- throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_YOUS);
|
|
|
453
|
- }
|
|
|
454
|
- //判断券状态
|
|
|
455
|
- if (userCoupon.getStatus().intValue() != CouponUseStatusEnum.NOT_USED.getCode()) {
|
|
|
456
|
- logger.warn("user this coupon is used:{},{}", userCoupon.getUid(), userCoupon.getCouponCode());
|
|
|
457
|
- throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_USE_DOUBLE);
|
|
|
458
|
- }
|
|
|
459
|
-
|
|
|
460
|
- //判断生失效时间
|
|
|
461
|
- if (userCoupon.getStartTime() > time || userCoupon.getEndTime() < time) {
|
|
|
462
|
- logger.warn("coupon has expire or not arrive time:{},{},{}", userCoupon.getStartTime(), userCoupon.getEndTime(), time);
|
|
|
463
|
- throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_ALLOW_OR_EXPIRE);
|
|
|
464
|
- }
|
|
|
465
|
-
|
|
|
466
|
- //券模板的状态
|
|
|
467
|
- Coupon coupon = couponCacheService.getCouponWithCache(userCoupon.getCouponToken());
|
|
|
468
|
- if (coupon == null) {
|
|
|
469
|
- logger.warn("coupons is null");
|
|
|
470
|
- throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_EXISTS);
|
|
|
471
|
- }
|
|
|
472
|
- if (coupon.getStatus() != CouponsStatusEnum.VALID.getCode()) {
|
|
|
473
|
- logger.warn("coupon status can't be use:{},{}", coupon.getId(), coupon.getStatus());
|
|
|
474
|
- throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
475
|
- }
|
|
|
476
|
- //所有的校验都通过了,生成bo
|
|
|
477
|
- Map<Integer, List<Integer>> couponProductIdMap = getProductIdListMap(Lists.newArrayList(coupon));
|
|
|
478
|
- List<Integer> productIds = couponProductIdMap.get(userCoupon.getCouponId());
|
|
|
479
|
-
|
|
|
480
|
- // 单个获取couponType
|
|
|
481
|
- CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType() != null ? userCoupon.getCouponType().intValue() : null);
|
|
|
482
|
-
|
|
|
483
|
- UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon, couponType);
|
|
|
484
|
- couponBo.setProductIdInclude(productIds);
|
|
|
485
|
-
|
|
|
486
|
- return couponBo;
|
|
|
487
|
-
|
|
|
488
|
- }).collect(Collectors.toList());
|
|
|
489
|
- return UserCouponsListBo.builder().coupons(couponBoList).build();
|
|
|
490
|
- }
|
|
|
491
|
} |
502
|
} |