Commit e67b62f2f6a2668e2c6326e1ebd84c6b51aa32cb
Committed by
Gitee
!604 优惠券优化
Merge pull request !604 from 疯狂的世界/coupon
Showing
11 changed files
with
65 additions
and
20 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/validation/InEnum.java
| @@ -17,7 +17,7 @@ import java.lang.annotation.*; | @@ -17,7 +17,7 @@ import java.lang.annotation.*; | ||
| 17 | @Retention(RetentionPolicy.RUNTIME) | 17 | @Retention(RetentionPolicy.RUNTIME) |
| 18 | @Documented | 18 | @Documented |
| 19 | @Constraint( | 19 | @Constraint( |
| 20 | - validatedBy = InEnumValidator.class | 20 | + validatedBy = {InEnumValidator.class, InEnumCollectionValidator.class} |
| 21 | ) | 21 | ) |
| 22 | public @interface InEnum { | 22 | public @interface InEnum { |
| 23 | 23 |
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/validation/InEnumCollectionValidator.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.common.validation; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | ||
| 5 | + | ||
| 6 | +import javax.validation.ConstraintValidator; | ||
| 7 | +import javax.validation.ConstraintValidatorContext; | ||
| 8 | +import java.util.Arrays; | ||
| 9 | +import java.util.Collection; | ||
| 10 | +import java.util.Collections; | ||
| 11 | +import java.util.List; | ||
| 12 | +import java.util.stream.Collectors; | ||
| 13 | + | ||
| 14 | +public class InEnumCollectionValidator implements ConstraintValidator<InEnum, Collection<Integer>> { | ||
| 15 | + | ||
| 16 | + private List<Integer> values; | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + public void initialize(InEnum annotation) { | ||
| 20 | + IntArrayValuable[] values = annotation.value().getEnumConstants(); | ||
| 21 | + if (values.length == 0) { | ||
| 22 | + this.values = Collections.emptyList(); | ||
| 23 | + } else { | ||
| 24 | + this.values = Arrays.stream(values[0].array()).boxed().collect(Collectors.toList()); | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + @Override | ||
| 29 | + public boolean isValid(Collection<Integer> list, ConstraintValidatorContext context) { | ||
| 30 | + // 校验通过 | ||
| 31 | + if (CollUtil.containsAll(values, list)) { | ||
| 32 | + return true; | ||
| 33 | + } | ||
| 34 | + // 校验不通过,自定义提示语句(因为,注解上的 value 是枚举类,无法获得枚举类的实际值) | ||
| 35 | + context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值 | ||
| 36 | + context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate() | ||
| 37 | + .replaceAll("\\{value}", CollUtil.join(list, ","))).addConstraintViolation(); // 重新添加错误提示语句 | ||
| 38 | + return false; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | +} | ||
| 42 | + |
yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/dto/CouponRespDTO.java
| @@ -71,9 +71,9 @@ public class CouponRespDTO { | @@ -71,9 +71,9 @@ public class CouponRespDTO { | ||
| 71 | */ | 71 | */ |
| 72 | private Integer productScope; | 72 | private Integer productScope; |
| 73 | /** | 73 | /** |
| 74 | - * 商品 SPU 编号的数组 | 74 | + * 商品范围编号的数组 |
| 75 | */ | 75 | */ |
| 76 | - private List<Long> productSpuIds; | 76 | + private List<Long> productScopeValues; |
| 77 | // ========== 使用规则 END ========== | 77 | // ========== 使用规则 END ========== |
| 78 | 78 | ||
| 79 | // ========== 使用效果 BEGIN ========== | 79 | // ========== 使用效果 BEGIN ========== |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/coupon/CouponBaseVO.java
| @@ -67,8 +67,8 @@ public class CouponBaseVO { | @@ -67,8 +67,8 @@ public class CouponBaseVO { | ||
| 67 | @InEnum(PromotionProductScopeEnum.class) | 67 | @InEnum(PromotionProductScopeEnum.class) |
| 68 | private Integer productScope; | 68 | private Integer productScope; |
| 69 | 69 | ||
| 70 | - @Schema(description = "商品 SPU 编号的数组", example = "1,3") | ||
| 71 | - private List<Long> productSpuIds; | 70 | + @Schema(description = "商品范围编号的数组", example = "1,3") |
| 71 | + private List<Long> productScopeValues; | ||
| 72 | // ========== 使用规则 END ========== | 72 | // ========== 使用规则 END ========== |
| 73 | 73 | ||
| 74 | // ========== 使用效果 BEGIN ========== | 74 | // ========== 使用效果 BEGIN ========== |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplateBaseVO.java
| @@ -54,8 +54,8 @@ public class CouponTemplateBaseVO { | @@ -54,8 +54,8 @@ public class CouponTemplateBaseVO { | ||
| 54 | @InEnum(PromotionProductScopeEnum.class) | 54 | @InEnum(PromotionProductScopeEnum.class) |
| 55 | private Integer productScope; | 55 | private Integer productScope; |
| 56 | 56 | ||
| 57 | - @Schema(description = "商品 SPU 编号的数组", example = "1,3") | ||
| 58 | - private List<Long> productSpuIds; | 57 | + @Schema(description = "商品范围编号的数组", example = "1,3") |
| 58 | + private List<Long> productScopeValues; | ||
| 59 | 59 | ||
| 60 | @Schema(description = "生效日期类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | 60 | @Schema(description = "生效日期类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
| 61 | @NotNull(message = "生效日期类型不能为空") | 61 | @NotNull(message = "生效日期类型不能为空") |
| @@ -95,11 +95,11 @@ public class CouponTemplateBaseVO { | @@ -95,11 +95,11 @@ public class CouponTemplateBaseVO { | ||
| 95 | @Schema(description = "折扣上限", example = "100") // 单位:分,仅在 discountType 为 PERCENT 使用 | 95 | @Schema(description = "折扣上限", example = "100") // 单位:分,仅在 discountType 为 PERCENT 使用 |
| 96 | private Integer discountLimitPrice; | 96 | private Integer discountLimitPrice; |
| 97 | 97 | ||
| 98 | - @AssertTrue(message = "商品 SPU 编号的数组不能为空") | 98 | + @AssertTrue(message = "商品范围编号的数组不能为空") |
| 99 | @JsonIgnore | 99 | @JsonIgnore |
| 100 | - public boolean isProductSpuIdsValid() { | 100 | + public boolean isProductScopeValuesValid() { |
| 101 | return Objects.equals(productScope, PromotionProductScopeEnum.ALL.getScope()) // 全部范围时,可以为空 | 101 | return Objects.equals(productScope, PromotionProductScopeEnum.ALL.getScope()) // 全部范围时,可以为空 |
| 102 | - || CollUtil.isNotEmpty(productSpuIds); | 102 | + || CollUtil.isNotEmpty(productScopeValues); |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | @AssertTrue(message = "生效开始时间不能为空") | 105 | @AssertTrue(message = "生效开始时间不能为空") |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/coupon/vo/template/AppCouponTemplateRespVO.java
| @@ -28,8 +28,8 @@ public class AppCouponTemplateRespVO { | @@ -28,8 +28,8 @@ public class AppCouponTemplateRespVO { | ||
| 28 | // @InEnum(PromotionProductScopeEnum.class) | 28 | // @InEnum(PromotionProductScopeEnum.class) |
| 29 | // private Integer productScope; | 29 | // private Integer productScope; |
| 30 | // | 30 | // |
| 31 | -// @Schema(description = "商品 SPU 编号的数组", example = "1,3") | ||
| 32 | -// private List<Long> productSpuIds; | 31 | +// @Schema(description = "商品范围编号的数组", example = "1,3") |
| 32 | +// private List<Long> productScopeValues; | ||
| 33 | 33 | ||
| 34 | @Schema(description = "生效日期类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | 34 | @Schema(description = "生效日期类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
| 35 | private Integer validityType; | 35 | private Integer validityType; |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/coupon/CouponConvert.java
| @@ -33,7 +33,7 @@ public interface CouponConvert { | @@ -33,7 +33,7 @@ public interface CouponConvert { | ||
| 33 | .setTakeType(template.getTakeType()) | 33 | .setTakeType(template.getTakeType()) |
| 34 | .setUsePrice(template.getUsePrice()) | 34 | .setUsePrice(template.getUsePrice()) |
| 35 | .setProductScope(template.getProductScope()) | 35 | .setProductScope(template.getProductScope()) |
| 36 | - .setProductSpuIds(template.getProductSpuIds()) | 36 | + .setProductScopeValues(template.getProductScopeValues()) |
| 37 | .setDiscountType(template.getDiscountType()) | 37 | .setDiscountType(template.getDiscountType()) |
| 38 | .setDiscountPercent(template.getDiscountPercent()) | 38 | .setDiscountPercent(template.getDiscountPercent()) |
| 39 | .setDiscountPrice(template.getDiscountPrice()) | 39 | .setDiscountPrice(template.getDiscountPrice()) |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/coupon/CouponDO.java
| @@ -89,12 +89,12 @@ public class CouponDO extends BaseDO { | @@ -89,12 +89,12 @@ public class CouponDO extends BaseDO { | ||
| 89 | */ | 89 | */ |
| 90 | private Integer productScope; | 90 | private Integer productScope; |
| 91 | /** | 91 | /** |
| 92 | - * 商品 SPU 编号的数组 | 92 | + * 商品范围编号的数组 |
| 93 | * | 93 | * |
| 94 | - * 冗余 {@link CouponTemplateDO#getProductSpuIds()} | 94 | + * 冗余 {@link CouponTemplateDO#getProductScopeValues()} |
| 95 | */ | 95 | */ |
| 96 | @TableField(typeHandler = LongListTypeHandler.class) | 96 | @TableField(typeHandler = LongListTypeHandler.class) |
| 97 | - private List<Long> productSpuIds; | 97 | + private List<Long> productScopeValues; |
| 98 | // ========== 使用规则 END ========== | 98 | // ========== 使用规则 END ========== |
| 99 | 99 | ||
| 100 | // ========== 使用效果 BEGIN ========== | 100 | // ========== 使用效果 BEGIN ========== |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/coupon/CouponTemplateDO.java
| @@ -85,10 +85,10 @@ public class CouponTemplateDO extends BaseDO { | @@ -85,10 +85,10 @@ public class CouponTemplateDO extends BaseDO { | ||
| 85 | */ | 85 | */ |
| 86 | private Integer productScope; | 86 | private Integer productScope; |
| 87 | /** | 87 | /** |
| 88 | - * 商品 SPU 编号的数组 | 88 | + * 商品范围编号的数组 |
| 89 | */ | 89 | */ |
| 90 | @TableField(typeHandler = LongListTypeHandler.class) | 90 | @TableField(typeHandler = LongListTypeHandler.class) |
| 91 | - private List<Long> productSpuIds; | 91 | + private List<Long> productScopeValues; |
| 92 | /** | 92 | /** |
| 93 | * 生效日期类型 | 93 | * 生效日期类型 |
| 94 | * | 94 | * |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeCouponPriceCalculator.java
| @@ -102,7 +102,10 @@ public class TradeCouponPriceCalculator implements TradePriceCalculator { | @@ -102,7 +102,10 @@ public class TradeCouponPriceCalculator implements TradePriceCalculator { | ||
| 102 | Predicate<TradePriceCalculateRespBO.OrderItem> matchPredicate = TradePriceCalculateRespBO.OrderItem::getSelected; | 102 | Predicate<TradePriceCalculateRespBO.OrderItem> matchPredicate = TradePriceCalculateRespBO.OrderItem::getSelected; |
| 103 | if (PromotionProductScopeEnum.SPU.getScope().equals(coupon.getProductScope())) { | 103 | if (PromotionProductScopeEnum.SPU.getScope().equals(coupon.getProductScope())) { |
| 104 | matchPredicate = matchPredicate // 额外加如下条件 | 104 | matchPredicate = matchPredicate // 额外加如下条件 |
| 105 | - .and(orderItem -> coupon.getProductSpuIds().contains(orderItem.getSpuId())); | 105 | + .and(orderItem -> coupon.getProductScopeValues().contains(orderItem.getSpuId())); |
| 106 | + } else if (PromotionProductScopeEnum.CATEGORY.getScope().equals(coupon.getProductScope())) { | ||
| 107 | + matchPredicate = matchPredicate // 额外加如下条件 | ||
| 108 | + .and(orderItem -> coupon.getProductScopeValues().contains(orderItem.getCategoryId())); | ||
| 106 | } | 109 | } |
| 107 | return filterList(result.getItems(), matchPredicate); | 110 | return filterList(result.getItems(), matchPredicate); |
| 108 | } | 111 | } |
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeCouponPriceCalculatorTest.java
| @@ -65,7 +65,7 @@ public class TradeCouponPriceCalculatorTest extends BaseMockitoUnitTest { | @@ -65,7 +65,7 @@ public class TradeCouponPriceCalculatorTest extends BaseMockitoUnitTest { | ||
| 65 | 65 | ||
| 66 | // mock 方法(优惠劵 Coupon 信息) | 66 | // mock 方法(优惠劵 Coupon 信息) |
| 67 | CouponRespDTO coupon = randomPojo(CouponRespDTO.class, o -> o.setId(1024L).setName("程序员节") | 67 | CouponRespDTO coupon = randomPojo(CouponRespDTO.class, o -> o.setId(1024L).setName("程序员节") |
| 68 | - .setProductScope(PromotionProductScopeEnum.SPU.getScope()).setProductSpuIds(asList(1L, 2L)) | 68 | + .setProductScope(PromotionProductScopeEnum.SPU.getScope()).setProductScopeValues(asList(1L, 2L)) |
| 69 | .setUsePrice(350).setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()) | 69 | .setUsePrice(350).setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()) |
| 70 | .setDiscountPercent(50).setDiscountLimitPrice(70)); | 70 | .setDiscountPercent(50).setDiscountLimitPrice(70)); |
| 71 | when(couponApi.validateCoupon(eq(new CouponValidReqDTO().setId(1024L).setUserId(233L)))).thenReturn(coupon); | 71 | when(couponApi.validateCoupon(eq(new CouponValidReqDTO().setId(1024L).setUserId(233L)))).thenReturn(coupon); |