|
|
package com.yohoufo.promotion.service.impl;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import com.google.common.collect.Range;
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.service.model.promotion.ProductLimitValueBo;
|
|
|
import com.yohobuy.ufo.model.promotion.CouponSimpleBo;
|
|
|
import com.yohobuy.ufo.model.promotion.UserCouponsBo;
|
|
|
import com.yohobuy.ufo.model.promotion.UserCouponsListBo;
|
|
|
import com.yohobuy.ufo.model.promotion.constant.CouponProductLimitTypeEnum;
|
|
|
import com.yohobuy.ufo.model.promotion.constant.CouponUseStatusEnum;
|
|
|
import com.yohobuy.ufo.model.promotion.constant.CouponsStatusEnum;
|
|
|
import com.yohoufo.dal.promotion.CouponProductLimitMapper;
|
|
|
import com.yohoufo.promotion.model.CouponReceiveChannel;
|
|
|
import com.yohobuy.ufo.model.promotion.request.CouponSendType;
|
|
|
import com.yohobuy.ufo.model.promotion.request.UserCouponListReq;
|
...
|
...
|
@@ -37,6 +40,7 @@ import org.slf4j.LoggerFactory; |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.lang.reflect.Array;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
...
|
...
|
@@ -62,6 +66,9 @@ public class CouponServiceImpl implements ICouponService { |
|
|
CouponMapper couponMapper;
|
|
|
|
|
|
@Autowired
|
|
|
CouponProductLimitMapper couponProductLimitMapper;
|
|
|
|
|
|
@Autowired
|
|
|
SingleCentCouponService singleCentSyncCoupNumService;
|
|
|
|
|
|
@Autowired
|
...
|
...
|
@@ -83,24 +90,27 @@ public class CouponServiceImpl implements ICouponService { |
|
|
if (CouponProductLimitTypeEnum.NON.getLimitType().equals(coupon.getProductLimitType().toString())) {
|
|
|
return new CouponInfo();
|
|
|
}
|
|
|
Map<Integer, List<Integer>> map = getAssociatedProductIdListMap(Arrays.asList(coupon));
|
|
|
if (map.isEmpty()) {
|
|
|
return null;
|
|
|
}
|
|
|
String productIds = StringUtils.join(map.values().iterator().next(), ",");
|
|
|
CouponInfo info = new CouponInfo();
|
|
|
if (CouponProductLimitTypeEnum.SPECIFIC_PRODUCT.getLimitType().equals(coupon.getProductLimitType().toString())) {
|
|
|
info.setContainsProductIds(productIds);
|
|
|
}
|
|
|
if (CouponProductLimitTypeEnum.EXCLUDE_PRODUCT.getLimitType().equals(coupon.getProductLimitType().toString())) {
|
|
|
info.setExcludeProductIds(productIds);
|
|
|
// Map<Integer, List<Integer>> map = getAssociatedProductIdListMap(Arrays.asList(coupon));
|
|
|
// if (map.isEmpty()) {
|
|
|
// return null;
|
|
|
// }
|
|
|
// String productIds = StringUtils.join(map.values().iterator().next(), ",");
|
|
|
// CouponInfo info = new CouponInfo();
|
|
|
// if (CouponProductLimitTypeEnum.SPECIFIC_PRODUCT.getLimitType().equals(coupon.getProductLimitType().toString())) {
|
|
|
// info.setContainsProductIds(productIds);
|
|
|
// }
|
|
|
// if (CouponProductLimitTypeEnum.EXCLUDE_PRODUCT.getLimitType().equals(coupon.getProductLimitType().toString())) {
|
|
|
// info.setExcludeProductIds(productIds);
|
|
|
// }
|
|
|
|
|
|
List<ProductLimitValueBo> productLimitValueBo = null;
|
|
|
Map<Integer, List<ProductLimitValueBo>> productLimitValueBoMap = getAssociatedProductIdMap(Arrays.asList(coupon));
|
|
|
if (productLimitValueBoMap != null){
|
|
|
productLimitValueBo = productLimitValueBoMap.get(coupon.getId());
|
|
|
}
|
|
|
|
|
|
CouponProductLimitTypeEnum limitTypeEnum = CouponProductLimitTypeEnum.of( String.valueOf(coupon.getProductLimitType())).orElse(null);
|
|
|
|
|
|
ProductLimitCollector.getSpecifiedProductLimitHandler(limitTypeEnum)
|
|
|
.ifPresent(limitValueHandler -> info.setProductLimitInfo(limitValueHandler.getProductLimitInfo(map.get(coupon.getId()) , coupon.getProductLimitValue() )));
|
|
|
|
|
|
CouponInfo info = new CouponInfo();
|
|
|
info.setProductLimitInfo(ProductLimitCollector.getProductLimitInfo(productLimitValueBo, coupon.getProductLimitType(), coupon.getProductLimitValue()));
|
|
|
|
|
|
return info;
|
|
|
}
|
...
|
...
|
@@ -422,17 +432,47 @@ public class CouponServiceImpl implements ICouponService { |
|
|
}).collect(Collectors.toMap(Coupon::getId, Function.identity()));
|
|
|
|
|
|
// 优惠券商品限制 key=couponId, value=ProductId集合
|
|
|
final Map<Integer, List<Integer>> couponProductIdMap = getAssociatedProductIdListMap(couponList);
|
|
|
// final Map<Integer, List<Integer>> couponProductIdMap = getAssociatedProductIdListMap(couponList);
|
|
|
Map<Integer, List<ProductLimitValueBo>> productLimitValueBoMap = getAssociatedProductIdMap(couponList);
|
|
|
|
|
|
//包装返回结果
|
|
|
List<UserCouponsBo> couponBoList = list.stream()
|
|
|
.filter(userCoupon -> couponMap.containsKey(userCoupon.getCouponId()))
|
|
|
.map(userCoupon -> wrapperUserCoupon(userCoupon, couponMap.get(userCoupon.getCouponId()), couponProductIdMap.get(userCoupon.getCouponId())))
|
|
|
.map(userCoupon -> wrapperUserCoupon(userCoupon, couponMap.get(userCoupon.getCouponId()), productLimitValueBoMap.get(userCoupon.getCouponId())))
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
return UserCouponsListBo.builder().coupons(couponBoList).build();
|
|
|
}
|
|
|
|
|
|
private Map<Integer, List<ProductLimitValueBo>> getAssociatedProductIdMap(List<Coupon> couponList){
|
|
|
|
|
|
|
|
|
List<CouponProductLimit> couponProductLimitList = couponProductLimitMapper.selectByCouponIds(couponList.stream().map(Coupon::getId).collect(Collectors.toList()));
|
|
|
|
|
|
Map<Integer, List<CouponProductLimit>> map = couponProductLimitList.stream().collect(Collectors.groupingBy(CouponProductLimit::getCouponId));
|
|
|
|
|
|
// key=couponId, value=
|
|
|
Map<Integer, List<ProductLimitValueBo>> productLimitValueBoMap = Maps.newHashMap();
|
|
|
|
|
|
for (Integer couponId : map.keySet()){
|
|
|
|
|
|
List<ProductLimitValueBo> productLimitValueBos = Lists.newArrayList();
|
|
|
|
|
|
// key=limitType, value=
|
|
|
Map<Integer, List<CouponProductLimit>> typeToItem = map.get(couponId).stream().collect(Collectors.groupingBy(CouponProductLimit::getLimitType));
|
|
|
for (Integer type : typeToItem.keySet()){
|
|
|
productLimitValueBos.add(ProductLimitValueBo.builder()
|
|
|
.limitType(type)
|
|
|
.productIds(typeToItem.get(type).stream().map(CouponProductLimit::getProductId).collect(Collectors.toList())).build());
|
|
|
}
|
|
|
|
|
|
productLimitValueBoMap.put(couponId, productLimitValueBos);
|
|
|
|
|
|
}
|
|
|
|
|
|
return productLimitValueBoMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 关联的商品id
|
|
|
*
|
...
|
...
|
@@ -478,7 +518,8 @@ public class CouponServiceImpl implements ICouponService { |
|
|
}).collect(Collectors.toMap(Coupon::getId, Function.identity()));
|
|
|
|
|
|
// 优惠券商品限制 key=couponId, value=ProductId集合
|
|
|
Map<Integer, List<Integer>> couponProductIdMap = getAssociatedProductIdListMap(couponList);
|
|
|
// Map<Integer, List<Integer>> couponProductIdMap = getAssociatedProductIdListMap(couponList);
|
|
|
Map<Integer, List<ProductLimitValueBo>> productLimitValueBoMap = getAssociatedProductIdMap(couponList);
|
|
|
|
|
|
|
|
|
int time = DateUtil.getCurrentTimeSecond();
|
...
|
...
|
@@ -514,19 +555,19 @@ public class CouponServiceImpl implements ICouponService { |
|
|
}
|
|
|
|
|
|
//所有的校验都通过了,生成bo
|
|
|
return wrapperUserCoupon(userCoupon, coupon, couponProductIdMap.get(userCoupon.getCouponId()));
|
|
|
return wrapperUserCoupon(userCoupon, coupon, productLimitValueBoMap.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) {
|
|
|
private UserCouponsBo wrapperUserCoupon(final UserCoupon userCoupon, final Coupon coupon, final List<ProductLimitValueBo> productLimitValueBo) {
|
|
|
|
|
|
// 单个获取couponType
|
|
|
CouponType couponType = couponCacheService.getCouponTypeWithCache(userCoupon.getCouponType() != null ? userCoupon.getCouponType().intValue() : null);
|
|
|
|
|
|
UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon, couponType, productIds);
|
|
|
UserCouponsBo couponBo = CouponConvert.covertCouponBo(userCoupon, coupon, couponType, productLimitValueBo);
|
|
|
|
|
|
return couponBo;
|
|
|
}
|
...
|
...
|
|