Commit f3dd628aba5bd909c6f570fdfcb54951819d31ac
Committed by
Gitee
!1070 【代码优化】商城: 满减送活动
Merge pull request !1070 from puhui999/develop
Showing
13 changed files
with
36 additions
and
45 deletions
yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/reward/dto/RewardActivityMatchRespDTO.java
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/reward/RewardActivityController.java
| ... | ... | @@ -2,23 +2,22 @@ package cn.iocoder.yudao.module.promotion.controller.admin.reward; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 5 | 6 | import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityCreateReqVO; |
| 6 | 7 | import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityRespVO; |
| 8 | 9 | import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityUpdateReqVO; |
| 9 | -import cn.iocoder.yudao.module.promotion.convert.reward.RewardActivityConvert; | |
| 10 | 10 | import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO; |
| 11 | 11 | import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityService; |
| 12 | 12 | import io.swagger.v3.oas.annotations.Operation; |
| 13 | 13 | import io.swagger.v3.oas.annotations.Parameter; |
| 14 | 14 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 15 | +import jakarta.annotation.Resource; | |
| 16 | +import jakarta.validation.Valid; | |
| 15 | 17 | import org.springframework.security.access.prepost.PreAuthorize; |
| 16 | 18 | import org.springframework.validation.annotation.Validated; |
| 17 | 19 | import org.springframework.web.bind.annotation.*; |
| 18 | 20 | |
| 19 | -import jakarta.annotation.Resource; | |
| 20 | -import jakarta.validation.Valid; | |
| 21 | - | |
| 22 | 21 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 23 | 22 | |
| 24 | 23 | @Tag(name = "管理后台 - 满减送活动") |
| ... | ... | @@ -69,7 +68,7 @@ public class RewardActivityController { |
| 69 | 68 | @PreAuthorize("@ss.hasPermission('promotion:reward-activity:query')") |
| 70 | 69 | public CommonResult<RewardActivityRespVO> getRewardActivity(@RequestParam("id") Long id) { |
| 71 | 70 | RewardActivityDO rewardActivity = rewardActivityService.getRewardActivity(id); |
| 72 | - return success(RewardActivityConvert.INSTANCE.convert(rewardActivity)); | |
| 71 | + return success(BeanUtils.toBean(rewardActivity, RewardActivityRespVO.class)); | |
| 73 | 72 | } |
| 74 | 73 | |
| 75 | 74 | @GetMapping("/page") |
| ... | ... | @@ -77,7 +76,7 @@ public class RewardActivityController { |
| 77 | 76 | @PreAuthorize("@ss.hasPermission('promotion:reward-activity:query')") |
| 78 | 77 | public CommonResult<PageResult<RewardActivityRespVO>> getRewardActivityPage(@Valid RewardActivityPageReqVO pageVO) { |
| 79 | 78 | PageResult<RewardActivityDO> pageResult = rewardActivityService.getRewardActivityPage(pageVO); |
| 80 | - return success(RewardActivityConvert.INSTANCE.convertPage(pageResult)); | |
| 79 | + return success(BeanUtils.toBean(pageResult, RewardActivityRespVO.class)); | |
| 81 | 80 | } |
| 82 | 81 | |
| 83 | 82 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/reward/vo/RewardActivityBaseVO.java
| ... | ... | @@ -79,13 +79,13 @@ public class RewardActivityBaseVO { |
| 79 | 79 | @Schema(description = "赠送的积分", requiredMode = Schema.RequiredMode.REQUIRED, example = "100") |
| 80 | 80 | private Integer point; |
| 81 | 81 | |
| 82 | - @Schema(description = "赠送的优惠劵编号的数组", example = "1,2,3") | |
| 83 | - private Map<Long, Integer> giveCouponsMap; | |
| 82 | + @Schema(description = "赠送的优惠劵编号的数组") | |
| 83 | + private Map<Long, Integer> giveCouponTemplateCounts; | |
| 84 | 84 | |
| 85 | - @AssertTrue(message = "赠送的积分不能小于 1") | |
| 85 | + @AssertTrue(message = "赠送的积分不能小于 0") | |
| 86 | 86 | @JsonIgnore |
| 87 | 87 | public boolean isPointValid() { |
| 88 | - return point == null || point >= 1; | |
| 88 | + return point == null || point >= 0; | |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/reward/RewardActivityConvert.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.convert.reward; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 4 | -import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityCreateReqVO; | |
| 5 | -import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityRespVO; | |
| 6 | -import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityUpdateReqVO; | |
| 7 | -import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO; | |
| 8 | 3 | import org.mapstruct.Mapper; |
| 9 | 4 | import org.mapstruct.factory.Mappers; |
| 10 | 5 | |
| ... | ... | @@ -18,12 +13,4 @@ public interface RewardActivityConvert { |
| 18 | 13 | |
| 19 | 14 | RewardActivityConvert INSTANCE = Mappers.getMapper(RewardActivityConvert.class); |
| 20 | 15 | |
| 21 | - RewardActivityDO convert(RewardActivityCreateReqVO bean); | |
| 22 | - | |
| 23 | - RewardActivityDO convert(RewardActivityUpdateReqVO bean); | |
| 24 | - | |
| 25 | - RewardActivityRespVO convert(RewardActivityDO bean); | |
| 26 | - | |
| 27 | - PageResult<RewardActivityRespVO> convertPage(PageResult<RewardActivityDO> page); | |
| 28 | - | |
| 29 | 16 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/reward/RewardActivityDO.java
| ... | ... | @@ -107,9 +107,12 @@ public class RewardActivityDO extends BaseDO { |
| 107 | 107 | /** |
| 108 | 108 | * 赠送的优惠劵 |
| 109 | 109 | * |
| 110 | - * key: 优惠劵编号,value:对应的优惠券数量 | |
| 110 | + * key: 优惠劵模版编号 | |
| 111 | + * value:对应的优惠券数量 | |
| 112 | + * | |
| 113 | + * 目的:用于订单支付后赠送优惠券 | |
| 111 | 114 | */ |
| 112 | - private Map<Long, Integer> giveCouponsMap; | |
| 115 | + private Map<Long, Integer> giveCouponTemplateCounts; | |
| 113 | 116 | |
| 114 | 117 | } |
| 115 | 118 | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityServiceImpl.java
| ... | ... | @@ -10,7 +10,6 @@ import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivi |
| 10 | 10 | import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityCreateReqVO; |
| 11 | 11 | import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO; |
| 12 | 12 | import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityUpdateReqVO; |
| 13 | -import cn.iocoder.yudao.module.promotion.convert.reward.RewardActivityConvert; | |
| 14 | 13 | import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO; |
| 15 | 14 | import cn.iocoder.yudao.module.promotion.dal.mysql.reward.RewardActivityMapper; |
| 16 | 15 | import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum; |
| ... | ... | @@ -54,7 +53,7 @@ public class RewardActivityServiceImpl implements RewardActivityService { |
| 54 | 53 | validateRewardActivitySpuConflicts(null, createReqVO); |
| 55 | 54 | |
| 56 | 55 | // 2. 插入 |
| 57 | - RewardActivityDO rewardActivity = RewardActivityConvert.INSTANCE.convert(createReqVO) | |
| 56 | + RewardActivityDO rewardActivity = BeanUtils.toBean(createReqVO, RewardActivityDO.class) | |
| 58 | 57 | .setStatus(PromotionUtils.calculateActivityStatus(createReqVO.getEndTime())); |
| 59 | 58 | rewardActivityMapper.insert(rewardActivity); |
| 60 | 59 | // 返回 |
| ... | ... | @@ -74,7 +73,7 @@ public class RewardActivityServiceImpl implements RewardActivityService { |
| 74 | 73 | validateRewardActivitySpuConflicts(updateReqVO.getId(), updateReqVO); |
| 75 | 74 | |
| 76 | 75 | // 2. 更新 |
| 77 | - RewardActivityDO updateObj = RewardActivityConvert.INSTANCE.convert(updateReqVO) | |
| 76 | + RewardActivityDO updateObj = BeanUtils.toBean(updateReqVO, RewardActivityDO.class) | |
| 78 | 77 | .setStatus(PromotionUtils.calculateActivityStatus(updateReqVO.getEndTime())); |
| 79 | 78 | rewardActivityMapper.updateById(updateObj); |
| 80 | 79 | } | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/order/TradeOrderDO.java
| ... | ... | @@ -296,7 +296,6 @@ public class TradeOrderDO extends BaseDO { |
| 296 | 296 | */ |
| 297 | 297 | private Integer vipPrice; |
| 298 | 298 | |
| 299 | - // TODO @puhui999:我们要不要把相关的字段,定义的更明确一点?例如说,giveCouponTemplateCounts 赠送的优惠劵模版数量,或者 giveCouponCounts 赠送的优惠劵数量。感受上,Coupons 和 Map 有点点重叠哈。 | |
| 300 | 299 | /** |
| 301 | 300 | * 赠送的优惠劵 |
| 302 | 301 | * |
| ... | ... | @@ -306,7 +305,7 @@ public class TradeOrderDO extends BaseDO { |
| 306 | 305 | * 目的:用于订单支付后赠送优惠券 |
| 307 | 306 | */ |
| 308 | 307 | @TableField(typeHandler = JacksonTypeHandler.class) |
| 309 | - private Map<Long, Integer> giveCouponsMap; | |
| 308 | + private Map<Long, Integer> giveCouponTemplateCounts; | |
| 310 | 309 | /** |
| 311 | 310 | * 赠送的优惠劵编号 |
| 312 | 311 | * | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java
| ... | ... | @@ -202,7 +202,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { |
| 202 | 202 | order.setProductCount(getSumValue(calculateRespBO.getItems(), TradePriceCalculateRespBO.OrderItem::getCount, Integer::sum)); |
| 203 | 203 | order.setUserIp(getClientIP()).setTerminal(getTerminal()); |
| 204 | 204 | // 使用 + 赠送优惠券 |
| 205 | - order.setGiveCouponsMap(calculateRespBO.getGiveCoupons()); | |
| 205 | + order.setGiveCouponTemplateCounts(calculateRespBO.getGiveCouponTemplateCounts()); | |
| 206 | 206 | // 支付 + 退款信息 |
| 207 | 207 | order.setAdjustPrice(0).setPayStatus(false); |
| 208 | 208 | order.setRefundStatus(TradeOrderRefundStatusEnum.NONE.getStatus()).setRefundPrice(0); | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/handler/TradeCouponOrderHandler.java
| ... | ... | @@ -42,11 +42,11 @@ public class TradeCouponOrderHandler implements TradeOrderHandler { |
| 42 | 42 | |
| 43 | 43 | @Override |
| 44 | 44 | public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) { |
| 45 | - if (CollUtil.isEmpty(order.getGiveCouponsMap())) { | |
| 45 | + if (CollUtil.isEmpty(order.getGiveCouponTemplateCounts())) { | |
| 46 | 46 | return; |
| 47 | 47 | } |
| 48 | 48 | // 赠送优惠券 |
| 49 | - List<Long> couponIds = couponApi.takeCouponsByAdmin(order.getGiveCouponsMap(), order.getUserId()); | |
| 49 | + List<Long> couponIds = couponApi.takeCouponsByAdmin(order.getGiveCouponTemplateCounts(), order.getUserId()); | |
| 50 | 50 | if (CollUtil.isEmpty(couponIds)) { |
| 51 | 51 | return; |
| 52 | 52 | } | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/bo/TradePriceCalculateRespBO.java
| ... | ... | @@ -76,10 +76,12 @@ public class TradePriceCalculateRespBO { |
| 76 | 76 | /** |
| 77 | 77 | * 赠送的优惠劵 |
| 78 | 78 | * |
| 79 | - * key: 优惠劵编号,value:对应的优惠券数量 | |
| 80 | - * 目的:用于后续取消或者售后订单时,需要扣减赠送 | |
| 79 | + * key: 优惠劵模版编号 | |
| 80 | + * value:对应的优惠券数量 | |
| 81 | + * | |
| 82 | + * 目的:用于订单支付后赠送优惠券 | |
| 81 | 83 | */ |
| 82 | - private Map<Long, Integer> giveCoupons; | |
| 84 | + private Map<Long, Integer> giveCouponTemplateCounts; | |
| 83 | 85 | |
| 84 | 86 | /** |
| 85 | 87 | * 订单价格 | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePriceCalculatorHelper.java
| ... | ... | @@ -32,7 +32,7 @@ public class TradePriceCalculatorHelper { |
| 32 | 32 | List<ProductSpuRespDTO> spuList, List<ProductSkuRespDTO> skuList) { |
| 33 | 33 | // 创建 PriceCalculateRespDTO 对象 |
| 34 | 34 | TradePriceCalculateRespBO result = new TradePriceCalculateRespBO(); |
| 35 | - result.setType(getOrderType(param)).setPromotions(new ArrayList<>()).setGiveCoupons(new LinkedHashMap<>()); | |
| 35 | + result.setType(getOrderType(param)).setPromotions(new ArrayList<>()).setGiveCouponTemplateCounts(new LinkedHashMap<>()); | |
| 36 | 36 | |
| 37 | 37 | // 创建它的 OrderItem 属性 |
| 38 | 38 | result.setItems(new ArrayList<>(param.getItems().size())); | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeRewardActivityPriceCalculator.java
| ... | ... | @@ -107,13 +107,13 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator |
| 107 | 107 | result.setFreeDelivery(true); |
| 108 | 108 | } |
| 109 | 109 | // 4.3 记录赠送的优惠券 |
| 110 | - if (CollUtil.isNotEmpty(rule.getGiveCoupons())) { | |
| 111 | - for (Map.Entry<Long, Integer> entry : rule.getGiveCoupons().entrySet()) { | |
| 112 | - Map<Long, Integer> giveCoupons = result.getGiveCoupons(); | |
| 113 | - if (giveCoupons.get(entry.getKey()) == null) { // 情况一:还没有赠送的优惠券 | |
| 114 | - result.setGiveCoupons(rule.getGiveCoupons()); | |
| 110 | + if (CollUtil.isNotEmpty(rule.getGiveCouponTemplateCounts())) { | |
| 111 | + for (Map.Entry<Long, Integer> entry : rule.getGiveCouponTemplateCounts().entrySet()) { | |
| 112 | + Map<Long, Integer> giveCouponTemplateCounts = result.getGiveCouponTemplateCounts(); | |
| 113 | + if (giveCouponTemplateCounts.get(entry.getKey()) == null) { // 情况一:还没有赠送的优惠券 | |
| 114 | + result.setGiveCouponTemplateCounts(rule.getGiveCouponTemplateCounts()); | |
| 115 | 115 | } else { // 情况二:别的满减活动送过同类优惠券,则直接增加数量 |
| 116 | - giveCoupons.put(entry.getKey(), giveCoupons.get(entry.getKey()) + entry.getValue()); | |
| 116 | + giveCouponTemplateCounts.put(entry.getKey(), giveCouponTemplateCounts.get(entry.getKey()) + entry.getValue()); | |
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | } | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeRewardActivityPriceCalculatorTest.java
| ... | ... | @@ -49,7 +49,7 @@ public class TradeRewardActivityPriceCalculatorTest extends BaseMockitoUnitTest |
| 49 | 49 | TradePriceCalculateRespBO result = new TradePriceCalculateRespBO() |
| 50 | 50 | .setType(TradeOrderTypeEnum.NORMAL.getType()) |
| 51 | 51 | .setPrice(new TradePriceCalculateRespBO.Price()) |
| 52 | - .setPromotions(new ArrayList<>()).setGiveCoupons(new LinkedHashMap<>()) | |
| 52 | + .setPromotions(new ArrayList<>()).setGiveCouponTemplateCounts(new LinkedHashMap<>()) | |
| 53 | 53 | .setItems(asList( |
| 54 | 54 | new TradePriceCalculateRespBO.OrderItem().setSkuId(10L).setCount(2).setSelected(true) |
| 55 | 55 | .setPrice(100).setSpuId(1L), | ... | ... |