trade:优化售后取消订单的实现代码

This commit is contained in:
YunaiV 2023-09-29 10:08:20 +08:00
parent f9bf41d773
commit d402bdb7e0
3 changed files with 42 additions and 26 deletions

View File

@ -140,4 +140,5 @@ public class AppProductSpuController {
return price - newPrice; return price - newPrice;
} }
// TODO 芋艿商品的浏览记录
} }

View File

@ -24,7 +24,8 @@ public enum TradeOrderOperateTypeEnum {
MEMBER_CANCEL(40, "取消订单"), MEMBER_CANCEL(40, "取消订单"),
SYSTEM_CANCEL(41, "到期未支付,系统自动取消订单"), SYSTEM_CANCEL(41, "到期未支付,系统自动取消订单"),
// 42 预留管理员取消订单 // 42 预留管理员取消订单
MEMBER_DELETE(43, "删除订单"), ADMIN_CANCEL_AFTER_SALE(43, "订单全部售后,管理员自动取消订单"),
MEMBER_DELETE(49, "删除订单"),
; ;
/** /**

View File

@ -663,6 +663,27 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
TradeOrderLogUtils.setOrderInfo(order.getId(), order.getStatus(), TradeOrderStatusEnum.CANCELED.getStatus()); TradeOrderLogUtils.setOrderInfo(order.getId(), order.getStatus(), TradeOrderStatusEnum.CANCELED.getStatus());
} }
/**
* 如果金额全部被退款则取消订单
* 如果还有未被退款的金额则无需取消订单
*
* @param order 订单
* @param refundPrice 退款金额
*/
@TradeOrderLog(operateType = TradeOrderOperateTypeEnum.ADMIN_CANCEL_AFTER_SALE)
public void cancelOrderByAfterSale(TradeOrderDO order, Integer refundPrice) {
// 1. 更新订单
if (refundPrice < order.getPayPrice()) {
return;
}
tradeOrderMapper.updateById(new TradeOrderDO().setId(order.getId())
.setStatus(TradeOrderStatusEnum.CANCELED.getStatus())
.setCancelType(TradeOrderCancelTypeEnum.AFTER_SALE_CLOSE.getType()).setCancelTime(LocalDateTime.now()));
// 2. 退还优惠券
couponApi.returnUsedCoupon(order.getCouponId());
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@TradeOrderLog(operateType = TradeOrderOperateTypeEnum.MEMBER_DELETE) @TradeOrderLog(operateType = TradeOrderOperateTypeEnum.MEMBER_DELETE)
@ -775,31 +796,35 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
updateOrderItemAfterSaleStatus(id, TradeOrderItemAfterSaleStatusEnum.APPLY.getStatus(), updateOrderItemAfterSaleStatus(id, TradeOrderItemAfterSaleStatusEnum.APPLY.getStatus(),
TradeOrderItemAfterSaleStatusEnum.SUCCESS.getStatus(), null); TradeOrderItemAfterSaleStatusEnum.SUCCESS.getStatus(), null);
// 2. 计算总的退款金额退回积分 // 2.1 更新订单的退款金额积分
TradeOrderItemDO orderItem = tradeOrderItemMapper.selectById(id); TradeOrderItemDO orderItem = tradeOrderItemMapper.selectById(id);
TradeOrderDO order = tradeOrderMapper.selectById(orderItem.getOrderId()); TradeOrderDO order = tradeOrderMapper.selectById(orderItem.getOrderId());
Integer orderRefundPrice = order.getRefundPrice() + refundPrice; Integer orderRefundPrice = order.getRefundPrice() + refundPrice;
Integer orderRefundPoint = order.getRefundPoint() + orderItem.getUsePoint(); Integer orderRefundPoint = order.getRefundPoint() + orderItem.getUsePoint();
if (isAllOrderItemAfterSaleSuccess(order.getId())) { // 如果都售后成功则需要取消订单 Integer refundStatus = isAllOrderItemAfterSaleSuccess(order.getId()) ?
cancelOrderByAfterSale(order, orderRefundPrice, orderRefundPoint); TradeOrderRefundStatusEnum.ALL.getStatus() // 如果都售后成功则需要取消订单
} else { // 如果部分售后则更新退款金额 : TradeOrderRefundStatusEnum.PART.getStatus();
tradeOrderMapper.updateById(new TradeOrderDO().setId(order.getId()) tradeOrderMapper.updateById(new TradeOrderDO().setId(order.getId())
.setRefundStatus(TradeOrderRefundStatusEnum.PART.getStatus()) .setRefundStatus(refundStatus)
.setRefundPrice(orderRefundPrice).setRefundPoint(orderRefundPoint)); .setRefundPrice(orderRefundPrice).setRefundPoint(orderRefundPoint));
} // 2.2 如果全部退款则进行取消订单
getSelf().cancelOrderByAfterSale(order, orderRefundPrice);
// TODO 芋艿这块扣减规则需要在考虑下 // TODO @puhui999活动相关的回滚
// 3. 回滚数据增加 SKU 库存
// 3. 回滚库存
productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(Collections.singletonList(orderItem))); productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(Collections.singletonList(orderItem)));
// 4.1 回滚数据扣减用户积分赠送的 // 4.1 回滚积分扣减用户积分赠送的
reduceUserPoint(order.getUserId(), orderItem.getGivePoint(), MemberPointBizTypeEnum.AFTER_SALE_DEDUCT_GIVE, orderItem.getAfterSaleId()); reduceUserPoint(order.getUserId(), orderItem.getGivePoint(), MemberPointBizTypeEnum.AFTER_SALE_DEDUCT_GIVE, orderItem.getAfterSaleId());
// 4.2 回滚数据增加用户积分返还抵扣 // 4.2 回滚积分增加用户积分返还抵扣
addUserPoint(order.getUserId(), orderItem.getUsePoint(), MemberPointBizTypeEnum.AFTER_SALE_REFUND_USED, orderItem.getAfterSaleId()); addUserPoint(order.getUserId(), orderItem.getUsePoint(), MemberPointBizTypeEnum.AFTER_SALE_REFUND_USED, orderItem.getAfterSaleId());
// 4.3 回滚数据扣减用户经验
// 5. 回滚经验扣减用户经验
// TODO @疯狂orderRefundPrice 是不是改成 refundPrice应该只退这个售后对应的经验
getSelf().reduceUserExperienceAsync(order.getUserId(), orderRefundPrice, orderItem.getAfterSaleId()); getSelf().reduceUserExperienceAsync(order.getUserId(), orderRefundPrice, orderItem.getAfterSaleId());
// 5. 回滚数据更新分佣记录为已失效 // 6. 回滚佣金更新分佣记录为已失效
getSelf().cancelBrokerageAsync(order.getUserId(), id); getSelf().cancelBrokerageAsync(order.getUserId(), id);
} }
@ -819,17 +844,6 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
} }
} }
@TradeOrderLog(operateType = TradeOrderOperateTypeEnum.MEMBER_CANCEL)
private void cancelOrderByAfterSale(TradeOrderDO order, Integer orderRefundPrice, Integer refundPoint) {
// 1. 更新订单
tradeOrderMapper.updateById(new TradeOrderDO().setId(order.getId())
.setRefundStatus(TradeOrderRefundStatusEnum.ALL.getStatus())
.setRefundPrice(orderRefundPrice).setRefundPoint(refundPoint)
.setCancelType(TradeOrderCancelTypeEnum.AFTER_SALE_CLOSE.getType()).setCancelTime(LocalDateTime.now()));
// 2. 退还优惠券
couponApi.returnUsedCoupon(order.getCouponId());
}
/** /**
* 判断指定订单的所有订单项是不是都售后成功 * 判断指定订单的所有订单项是不是都售后成功
* *