...
|
...
|
@@ -6,6 +6,7 @@ import com.google.common.base.Splitter; |
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.core.rabbitmq.YhProducer;
|
|
|
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.ufo.coupon.service.ICouponService;
|
|
|
import com.yoho.ufo.dal.CouponMapper;
|
...
|
...
|
@@ -28,6 +29,8 @@ import com.yohobuy.ufo.coupon.req.CouponSaveUpdateReq; |
|
|
import com.yohobuy.ufo.coupon.req.CouponSendReq;
|
|
|
import com.yohobuy.ufo.coupon.req.UserCouponQueryReq;
|
|
|
import com.yohobuy.ufo.model.common.PageResponseBO;
|
|
|
import com.yohobuy.ufo.model.order.constants.SkupType;
|
|
|
import com.yohobuy.ufo.model.promotion.constant.CouponTypeEnum;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang.math.NumberUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
...
|
...
|
@@ -139,6 +142,26 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ |
|
|
public ApiResponse saveOrUpdateCoupon(CouponSaveUpdateReq req) {
|
|
|
LOGGER.info("enter saveOrUpdateCoupon,param is {}",req);
|
|
|
checkSaveOrUpdateCouponParam(req);
|
|
|
|
|
|
// 检查优惠券类型
|
|
|
Optional<CouponTypeEnum> couponTypeEnum = CouponTypeEnum.of(req.getCouponType());
|
|
|
if (!couponTypeEnum.isPresent()){
|
|
|
throw new ServiceException(400, "错误:不合法的优惠券类型");
|
|
|
}
|
|
|
|
|
|
// 检查限制的商品类型
|
|
|
if (StringUtils.isNoneBlank(req.getSkupForbidType())){
|
|
|
String[] limitTypes = req.getSkupForbidType().split(",");
|
|
|
for (String limitType : limitTypes){
|
|
|
if (!limitType.matches("\\d+")){
|
|
|
throw new ServiceException(400, "错误:不合法的限制类型");
|
|
|
}
|
|
|
if (!SkupType.of(Integer.parseInt(limitType)).isPresent()){
|
|
|
throw new ServiceException(401, "错误:不合法的限制类型");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int productLimitType = req.getProductLimitType().intValue();
|
|
|
List<Integer> productIds = getProductIdsAndCheckProductLimitParam(productLimitType,req.getProductLimitValue());
|
|
|
|
...
|
...
|
@@ -147,21 +170,17 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ |
|
|
req.setPid(new UserHelper().getUserId());
|
|
|
req.setCouponType(100);
|
|
|
|
|
|
|
|
|
// 如果是修改获取并校验优惠券信息
|
|
|
Coupon coupon;
|
|
|
Coupon coupon = null;
|
|
|
if (req.getId() != null) {
|
|
|
coupon = couponMapper.selectById(req.getId());
|
|
|
if (Objects.isNull(coupon)) {
|
|
|
throw new ServiceException(400, "错误:优惠券信息不存在");
|
|
|
}
|
|
|
if (coupon.getSendNum() > 0) {
|
|
|
LOGGER.info("checkUpdateCouponParam failed! coupon is using! sendNum is {}", coupon.getSendNum());
|
|
|
throw new ServiceException(500, "修改失败,优惠券已发放");
|
|
|
}
|
|
|
} else {
|
|
|
coupon = null;
|
|
|
}
|
|
|
|
|
|
// 插入
|
|
|
if(Objects.isNull(coupon)){
|
|
|
LOGGER.info("before saveOrUpdateCoupon#insert,req is {}",req);
|
|
|
req.setCouponToken(UUID.randomUUID().toString());
|
...
|
...
|
@@ -169,34 +188,63 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ |
|
|
req.setStatus(Coupon.CouponStatusEnum.IN_EFFECT.getValue());
|
|
|
couponMapper.insertByCouponSaveUpdateReq(req);
|
|
|
LOGGER.info("after saveOrUpdateCoupon#insert,req is {}",req);
|
|
|
}else {
|
|
|
|
|
|
saveProductLimitItems(req, productLimitType, productIds);
|
|
|
|
|
|
}
|
|
|
// 更新
|
|
|
else {
|
|
|
LOGGER.info("before saveOrUpdateCoupon#deleteByCouponId,couponId is {}",req.getId());
|
|
|
if(coupon.getProductLimitType() == Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT){
|
|
|
couponProductLimitMapper.deleteByCouponId(req.getId());
|
|
|
|
|
|
// 已经有了优惠券的发放记录
|
|
|
if (coupon.getSendNum() > 0){
|
|
|
CouponSaveUpdateReq couponSaveUpdateReq = new CouponSaveUpdateReq();
|
|
|
couponSaveUpdateReq.setId(coupon.getId());
|
|
|
|
|
|
// 名称
|
|
|
couponSaveUpdateReq.setCouponName(req.getCouponName());
|
|
|
|
|
|
// 数量
|
|
|
couponSaveUpdateReq.setCouponNum(req.getCouponNum());
|
|
|
|
|
|
// 优惠券说明
|
|
|
couponSaveUpdateReq.setRemark(req.getRemark());
|
|
|
|
|
|
couponMapper.updateByCouponSaveUpdateReq(couponSaveUpdateReq);
|
|
|
}else{
|
|
|
|
|
|
couponMapper.updateByCouponSaveUpdateReq(req);
|
|
|
|
|
|
saveProductLimitItems(req, productLimitType, productIds);
|
|
|
}
|
|
|
LOGGER.info("before saveOrUpdateCoupon#update,req is {}",req);
|
|
|
couponMapper.updateByCouponSaveUpdateReq(req);
|
|
|
|
|
|
LOGGER.info("after saveOrUpdateCoupon#update,req is {}",req);
|
|
|
}
|
|
|
|
|
|
|
|
|
if (Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT == productLimitType) {
|
|
|
LOGGER.info("before saveOrUpdateCoupon#couponProductLimitMapper.insertBatchByProductIds,productIds is {}",productIds);
|
|
|
couponProductLimitMapper.insertBatchByProductIds(productIds,Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT,req.getId());
|
|
|
}
|
|
|
|
|
|
return new ApiResponse.ApiResponseBuilder().build();
|
|
|
}
|
|
|
|
|
|
private void saveProductLimitItems(CouponSaveUpdateReq req, int productLimitType, List<Integer> productIds) {
|
|
|
if(req.getProductLimitType() == Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT || req.getProductLimitType() == Coupon.PRODUCTLIMITTYPE_EXCLUDE){
|
|
|
// 清理之前的记录
|
|
|
couponProductLimitMapper.deleteByCouponId(req.getId());
|
|
|
// 重新插入
|
|
|
couponProductLimitMapper.insertBatchByProductIds(productIds, productLimitType, req.getId());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private List<Integer> getProductIdsAndCheckProductLimitParam(int productLimitType, String productLimitValue) {
|
|
|
if (Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT == productLimitType) {
|
|
|
if (Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT == productLimitType || Coupon.PRODUCTLIMITTYPE_EXCLUDE == productLimitType) {
|
|
|
if (StringUtils.isBlank(productLimitValue)) {
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitValue is blank.");
|
|
|
throw new ServiceException(400, "错误:商品限制条件为特定商品,必须导入商品");
|
|
|
throw new ServiceException(400, "错误:商品限制条件为特定商品|排除商品,必须导入商品");
|
|
|
}
|
|
|
List<Integer> productIds = Splitter.on(",").splitToList(productLimitValue).stream().limit(500).map(Integer::valueOf).collect(Collectors.toList());
|
|
|
if (CollectionUtils.isEmpty(productIds)) {
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitValue is blank.");
|
|
|
throw new ServiceException(400, "错误:商品限制条件为特定商品,必须导入商品");
|
|
|
throw new ServiceException(400, "错误:商品限制条件为特定商品|排除商品,必须导入商品");
|
|
|
} else {
|
|
|
return productIds;
|
|
|
}
|
...
|
...
|
|