Prechádzať zdrojové kódy

feat(order): 添加市场平台订单自营商品自动拆分功能

- 在CustomerInfoServiceImpl中增加市场平台数据权限控制
- 在OrderMainServiceImpl中引入产品服务远程调用依赖
- 实现市场平台订单确认时的自营商品识别和拆分逻辑
- 添加订单商品按自营属性自动分配到不同子订单的规则
- 集成订单分配服务实现拆单功能
- 优化商品详情查询方式避免循环查库
- 增加拆分日志记录便于问题追踪
hurx 2 týždňov pred
rodič
commit
3eb18310d3

+ 13 - 0
ruoyi-modules/ruoyi-customer/src/main/java/org/dromara/customer/service/impl/CustomerInfoServiceImpl.java

@@ -743,6 +743,19 @@ public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, Cus
         Map<String, Object> params = bo.getParams();
         LambdaQueryWrapper<CustomerInfo> lqw = Wrappers.lambdaQuery();
         lqw.orderByDesc(CustomerInfo::getId);
+        String platformCode = PlatformContext.getPlatform();
+        if (ObjectUtils.isNotEmpty(platformCode) && "market".equals(platformCode)) {
+            Long userId = LoginHelper.getLoginUser().getUserId();
+            if (null != userId) {
+                lqw.and(wrapper -> wrapper
+                    .eq(CustomerInfo::getCreateBy, userId)
+                    .or()
+                    .eq(CustomerInfo::getCreateBy, 1L)
+                    .or()
+                    .eq(CustomerInfo::getCreateBy, -1L)
+                );
+            }
+        }
         lqw.eq(StringUtils.isNotBlank(bo.getCustomerNo()), CustomerInfo::getCustomerNo, bo.getCustomerNo());
         lqw.eq(bo.getBelongCompanyId() != null, CustomerInfo::getBelongCompanyId, bo.getBelongCompanyId());
         lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), CustomerInfo::getCompanyName, bo.getCompanyName());

+ 71 - 4
ruoyi-modules/ruoyi-order/src/main/java/org/dromara/order/service/impl/OrderMainServiceImpl.java

@@ -33,12 +33,10 @@ import org.dromara.external.api.zhongche.RemoteZhongChePullService;
 import org.dromara.external.api.zhongche.domain.bo.OrderConfirmBo;
 import org.dromara.external.api.zhongche.domain.bo.OrderRejectBo;
 import org.dromara.external.api.zhongche.domain.vo.GoodsUpdateVo;
-import org.dromara.order.domain.OrderDeliver;
-import org.dromara.order.domain.OrderMain;
-import org.dromara.order.domain.OrderMainCrrcExt;
-import org.dromara.order.domain.OrderProduct;
+import org.dromara.order.domain.*;
 import org.dromara.order.domain.bo.OrderMainBo;
 import org.dromara.order.domain.bo.OrderProductBo;
+import org.dromara.order.domain.bo.OrderSplitAssignBo;
 import org.dromara.order.domain.bo.PcSubmitOrderBo;
 import org.dromara.order.domain.dto.AssignmentStatsDto;
 import org.dromara.order.domain.vo.*;
@@ -48,7 +46,9 @@ import org.dromara.order.utils.kd100.Kd100Util;
 import org.dromara.order.utils.kd100.domain.QueryTrackDTO;
 import org.dromara.order.utils.kd100.domain.TrackData;
 import org.dromara.order.utils.kd100.domain.TrackVO;
+import org.dromara.product.api.RemoteProductService;
 import org.dromara.product.api.RemoteProductShoppingCartService;
+import org.dromara.product.api.domain.ProductVo;
 import org.dromara.system.api.*;
 import org.dromara.system.api.model.LoginUser;
 import org.mybatis.spring.MyBatisSystemException;
@@ -110,6 +110,9 @@ public class OrderMainServiceImpl extends ServiceImpl<OrderMainMapper, OrderMain
     @DubboReference
     private RemoteProductShoppingCartService remoteProductShoppingCartService;
 
