Commit 8a1798607ca771c322b3b03cb37190c128b38659

Authored by 痴货
1 parent b69ba946

【BUG】修改电子优惠券指定发放不能保存的问题

yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplateBaseVO.java
... ... @@ -2,9 +2,11 @@ package cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.template;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.util.ObjectUtil;
  5 +import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
5 6 import cn.iocoder.yudao.framework.common.validation.InEnum;
6 7 import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
7 8 import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
  9 +import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum;
8 10 import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTemplateValidityTypeEnum;
9 11 import com.fasterxml.jackson.annotation.JsonFormat;
10 12 import com.fasterxml.jackson.annotation.JsonIgnore;
... ... @@ -12,6 +14,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
12 14 import lombok.Data;
13 15 import org.springframework.format.annotation.DateTimeFormat;
14 16  
  17 +import jakarta.validation.Validator;
15 18 import jakarta.validation.constraints.AssertTrue;
16 19 import jakarta.validation.constraints.Min;
17 20 import jakarta.validation.constraints.NotNull;
... ... @@ -37,11 +40,11 @@ public class CouponTemplateBaseVO {
37 40 private String description;
38 41  
39 42 @Schema(description = "发行总量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") // -1 - 则表示不限制发放数量
40   - @NotNull(message = "发行总量不能为空")
  43 + @NotNull(message = "发行总量不能为空", groups = {User.class})
41 44 private Integer totalCount;
42 45  
43 46 @Schema(description = "每人限领个数", requiredMode = Schema.RequiredMode.REQUIRED, example = "66") // -1 - 则表示不限制
44   - @NotNull(message = "每人限领个数不能为空")
  47 + @NotNull(message = "每人限领个数不能为空", groups = {User.class})
45 48 private Integer takeLimitCount;
46 49  
47 50 @Schema(description = "领取方式", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
... ... @@ -89,13 +92,16 @@ public class CouponTemplateBaseVO {
89 92 private Integer discountType;
90 93  
91 94 @Schema(description = "折扣百分比", example = "80") // 例如说,80% 为 80
  95 + @NotNull(message = "折扣百分比不能为空", groups = {Percent.class})
92 96 private Integer discountPercent;
93 97  
94 98 @Schema(description = "优惠金额", example = "10")
95 99 @Min(value = 0, message = "优惠金额需要大于等于 0")
  100 + @NotNull(message = "优惠金额不能为空", groups = {Price.class})
96 101 private Integer discountPrice;
97 102  
98 103 @Schema(description = "折扣上限", example = "100") // 单位:分,仅在 discountType 为 PERCENT 使用
  104 + @NotNull(message = "折扣上限不能为空", groups = {Percent.class})
99 105 private Integer discountLimitPrice;
100 106  
101 107 @AssertTrue(message = "商品范围编号的数组不能为空")
... ... @@ -154,4 +160,54 @@ public class CouponTemplateBaseVO {
154 160 || discountLimitPrice != null;
155 161 }
156 162  
  163 + //-------------------------领取方式校验start----------------------------
  164 +
  165 + /**
  166 + * 直接领取
  167 + */
  168 + public interface User {
  169 + }
  170 +
  171 + /**
  172 + * 指定发放
  173 + */
  174 + public interface Admin {
  175 + }
  176 +
  177 + //-------------------------领取方式校验end------------------------------
  178 +
  179 + //-------------------------优惠类型校验start----------------------------
  180 +
  181 + /**
  182 + * 满减
  183 + */
  184 + public interface Price {
  185 + }
  186 +
  187 + /**
  188 + * 折扣
  189 + */
  190 + public interface Percent {
  191 + }
  192 +
  193 + //-------------------------优惠类型校验end------------------------------
  194 +
  195 + public void validate(Validator validator) {
  196 +
  197 + //领取方式校验
  198 + if (CouponTakeTypeEnum.USER.getType().equals(takeType)) {
  199 + ValidationUtils.validate(validator, this, User.class);
  200 + } else if (CouponTakeTypeEnum.ADMIN.getType().equals(takeType)) {
  201 + ValidationUtils.validate(validator, this, Admin.class);
  202 + }
  203 +
  204 + //优惠类型校验
  205 + if (PromotionDiscountTypeEnum.PRICE.getType().equals(discountType)){
  206 + ValidationUtils.validate(validator, this, Price.class);
  207 + } else if (PromotionDiscountTypeEnum.PERCENT.getType().equals(discountType)) {
  208 + ValidationUtils.validate(validator, this, Percent.class);
  209 + }
  210 +
  211 + }
  212 +
157 213 }
... ...
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImpl.java
... ... @@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.promotion.dal.mysql.coupon.CouponTemplateMapper;
13 13 import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
14 14 import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum;
15 15 import org.springframework.stereotype.Service;
  16 +import jakarta.validation.Validator;
16 17 import org.springframework.validation.annotation.Validated;
17 18  
18 19 import jakarta.annotation.Resource;
... ... @@ -40,9 +41,13 @@ public class CouponTemplateServiceImpl implements CouponTemplateService {
40 41 private ProductCategoryApi productCategoryApi;
41 42 @Resource
42 43 private ProductSpuApi productSpuApi;
  44 + @Resource
  45 + private Validator validator;
43 46  
44 47 @Override
45 48 public Long createCouponTemplate(CouponTemplateCreateReqVO createReqVO) {
  49 + // 校验参数
  50 + createReqVO.validate(validator);
46 51 // 校验商品范围
47 52 validateProductScope(createReqVO.getProductScope(), createReqVO.getProductScopeValues());
48 53 // 插入
... ...