Authored by tanling

优惠券优化

... ... @@ -31,5 +31,9 @@
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-dal</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.ufo.model</groupId>
<artifactId>promotion-ufo-model</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
... ... @@ -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;
}
... ...
... ... @@ -29,6 +29,11 @@ public class Coupon implements Serializable {
public static final int PRODUCTLIMITTYPE_SPECIALPRODUCT = 1;
/**商品限制条件-无限制*/
public static final int PRODUCTLIMITTYPE_NON = 2;
/**
* 商品限制 -- 排除商品
*/
public static final int PRODUCTLIMITTYPE_EXCLUDE = 3;
public enum CouponStatusEnum {
//0:待审核,1:有效,2:审核驳回,3:作废
WAITE_CHECK(0,"待审核"),IN_EFFECT(1,"有效"),REVIEW_REJECTION(2,"审核驳回"),INVALID(3,"已作废");
... ...
... ... @@ -71,7 +71,7 @@
<if test="param.productLimitType != 1">
product_limit_value,
</if>
start_time,end_time,status,create_time,pid,remark)
start_time,end_time,status,create_time,pid,remark,skup_forbid_type)
values(#{param.couponToken},#{param.couponName},#{param.couponAmount},#{param.couponType}
,#{param.couponNum},#{param.useNum},#{param.sendNum},#{param.useLimitType},
<if test="param.useLimitType == 2">
... ... @@ -81,7 +81,7 @@
<if test="param.productLimitType != 1">
#{param.productLimitValue},
</if>
#{param.startTime},#{param.endTime},#{param.status},#{param.createTime},#{param.pid},#{param.remark})
#{param.startTime},#{param.endTime},#{param.status},#{param.createTime},#{param.pid},#{param.remark},#{param.skupForbidType})
</insert>
<update id="updateByCouponSaveUpdateReq">
update coupon
... ... @@ -137,6 +137,9 @@
<if test="param.remark != null">
remark=#{param.remark},
</if>
<if test="param.skupForbidType != null">
skup_forbid_type=#{param.skupForbidType},
</if>
</set>
where id = #{param.id}
</update>
... ...
... ... @@ -51,6 +51,11 @@
</dependency>
<dependency>
<groupId>com.yoho.ufo.model</groupId>
<artifactId>promotion-ufo-model</artifactId>
<version>${ufo.model.version}</version>
</dependency>
<dependency>
<groupId>com.yoho.ufo.model</groupId>
<artifactId>searchword-ufo-model</artifactId>
<version>${ufo.model.version}</version>
</dependency>
... ...