Authored by LUOXC

fixbug

@@ -2,6 +2,8 @@ package com.yoho.ufo.coupon.service.impl; @@ -2,6 +2,8 @@ package com.yoho.ufo.coupon.service.impl;
2 2
3 import com.alibaba.fastjson.JSON; 3 import com.alibaba.fastjson.JSON;
4 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONObject;
  5 +import com.google.common.base.Splitter;
  6 +import com.google.common.collect.Lists;
5 import com.yoho.core.common.utils.DateUtil; 7 import com.yoho.core.common.utils.DateUtil;
6 import com.yoho.error.exception.ServiceException; 8 import com.yoho.error.exception.ServiceException;
7 import com.yoho.ufo.coupon.service.ICouponService; 9 import com.yoho.ufo.coupon.service.ICouponService;
@@ -30,7 +32,9 @@ import org.springframework.stereotype.Service; @@ -30,7 +32,9 @@ import org.springframework.stereotype.Service;
30 import javax.servlet.http.HttpServletRequest; 32 import javax.servlet.http.HttpServletRequest;
31 import java.util.ArrayList; 33 import java.util.ArrayList;
32 import java.util.List; 34 import java.util.List;
  35 +import java.util.Objects;
33 import java.util.UUID; 36 import java.util.UUID;
  37 +import java.util.stream.Collectors;