+    @DubboReference
+    private RemoteProductService remoteProductService;
+
     private final IOrderCustomerFlowLinkService orderCustomerFlowLinkService;
 
     private final IOrderCustomerFlowNodeLinkService orderCustomerFlowNodeLinkService;
@@ -136,6 +139,8 @@ public class OrderMainServiceImpl extends ServiceImpl<OrderMainMapper, OrderMain
     @DubboReference
     private final RemoteZhongChePullService zhongChePullService;
 
+    private final IOrderAssignmentService orderAssignmentService;
+
     /**
      * 查询订单主信息
      *
@@ -698,6 +703,68 @@ public class OrderMainServiceImpl extends ServiceImpl<OrderMainMapper, OrderMain
         if (existingOrder == null) {
             throw new ServiceException("订单不存在");
         }
+        String platformCode = PlatformContext.getPlatform();
+        // 判断是否为 market 平台,且是未拆分的子订单
+        if (ObjectUtils.isNotEmpty(platformCode)
+            && "market".equals(platformCode)
+            && SysPlatformYesNo.NO.getCode().equals(existingOrder.getIsSplitChild())
+            // 确保订单还未被拆分过,防止重复拆分
+        ) {
+
+            log.info("Market平台订单确认,开始处理自营商品拆分,订单ID: {}", orderId);
+            // 1. 提取所有订单商品ID
+            List<Long> orderProductIds = updatedProducts.stream()
+                .map(OrderProductBo::getId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList());
+
+            List<OrderProductVo> orderProductVoList = orderProductMapper.selectVoByIds(orderProductIds);
+            // 1. 提取所有商品ID
+            List<Long> productIds = orderProductVoList.stream()
+                .map(OrderProductVo::getProductId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList());
+
+            if (CollUtil.isNotEmpty(productIds)) {
+                // 2. 一次性获取所有商品详情 (避免循环查库)
+                List<ProductVo> productDetails = remoteProductService.getProductDetails(productIds);
+
+                // 3. 构建 自营商品ID -> ProductVo 的映射
+                // 假设 getIsSelf() 返回 Integer, 1 代表自营
+                Map<Long, ProductVo> selfProductMap = productDetails.stream()
+                    .filter(p -> Integer.valueOf(1).equals(p.getIsSelf()))
+                    .collect(Collectors.toMap(ProductVo::getId, Function.identity()));
+
+                // 4. 如果存在自营商品,则构建分配规则
+                if (!selfProductMap.isEmpty()) {
+                    OrderSplitAssignBo assignBo = new OrderSplitAssignBo();
+                    assignBo.setOrderId(orderId);
+                    assignBo.setRemark("Market平台自动拆分自营订单");
+
+                    List<OrderProductAssignRule> rules = new ArrayList<>();
+
+                    for (OrderProductVo bo : orderProductVoList) {
+                        Long productId = bo.getProductId();
+                        // 如果该商品是自营商品
+                        if (selfProductMap.containsKey(productId)) {
+                            OrderProductAssignRule rule = new OrderProductAssignRule();
+                            rule.setItemId(bo.getId()); // 设置订单商品行ID
+                            // 假设自营商品分配给特定的供应商ID,或者设置为 CUSTOMER 类型
+                            rule.setAssigneeId(null);
+                            rule.setAssigneeType(AssigneeTypeConstants.CUSTOMER.getCode());
+                            rules.add(rule);
+                        }
+                    }
+
+                    if (!rules.isEmpty()) {
+                        assignBo.setItemRules(rules);
+                        // 调用分配服务进行拆单
+                        orderAssignmentService.splitAssign(assignBo);
+                        log.info("订单 {} 的自营商品已成功拆分为新子单", orderId);
+                    }
+                }
+            }
+        }
 
         // 2. 检查当前状态:只允许从“待确认”(比如状态0或1)变为“待发货”(2)
         if (!OrderStatus.PENDING_PAYMENT.getCode().equals(existingOrder.getOrderStatus()) && !OrderStatus.PENDING_CONFIRMATION.getCode().equals(existingOrder.getOrderStatus())) {