...
|
...
|
@@ -2,6 +2,8 @@ package com.yoho.ufo.coupon.service.impl; |
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.base.Splitter;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.ufo.coupon.service.ICouponService;
|
...
|
...
|
@@ -30,7 +32,9 @@ import org.springframework.stereotype.Service; |
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.UUID;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by shengguo.cai on 2018/11/20.
|
...
|
...
|
@@ -121,35 +125,73 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ |
|
|
if(!checkSaveOrUpdateCouponParam(req)){
|
|
|
return new ApiResponse.ApiResponseBuilder().code(500).message("参数有误").build();
|
|
|
}
|
|
|
//TODO 每次更新必须更新该字段,否则我怎么判断是否更新该字段呢?productLimitType为1时,是否一定要设置该关联对象?
|
|
|
//TODO 若productLimitType=1时关联对象必填,则可用:req.getId()!=null && (req.getProductLimitType == 2 || req.getProductLimitValue()!=null)
|
|
|
if(req.getId() != null){
|
|
|
LOGGER.info("before saveOrUpdateCoupon#deleteByCouponId,couponId is {}",req.getId());
|
|
|
couponProductLimitMapper.deleteByCouponId(req.getId());
|
|
|
}
|
|
|
int productLimitType = req.getProductLimitType().intValue();
|
|
|
List<Integer> productIds = getProductIdsAndCheckProductLimitParam(productLimitType,req.getProductLimitValue());
|
|
|
|
|
|
// 初始化非必填数据
|
|
|
req.setSendNum(null == req.getSendNum()?0:req.getSendNum());
|
|
|
req.setPid(new UserHelper().getUserId());
|
|
|
//TODO 新增和修改暂时都写死的数据
|
|
|
req.setProductLimitType(Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT);
|
|
|
req.setCouponType(100);
|
|
|
|
|
|
if(null == req.getId()){
|
|
|
// 如果是修改获取并校验优惠券信息
|
|
|
Coupon coupon;
|
|
|
if (req.getId() != null) {
|
|
|
coupon = couponMapper.selectById(req.getId());
|
|
|
if (Objects.isNull(coupon)) {
|
|
|
throw new ServiceException(400, "错误:优惠券信息不存在");
|
|
|
}
|
|
|
} else {
|
|
|
coupon = null;
|
|
|
}
|
|
|
|
|
|
if(Objects.isNull(coupon)){
|
|
|
LOGGER.info("before saveOrUpdateCoupon#insert,req is {}",req);
|
|
|
req.setCouponToken(UUID.randomUUID().toString());
|
|
|
req.setCreateTime(DateUtil.getCurrentTimeSecond());
|
|
|
req.setStatus(Coupon.CouponStatusEnum.IN_EFFECT.getValue());
|
|
|
couponMapper.insertByCouponSaveUpdateReq(req);
|
|
|
}else{
|
|
|
LOGGER.info("after saveOrUpdateCoupon#insert,req is {}",req);
|
|
|
}else {
|
|
|
LOGGER.info("before saveOrUpdateCoupon#deleteByCouponId,couponId is {}",req.getId());
|
|
|
if(coupon.getProductLimitType() == Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT){
|
|
|
couponProductLimitMapper.deleteByCouponId(req.getId());
|
|
|
}
|
|
|
LOGGER.info("before saveOrUpdateCoupon#update,req is {}",req);
|
|
|
couponMapper.updateByCouponSaveUpdateReq(req);
|
|
|
LOGGER.info("after saveOrUpdateCoupon#update,req is {}",req);
|
|
|
}
|
|
|
LOGGER.info("after saveOrUpdateCoupon#insert or update,req is {}",req);
|
|
|
String[] productIds = StringUtils.isBlank(req.getProductLimitValue())?null:req.getProductLimitValue().split(",");
|
|
|
LOGGER.info("before saveOrUpdateCoupon#couponProductLimitMapper.insertBatchByProductIds,productIds is {}",productIds);
|
|
|
if(productIds != null){
|
|
|
|
|
|
|
|
|
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 List<Integer> getProductIdsAndCheckProductLimitParam(int productLimitType, String productLimitValue) {
|
|
|
if (Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT == productLimitType) {
|
|
|
if (StringUtils.isBlank(productLimitValue)) {
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitValue is blank.");
|
|
|
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, "错误:商品限制条件为特定商品,必须导入商品");
|
|
|
} else {
|
|
|
return productIds;
|
|
|
}
|
|
|
} else if (Coupon.PRODUCTLIMITTYPE_NON == productLimitType) {
|
|
|
return Lists.newArrayList();
|
|
|
} else {
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitType is {}.", productLimitType);
|
|
|
throw new ServiceException(400, "错误:商品限制条件未知");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse getCouponInfo(Integer id) {
|
|
|
LOGGER.info("enter getCouponInfo, id is {}",id);
|
...
|
...
|
@@ -221,6 +263,10 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ |
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! remark is blank.");
|
|
|
return false;
|
|
|
}
|
|
|
if(Objects.isNull(req.getProductLimitType())){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitType is blank.");
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
...
|
...
|
|