重构:将订单的分支流程,抽到 TradeOrderHandler 实现类中(取消的排重逻辑)

This commit is contained in:
YunaiV 2023-10-10 20:38:20 +08:00
parent 2e5cc0537b
commit 150a1c9771
4 changed files with 68 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.trade.service.order.handler; package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi; import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
@ -53,21 +54,36 @@ public class TradeBrokerageOrderHandler implements TradeOrderHandler {
@Override @Override
public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) { public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (order.getBrokerageUserId() != null) { if (order.getBrokerageUserId() == null) {
addBrokerage(order.getUserId(), orderItems); return;
} }
addBrokerage(order.getUserId(), orderItems);
} }
@Override @Override
public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) { public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// TODO 芋艿取消支付时需要处理下 // 如果是未支付的订单不会产生分销结果所以直接 return
if (!order.getPayStatus()) {
return;
}
if (order.getBrokerageUserId() == null) {
return;
}
// 售后的订单项已经在 afterCancelOrderItem 回滚库存所以这里不需要重复回滚
orderItems = filterOrderItemListByNoneAfterSale(orderItems);
if (CollUtil.isEmpty(orderItems)) {
return;
}
orderItems.forEach(orderItem -> afterCancelOrderItem(order, orderItem));
} }
@Override @Override
public void afterCancelOrderItem(TradeOrderDO order, TradeOrderItemDO orderItem) { public void afterCancelOrderItem(TradeOrderDO order, TradeOrderItemDO orderItem) {
if (order.getBrokerageUserId() != null) { if (order.getBrokerageUserId() == null) {
cancelBrokerage(order.getId(), orderItem.getOrderId()); return;
} }
cancelBrokerage(order.getId(), orderItem.getOrderId());
} }
/** /**

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.trade.service.order.handler; package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.member.api.level.MemberLevelApi; import cn.iocoder.yudao.module.member.api.level.MemberLevelApi;
import cn.iocoder.yudao.module.member.api.point.MemberPointApi; import cn.iocoder.yudao.module.member.api.point.MemberPointApi;
import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum; import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum;
@ -13,6 +14,8 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue;
/** /**
* 会员积分等级的 {@link TradeOrderHandler} 实现类 * 会员积分等级的 {@link TradeOrderHandler} 实现类
* *
@ -48,10 +51,29 @@ public class TradeMemberPointOrderHandler implements TradeOrderHandler {
@Override @Override
public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) { public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 扣减回滚用户积分订单抵扣 // 售后的订单项已经在 afterCancelOrderItem 回滚库存所以这里不需要重复回滚
addPoint(order.getUserId(), order.getUsePoint(), MemberPointBizTypeEnum.ORDER_CANCEL, orderItems = filterOrderItemListByNoneAfterSale(orderItems);
if (CollUtil.isEmpty(orderItems)) {
return;
}
// 增加回滚用户积分订单抵扣
Integer usePoint = getSumValue(orderItems, TradeOrderItemDO::getUsePoint, Integer::sum);
addPoint(order.getUserId(), usePoint, MemberPointBizTypeEnum.ORDER_CANCEL,
order.getId()); order.getId());
// TODO 芋艿需要校验如果部分子订单已经售后退款则不进行整单退因为已经退了一部分积分了 // 如下的返还需要经过支持也就是经历 afterPayOrder 流程
if (!order.getPayStatus()) {
return;
}
// 扣减回滚积分订单赠送
Integer givePoint = getSumValue(orderItems, TradeOrderItemDO::getGivePoint, Integer::sum);
reducePoint(order.getUserId(), givePoint, MemberPointBizTypeEnum.ORDER_CANCEL,
order.getId());
// 扣减回滚用户经验
int payPrice = order.getPayPrice() - order.getRefundPrice();
// TODO @疯狂这里的 bizId afterCancelOrderItem 不一致了有什么建议的处理方案
memberLevelApi.addExperience(order.getUserId(), -payPrice, MemberExperienceBizTypeEnum.REFUND.getType(),
String.valueOf(order.getId()));
} }
@Override @Override
@ -59,7 +81,7 @@ public class TradeMemberPointOrderHandler implements TradeOrderHandler {
// 扣减回滚积分订单赠送 // 扣减回滚积分订单赠送
reducePoint(order.getUserId(), orderItem.getGivePoint(), MemberPointBizTypeEnum.AFTER_SALE_DEDUCT_GIVE, reducePoint(order.getUserId(), orderItem.getGivePoint(), MemberPointBizTypeEnum.AFTER_SALE_DEDUCT_GIVE,
orderItem.getAfterSaleId()); orderItem.getAfterSaleId());
// 扣减回滚积分增加用户积分返还抵扣 // 增加回滚积分订单抵扣
addPoint(order.getUserId(), orderItem.getUsePoint(), MemberPointBizTypeEnum.AFTER_SALE_REFUND_USED, addPoint(order.getUserId(), orderItem.getUsePoint(), MemberPointBizTypeEnum.AFTER_SALE_REFUND_USED,
orderItem.getAfterSaleId()); orderItem.getAfterSaleId());

View File

@ -1,7 +1,9 @@
package cn.iocoder.yudao.module.trade.service.order.handler; package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderItemAfterSaleStatusEnum;
import java.util.List; import java.util.List;
@ -60,4 +62,17 @@ public interface TradeOrderHandler {
*/ */
default void beforeDeliveryOrder(TradeOrderDO order) {} default void beforeDeliveryOrder(TradeOrderDO order) {}
// ========== 公用方法 ==========
/**
* 过滤未售后的订单项列表
*
* @param orderItems 订单项列表
* @return 过滤后的订单项列表
*/
default List<TradeOrderItemDO> filterOrderItemListByNoneAfterSale(List<TradeOrderItemDO> orderItems) {
CollUtil.filterNew(orderItems, item -> !TradeOrderItemAfterSaleStatusEnum.isNone(item.getAfterSaleStatus()));
return orderItems;
}
} }

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.trade.service.order.handler; package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert; import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO; import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
@ -29,7 +30,11 @@ public class TradeProductSkuOrderHandler implements TradeOrderHandler {
@Override @Override
public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) { public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// TODO 芋艿如果部分售后最后导致取消这里需要过滤下 // 售后的订单项已经在 afterCancelOrderItem 回滚库存所以这里不需要重复回滚
orderItems = filterOrderItemListByNoneAfterSale(orderItems);
if (CollUtil.isEmpty(orderItems)) {
return;
}
productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(orderItems)); productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(orderItems));
} }