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

This commit is contained in:
痴货 2024-09-14 19:46:02 +08:00
parent b69ba94660
commit 8a1798607c
2 changed files with 63 additions and 2 deletions

View File

@ -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.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; 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.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum; 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.common.PromotionProductScopeEnum;
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum;
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTemplateValidityTypeEnum; import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTemplateValidityTypeEnum;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
@ -12,6 +14,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import jakarta.validation.Validator;
import jakarta.validation.constraints.AssertTrue; import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@ -37,11 +40,11 @@ public class CouponTemplateBaseVO {
private String description; private String description;
@Schema(description = "发行总量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") // -1 - 则表示不限制发放数量 @Schema(description = "发行总量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") // -1 - 则表示不限制发放数量
@NotNull(message = "发行总量不能为空") @NotNull(message = "发行总量不能为空", groups = {User.class})
private Integer totalCount; private Integer totalCount;
@Schema(description = "每人限领个数", requiredMode = Schema.RequiredMode.REQUIRED, example = "66") // -1 - 则表示不限制 @Schema(description = "每人限领个数", requiredMode = Schema.RequiredMode.REQUIRED, example = "66") // -1 - 则表示不限制
@NotNull(message = "每人限领个数不能为空") @NotNull(message = "每人限领个数不能为空", groups = {User.class})
private Integer takeLimitCount; private Integer takeLimitCount;
@Schema(description = "领取方式", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "领取方式", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@ -89,13 +92,16 @@ public class CouponTemplateBaseVO {
private Integer discountType; private Integer discountType;
@Schema(description = "折扣百分比", example = "80") // 例如说80% 80 @Schema(description = "折扣百分比", example = "80") // 例如说80% 80
@NotNull(message = "折扣百分比不能为空", groups = {Percent.class})
private Integer discountPercent; private Integer discountPercent;
@Schema(description = "优惠金额", example = "10") @Schema(description = "优惠金额", example = "10")
@Min(value = 0, message = "优惠金额需要大于等于 0") @Min(value = 0, message = "优惠金额需要大于等于 0")
@NotNull(message = "优惠金额不能为空", groups = {Price.class})
private Integer discountPrice; private Integer discountPrice;
@Schema(description = "折扣上限", example = "100") // 单位仅在 discountType PERCENT 使用 @Schema(description = "折扣上限", example = "100") // 单位仅在 discountType PERCENT 使用
@NotNull(message = "折扣上限不能为空", groups = {Percent.class})
private Integer discountLimitPrice; private Integer discountLimitPrice;
@AssertTrue(message = "商品范围编号的数组不能为空") @AssertTrue(message = "商品范围编号的数组不能为空")
@ -154,4 +160,54 @@ public class CouponTemplateBaseVO {
|| discountLimitPrice != null; || 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);
}
}
} }

View File

@ -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.common.PromotionProductScopeEnum;
import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import jakarta.validation.Validator;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -40,9 +41,13 @@ public class CouponTemplateServiceImpl implements CouponTemplateService {
private ProductCategoryApi productCategoryApi; private ProductCategoryApi productCategoryApi;
@Resource @Resource
private ProductSpuApi productSpuApi; private ProductSpuApi productSpuApi;
@Resource
private Validator validator;
@Override @Override
public Long createCouponTemplate(CouponTemplateCreateReqVO createReqVO) { public Long createCouponTemplate(CouponTemplateCreateReqVO createReqVO) {
// 校验参数
createReqVO.validate(validator);
// 校验商品范围 // 校验商品范围
validateProductScope(createReqVO.getProductScope(), createReqVO.getProductScopeValues()); validateProductScope(createReqVO.getProductScope(), createReqVO.getProductScopeValues());
// 插入 // 插入