重构:将订单的分支流程,抽到 TradeOrderHandler 实现类中

This commit is contained in:
YunaiV 2023-10-10 19:20:12 +08:00
parent 7b727d5ce2
commit 2e5cc0537b
10 changed files with 355 additions and 232 deletions

View File

@ -14,23 +14,12 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.module.member.api.address.AddressApi;
import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO;
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.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderRespDTO;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
import cn.iocoder.yudao.module.product.api.comment.ProductCommentApi;
import cn.iocoder.yudao.module.product.api.comment.dto.ProductCommentCreateReqDTO;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.promotion.api.combination.CombinationRecordApi;
import cn.iocoder.yudao.module.promotion.api.coupon.CouponApi;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponUseReqDTO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderDeliveryReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderRemarkReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderUpdateAddressReqVO;
@ -40,7 +29,6 @@ import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderSettle
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderSettlementRespVO;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.item.AppTradeOrderItemCommentCreateReqVO;
import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageUserDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.cart.CartDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
@ -48,15 +36,11 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderItemMapper;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderMapper;
import cn.iocoder.yudao.module.trade.dal.redis.no.TradeNoRedisDAO;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.*;
import cn.iocoder.yudao.module.trade.framework.order.config.TradeOrderProperties;
import cn.iocoder.yudao.module.trade.framework.order.core.annotations.TradeOrderLog;
import cn.iocoder.yudao.module.trade.framework.order.core.utils.TradeOrderLogUtils;
import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageRecordService;
import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageUserService;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.cart.CartService;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressService;
import cn.iocoder.yudao.module.trade.service.message.TradeMessageService;
@ -68,13 +52,15 @@ import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
@ -109,30 +95,12 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
private DeliveryExpressService deliveryExpressService;
@Resource
private TradeMessageService tradeMessageService;
@Resource
private BrokerageUserService brokerageUserService;
@Resource
private BrokerageRecordService brokerageRecordService;
@Resource
private ProductSpuApi productSpuApi;
@Resource
private ProductSkuApi productSkuApi;
@Resource
private PayOrderApi payOrderApi;
@Resource
private AddressApi addressApi;
@Resource
private CouponApi couponApi;
@Resource
private CombinationRecordApi combinationRecordApi;
@Resource
private MemberUserApi memberUserApi;
@Resource
private MemberLevelApi memberLevelApi;
@Resource
private MemberPointApi memberPointApi;
@Resource
private ProductCommentApi productCommentApi;
@Resource
@ -199,7 +167,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
List<TradeOrderItemDO> orderItems = buildTradeOrderItems(order, calculateRespBO);
// 2. 订单创建前的逻辑
beforeCreateTradeOrder(order, orderItems);
tradeOrderHandlers.forEach(handler -> handler.beforeOrderCreate(order, orderItems));
// 3. 保存订单
tradeOrderMapper.insert(order);
@ -234,11 +202,6 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
order.setReceiverName(createReqVO.getReceiverName()).setReceiverMobile(createReqVO.getReceiverMobile());
order.setPickUpVerifyCode(RandomUtil.randomNumbers(8)); // 随机一个核销码长度为 8
}
// 设置订单推广人
BrokerageUserDO brokerageUser = brokerageUserService.getBrokerageUser(order.getUserId());
if (brokerageUser != null && brokerageUser.getBindUserId() != null) {
order.setBrokerageUserId(brokerageUser.getBindUserId());
}
return order;
}
@ -247,21 +210,6 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
return TradeOrderConvert.INSTANCE.convertList(tradeOrderDO, calculateRespBO);
}
/**
* 订单创建前执行前置逻辑
*
* @param order 订单
* @param orderItems 订单项
*/
private void beforeCreateTradeOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 1. 执行订单创建前置处理器
// TODO @puhui999这里有个纠结点handler 的定义是只处理指定类型的订单的拓展逻辑还是通用的 handler类似可以处理优惠劵等等
tradeOrderHandlers.forEach(handler -> handler.beforeOrderCreate(order, orderItems));
// 2. 下单时扣减商品库存
productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convertNegative(orderItems));
}
/**
* 订单创建后执行后置逻辑
* <p>
@ -276,27 +224,16 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
// 1. 执行订单创建后置处理器
tradeOrderHandlers.forEach(handler -> handler.afterOrderCreate(order, orderItems));
// 2. 有使用优惠券时更新
// 不在前置扣减的原因是因为优惠劵要记录使用的订单号
if (order.getCouponId() != null) {
couponApi.useCoupon(new CouponUseReqDTO().setId(order.getCouponId()).setUserId(order.getUserId())
.setOrderId(order.getId()));
}
// 3. 扣减积分抵扣
// 不在前置扣减的原因是因为积分扣减时需要记录关联业务
reduceUserPoint(order.getUserId(), order.getUsePoint(), MemberPointBizTypeEnum.ORDER_USE, order.getId());
// 4. 删除购物车商品
// 2. 删除购物车商品
Set<Long> cartIds = convertSet(createReqVO.getItems(), AppTradeOrderSettlementReqVO.Item::getCartId);
if (CollUtil.isNotEmpty(cartIds)) {
cartService.deleteCart(order.getUserId(), cartIds);
}
// 5. 生成预支付
// 3. 生成预支付
createPayOrder(order, orderItems);
// 6. 插入订单日志
// 4. 插入订单日志
TradeOrderLogUtils.setOrderInfo(order.getId(), null, order.getStatus());
// TODO @LeeYan9: 是可以思考下, 订单的营销优惠记录, 应该记录在哪里, 微信讨论起来!
@ -330,18 +267,11 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
throw exception(ORDER_UPDATE_PAID_STATUS_NOT_UNPAID);
}
// 3订单支付成功后
// 3. 执行 TradeOrderHandler 的后置处理
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderId(id);
tradeOrderHandlers.forEach(handler -> handler.afterPayOrder(order, orderItems));
// 4.1 增加用户积分赠送
addUserPoint(order.getUserId(), order.getGivePoint(), MemberPointBizTypeEnum.ORDER_GIVE, order.getId());
// 4.2 增加用户经验
getSelf().addUserExperienceAsync(order.getUserId(), order.getPayPrice(), order.getId());
// 4.3 增加用户佣金
getSelf().addBrokerageAsync(order.getUserId(), order.getId());
// 5. 记录订单日志
// 4. 记录订单日志
TradeOrderLogUtils.setOrderInfo(order.getId(), order.getStatus(), TradeOrderStatusEnum.UNDELIVERED.getStatus());
TradeOrderLogUtils.setUserInfo(order.getUserId(), UserTypeEnum.MEMBER.getValue());
}
@ -434,8 +364,8 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
.put("logisticsNo", express != null ? deliveryReqVO.getLogisticsNo() : "").build());
// 4. 发送站内信
tradeMessageService.sendMessageWhenDeliveryOrder(new TradeOrderMessageWhenDeliveryOrderReqBO().setOrderId(order.getId())
.setUserId(order.getUserId()).setMessage(null));
tradeMessageService.sendMessageWhenDeliveryOrder(new TradeOrderMessageWhenDeliveryOrderReqBO()
.setOrderId(order.getId()).setUserId(order.getUserId()).setMessage(null));
}
/**
@ -448,18 +378,13 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
*/
private TradeOrderDO validateOrderDeliverable(Long id) {
TradeOrderDO order = validateOrderExists(id);
// 校验订单是否退款
// 1. 校验订单是否未发货
if (ObjectUtil.notEqual(TradeOrderRefundStatusEnum.NONE.getStatus(), order.getRefundStatus())) {
throw exception(ORDER_DELIVERY_FAIL_REFUND_STATUS_NOT_NONE);
}
// 订单类型拼团
if (Objects.equals(TradeOrderTypeEnum.COMBINATION.getType(), order.getType())) {
// 校验订单拼团是否成功
if (!combinationRecordApi.isCombinationRecordSuccess(order.getUserId(), order.getId())) {
throw exception(ORDER_DELIVERY_FAIL_COMBINATION_RECORD_STATUS_NOT_SUCCESS);
}
}
// 2. 执行 TradeOrderHandler 前置处理
tradeOrderHandlers.forEach(handler -> handler.beforeDeliveryOrder(order));
return order;
}
@ -616,30 +541,19 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
* @param cancelType 取消类型
*/
private void cancelOrder0(TradeOrderDO order, TradeOrderCancelTypeEnum cancelType) {
Long id = order.getId();
// 1. 更新 TradeOrderDO 状态为已取消
int updateCount = tradeOrderMapper.updateByIdAndStatus(id, order.getStatus(),
int updateCount = tradeOrderMapper.updateByIdAndStatus(order.getId(), order.getStatus(),
new TradeOrderDO().setStatus(TradeOrderStatusEnum.CANCELED.getStatus())
.setCancelType(cancelType.getType()).setCancelTime(LocalDateTime.now()));
if (updateCount == 0) {
throw exception(ORDER_CANCEL_FAIL_STATUS_NOT_UNPAID);
}
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderId(id);
// 3. 回滚库存
productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(orderItems));
// 3.1 活动相关的回滚
tradeOrderHandlers.forEach(handler -> handler.cancelOrder(order, orderItems));
// 2. 执行 TradeOrderHandler 的后置处理
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderId(order.getId());
tradeOrderHandlers.forEach(handler -> handler.afterCancelOrder(order, orderItems));
// 4. 回滚优惠券
if (order.getCouponId() != null && order.getCouponId() > 0) {
couponApi.returnUsedCoupon(order.getCouponId());
}
// 5. 回滚积分抵扣的
addUserPoint(order.getUserId(), order.getUsePoint(), MemberPointBizTypeEnum.ORDER_CANCEL, order.getId());
// 6. 增加订单日志
// 3. 增加订单日志
TradeOrderLogUtils.setOrderInfo(order.getId(), order.getStatus(), TradeOrderStatusEnum.CANCELED.getStatus());
}
@ -660,8 +574,9 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
.setStatus(TradeOrderStatusEnum.CANCELED.getStatus())
.setCancelType(TradeOrderCancelTypeEnum.AFTER_SALE_CLOSE.getType()).setCancelTime(LocalDateTime.now()));
// 2. 退还优惠券
couponApi.returnUsedCoupon(order.getCouponId());
// 2. 执行 TradeOrderHandler 的后置处理
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderId(order.getId());
tradeOrderHandlers.forEach(handler -> handler.afterCancelOrder(order, orderItems));
}
@Override
@ -790,13 +705,15 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Override
@Transactional(rollbackFor = Exception.class)
public void updateOrderItemWhenAfterSaleSuccess(Long id, Integer refundPrice) {
// 1. 更新订单项
// 1.1 更新订单项
updateOrderItemAfterSaleStatus(id, TradeOrderItemAfterSaleStatusEnum.APPLY.getStatus(),
TradeOrderItemAfterSaleStatusEnum.SUCCESS.getStatus(), null);
// 2.1 更新订单的退款金额积分
// 1.2 执行 TradeOrderHandler 的后置处理
TradeOrderItemDO orderItem = tradeOrderItemMapper.selectById(id);
TradeOrderDO order = tradeOrderMapper.selectById(orderItem.getOrderId());
tradeOrderHandlers.forEach(handler -> handler.afterCancelOrderItem(order, orderItem));
// 2.1 更新订单的退款金额积分
Integer orderRefundPrice = order.getRefundPrice() + refundPrice;
Integer orderRefundPoint = order.getRefundPoint() + orderItem.getUsePoint();
Integer refundStatus = isAllOrderItemAfterSaleSuccess(order.getId()) ?
@ -807,23 +724,6 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
.setRefundPrice(orderRefundPrice).setRefundPoint(orderRefundPoint));
// 2.2 如果全部退款则进行取消订单
getSelf().cancelOrderByAfterSale(order, orderRefundPrice);
// 3. 回滚库存
productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(Collections.singletonList(orderItem)));
// 3.1 活动相关的回滚
tradeOrderHandlers.forEach(handler -> handler.cancelOrder(order, Collections.singletonList(orderItem)));
// 4.1 回滚积分扣减用户积分赠送的
reduceUserPoint(order.getUserId(), orderItem.getGivePoint(), MemberPointBizTypeEnum.AFTER_SALE_DEDUCT_GIVE, orderItem.getAfterSaleId());
// 4.2 回滚积分增加用户积分返还抵扣
addUserPoint(order.getUserId(), orderItem.getUsePoint(), MemberPointBizTypeEnum.AFTER_SALE_REFUND_USED, orderItem.getAfterSaleId());
// 5. 回滚经验扣减用户经验
getSelf().reduceUserExperienceAsync(order.getUserId(), refundPrice, orderItem.getAfterSaleId());
// 6. 回滚佣金更新分佣记录为已失效
getSelf().cancelBrokerageAsync(order.getUserId(), id);
}
@Override
@ -840,6 +740,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
if (updateCount <= 0) {
throw exception(ORDER_ITEM_UPDATE_AFTER_SALE_STATUS_FAIL);
}
}
/**
@ -940,7 +841,8 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@TradeOrderLog(operateType = TradeOrderOperateTypeEnum.SYSTEM_COMMENT)
public void createOrderItemCommentBySystemBySystem(TradeOrderDO order) {
// 1. 查询未评论的订单项
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderIdAndCommentStatus(order.getId(), Boolean.FALSE);
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderIdAndCommentStatus(
order.getId(), Boolean.FALSE);
if (CollUtil.isEmpty(orderItems)) {
return;
}
@ -983,73 +885,6 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
// =================== 营销相关的操作 ===================
@Async
protected void addUserExperienceAsync(Long userId, Integer payPrice, Long orderId) {
int bizType = MemberExperienceBizTypeEnum.ORDER.getType();
memberLevelApi.addExperience(userId, payPrice, bizType, String.valueOf(orderId));
}
@Async
protected void reduceUserExperienceAsync(Long userId, Integer refundPrice, Long afterSaleId) {
int bizType = MemberExperienceBizTypeEnum.REFUND.getType();
memberLevelApi.addExperience(userId, -refundPrice, bizType, String.valueOf(afterSaleId));
}
/**
* 添加用户积分
* <p>
* 目前是支付成功后就会创建积分记录
* <p>
* 业内还有两种做法可以根据自己的业务调整
* 1. 确认收货后才创建积分记录
* 2. 支付 or 下单成功时创建积分记录冻结确认收货解冻或者 n 天后解冻
*
* @param userId 用户编号
* @param point 增加积分数量
* @param bizType 业务编号
* @param bizId 业务编号
*/
protected void addUserPoint(Long userId, Integer point, MemberPointBizTypeEnum bizType, Long bizId) {
if (point != null && point > 0) {
memberPointApi.addPoint(userId, point, bizType.getType(), String.valueOf(bizId));
}
}
protected void reduceUserPoint(Long userId, Integer point, MemberPointBizTypeEnum bizType, Long bizId) {
if (point != null && point > 0) {
memberPointApi.reducePoint(userId, point, bizType.getType(), String.valueOf(bizId));
}
}
/**
* 创建分销记录
* <p>
* 目前是支付成功后就会创建分销记录
* <p>
* 业内还有两种做法可以根据自己的业务调整
* 1. 确认收货后才创建分销记录
* 2. 支付 or 下单成功时创建分销记录冻结确认收货解冻或者 n 天后解冻
*
* @param userId 用户编号
* @param orderId 订单编号
*/
@Async
protected void addBrokerageAsync(Long userId, Long orderId) {
MemberUserRespDTO user = memberUserApi.getUser(userId);
Assert.notNull(user);
// 每一个订单项都会去生成分销记录
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderId(orderId);
List<BrokerageAddReqBO> addList = convertList(orderItems,
item -> TradeOrderConvert.INSTANCE.convert(user, item,
productSpuApi.getSpu(item.getSpuId()), productSkuApi.getSku(item.getSkuId())));
brokerageRecordService.addBrokerage(userId, BrokerageRecordBizTypeEnum.ORDER, addList);
}
@Async
protected void cancelBrokerageAsync(Long userId, Long orderItemId) {
brokerageRecordService.cancelBrokerage(userId, BrokerageRecordBizTypeEnum.ORDER, String.valueOf(orderItemId));
}
/**
* 获得自身的代理对象解决 AOP 生效问题
*

View File

@ -11,12 +11,12 @@ import javax.annotation.Resource;
import java.util.List;
/**
* 砍价订单 handler 实现类
* 砍价订单 {@link TradeOrderHandler} 实现类
*
* @author HUIHUI
*/
@Component
public class TradeBargainHandler implements TradeOrderHandler {
public class TradeBargainOrderHandler implements TradeOrderHandler {
@Resource
private BargainActivityApi bargainActivityApi;
@ -25,7 +25,7 @@ public class TradeBargainHandler implements TradeOrderHandler {
@Override
public void beforeOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (TradeOrderTypeEnum.isBargain(order.getType())) {
if (!TradeOrderTypeEnum.isBargain(order.getType())) {
return;
}
@ -36,7 +36,7 @@ public class TradeBargainHandler implements TradeOrderHandler {
@Override
public void afterOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (TradeOrderTypeEnum.isBargain(order.getType())) {
if (!TradeOrderTypeEnum.isBargain(order.getType())) {
return;
}
@ -44,12 +44,4 @@ public class TradeBargainHandler implements TradeOrderHandler {
bargainRecordApi.updateBargainRecordOrderId(order.getBargainRecordId(), order.getId());
}
@Override
public void cancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (TradeOrderTypeEnum.isBargain(order.getType())) {
return;
}
// TODO 芋艿取消订单时需要增加库存
}
}

View File

@ -0,0 +1,102 @@
package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageUserDO;
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.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageRecordService;
import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageUserService;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
/**
* 订单分销的 {@link TradeOrderHandler} 实现类
*
* @author 芋道源码
*/
@Component
public class TradeBrokerageOrderHandler implements TradeOrderHandler {
@Resource
private MemberUserApi memberUserApi;
@Resource
private ProductSpuApi productSpuApi;
@Resource
private ProductSkuApi productSkuApi;
@Resource
private BrokerageRecordService brokerageRecordService;
@Resource
private BrokerageUserService brokerageUserService;
@Override
public void beforeOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 设置订单推广人
BrokerageUserDO brokerageUser = brokerageUserService.getBrokerageUser(order.getUserId());
if (brokerageUser != null && brokerageUser.getBindUserId() != null) {
order.setBrokerageUserId(brokerageUser.getBindUserId());
}
}
@Override
public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (order.getBrokerageUserId() != null) {
addBrokerage(order.getUserId(), orderItems);
}
}
@Override
public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// TODO 芋艿取消支付时需要处理下
}
@Override
public void afterCancelOrderItem(TradeOrderDO order, TradeOrderItemDO orderItem) {
if (order.getBrokerageUserId() != null) {
cancelBrokerage(order.getId(), orderItem.getOrderId());
}
}
/**
* 创建分销记录
* <p>
* 目前是支付成功后就会创建分销记录
* <p>
* 业内还有两种做法可以根据自己的业务调整
* 1. 确认收货后才创建分销记录
* 2. 支付 or 下单成功时创建分销记录冻结确认收货解冻或者 n 天后解冻
*
* @param userId 用户编号
* @param orderItems 订单项
*/
protected void addBrokerage(Long userId, List<TradeOrderItemDO> orderItems) {
MemberUserRespDTO user = memberUserApi.getUser(userId);
Assert.notNull(user);
ProductSpuRespDTO spu = productSpuApi.getSpu(orderItems.get(0).getSpuId());
Assert.notNull(spu);
ProductSkuRespDTO sku = productSkuApi.getSku(orderItems.get(0).getSkuId());
// 每一个订单项都会去生成分销记录
List<BrokerageAddReqBO> addList = convertList(orderItems,
item -> TradeOrderConvert.INSTANCE.convert(user, item, spu, sku));
brokerageRecordService.addBrokerage(userId, BrokerageRecordBizTypeEnum.ORDER, addList);
}
protected void cancelBrokerage(Long userId, Long orderItemId) {
brokerageRecordService.cancelBrokerage(userId, BrokerageRecordBizTypeEnum.ORDER, String.valueOf(orderItemId));
}
}

View File

@ -11,13 +11,16 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.ORDER_DELIVERY_FAIL_COMBINATION_RECORD_STATUS_NOT_SUCCESS;
/**
* 拼团订单 handler 接口实现类
* 拼团订单 {@link TradeOrderHandler} 实现类
*
* @author HUIHUI
*/
@Component
public class TradeCombinationHandler implements TradeOrderHandler {
public class TradeCombinationOrderHandler implements TradeOrderHandler {
@Resource
private CombinationRecordApi combinationRecordApi;
@ -25,7 +28,7 @@ public class TradeCombinationHandler implements TradeOrderHandler {
@Override
public void beforeOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 如果不是拼团订单则结束
if (TradeOrderTypeEnum.isCombination(order.getType())) {
if (!TradeOrderTypeEnum.isCombination(order.getType())) {
return;
}
Assert.isTrue(orderItems.size() == 1, "拼团时,只允许选择一个商品");
@ -40,22 +43,26 @@ public class TradeCombinationHandler implements TradeOrderHandler {
@Override
public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 如果不是拼团订单则结束
if (TradeOrderTypeEnum.isCombination(order.getType())) {
if (!TradeOrderTypeEnum.isCombination(order.getType())) {
return;
}
Assert.isTrue(orderItems.size() == 1, "拼团时,只允许选择一个商品");
// 获取商品信息
TradeOrderItemDO item = orderItems.get(0);
// 创建拼团记录
TradeOrderItemDO item = orderItems.get(0);
combinationRecordApi.createCombinationRecord(TradeOrderConvert.INSTANCE.convert(order, item));
}
@Override
public void cancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (TradeOrderTypeEnum.isCombination(order.getType())) {
public void beforeDeliveryOrder(TradeOrderDO order) {
if (!TradeOrderTypeEnum.isCombination(order.getType())) {
return;
}
// 校验订单拼团是否成功
if (!combinationRecordApi.isCombinationRecordSuccess(order.getUserId(), order.getId())) {
throw exception(ORDER_DELIVERY_FAIL_COMBINATION_RECORD_STATUS_NOT_SUCCESS);
}
}
}

View File

@ -0,0 +1,42 @@
package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.iocoder.yudao.module.promotion.api.coupon.CouponApi;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponUseReqDTO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* 优惠劵的 {@link TradeOrderHandler} 实现类
*
* @author 芋道源码
*/
@Component
public class TradeCouponOrderHandler implements TradeOrderHandler {
@Resource
private CouponApi couponApi;
@Override
public void afterOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (order.getCouponId() == null || order.getCouponId() <= 0) {
return;
}
// 不在前置扣减的原因是因为优惠劵要记录使用的订单号
couponApi.useCoupon(new CouponUseReqDTO().setId(order.getCouponId()).setUserId(order.getUserId())
.setOrderId(order.getId()));
}
@Override
public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (order.getCouponId() == null || order.getCouponId() <= 0) {
return;
}
// 退回优惠劵
couponApi.returnUsedCoupon(order.getCouponId());
}
}

View File

@ -0,0 +1,98 @@
package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.iocoder.yudao.module.member.api.level.MemberLevelApi;
import cn.iocoder.yudao.module.member.api.point.MemberPointApi;
import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.trade.dal.dataobject.aftersale.AfterSaleDO;
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.service.aftersale.AfterSaleService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* 会员积分等级的 {@link TradeOrderHandler} 实现类
*
* @author owen
*/
@Component
public class TradeMemberPointOrderHandler implements TradeOrderHandler {
@Resource
private MemberPointApi memberPointApi;
@Resource
private MemberLevelApi memberLevelApi;
@Resource
private AfterSaleService afterSaleService;
@Override
public void afterOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 扣减用户积分订单抵扣不在前置扣减的原因是因为积分扣减时需要记录关联业务
reducePoint(order.getUserId(), order.getUsePoint(), MemberPointBizTypeEnum.ORDER_USE, order.getId());
}
@Override
public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 增加用户积分订单赠送
addPoint(order.getUserId(), order.getGivePoint(), MemberPointBizTypeEnum.ORDER_GIVE,
order.getId());
// 增加用户经验
memberLevelApi.addExperience(order.getUserId(), order.getPayPrice(), MemberExperienceBizTypeEnum.ORDER.getType(),
String.valueOf(order.getId()));
}
@Override
public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// 扣减回滚用户积分订单抵扣
addPoint(order.getUserId(), order.getUsePoint(), MemberPointBizTypeEnum.ORDER_CANCEL,
order.getId());
// TODO 芋艿需要校验如果部分子订单已经售后退款则不进行整单退因为已经退了一部分积分了
}
@Override
public void afterCancelOrderItem(TradeOrderDO order, TradeOrderItemDO orderItem) {
// 扣减回滚积分订单赠送
reducePoint(order.getUserId(), orderItem.getGivePoint(), MemberPointBizTypeEnum.AFTER_SALE_DEDUCT_GIVE,
orderItem.getAfterSaleId());
// 扣减回滚积分增加用户积分返还抵扣
addPoint(order.getUserId(), orderItem.getUsePoint(), MemberPointBizTypeEnum.AFTER_SALE_REFUND_USED,
orderItem.getAfterSaleId());
// 扣减回滚用户经验
AfterSaleDO afterSale = afterSaleService.getAfterSale(orderItem.getAfterSaleId());
memberLevelApi.addExperience(order.getUserId(), -afterSale.getRefundPrice(), MemberExperienceBizTypeEnum.REFUND.getType(),
String.valueOf(orderItem.getAfterSaleId()));
}
/**
* 添加用户积分
* <p>
* 目前是支付成功后就会创建积分记录
* <p>
* 业内还有两种做法可以根据自己的业务调整
* 1. 确认收货后才创建积分记录
* 2. 支付 or 下单成功时创建积分记录冻结确认收货解冻或者 n 天后解冻
*
* @param userId 用户编号
* @param point 增加积分数量
* @param bizType 业务编号
* @param bizId 业务编号
*/
protected void addPoint(Long userId, Integer point, MemberPointBizTypeEnum bizType, Long bizId) {
if (point != null && point > 0) {
memberPointApi.addPoint(userId, point, bizType.getType(), String.valueOf(bizId));
}
}
protected void reducePoint(Long userId, Integer point, MemberPointBizTypeEnum bizType, Long bizId) {
if (point != null && point > 0) {
memberPointApi.reducePoint(userId, point, bizType.getType(), String.valueOf(bizId));
}
}
}

View File

@ -35,16 +35,29 @@ public interface TradeOrderHandler {
* @param order 订单
* @param orderItems 订单项
*/
default void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
}
default void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {}
/**
* 订单取消
* 订单取消
*
* @param order 订单
* @param orderItems 订单项
*/
default void cancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
}
default void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {}
/**
* 订单项取消后
*
* @param order 订单
* @param orderItem 订单项
*/
default void afterCancelOrderItem(TradeOrderDO order, TradeOrderItemDO orderItem) {}
/**
* 订单发货前
*
* @param order 订单
*/
default void beforeDeliveryOrder(TradeOrderDO order) {}
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.yudao.module.trade.service.order.handler;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
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.TradeOrderItemDO;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import static java.util.Collections.singletonList;
/**
* 商品 SKU 库存的 {@link TradeOrderHandler} 实现类
*
* @author 芋道源码
*/
@Component
public class TradeProductSkuOrderHandler implements TradeOrderHandler {
@Resource
private ProductSkuApi productSkuApi;
@Override
public void beforeOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convertNegative(orderItems));
}
@Override
public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
// TODO 芋艿如果部分售后最后导致取消这里需要过滤下
productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(orderItems));
}
@Override
public void afterCancelOrderItem(TradeOrderDO order, TradeOrderItemDO orderItem) {
productSkuApi.updateSkuStock(TradeOrderConvert.INSTANCE.convert(singletonList(orderItem)));
}
}

View File

@ -10,19 +10,19 @@ import javax.annotation.Resource;
import java.util.List;
/**
* 秒杀订单 handler 实现类
* 秒杀订单 {@link TradeOrderHandler} 实现类
*
* @author HUIHUI
*/
@Component
public class TradeSeckillHandler implements TradeOrderHandler {
public class TradeSeckillOrderHandler implements TradeOrderHandler {
@Resource
private SeckillActivityApi seckillActivityApi;
@Override
public void beforeOrderCreate(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (TradeOrderTypeEnum.isSeckill(order.getType())) {
if (!TradeOrderTypeEnum.isSeckill(order.getType())) {
return;
}
@ -31,12 +31,4 @@ public class TradeSeckillHandler implements TradeOrderHandler {
orderItems.get(0).getSkuId(), orderItems.get(0).getCount());
}
@Override
public void cancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (TradeOrderTypeEnum.isSeckill(order.getType())) {
return;
}
}
}

View File

@ -18,6 +18,7 @@ public interface MemberLevelApi {
*/
MemberLevelRespDTO getMemberLevel(Long id);
// TODO 芋艿后续增加一个减少接口
/**
* 增加会员经验
*