|
@@ -258,6 +258,7 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Boolean splitAssign(OrderSplitAssignBo bo) {
|
|
public Boolean splitAssign(OrderSplitAssignBo bo) {
|
|
|
Long parentId = bo.getOrderId();
|
|
Long parentId = bo.getOrderId();
|
|
|
|
|
+ String platform = PlatformContext.getPlatform(); //当前平台标识
|
|
|
List<OrderProductAssignRule> rules = bo.getItemRules();
|
|
List<OrderProductAssignRule> rules = bo.getItemRules();
|
|
|
|
|
|
|
|
if (CollUtil.isEmpty(rules)) {
|
|
if (CollUtil.isEmpty(rules)) {
|
|
@@ -272,11 +273,14 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
if (parentOrder == null) {
|
|
if (parentOrder == null) {
|
|
|
throw new ServiceException("父订单不存在");
|
|
throw new ServiceException("父订单不存在");
|
|
|
}
|
|
}
|
|
|
- /* if (OrderSplitStatus.SPLITED.getCode().equals(parentOrder.getSplitStatus())) {
|
|
|
|
|
- throw new ServiceException("该订单已被拆分,不可重复操作");
|
|
|
|
|
- }*/
|
|
|
|
|
|
|
|
|
|
- // 2. 验证商品行(必须属于该主订单且未分配)
|
|
|
|
|
|
|
+ // 1.1 校验订单层级:只有1级和2级订单可以继续拆分,3级订单不允许再拆分
|
|
|
|
|
+ Integer parentLevel = parentOrder.getCurrentLevel() != null ? parentOrder.getCurrentLevel() : 1;
|
|
|
|
|
+ if (parentLevel >= 3) {
|
|
|
|
|
+ throw new ServiceException("3级订单不允许再次拆分分配");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 验证商品行(必须属于该订单且未分配)
|
|
|
List<Long> itemIds = rules.stream().map(OrderProductAssignRule::getItemId).collect(Collectors.toList());
|
|
List<Long> itemIds = rules.stream().map(OrderProductAssignRule::getItemId).collect(Collectors.toList());
|
|
|
List<OrderProduct> items = orderProductMapper.selectBatchIds(itemIds);
|
|
List<OrderProduct> items = orderProductMapper.selectBatchIds(itemIds);
|
|
|
Map<Long, OrderProduct> itemMap = items.stream()
|
|
Map<Long, OrderProduct> itemMap = items.stream()
|
|
@@ -291,11 +295,20 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
throw new ServiceException("商品 [" + item.getProductName() + "] 已分配,不可重复操作");
|
|
throw new ServiceException("商品 [" + item.getProductName() + "] 已分配,不可重复操作");
|
|
|
}
|
|
}
|
|
|
String type = rule.getAssigneeType();
|
|
String type = rule.getAssigneeType();
|
|
|
|
|
+ // 支持多种分配类型:srm(供应商)、bp(伙伴商)、zy(自营)、mkt(平台)
|
|
|
if (!AssigneeTypeConstants.SUPPLIER.getCode().equals(type) &&
|
|
if (!AssigneeTypeConstants.SUPPLIER.getCode().equals(type) &&
|
|
|
!AssigneeTypeConstants.PARTNER.getCode().equals(type) &&
|
|
!AssigneeTypeConstants.PARTNER.getCode().equals(type) &&
|
|
|
- !AssigneeTypeConstants.CUSTOMER.getCode().equals(type)) {
|
|
|
|
|
|
|
+ !AssigneeTypeConstants.CUSTOMER.getCode().equals(type) &&
|
|
|
|
|
+ !AssigneeTypeConstants.MKT.getCode().equals(type)) {
|
|
|
throw new ServiceException("分配对象类型不支持: " + type);
|
|
throw new ServiceException("分配对象类型不支持: " + type);
|
|
|
}
|
|
}
|
|
|
|
|
+ // 校验:srm和bp必须有assigneeId,zy和mkt不能有assigneeId
|
|
|
|
|
+ if (("srm".equals(type) || "bp".equals(type)) && rule.getAssigneeId() == null) {
|
|
|
|
|
+ throw new ServiceException("分配类型为 [" + type + "] 时,必须指定分配对象ID");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (("zy".equals(type) || "mkt".equals(type)) && rule.getAssigneeId() != null) {
|
|
|
|
|
+ throw new ServiceException("分配类型为 [" + type + "] 时,不能指定分配对象ID");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 3. 按 (assigneeId, assigneeType) 分组
|
|
// 3. 按 (assigneeId, assigneeType) 分组
|
|
@@ -333,8 +346,8 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 创建子订单(从主订单拷贝必要字段)
|
|
|
|
|
- OrderMain childOrder = copyParentToChild(parentOrder, key.id(), key.type());
|
|
|
|
|
|
|
+ // 创建子订单(从父订单拷贝必要字段,并设置正确的层级关系)
|
|
|
|
|
+ OrderMain childOrder = copyParentToChild(parentOrder, key.id(), key.type(), parentLevel);
|
|
|
|
|
|
|
|
// 用刚才累加的正确数量,覆盖掉从父订单继承的数量
|
|
// 用刚才累加的正确数量,覆盖掉从父订单继承的数量
|
|
|
childOrder.setProductQuantity(childTotalQuantity);
|
|
childOrder.setProductQuantity(childTotalQuantity);
|
|
@@ -351,7 +364,7 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
for (OrderProductAssignRule rule : groupRules) {
|
|
for (OrderProductAssignRule rule : groupRules) {
|
|
|
OrderProduct parentItem = itemMap.get(rule.getItemId());
|
|
OrderProduct parentItem = itemMap.get(rule.getItemId());
|
|
|
|
|
|
|
|
- // 1. 收集主订单商品ID,用于后续批量更新分配状态
|
|
|
|
|
|
|
+ // 1. 收集父订单商品ID,用于后续批量更新分配状态
|
|
|
parentItemIdsToUpdate.add(parentItem.getId());
|
|
parentItemIdsToUpdate.add(parentItem.getId());
|
|
|
|
|
|
|
|
// 2. 创建子订单商品副本
|
|
// 2. 创建子订单商品副本
|
|
@@ -362,7 +375,15 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
|
|
|
|
|
// 关联信息
|
|
// 关联信息
|
|
|
childProduct.setOrderId(childOrderId); // 归属子订单
|
|
childProduct.setOrderId(childOrderId); // 归属子订单
|
|
|
- childProduct.setOriginalItemId(parentItem.getId()); // 指向主订单商品
|
|
|
|
|
|
|
+ childProduct.setParentItemId(parentItem.getId()); // 指向上一级商品ID
|
|
|
|
|
+
|
|
|
|
|
+ // 设置根商品ID:如果父商品已有rootItemId则继承,否则父商品本身就是根商品
|
|
|
|
|
+ if (parentItem.getRootItemId() != null) {
|
|
|
|
|
+ childProduct.setRootItemId(parentItem.getRootItemId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 父商品是1级商品,它自己就是根
|
|
|
|
|
+ childProduct.setRootItemId(parentItem.getId());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// 订单基础信息
|
|
// 订单基础信息
|
|
|
childProduct.setOrderNo(childOrder.getOrderNo()); // 子订单编号
|
|
childProduct.setOrderNo(childOrder.getOrderNo()); // 子订单编号
|
|
@@ -395,7 +416,17 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
childProduct.setReturnAmount(BigDecimal.ZERO);
|
|
childProduct.setReturnAmount(BigDecimal.ZERO);
|
|
|
|
|
|
|
|
// 状态与标志
|
|
// 状态与标志
|
|
|
- childProduct.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode()); // 已分配
|
|
|
|
|
|
|
+ childProduct.setCurrentLevel(parentLevel + 1); // 子订单商品层级 = 父层级 + 1
|
|
|
|
|
+
|
|
|
|
|
+ // 根据分配对象类型设置商品分配状态
|
|
|
|
|
+ String assigneeType = key.type();
|
|
|
|
|
+ if ("mkt".equals(assigneeType)) {
|
|
|
|
|
+ // 分配给平台(mkt):商品保持待分配状态,以便在market平台再次分配
|
|
|
|
|
+ childProduct.setAssignmentStatus(OrderAssignStatus.WAIT_ASSIGN.getCode());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 分配给其他对象(srm/bp/zy):商品标记为已分配
|
|
|
|
|
+ childProduct.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// 时间 & 其他
|
|
// 时间 & 其他
|
|
|
childProduct.setPreDeliveryDate(parentItem.getPreDeliveryDate());
|
|
childProduct.setPreDeliveryDate(parentItem.getPreDeliveryDate());
|
|
@@ -409,7 +440,7 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
orderProductMapper.insertBatch(childProducts);
|
|
orderProductMapper.insertBatch(childProducts);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 批量更新主订单商品:标记已分配 + 记录分配到的子订单ID
|
|
|
|
|
|
|
+ // 批量更新父订单商品:标记已分配 + 记录分配到的子订单ID
|
|
|
if (!parentItemIdsToUpdate.isEmpty()) {
|
|
if (!parentItemIdsToUpdate.isEmpty()) {
|
|
|
orderProductMapper.updateAssignmentInfo(
|
|
orderProductMapper.updateAssignmentInfo(
|
|
|
parentItemIdsToUpdate,
|
|
parentItemIdsToUpdate,
|
|
@@ -492,12 +523,48 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- private OrderMain copyParentToChild(OrderMain parent, Long assigneeId, String assigneeType) {
|
|
|
|
|
|
|
+ private OrderMain copyParentToChild(OrderMain parent, Long assigneeId, String assigneeType, Integer parentLevel) {
|
|
|
OrderMain child = new OrderMain();
|
|
OrderMain child = new OrderMain();
|
|
|
|
|
|
|
|
- child.setParentOrderId(parent.getId());
|
|
|
|
|
- child.setIsSplitChild("0"); // 0=是子订单
|
|
|
|
|
|
|
+ // 设置子订单编号
|
|
|
child.setOrderNo(SequenceUtils.generateOrderCode("ZD"));
|
|
child.setOrderNo(SequenceUtils.generateOrderCode("ZD"));
|
|
|
|
|
+
|
|
|
|
|
+ // 计算子订单层级和订单类型
|
|
|
|
|
+ Integer childLevel;
|
|
|
|
|
+ String orderType = parent.getOrderType(); // 继承父订单的订单类型
|
|
|
|
|
+
|
|
|
|
|
+ // 根据订单类型和分配对象决定子订单层级
|
|
|
|
|
+ if ("1".equals(orderType)) {
|
|
|
|
|
+ // 项目订单 (orderType=1) - DMS平台分配
|
|
|
|
|
+ // 分配给 mkt 或 bp 都生成2级订单
|
|
|
|
|
+ childLevel = parentLevel + 1; // 通常是 1 -> 2
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 自营订单 (orderType=0) - Market平台分配
|
|
|
|
|
+ // 分配给 zy 或 srm 都生成3级订单(如果是从1级直接分配)或下一级
|
|
|
|
|
+ childLevel = parentLevel + 1; // 通常是 2 -> 3
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ child.setCurrentLevel(childLevel);
|
|
|
|
|
+
|
|
|
|
|
+ // 根据父订单层级设置不同的父子关系字段
|
|
|
|
|
+ if (parentLevel == 1) {
|
|
|
|
|
+ // 父订单是1级,子订单是2级
|
|
|
|
|
+ // 2级订单:parentOrderId/parentOrderNo 指向1级订单,subOrderId/subOrderNo 为空
|
|
|
|
|
+ child.setParentOrderId(parent.getId());
|
|
|
|
|
+ child.setParentOrderNo(parent.getOrderNo());
|
|
|
|
|
+ child.setSubOrderId(null);
|
|
|
|
|
+ child.setSubOrderNo(null);
|
|
|
|
|
+ } else if (parentLevel == 2) {
|
|
|
|
|
+ // 父订单是2级,子订单是3级
|
|
|
|
|
+ // 3级订单:parentOrderId/parentOrderNo 指向1级订单,subOrderId/subOrderNo 指向2级订单
|
|
|
|
|
+ child.setParentOrderId(parent.getParentOrderId()); // 继承1级订单ID
|
|
|
|
|
+ child.setParentOrderNo(parent.getParentOrderNo()); // 继承1级订单编号
|
|
|
|
|
+ child.setSubOrderId(parent.getId()); // 指向2级订单(当前父订单)
|
|
|
|
|
+ child.setSubOrderNo(parent.getOrderNo()); // 指向2级订单编号
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 复制其他基础字段
|
|
|
|
|
+ child.setIsSplitChild("0"); // 0=是子订单
|
|
|
child.setCompanyId(parent.getCompanyId());
|
|
child.setCompanyId(parent.getCompanyId());
|
|
|
child.setCustomerId(parent.getCustomerId());
|
|
child.setCustomerId(parent.getCustomerId());
|
|
|
child.setCustomerCode(parent.getCustomerCode());
|
|
child.setCustomerCode(parent.getCustomerCode());
|
|
@@ -520,7 +587,16 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
child.setOrderSource(parent.getOrderSource());
|
|
child.setOrderSource(parent.getOrderSource());
|
|
|
child.setProductQuantity(parent.getProductQuantity());
|
|
child.setProductQuantity(parent.getProductQuantity());
|
|
|
child.setQuantityShipped(parent.getQuantityShipped());
|
|
child.setQuantityShipped(parent.getQuantityShipped());
|
|
|
- child.setOrderStatus(OrderStatus.PENDING_CONFIRMATION.getCode()); // 子订单状态初始化为待确认 被分配者需要重新确认
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 根据分配对象类型设置订单状态
|
|
|
|
|
+ if ("zy".equals(assigneeType) || "mkt".equals(assigneeType)) {
|
|
|
|
|
+ child.setOrderStatus(OrderStatus.PENDING_SHIPMENT.getCode()); // 自营:待发货,不需要确认
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // srm 或 bp:待确认
|
|
|
|
|
+ child.setOrderStatus(OrderStatus.PENDING_CONFIRMATION.getCode());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
child.setOrderTime(parent.getOrderTime());
|
|
child.setOrderTime(parent.getOrderTime());
|
|
|
child.setConfirmTime(parent.getConfirmTime());
|
|
child.setConfirmTime(parent.getConfirmTime());
|
|
|
child.setDeliveryType(parent.getDeliveryType());
|
|
child.setDeliveryType(parent.getDeliveryType());
|
|
@@ -532,13 +608,50 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
child.setCreateBy(parent.getCreateBy());
|
|
child.setCreateBy(parent.getCreateBy());
|
|
|
child.setCreateDept(parent.getCreateDept());
|
|
child.setCreateDept(parent.getCreateDept());
|
|
|
child.setPaymentStatus(parent.getPaymentStatus());
|
|
child.setPaymentStatus(parent.getPaymentStatus());
|
|
|
|
|
+ child.setOrderType(parent.getOrderType());
|
|
|
// 新增分配字段
|
|
// 新增分配字段
|
|
|
child.setAssigneeId(assigneeId);
|
|
child.setAssigneeId(assigneeId);
|
|
|
child.setAssigneeType(assigneeType);
|
|
child.setAssigneeType(assigneeType);
|
|
|
|
|
|
|
|
// 子订单状态初始化
|
|
// 子订单状态初始化
|
|
|
- child.setSplitStatus(OrderSplitStatus.WAIT_SPLIT.getCode()); // 子订单默认未拆(通常不再拆)
|
|
|
|
|
- child.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode()); // 已分配
|
|
|
|
|
|
|
+ child.setSplitStatus(OrderSplitStatus.WAIT_SPLIT.getCode()); // 子订单默认未拆
|
|
|
|
|
+
|
|
|
|
|
+ // 根据订单类型、分配对象和层级设置分配状态
|
|
|
|
|
+ if ("1".equals(orderType)) {
|
|
|
|
|
+ // 项目订单 (DMS平台分配)
|
|
|
|
|
+ if ("mkt".equals(assigneeType)) {
|
|
|
|
|
+ // 分配给平台(mkt):2级订单,待分配状态,可在market再次分配
|
|
|
|
|
+ child.setAssignmentStatus(OrderAssignStatus.WAIT_ASSIGN.getCode());
|
|
|
|
|
+ } else if ("bp".equals(assigneeType)) {
|
|
|
|
|
+ // 分配给伙伴商(bp):2级订单,已分配状态
|
|
|
|
|
+ child.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode());
|
|
|
|
|
+ /*
|
|
|
|
|
+ * 生成发货单号*/
|
|
|
|
|
+ child.setShipmentNo(SequenceUtils.generateOrderCode("YO"));
|
|
|
|
|
+ } else if ("zy".equals(assigneeType) || "srm".equals(assigneeType)) {
|
|
|
|
|
+ // 3级订单:已分配状态,不可再分配
|
|
|
|
|
+ child.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode());
|
|
|
|
|
+ /*
|
|
|
|
|
+ * 生成发货单号*/
|
|
|
|
|
+ child.setShipmentNo(SequenceUtils.generateOrderCode("YO"));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 其他情况:2级订单默认待分配
|
|
|
|
|
+ child.setAssignmentStatus(OrderAssignStatus.WAIT_ASSIGN.getCode());
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 自营订单 (Market平台分配)
|
|
|
|
|
+ if ("zy".equals(assigneeType) || "srm".equals(assigneeType)) {
|
|
|
|
|
+ // 2级订单:已分配状态,不可再分配
|
|
|
|
|
+ child.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode());
|
|
|
|
|
+ /*
|
|
|
|
|
+ * 生成发货单号*/
|
|
|
|
|
+ child.setShipmentNo(SequenceUtils.generateOrderCode("YO"));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 其他层级:待分配
|
|
|
|
|
+ child.setAssignmentStatus(OrderAssignStatus.WAIT_ASSIGN.getCode());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
return child;
|
|
return child;
|
|
|
}
|
|
}
|
|
|
|
|
|