Commit b1ac3a0b8b6b832cd4dadb2c0be88c5c869dd4ff
1 parent
f9aec7d8
优惠券:完善优惠券匹配接口
Showing
6 changed files
with
46 additions
and
30 deletions
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/coupon/AppCouponController.java
| ... | ... | @@ -17,11 +17,8 @@ import org.springframework.validation.annotation.Validated; |
| 17 | 17 | import org.springframework.web.bind.annotation.*; |
| 18 | 18 | |
| 19 | 19 | import javax.annotation.Resource; |
| 20 | -import java.time.LocalDateTime; | |
| 21 | -import java.util.ArrayList; | |
| 22 | 20 | import java.util.Collections; |
| 23 | 21 | import java.util.List; |
| 24 | -import java.util.Random; | |
| 25 | 22 | |
| 26 | 23 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 27 | 24 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| ... | ... | @@ -42,36 +39,11 @@ public class AppCouponController { |
| 42 | 39 | return success(1L); |
| 43 | 40 | } |
| 44 | 41 | |
| 45 | - // TODO 芋艿:待实现 | |
| 46 | 42 | @GetMapping("/match-list") |
| 47 | 43 | @Operation(summary = "获得匹配指定商品的优惠劵列表") |
| 48 | 44 | public CommonResult<List<AppCouponMatchRespVO>> getMatchCouponList(AppCouponMatchReqVO matchReqVO) { |
| 49 | - List<AppCouponMatchRespVO> list = new ArrayList<>(); | |
| 50 | - Random random = new Random(); | |
| 51 | - for (int i = 0; i < 10; i++) { | |
| 52 | - AppCouponMatchRespVO vo = new AppCouponMatchRespVO(); | |
| 53 | - vo.setId(i + 1L); | |
| 54 | - vo.setName("优惠劵" + (i + 1)); | |
| 55 | - vo.setUsePrice(random.nextInt(100) * 100); | |
| 56 | - vo.setValidStartTime(LocalDateTime.now().plusDays(random.nextInt(10))); | |
| 57 | - vo.setValidEndTime(LocalDateTime.now().plusDays(random.nextInt(20) + 10)); | |
| 58 | - vo.setDiscountType(random.nextInt(2) + 1); | |
| 59 | - if (vo.getDiscountType() == 1) { | |
| 60 | - vo.setDiscountPercent(null); | |
| 61 | - vo.setDiscountPrice(random.nextInt(50) * 100); | |
| 62 | - vo.setDiscountLimitPrice(null); | |
| 63 | - } else { | |
| 64 | - vo.setDiscountPercent(random.nextInt(90) + 10); | |
| 65 | - vo.setDiscountPrice(null); | |
| 66 | - vo.setDiscountLimitPrice(random.nextInt(200) * 100); | |
| 67 | - } | |
| 68 | - vo.setMatch(random.nextBoolean()); | |
| 69 | - if (!vo.getMatch()) { | |
| 70 | - vo.setDescription("不符合条件噢"); | |
| 71 | - } | |
| 72 | - list.add(vo); | |
| 73 | - } | |
| 74 | - return success(list); | |
| 45 | + // todo: 优惠金额倒序 | |
| 46 | + return success(CouponConvert.INSTANCE.convertList(couponService.getMatchCouponList(getLoginUserId(), matchReqVO))); | |
| 75 | 47 | } |
| 76 | 48 | |
| 77 | 49 | @GetMapping("/page") | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/coupon/CouponConvert.java
| ... | ... | @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | 4 | import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponRespDTO; |
| 5 | 5 | import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.coupon.CouponPageItemRespVO; |
| 6 | 6 | import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.coupon.CouponPageReqVO; |
| 7 | +import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponMatchRespVO; | |
| 7 | 8 | import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponPageReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponRespVO; |
| 9 | 10 | import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO; |
| ... | ... | @@ -15,6 +16,7 @@ import org.mapstruct.factory.Mappers; |
| 15 | 16 | |
| 16 | 17 | import java.time.LocalDateTime; |
| 17 | 18 | import java.util.Collection; |
| 19 | +import java.util.List; | |
| 18 | 20 | |
| 19 | 21 | /** |
| 20 | 22 | * 优惠劵 Convert |
| ... | ... | @@ -57,4 +59,6 @@ public interface CouponConvert { |
| 57 | 59 | CouponPageReqVO convert(AppCouponPageReqVO pageReqVO, Collection<Long> userIds); |
| 58 | 60 | |
| 59 | 61 | PageResult<AppCouponRespVO> convertAppPage(PageResult<CouponDO> pageResult); |
| 62 | + | |
| 63 | + List<AppCouponMatchRespVO> convertList(List<CouponDO> list); | |
| 60 | 64 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/coupon/CouponMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.dal.mysql.coupon; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | +import cn.hutool.core.util.StrUtil; | |
| 4 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 6 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 6 | 7 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 7 | 8 | import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.coupon.CouponPageReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO; |
| 10 | +import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum; | |
| 9 | 11 | import cn.iocoder.yudao.module.promotion.service.coupon.bo.CouponTakeCountBO; |
| 10 | 12 | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| 11 | 13 | import com.github.yulichang.toolkit.MPJWrappers; |
| ... | ... | @@ -13,6 +15,8 @@ import org.apache.ibatis.annotations.Mapper; |
| 13 | 15 | |
| 14 | 16 | import java.util.Collection; |
| 15 | 17 | import java.util.List; |
| 18 | +import java.util.function.Function; | |
| 19 | +import java.util.stream.Collectors; | |
| 16 | 20 | |
| 17 | 21 | /** |
| 18 | 22 | * 优惠劵 Mapper |
| ... | ... | @@ -73,4 +77,21 @@ public interface CouponMapper extends BaseMapperX<CouponDO> { |
| 73 | 77 | .in(CouponDO::getTemplateId, templateIds) |
| 74 | 78 | .groupBy(CouponDO::getTemplateId)), CouponTakeCountBO.class); |
| 75 | 79 | } |
| 80 | + | |
| 81 | + default List<CouponDO> selectListByUserIdAndStatusAndUsePriceLeAndProductScope( | |
| 82 | + Long userId, Integer status, Integer usePrice, List<Long> spuIds, List<Long> categoryIds) { | |
| 83 | + | |
| 84 | + Function<List<Long>, String> productScopeValuesFindInSetFunc = ids -> ids.stream() | |
| 85 | + .map(id -> StrUtil.format("FIND_IN_SET({}, product_scope_values) ", id)) | |
| 86 | + .collect(Collectors.joining(" OR ")); | |
| 87 | + return selectList(new LambdaQueryWrapperX<CouponDO>() | |
| 88 | + .eq(CouponDO::getUserId, userId) | |
| 89 | + .eq(CouponDO::getStatus, status) | |
| 90 | + .le(CouponDO::getUsePrice, usePrice) | |
| 91 | + .and(w -> w.eq(CouponDO::getProductScope, PromotionProductScopeEnum.ALL.getScope()) | |
| 92 | + .or(ww -> ww.eq(CouponDO::getProductScope, PromotionProductScopeEnum.SPU.getScope()) | |
| 93 | + .apply(productScopeValuesFindInSetFunc.apply(spuIds))) | |
| 94 | + .or(ww -> ww.eq(CouponDO::getProductScope, PromotionProductScopeEnum.CATEGORY.getScope()) | |
| 95 | + .apply(productScopeValuesFindInSetFunc.apply(categoryIds))))); | |
| 96 | + } | |
| 76 | 97 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponService.java
| ... | ... | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.promotion.service.coupon; |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.coupon.CouponPageReqVO; |
| 6 | +import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponMatchReqVO; | |
| 6 | 7 | import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO; |
| 7 | 8 | import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; |
| 8 | 9 | import cn.iocoder.yudao.module.promotion.service.coupon.bo.CouponTakeCountBO; |
| ... | ... | @@ -147,4 +148,13 @@ public interface CouponService { |
| 147 | 148 | * @return 领取优惠券的数量 |
| 148 | 149 | */ |
| 149 | 150 | List<CouponTakeCountBO> getTakeCountListByTemplateIds(Collection<Long> templateIds, Long userId); |
| 151 | + | |
| 152 | + /** | |
| 153 | + * 获取用户匹配的优惠券列表 | |
| 154 | + * | |
| 155 | + * @param userId 用户编号 | |
| 156 | + * @param matchReqVO 匹配参数 | |
| 157 | + * @return 优惠券列表 | |
| 158 | + */ | |
| 159 | + List<CouponDO> getMatchCouponList(Long userId, AppCouponMatchReqVO matchReqVO); | |
| 150 | 160 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponServiceImpl.java
| ... | ... | @@ -12,6 +12,7 @@ import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; |
| 12 | 12 | import cn.iocoder.yudao.module.member.api.user.MemberUserApi; |
| 13 | 13 | import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; |
| 14 | 14 | import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.coupon.CouponPageReqVO; |
| 15 | +import cn.iocoder.yudao.module.promotion.controller.app.coupon.vo.coupon.AppCouponMatchReqVO; | |
| 15 | 16 | import cn.iocoder.yudao.module.promotion.convert.coupon.CouponConvert; |
| 16 | 17 | import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO; |
| 17 | 18 | import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO; |
| ... | ... | @@ -186,6 +187,12 @@ public class CouponServiceImpl implements CouponService { |
| 186 | 187 | return couponMapper.selectCountByUserIdAndTemplateIdIn(userId, templateIds); |
| 187 | 188 | } |
| 188 | 189 | |
| 190 | + @Override | |
| 191 | + public List<CouponDO> getMatchCouponList(Long userId, AppCouponMatchReqVO matchReqVO) { | |
| 192 | + return couponMapper.selectListByUserIdAndStatusAndUsePriceLeAndProductScope(userId, CouponStatusEnum.UNUSED.getStatus(), | |
| 193 | + matchReqVO.getPrice(), matchReqVO.getSpuIds(), matchReqVO.getCategoryIds()); | |
| 194 | + } | |
| 195 | + | |
| 189 | 196 | /** |
| 190 | 197 | * 校验优惠券是否可以领取 |
| 191 | 198 | * | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeOrderSettlementRespVO.java
| ... | ... | @@ -37,6 +37,8 @@ public class AppTradeOrderSettlementRespVO { |
| 37 | 37 | |
| 38 | 38 | // ========== SPU 信息 ========== |
| 39 | 39 | |
| 40 | + @Schema(description = "品类编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048") | |
| 41 | + private Long categoryId; | |
| 40 | 42 | @Schema(description = "SPU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048") |
| 41 | 43 | private Long spuId; |
| 42 | 44 | @Schema(description = "SPU 名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "Apple iPhone 12") | ... | ... |