From 8a1798607ca771c322b3b03cb37190c128b38659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=97=B4=E8=B4=A7?= <252048765@qq.com> Date: Sat, 14 Sep 2024 19:46:02 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90BUG=E3=80=91=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=94=B5=E5=AD=90=E4=BC=98=E6=83=A0=E5=88=B8=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E5=8F=91=E6=94=BE=E4=B8=8D=E8=83=BD=E4=BF=9D=E5=AD=98=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vo/template/CouponTemplateBaseVO.java | 60 ++++++++++++++++++- .../coupon/CouponTemplateServiceImpl.java | 5 ++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplateBaseVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplateBaseVO.java index 6885246b4..5b8a68f38 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplateBaseVO.java +++ b/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; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; +import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; import cn.iocoder.yudao.framework.common.validation.InEnum; import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum; import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum; +import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTemplateValidityTypeEnum; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -12,6 +14,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import org.springframework.format.annotation.DateTimeFormat; +import jakarta.validation.Validator; import jakarta.validation.constraints.AssertTrue; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; @@ -37,11 +40,11 @@ public class CouponTemplateBaseVO { private String description; @Schema(description = "发行总量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") // -1 - 则表示不限制发放数量 - @NotNull(message = "发行总量不能为空") + @NotNull(message = "发行总量不能为空", groups = {User.class}) private Integer totalCount; @Schema(description = "每人限领个数", requiredMode = Schema.RequiredMode.REQUIRED, example = "66") // -1 - 则表示不限制 - @NotNull(message = "每人限领个数不能为空") + @NotNull(message = "每人限领个数不能为空", groups = {User.class}) private Integer takeLimitCount; @Schema(description = "领取方式", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @@ -89,13 +92,16 @@ public class CouponTemplateBaseVO { private Integer discountType; @Schema(description = "折扣百分比", example = "80") // 例如说,80% 为 80 + @NotNull(message = "折扣百分比不能为空", groups = {Percent.class}) private Integer discountPercent; @Schema(description = "优惠金额", example = "10") @Min(value = 0, message = "优惠金额需要大于等于 0") + @NotNull(message = "优惠金额不能为空", groups = {Price.class}) private Integer discountPrice; @Schema(description = "折扣上限", example = "100") // 单位:分,仅在 discountType 为 PERCENT 使用 + @NotNull(message = "折扣上限不能为空", groups = {Percent.class}) private Integer discountLimitPrice; @AssertTrue(message = "商品范围编号的数组不能为空") @@ -154,4 +160,54 @@ public class CouponTemplateBaseVO { || discountLimitPrice != null; } + //-------------------------领取方式校验start---------------------------- + + /** + * 直接领取 + */ + public interface User { + } + + /** + * 指定发放 + */ + public interface Admin { + } + + //-------------------------领取方式校验end------------------------------ + + //-------------------------优惠类型校验start---------------------------- + + /** + * 满减 + */ + public interface Price { + } + + /** + * 折扣 + */ + public interface Percent { + } + + //-------------------------优惠类型校验end------------------------------ + + public void validate(Validator validator) { + + //领取方式校验 + if (CouponTakeTypeEnum.USER.getType().equals(takeType)) { + ValidationUtils.validate(validator, this, User.class); + } else if (CouponTakeTypeEnum.ADMIN.getType().equals(takeType)) { + ValidationUtils.validate(validator, this, Admin.class); + } + + //优惠类型校验 + if (PromotionDiscountTypeEnum.PRICE.getType().equals(discountType)){ + ValidationUtils.validate(validator, this, Price.class); + } else if (PromotionDiscountTypeEnum.PERCENT.getType().equals(discountType)) { + ValidationUtils.validate(validator, this, Percent.class); + } + + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImpl.java index 019c45dae..360787978 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImpl.java +++ b/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; import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum; import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; import org.springframework.stereotype.Service; +import jakarta.validation.Validator; import org.springframework.validation.annotation.Validated; import jakarta.annotation.Resource; @@ -40,9 +41,13 @@ public class CouponTemplateServiceImpl implements CouponTemplateService { private ProductCategoryApi productCategoryApi; @Resource private ProductSpuApi productSpuApi; + @Resource + private Validator validator; @Override public Long createCouponTemplate(CouponTemplateCreateReqVO createReqVO) { + // 校验参数 + createReqVO.validate(validator); // 校验商品范围 validateProductScope(createReqVO.getProductScope(), createReqVO.getProductScopeValues()); // 插入