From b3fcefa677bda5be09a6e44647daa3abb75d5b5d Mon Sep 17 00:00:00 2001 From: owen Date: Fri, 1 Sep 2023 20:37:20 +0800 Subject: [PATCH] =?UTF-8?q?trade:=20=E4=BC=9A=E5=91=98=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../promotion/api/coupon/CouponApi.java | 6 +++ .../promotion/enums/ErrorCodeConstants.java | 1 + .../promotion/api/coupon/CouponApiImpl.java | 5 +++ .../service/coupon/CouponService.java | 6 +++ .../service/coupon/CouponServiceImpl.java | 23 +++++++++++ .../trade/enums/ErrorCodeConstants.java | 1 + .../app/order/AppTradeOrderController.java | 2 +- .../convert/order/TradeOrderConvert.java | 6 +++ .../order/TradeOrderUpdateService.java | 7 ++++ .../order/TradeOrderUpdateServiceImpl.java | 39 ++++++++++++++++++- 10 files changed, 93 insertions(+), 3 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 ce7a712da7..71e3ca18b9 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 @@ -28,4 +28,10 @@ public interface CouponApi { */ CouponRespDTO validateCoupon(@Valid CouponValidReqDTO validReqDTO); + /** + * 退还已使用的优惠券 + * + * @param id 优惠券编号 + */ + void returnUsedCoupon(Long id); } diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java index e17f0de44b..5c98d04e94 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java @@ -33,6 +33,7 @@ public interface ErrorCodeConstants { ErrorCode COUPON_DELETE_FAIL_USED = new ErrorCode(1013005001, "回收优惠劵失败,优惠劵已被使用"); ErrorCode COUPON_STATUS_NOT_UNUSED = new ErrorCode(1013005002, "优惠劵不处于待使用状态"); ErrorCode COUPON_VALID_TIME_NOT_NOW = new ErrorCode(1013005003, "优惠券不在使用时间范围内"); + ErrorCode COUPON_STATUS_NOT_USED = new ErrorCode(1013005004, "优惠劵不是已使用状态"); // ========== 满减送活动 1013006000 ========== ErrorCode REWARD_ACTIVITY_NOT_EXISTS = new ErrorCode(1013006000, "满减送活动不存在"); 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 a06ab57cd2..d9ecc7d9ce 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 @@ -34,4 +34,9 @@ public class CouponApiImpl implements CouponApi { 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 5981f6ca9e..4c3b659fa7 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 @@ -75,4 +75,10 @@ public interface CouponService { */ Long getUnusedCouponCount(Long userId); + /** + * 退还已使用的优惠券 + * + * @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 fb661241c6..9754f6fd52 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 @@ -125,4 +125,27 @@ public class CouponServiceImpl implements CouponService { return couponMapper.selectCountByUserIdAndStatus(userId, CouponStatusEnum.UNUSED.getStatus()); } + @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-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java index 3aa0e5dd3a..40f604e94e 100644 --- a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java +++ b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java @@ -33,6 +33,7 @@ public interface ErrorCodeConstants { ErrorCode ORDER_DELIVERY_FAIL_COMBINATION_RECORD_STATUS_NOT_SUCCESS = new ErrorCode(1011000022, "交易订单发货失败,拼团未成功"); ErrorCode ORDER_DELIVERY_FAIL_BARGAIN_RECORD_STATUS_NOT_SUCCESS = new ErrorCode(1011000023, "交易订单发货失败,砍价未成功"); ErrorCode ORDER_DELIVERY_FAIL_DELIVERY_TYPE_NOT_EXPRESS = new ErrorCode(1011000024, "交易订单发货失败,发货类型不是快递"); + ErrorCode ORDER_CANCEL_FAIL_STATUS_NOT_UNPAID = new ErrorCode(1011000025, "交易订单取消失败,订单不是【待支付】状态"); // ========== After Sale 模块 1011000100 ========== ErrorCode AFTER_SALE_NOT_FOUND = new ErrorCode(1011000100, "售后单不存在"); diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java index e3205fcaf4..a28815df7d 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java @@ -150,7 +150,7 @@ public class AppTradeOrderController { @Operation(summary = "取消交易订单") @Parameter(name = "id", description = "交易订单编号") public CommonResult cancelOrder(@RequestParam("id") Long id) { - // TODO @芋艿:未实现,mock 用 + tradeOrderUpdateService.cancelOrder(getLoginUserId(), id); return success(true); } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java index 0285a63546..e3a6e84be2 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java @@ -86,6 +86,12 @@ public interface TradeOrderConvert { default ProductSkuUpdateStockReqDTO convert(List list) { return new ProductSkuUpdateStockReqDTO(TradeOrderConvert.INSTANCE.convertList(list)); } + + default ProductSkuUpdateStockReqDTO convertNegative(List list) { + List items = TradeOrderConvert.INSTANCE.convertList(list); + items.forEach(item -> item.setIncrCount(-item.getIncrCount())); + return new ProductSkuUpdateStockReqDTO(items); + } List convertList(List list); @Mappings({ @Mapping(source = "skuId", target = "id"), diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java index bf52358e6b..ee9128b6cf 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateService.java @@ -117,4 +117,11 @@ public interface TradeOrderUpdateService { */ Long createOrderItemComment(Long userId, AppTradeOrderItemCommentCreateReqVO createReqVO); + /** + * 【会员】取消订单 + * + * @param userId 用户ID + * @param id 订单编号 + */ + void cancelOrder(Long userId, Long id); } 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 41da62daba..900b8ccc4d 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 @@ -291,7 +291,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { TradeOrderDO tradeOrderDO, List orderItems, TradePriceCalculateRespBO calculateRespBO) { // 下单时扣减商品库存 - productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(orderItems)); + productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convertNegative(orderItems)); // 删除购物车商品 Set cartIds = convertSet(createReqVO.getItems(), AppTradeOrderSettlementReqVO.Item::getCartId); @@ -299,7 +299,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { cartService.deleteCart(userId, cartIds); } - // 扣减积分 TODO 芋艿:待实现 + // 扣减积分 TODO 芋艿:待实现, 这个是不是应该放到支付成功之后? // 有使用优惠券时更新 if (createReqVO.getCouponId() != null) { @@ -655,6 +655,41 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { return comment; } + @Override + public void cancelOrder(Long userId, Long id) { + // 校验存在 + TradeOrderDO order = tradeOrderMapper.selectOrderByIdAndUserId(id, userId); + if (order == null) { + throw exception(ORDER_NOT_FOUND); + } + // 校验状态 + if (ObjectUtil.notEqual(order.getStatus(), TradeOrderStatusEnum.UNPAID.getStatus())) { + throw exception(ORDER_CANCEL_FAIL_STATUS_NOT_UNPAID); + } + + // 1.更新 TradeOrderDO 状态为已取消 + int updateCount = tradeOrderMapper.updateByIdAndStatus(id, order.getStatus(), + new TradeOrderDO().setStatus(TradeOrderStatusEnum.CANCELED.getStatus()) + .setCancelTime(LocalDateTime.now()) + .setCancelType(TradeOrderCancelTypeEnum.MEMBER_CANCEL.getType())); + if (updateCount == 0) { + throw exception(ORDER_CANCEL_FAIL_STATUS_NOT_UNPAID); + } + + // 2.回滚库存 + List orderItems = tradeOrderItemMapper.selectListByOrderId(id); + productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(orderItems)); + + // 3.回滚优惠券 + couponApi.returnUsedCoupon(order.getCouponId()); + + // 4.回滚积分:积分是支付成功后才增加的吧? + + // TODO 芋艿:OrderLog + + // TODO 芋艿:lili 发送订单变化的消息 + } + /** * 判断指定订单的所有订单项,是不是都售后成功 *