34 38
35 /** 39 /**
36 * Created by shengguo.cai on 2018/11/20. 40 * Created by shengguo.cai on 2018/11/20.
@@ -121,35 +125,73 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ @@ -121,35 +125,73 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{
121 if(!checkSaveOrUpdateCouponParam(req)){ 125 if(!checkSaveOrUpdateCouponParam(req)){
122 return new ApiResponse.ApiResponseBuilder().code(500).message("参数有误").build(); 126 return new ApiResponse.ApiResponseBuilder().code(500).message("参数有误").build();
123 } 127 }
124 - //TODO 每次更新必须更新该字段,否则我怎么判断是否更新该字段呢?productLimitType为1时,是否一定要设置该关联对象?  
125 - //TODO 若productLimitType=1时关联对象必填,则可用:req.getId()!=null && (req.getProductLimitType == 2 || req.getProductLimitValue()!=null)  
126 - if(req.getId() != null){  
127 - LOGGER.info("before saveOrUpdateCoupon#deleteByCouponId,couponId is {}",req.getId());  
128 - couponProductLimitMapper.deleteByCouponId(req.getId());  
129 - } 128 + int productLimitType = req.getProductLimitType().intValue();
  129 + List<Integer> productIds = getProductIdsAndCheckProductLimitParam(productLimitType,req.getProductLimitValue());
  130 +
  131 + // 初始化非必填数据
130 req.setSendNum(null == req.getSendNum()?0:req.getSendNum()); 132 req.setSendNum(null == req.getSendNum()?0:req.getSendNum());
131 req.setPid(new UserHelper().getUserId()); 133 req.setPid(new UserHelper().getUserId());
132 - //TODO 新增和修改暂时都写死的数据  
133 - req.setProductLimitType(Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT);  
134 req.setCouponType(100); 134 req.setCouponType(100);
135 135
136 - if(null == req.getId()){ 136 + // 如果是修改获取并校验优惠券信息
  137 + Coupon coupon;
  138 + if (req.getId() != null) {
  139 + coupon = couponMapper.selectById(req.getId());
  140 + if (Objects.isNull(coupon)) {
  141 + throw new ServiceException(400, "错误:优惠券信息不存在");
  142 + }
  143 + } else {
  144 + coupon = null;
  145 + }
  146 +
  147 + if(Objects.isNull(coupon)){
  148 + LOGGER.info("before saveOrUpdateCoupon#insert,req is {}",req);
137 req.setCouponToken(UUID.randomUUID().toString()); 149 req.setCouponToken(UUID.randomUUID().toString());
138 req.setCreateTime(DateUtil.getCurrentTimeSecond()); 150 req.setCreateTime(DateUtil.getCurrentTimeSecond());
139 req.setStatus(Coupon.CouponStatusEnum.IN_EFFECT.getValue()); 151 req.setStatus(Coupon.CouponStatusEnum.IN_EFFECT.getValue());
140 couponMapper.insertByCouponSaveUpdateReq(req); 152 couponMapper.insertByCouponSaveUpdateReq(req);
141 - }else{ 153 + LOGGER.info("after saveOrUpdateCoupon#insert,req is {}",req);
  154 + }else {
  155 + LOGGER.info("before saveOrUpdateCoupon#deleteByCouponId,couponId is {}",req.getId());
  156 + if(coupon.getProductLimitType() == Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT){
  157 + couponProductLimitMapper.deleteByCouponId(req.getId());
  158 + }
  159 + LOGGER.info("before saveOrUpdateCoupon#update,req is {}",req);
142 couponMapper.updateByCouponSaveUpdateReq(req); 160 couponMapper.updateByCouponSaveUpdateReq(req);
  161 + LOGGER.info("after saveOrUpdateCoupon#update,req is {}",req);
143 } 162 }
144 - LOGGER.info("after saveOrUpdateCoupon#insert or update,req is {}",req);  
145 - String[] productIds = StringUtils.isBlank(req.getProductLimitValue())?null:req.getProductLimitValue().split(",");  
146 - LOGGER.info("before saveOrUpdateCoupon#couponProductLimitMapper.insertBatchByProductIds,productIds is {}",productIds);  
147 - if(productIds != null){ 163 +
  164 +
  165 + if (Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT == productLimitType) {
  166 + LOGGER.info("before saveOrUpdateCoupon#couponProductLimitMapper.insertBatchByProductIds,productIds is {}",productIds);
148 couponProductLimitMapper.insertBatchByProductIds(productIds,Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT,req.getId()); 167 couponProductLimitMapper.insertBatchByProductIds(productIds,Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT,req.getId());
149 } 168 }
150 return new ApiResponse.ApiResponseBuilder().build(); 169 return new ApiResponse.ApiResponseBuilder().build();
151 } 170 }
152 171
  172 + private List<Integer> getProductIdsAndCheckProductLimitParam(int productLimitType, String productLimitValue) {
  173 + if (Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT == productLimitType) {
  174 + if (StringUtils.isBlank(productLimitValue)) {
  175 + LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitValue is blank.");
  176 + throw new ServiceException(400, "错误:商品限制条件为特定商品,必须导入商品");
  177 + }
  178 + List<Integer> productIds = Splitter.on(",").splitToList(productLimitValue).stream().limit(500).map(Integer::valueOf).collect(Collectors.toList());
  179 + if (CollectionUtils.isEmpty(productIds)) {
  180 + LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitValue is blank.");
  181 + throw new ServiceException(400, "错误:商品限制条件为特定商品,必须导入商品");
  182 + } else {
  183 + return productIds;
  184 + }
  185 + } else if (Coupon.PRODUCTLIMITTYPE_NON == productLimitType) {
  186 + return Lists.newArrayList();
  187 + } else {
  188 + LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitType is {}.", productLimitType);
  189 + throw new ServiceException(400, "错误:商品限制条件未知");
  190 + }
  191 + }
  192 +
  193 +
  194 +
153 @Override 195 @Override
154 public ApiResponse getCouponInfo(Integer id) { 196 public ApiResponse getCouponInfo(Integer id) {
155 LOGGER.info("enter getCouponInfo, id is {}",id); 197 LOGGER.info("enter getCouponInfo, id is {}",id);
@@ -221,6 +263,10 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{ @@ -221,6 +263,10 @@ public class CouponServiceImpl implements ICouponService,IBusinessExportService{
221 LOGGER.info("checkSaveOrUpdateCouponParam failed! remark is blank."); 263 LOGGER.info("checkSaveOrUpdateCouponParam failed! remark is blank.");
222 return false; 264 return false;
223 } 265 }
  266 + if(Objects.isNull(req.getProductLimitType())){
  267 + LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitType is blank.");
  268 + return false;
  269 + }
224 return true; 270 return true;
225 } 271 }
226 272
@@ -11,7 +11,7 @@ import java.util.List; @@ -11,7 +11,7 @@ import java.util.List;
11 public interface CouponProductLimitMapper { 11 public interface CouponProductLimitMapper {
12 void deleteByCouponId(@Param("id") Integer id); 12 void deleteByCouponId(@Param("id") Integer id);
13 13
14 - void insertBatchByProductIds(@Param("productIds") String[] productIds, @Param("limitType") int limitType, @Param("couponId") int couponId); 14 + void insertBatchByProductIds(@Param("productIds") List<Integer> productIds, @Param("limitType") int limitType, @Param("couponId") int couponId);
15 15
16 List<CouponProductLimit> selectByCouponId(@Param("id") Integer id); 16 List<CouponProductLimit> selectByCouponId(@Param("id") Integer id);
17 17
@@ -27,6 +27,8 @@ public class Coupon implements Serializable { @@ -27,6 +27,8 @@ public class Coupon implements Serializable {
27 private String remark; 27 private String remark;
28 /**商品限制条件-特定商品*/ 28 /**商品限制条件-特定商品*/
29 public static final int PRODUCTLIMITTYPE_SPECIALPRODUCT = 1; 29 public static final int PRODUCTLIMITTYPE_SPECIALPRODUCT = 1;
  30 + /**商品限制条件-无限制*/
  31 + public static final int PRODUCTLIMITTYPE_NON = 2;
30 public enum CouponStatusEnum { 32 public enum CouponStatusEnum {
31 //0:待审核,1:有效,2:审核驳回,3:作废 33 //0:待审核,1:有效,2:审核驳回,3:作废
32 WAITE_CHECK(0,"待审核"),IN_EFFECT(1,"有效"),REVIEW_REJECTION(2,"审核驳回"),INVALID(3,"已作废"); 34 WAITE_CHECK(0,"待审核"),IN_EFFECT(1,"有效"),REVIEW_REJECTION(2,"审核驳回"),INVALID(3,"已作废");
  1 +<!DOCTYPE html>
  2 +<html lang="en">
  3 +<head>
  4 + <meta charset="UTF-8">
  5 + <title>$Title$</title>
  6 +</head>
  7 +<body>
  8 +$END$
  9 +</body>
  10 +</html>