...
|
...
|
@@ -32,10 +32,7 @@ import java.time.LocalDate; |
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
import java.time.ZoneOffset;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.*;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
...
|
...
|
@@ -279,9 +276,10 @@ public class CouponServiceImpl implements ICouponService { |
|
|
}
|
|
|
|
|
|
// 批量获取coupon
|
|
|
List<String> couponIdList = list.stream().map(UserCoupon::getCouponToken).collect(Collectors.toList());
|
|
|
List<Coupon> couponList = couponCacheService.getCouponsWithCache(couponIdList);
|
|
|
Map<Integer, Coupon> couponMap = couponList.stream().filter(coupon -> {
|
|
|
List<String> couponTokens = list.stream().map(UserCoupon::getCouponToken).collect(Collectors.toList());
|
|
|
List<Coupon> couponList = couponCacheService.getCouponsWithCache(couponTokens);
|
|
|
|
|
|
final Map<Integer, Coupon> couponMap = couponList.stream().filter(coupon -> {
|
|
|
if (coupon.getStatus() != null && coupon.getStatus().intValue() == CouponsStatusEnum.VALID.getCode()) {
|
|
|
return true;
|
|
|
} else {
|
...
|
...
|
@@ -290,34 +288,28 @@ public class CouponServiceImpl implements ICouponService { |
|
|
}).collect(Collectors.toMap(Coupon::getId, Function.identity()));
|
|
|
|
|
|
// 优惠券商品限制 key=couponId, value=ProductId集合
|
|
|
Map<Integer, List<Integer>> couponProductIdMap = getProductIdListMap(couponList);
|
|
|
|
|
|
List<UserCouponsBo> couponBoList = list.stream().filter(userCoupon -> {
|
|
|
return couponMap.containsKey(userCoupon.getCouponId());
|
|
|
}).map(userCoupon -> {
|
|
|
|
|
|
Coupon coupon = couponMap.get(userCoupon.getCouponId());
|
|
|
List<Integer> productIds = couponProductIdMap.get(userCoupon.getCouponId());
|
|
|
|
|
|
// 单个获取couponType
|
|
|
CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType() != null ? userCoupon.getCouponType().intValue() : null);
|
|
|
|
|
|
UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon, couponType);
|
|
|
couponBo.setProductIdInclude(productIds);
|
|
|
|
|
|
return couponBo;
|
|
|
}).collect(Collectors.toList());
|
|
|
final Map<Integer, List<Integer>> couponProductIdMap = getAssociatedProductIdListMap(couponList);
|
|
|
|
|
|
//包装返回结果
|
|
|
List<UserCouponsBo> couponBoList = list.stream()
|
|
|
.filter(userCoupon -> couponMap.containsKey(userCoupon.getCouponId()))
|
|
|
.map(userCoupon -> wrapperUserCoupon(userCoupon, couponMap.get(userCoupon.getCouponId()), couponProductIdMap.get(userCoupon.getCouponId())))
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
return UserCouponsListBo.builder().coupons(couponBoList).build();
|
|
|
}
|
|
|
|
|
|
private Map<Integer, List<Integer>> getProductIdListMap(List<Coupon> couponList) {
|
|
|
// 批量获取 coupon_product_limt 过滤出商品限制的优惠券id
|
|
|
/**
|
|
|
* 关联的商品id
|
|
|
* @param couponList
|
|
|
* @return
|
|
|
*/
|
|
|
private Map<Integer, List<Integer>> getAssociatedProductIdListMap(List<Coupon> couponList) {
|
|
|
// 批量获取 coupon_product_limit 过滤出商品限制的优惠券id
|
|
|
List<Integer> productLimitCouponIds = couponList.stream().filter(coupon -> {
|
|
|
if (coupon.getProductLimitType()!=null && CouponProductLimitTypeEnum.SPECIFIC_PRODUCT.getLimitType().equals(String.valueOf(coupon.getProductLimitType()))){
|
|
|
if (coupon.getProductLimitType() != null && !StringUtils.equals(CouponProductLimitTypeEnum.NON.getLimitType(),String.valueOf(coupon.getProductLimitType()))) {
|
|
|
return true;
|
|
|
}else{
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}).map(Coupon::getId).collect(Collectors.toList());
|
...
|
...
|
@@ -326,6 +318,84 @@ public class CouponServiceImpl implements ICouponService { |
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public UserCouponsListBo checkAndGetCoupons(int uid, List<String> couponCodes) {
|
|
|
//数据校验
|
|
|
if (uid <= 0 || CollectionUtils.isEmpty(couponCodes)) {
|
|
|
logger.warn("check coupon use param error:{}, {}", uid, couponCodes);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_PARAM_IS_ERROR);
|
|
|
}
|
|
|
|
|
|
// 获取用户券记录
|
|
|
List<UserCoupon> userCoupons = userCouponMapper.selectByUidAndCouponCodes(uid, couponCodes);
|
|
|
|
|
|
// 批量获取coupon
|
|
|
List<String> couponTokens = userCoupons.stream().map(UserCoupon::getCouponToken).collect(Collectors.toList());
|
|
|
|
|
|
List<Coupon> couponList = couponCacheService.getCouponsWithCache(couponTokens);
|
|
|
|
|
|
final Map<Integer, Coupon> couponMap = couponList.stream().filter(coupon -> {
|
|
|
if (coupon.getStatus() != null && coupon.getStatus().intValue() == CouponsStatusEnum.VALID.getCode()) {
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}).collect(Collectors.toMap(Coupon::getId, Function.identity()));
|
|
|
|
|
|
// 优惠券商品限制 key=couponId, value=ProductId集合
|
|
|
Map<Integer, List<Integer>> couponProductIdMap = getAssociatedProductIdListMap(couponList);
|
|
|
|
|
|
|
|
|
int time = DateUtil.getCurrentTimeSecond();
|
|
|
List<UserCouponsBo> couponBoList = couponCodes.stream().map(couponCode -> {
|
|
|
|
|
|
UserCoupon userCoupon = userCoupons.stream().filter(e -> couponCode.equals(e.getCouponCode())).findAny().orElse(null);
|
|
|
logger.info("user coupon {},{},{}", uid, couponCode, userCoupon);
|
|
|
if (userCoupon == null) {
|
|
|
logger.warn("not find user coupon by{},{}", uid, couponCode);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_YOUS);
|
|
|
}
|
|
|
//判断券状态
|
|
|
if (userCoupon.getStatus().intValue() != CouponUseStatusEnum.NOT_USED.getCode()) {
|
|
|
logger.warn("user this coupon is used:{},{}", userCoupon.getUid(), userCoupon.getCouponCode());
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_USE_DOUBLE);
|
|
|
}
|
|
|
|
|
|
//判断生失效时间
|
|
|
if (userCoupon.getStartTime() > time || userCoupon.getEndTime() < time) {
|
|
|
logger.warn("coupon has expire or not arrive time:{},{},{}", userCoupon.getStartTime(), userCoupon.getEndTime(), time);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_ALLOW_OR_EXPIRE);
|
|
|
}
|
|
|
|
|
|
//券模板的状态
|
|
|
Coupon coupon = couponMap.get(userCoupon.getCouponId());
|
|
|
if (coupon == null) {
|
|
|
logger.warn("not find coupon by couponId:{}",userCoupon.getCouponId());
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_EXISTS);
|
|
|
}
|
|
|
if (coupon.getStatus() != CouponsStatusEnum.VALID.getCode()) {
|
|
|
logger.warn("coupon status can't be use:{},{}", coupon.getId(), coupon.getStatus());
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
}
|
|
|
|
|
|
//所有的校验都通过了,生成bo
|
|
|
return wrapperUserCoupon(userCoupon, coupon, couponProductIdMap.get(userCoupon.getCouponId()));
|
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
return UserCouponsListBo.builder().coupons(couponBoList).build();
|
|
|
}
|
|
|
|
|
|
private UserCouponsBo wrapperUserCoupon(final UserCoupon userCoupon, final Coupon coupon, final List<Integer> productIds) {
|
|
|
|
|
|
// 单个获取couponType
|
|
|
CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType() != null ? userCoupon.getCouponType().intValue() : null);
|
|
|
|
|
|
UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon, couponType, productIds);
|
|
|
|
|
|
return couponBo;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -429,63 +499,4 @@ public class CouponServiceImpl implements ICouponService { |
|
|
.size(req.getLimit())
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public UserCouponsListBo checkAndGetCoupons(int uid, List<String> couponCodes) {
|
|
|
//数据校验
|
|
|
if (uid <= 0 || CollectionUtils.isEmpty(couponCodes)) {
|
|
|
logger.warn("check coupon use param error:{}, {}", uid, couponCodes);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_PARAM_IS_ERROR);
|
|
|
}
|
|
|
|
|
|
// 获取用户券记录
|
|
|
List<UserCoupon> userCoupons = userCouponMapper.selectByUidAndCouponCodes(uid, couponCodes);
|
|
|
|
|
|
int time = DateUtil.getCurrentTimeSecond();
|
|
|
|
|
|
List<UserCouponsBo> couponBoList = couponCodes.stream().map(couponCode -> {
|
|
|
|
|
|
UserCoupon userCoupon = userCoupons.stream().filter(e -> couponCode.equals(e.getCouponCode())).findAny().orElse(null);
|
|
|
logger.info("user coupon {},{},{}", uid, couponCode, userCoupon);
|
|
|
if (userCoupon == null) {
|
|
|
logger.warn("not find user coupon by{},{}", uid, couponCode);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_YOUS);
|
|
|
}
|
|
|
//判断券状态
|
|
|
if (userCoupon.getStatus().intValue() != CouponUseStatusEnum.NOT_USED.getCode()) {
|
|
|
logger.warn("user this coupon is used:{},{}", userCoupon.getUid(), userCoupon.getCouponCode());
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_USE_DOUBLE);
|
|
|
}
|
|
|
|
|
|
//判断生失效时间
|
|
|
if (userCoupon.getStartTime() > time || userCoupon.getEndTime() < time) {
|
|
|
logger.warn("coupon has expire or not arrive time:{},{},{}", userCoupon.getStartTime(), userCoupon.getEndTime(), time);
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_ALLOW_OR_EXPIRE);
|
|
|
}
|
|
|
|
|
|
//券模板的状态
|
|
|
Coupon coupon = couponCacheService.getCouponWithCache(userCoupon.getCouponToken());
|
|
|
if (coupon == null) {
|
|
|
logger.warn("coupons is null");
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_EXISTS);
|
|
|
}
|
|
|
if (coupon.getStatus() != CouponsStatusEnum.VALID.getCode()) {
|
|
|
logger.warn("coupon status can't be use:{},{}", coupon.getId(), coupon.getStatus());
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
|
|
|
}
|
|
|
//所有的校验都通过了,生成bo
|
|
|
Map<Integer, List<Integer>> couponProductIdMap = getProductIdListMap(Lists.newArrayList(coupon));
|
|
|
List<Integer> productIds = couponProductIdMap.get(userCoupon.getCouponId());
|
|
|
|
|
|
// 单个获取couponType
|
|
|
CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType() != null ? userCoupon.getCouponType().intValue() : null);
|
|
|
|
|
|
UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon, couponType);
|
|
|
couponBo.setProductIdInclude(productIds);
|
|
|
|
|
|
return couponBo;
|
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
return UserCouponsListBo.builder().coupons(couponBoList).build();
|
|
|
}
|
|
|
} |
...
|
...
|
|