diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/common/PromotionProductScopeEnum.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/common/PromotionProductScopeEnum.java index 0a7a4994d..6ae9f9f46 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/common/PromotionProductScopeEnum.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/common/PromotionProductScopeEnum.java @@ -15,8 +15,9 @@ import java.util.Arrays; @AllArgsConstructor public enum PromotionProductScopeEnum implements IntArrayValuable { - ALL(1, "全部商品参与"), - SPU(2, "指定商品参与"), + ALL(1, "通用卷"), // 全部商品 + SPU(2, "商品卷"), // 指定商品 + CATEGORY(3, "品类卷"), // 指定商品 ; public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(PromotionProductScopeEnum::getScope).toArray(); diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/coupon/CouponTakeTypeEnum.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/coupon/CouponTakeTypeEnum.java index 50263c6ce..1513e62ea 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/coupon/CouponTakeTypeEnum.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/coupon/CouponTakeTypeEnum.java @@ -15,9 +15,9 @@ import java.util.Arrays; @Getter public enum CouponTakeTypeEnum implements IntArrayValuable { - BY_USER(1, "直接领取"), // 用户可在首页、每日领劵直接领取 - BY_ADMIN(2, "指定发放"), // 后台指定会员赠送优惠劵 - BY_REGISTER(3, "新人券"), // 注册时自动领取 + USER(1, "直接领取"), // 用户可在首页、每日领劵直接领取 + ADMIN(2, "指定发放"), // 后台指定会员赠送优惠劵 + REGISTER(3, "新人券"), // 注册时自动领取 ; public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(CouponTakeTypeEnum::getValue).toArray(); diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplatePageReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplatePageReqVO.java index a95265100..abc7134e1 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplatePageReqVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/template/CouponTemplatePageReqVO.java @@ -1,6 +1,8 @@ package cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.template; import cn.iocoder.yudao.framework.common.pojo.PageParam; +import cn.iocoder.yudao.framework.common.validation.InEnum; +import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.EqualsAndHashCode; @@ -31,6 +33,8 @@ public class CouponTemplatePageReqVO extends PageParam { @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) private LocalDateTime[] createTime; - @Schema(description = "可以领取的类型", example = "[1,2]") + @Schema(description = "可以领取的类型", example = "[1,2, 3]") + @InEnum(value = CouponTakeTypeEnum.class, message = "可以领取的类型,必须是 {value}") private List canTakeTypes; + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/coupon/CouponTemplateMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/coupon/CouponTemplateMapper.java index 25833ff1b..c95513e18 100755 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/coupon/CouponTemplateMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/coupon/CouponTemplateMapper.java @@ -23,9 +23,9 @@ import java.util.function.Consumer; public interface CouponTemplateMapper extends BaseMapperX { default PageResult selectPage(CouponTemplatePageReqVO reqVO) { + // 构建可领取的查询条件, 好啰嗦 ( ╯-_-)╯┴—┴ Consumer> canTakeConsumer = null; if (CollUtil.isNotEmpty(reqVO.getCanTakeTypes())) { - // 构建可领取的查询条件, 好啰嗦 ( ╯-_-)╯┴—┴ canTakeConsumer = w -> w.eq(CouponTemplateDO::getStatus, CommonStatusEnum.ENABLE.getStatus()) // 1. 状态为可用的 .in(CouponTemplateDO::getTakeType, reqVO.getCanTakeTypes()) // 2. 领取方式一致 @@ -33,7 +33,7 @@ public interface CouponTemplateMapper extends BaseMapperX { .or().gt(CouponTemplateDO::getValidEndTime, LocalDateTime.now())) .apply(" take_count < total_count "); // 4. 剩余数量大于 0 } - + // 执行分页查询 return selectPage(reqVO, new LambdaQueryWrapperX() .likeIfPresent(CouponTemplateDO::getName, reqVO.getName()) .eqIfPresent(CouponTemplateDO::getStatus, reqVO.getStatus()) diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponService.java index 11e3fa826..17f922ae8 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponService.java @@ -94,7 +94,7 @@ public interface CouponService { * @param userIds 用户编号列表 */ default void takeCouponByAdmin(Long templateId, Set userIds) { - takeCoupon(templateId, userIds, CouponTakeTypeEnum.BY_ADMIN); + takeCoupon(templateId, userIds, CouponTakeTypeEnum.ADMIN); } /** @@ -104,7 +104,7 @@ public interface CouponService { * @param userId 用户编号 */ default void takeCouponByUser(Long templateId, Long userId) { - takeCoupon(templateId, CollUtil.newHashSet(userId), CouponTakeTypeEnum.BY_USER); + takeCoupon(templateId, CollUtil.newHashSet(userId), CouponTakeTypeEnum.USER); } /** @@ -114,6 +114,6 @@ public interface CouponService { * @param userId 用户编号列表 */ default void takeCouponByRegister(Long templateId, Long userId) { - takeCoupon(templateId, CollUtil.newHashSet(userId), CouponTakeTypeEnum.BY_REGISTER); + takeCoupon(templateId, CollUtil.newHashSet(userId), CouponTakeTypeEnum.REGISTER); } } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponServiceImpl.java index b0ffc1749..66aa9e7d8 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponServiceImpl.java @@ -138,8 +138,8 @@ public class CouponServiceImpl implements CouponService { public void takeCoupon(Long templateId, Set userIds, CouponTakeTypeEnum takeType) { CouponTemplateDO template = couponTemplateService.getCouponTemplate(templateId); // 1. 过滤掉达到领取限制的用户 - removeTakeLimitUserId(template, userIds); - // 2. 校验用户 + removeTakeLimitUserId(userIds, template); + // 2. 校验优惠劵是否可以领取 validateCanTake(template, userIds, takeType); // 3. 批量保存优惠劵 @@ -158,7 +158,7 @@ public class CouponServiceImpl implements CouponService { */ private void validateCanTake(CouponTemplateDO couponTemplate, Set userIds, CouponTakeTypeEnum takeType) { // 如果所有用户都领取过,则抛出异常 - if (userIds.isEmpty()) { + if (CollUtil.isEmpty(userIds)) { throw exception(COUPON_TEMPLATE_USER_ALREADY_TAKE); } @@ -176,22 +176,30 @@ public class CouponServiceImpl implements CouponService { throw exception(COUPON_TEMPLATE_EXPIRED); } } - // 校验领取方式 if (ObjectUtil.notEqual(couponTemplate.getTakeType(), takeType.getValue())) { throw exception(COUPON_TEMPLATE_CANNOT_TAKE); } } - private void removeTakeLimitUserId(CouponTemplateDO couponTemplate, Set userIds) { + /** + * 过滤掉达到领取上线的用户 + * + * @param userIds 用户编号数组 + * @param couponTemplate 优惠劵模版 + */ + private void removeTakeLimitUserId(Set userIds, CouponTemplateDO couponTemplate) { + if (couponTemplate.getTakeLimitCount() <= 0) { + return; + } // 查询已领过券的用户 List alreadyTakeCoupons = couponMapper.selectListByTemplateIdAndUserId(couponTemplate.getId(), userIds); - - // 移除达到领取限制的用户 - if (couponTemplate.getTakeLimitCount() > 0) { - Map userTakeCountMap = CollStreamUtil.groupBy(alreadyTakeCoupons, CouponDO::getUserId, Collectors.summingInt(c -> 1)); - userIds.removeIf(userId -> MapUtil.getInt(userTakeCountMap, userId, 0) >= couponTemplate.getTakeLimitCount()); + if (CollUtil.isEmpty(alreadyTakeCoupons)) { + return; } + // 移除达到领取限制的用户 + Map userTakeCountMap = CollStreamUtil.groupBy(alreadyTakeCoupons, CouponDO::getUserId, Collectors.summingInt(c -> 1)); + userIds.removeIf(userId -> MapUtil.getInt(userTakeCountMap, userId, 0) >= couponTemplate.getTakeLimitCount()); } }