From c25bf38f50eb8d9917faa7db4754723793e8acb5 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 2 Sep 2023 00:52:31 +0800 Subject: [PATCH] =?UTF-8?q?code=20review=EF=BC=9A=E4=BA=A4=E6=98=93?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E7=9A=84=E5=8F=96=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../promotion/api/coupon/CouponApi.java | 13 +++--- .../promotion/api/coupon/CouponApiImpl.java | 10 ++-- .../service/coupon/CouponService.java | 13 +++--- .../service/coupon/CouponServiceImpl.java | 46 +++++++++---------- .../order/TradeOrderUpdateServiceImpl.java | 2 +- 5 files changed, 43 insertions(+), 41 deletions(-) diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/CouponApi.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/CouponApi.java index 71e3ca18b..ab970c0a3 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/CouponApi.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/CouponApi.java @@ -20,6 +20,13 @@ public interface CouponApi { */ void useCoupon(@Valid CouponUseReqDTO useReqDTO); + /** + * 退还已使用的优惠券 + * + * @param id 优惠券编号 + */ + void returnUsedCoupon(Long id); + /** * 校验优惠劵 * @@ -28,10 +35,4 @@ public interface CouponApi { */ CouponRespDTO validateCoupon(@Valid CouponValidReqDTO validReqDTO); - /** - * 退还已使用的优惠券 - * - * @param id 优惠券编号 - */ - void returnUsedCoupon(Long id); } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/CouponApiImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/CouponApiImpl.java index d9ecc7d9c..94d00e35c 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/CouponApiImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/api/coupon/CouponApiImpl.java @@ -28,15 +28,15 @@ public class CouponApiImpl implements CouponApi { useReqDTO.getOrderId()); } + @Override + public void returnUsedCoupon(Long id) { + couponService.returnUsedCoupon(id); + } + @Override public CouponRespDTO validateCoupon(CouponValidReqDTO validReqDTO) { CouponDO coupon = couponService.validCoupon(validReqDTO.getId(), validReqDTO.getUserId()); return CouponConvert.INSTANCE.convert(coupon); } - @Override - public void returnUsedCoupon(Long id) { - couponService.returnUsedCoupon(id); - } - } 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 bfe42f363..cf22fe2b3 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 @@ -54,6 +54,13 @@ public interface CouponService { */ void useCoupon(Long id, Long userId, Long orderId); + /** + * 退还已使用的优惠券 + * + * @param id 优惠券编号 + */ + void returnUsedCoupon(Long id); + /** * 回收优惠劵 * @@ -117,10 +124,4 @@ public interface CouponService { takeCoupon(templateId, CollUtil.newHashSet(userId), CouponTakeTypeEnum.REGISTER); } - /** - * 退还已使用的优惠券 - * - * @param id 优惠券编号 - */ - void returnUsedCoupon(Long id); } 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 5e608e696..9abbd3c5f 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 @@ -102,6 +102,29 @@ public class CouponServiceImpl implements CouponService { } } + @Override + public void returnUsedCoupon(Long id) { + // 校验存在 + CouponDO coupon = couponMapper.selectById(id); + if (coupon == null) { + throw exception(COUPON_NOT_EXISTS); + } + // 校验状态 + if (ObjectUtil.notEqual(coupon.getTemplateId(), CouponStatusEnum.USED.getStatus())) { + throw exception(COUPON_STATUS_NOT_USED); + } + + // 退还 + // TODO @疯狂:最好 where status,避免可能存在的并发问题 + Integer status = LocalDateTimeUtils.beforeNow(coupon.getValidEndTime()) + // 退还时可能已经过期了 + ? CouponStatusEnum.EXPIRE.getStatus() + : CouponStatusEnum.UNUSED.getStatus(); + couponMapper.updateById(new CouponDO().setId(id).setStatus(status)); + + // TODO 增加优惠券变动记录? + } + @Override @Transactional public void deleteCoupon(Long id) { @@ -202,27 +225,4 @@ public class CouponServiceImpl implements CouponService { userIds.removeIf(userId -> MapUtil.getInt(userTakeCountMap, userId, 0) >= couponTemplate.getTakeLimitCount()); } - @Override - public void returnUsedCoupon(Long id) { - // 校验存在 - CouponDO coupon = couponMapper.selectById(id); - if (coupon == null) { - throw exception(COUPON_NOT_EXISTS); - } - - // 校验状态 - if (!CouponStatusEnum.USED.getStatus().equals(coupon.getStatus())) { - throw exception(COUPON_STATUS_NOT_USED); - } - - // 退还 - Integer status = LocalDateTimeUtils.beforeNow(coupon.getValidEndTime()) - // 退还时可能已经过期了 - ? CouponStatusEnum.EXPIRE.getStatus() - : CouponStatusEnum.UNUSED.getStatus(); - couponMapper.updateById(new CouponDO().setId(id).setStatus(status)); - - // TODO 增加优惠券变动记录? - } - } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java index 4c88226ea..f887b71e8 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java @@ -699,7 +699,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { // 3.回滚优惠券 couponApi.returnUsedCoupon(order.getCouponId()); - // 4.回滚积分:积分是支付成功后才增加的吧? + // 4.回滚积分:积分是支付成功后才增加的吧? 回复:每个项目不同,目前看下来,确认收货貌似更合适,我再看看其它项目的业务选择; // TODO 芋艿:OrderLog