|
|
@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.dromara.common.core.context.PlatformContext;
|
|
|
+import org.dromara.common.core.enums.AssigneeTypeConstants;
|
|
|
import org.dromara.common.core.enums.OrderAssignStatus;
|
|
|
import org.dromara.common.core.enums.OrderSplitStatus;
|
|
|
import org.dromara.common.core.enums.SysPlatformCode;
|
|
|
@@ -207,72 +208,92 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
if (parentOrder == null) {
|
|
|
throw new ServiceException("父订单不存在");
|
|
|
}
|
|
|
- if (OrderSplitStatus.SPLITED.getCode().equals(parentOrder.getSplitStatus())) {
|
|
|
+ if ("1".equals(parentOrder.getSplitStatus())) { // 已拆分
|
|
|
throw new ServiceException("该订单已被拆分,不可重复操作");
|
|
|
}
|
|
|
|
|
|
- // 2. 查询所有待分配的商品行(验证存在性 & 所属订单)
|
|
|
+ // 2. 验证商品行
|
|
|
List<Long> itemIds = rules.stream().map(OrderProductAssignRule::getItemId).collect(Collectors.toList());
|
|
|
List<OrderProduct> items = orderProductMapper.selectBatchIds(itemIds);
|
|
|
Map<Long, OrderProduct> itemMap = items.stream()
|
|
|
.collect(Collectors.toMap(OrderProduct::getId, Function.identity()));
|
|
|
|
|
|
- // 验证:所有商品属于该父订单,且未分配
|
|
|
for (OrderProductAssignRule rule : rules) {
|
|
|
OrderProduct item = itemMap.get(rule.getItemId());
|
|
|
if (item == null || !parentId.equals(item.getOrderId())) {
|
|
|
throw new ServiceException("商品行不属于该订单或不存在");
|
|
|
}
|
|
|
- if (OrderAssignStatus.ASSIGNED.getCode().equals(item.getAssignmentStatus())) {
|
|
|
+ if ("1".equals(item.getAssignmentStatus())) { // 已分配
|
|
|
throw new ServiceException("商品 [" + item.getProductName() + "] 已分配,不可重复操作");
|
|
|
}
|
|
|
+ // 校验 assigneeType 合法性
|
|
|
+ String type = rule.getAssigneeType();
|
|
|
+ if (!AssigneeTypeConstants.SUPPLIER.getCode().equals(type) && !AssigneeTypeConstants.PARTNER.getCode().equals(type)) {
|
|
|
+ throw new ServiceException("分配对象类型不支持: " + type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 按 (assigneeId, assigneeType) 分组
|
|
|
+ record AssigneeKey(Long id, String type) {
|
|
|
}
|
|
|
+ Map<AssigneeKey, List<OrderProductAssignRule>> assigneeGroups = rules.stream()
|
|
|
+ .collect(Collectors.groupingBy(rule -> new AssigneeKey(rule.getAssigneeId(), rule.getAssigneeType())));
|
|
|
|
|
|
- // 3. 按目标平台分组
|
|
|
- Map<String, List<OrderProductAssignRule>> platformToRules = rules.stream()
|
|
|
- .collect(Collectors.groupingBy(OrderProductAssignRule::getTargetPlatform));
|
|
|
- OrderMain childOrder = null;
|
|
|
- // 4. 为每个平台创建子订单,并迁移商品
|
|
|
- for (Map.Entry<String, List<OrderProductAssignRule>> entry : platformToRules.entrySet()) {
|
|
|
- String targetPlatform = entry.getKey();
|
|
|
+ // 4. 为每个分配对象创建子订单
|
|
|
+ for (Map.Entry<AssigneeKey, List<OrderProductAssignRule>> entry : assigneeGroups.entrySet()) {
|
|
|
+ AssigneeKey key = entry.getKey();
|
|
|
List<OrderProductAssignRule> groupRules = entry.getValue();
|
|
|
- childOrder = copyParentToChild(parentOrder, targetPlatform);
|
|
|
+
|
|
|
+ // 创建子订单
|
|
|
+ OrderMain childOrder = copyParentToChild(parentOrder, key.id(), key.type());
|
|
|
orderMainMapper.insert(childOrder);
|
|
|
|
|
|
- // 迁移商品行:更新 orderId + platform_code + assignment_status
|
|
|
+ // 更新商品:指向子订单 + 标记已分配
|
|
|
List<Long> groupItemIds = groupRules.stream().map(OrderProductAssignRule::getItemId).collect(Collectors.toList());
|
|
|
orderProductMapper.updateForSplitAssign(
|
|
|
groupItemIds,
|
|
|
childOrder.getId(),
|
|
|
- targetPlatform,
|
|
|
- OrderAssignStatus.ASSIGNED.getCode()
|
|
|
+ OrderAssignStatus.ASSIGNED.getCode() // assignmentStatus = 已分配
|
|
|
);
|
|
|
|
|
|
- // 插入分配记录(子订单维度)
|
|
|
+ // 插入分配记录
|
|
|
OrderAssignment assignment = new OrderAssignment();
|
|
|
assignment.setOrderId(childOrder.getId());
|
|
|
assignment.setOrderNo(childOrder.getOrderNo());
|
|
|
assignment.setPlatformBefore(parentOrder.getPlatformCode());
|
|
|
- assignment.setPlatformAfter(targetPlatform);
|
|
|
assignment.setAssignedBy(operatorId);
|
|
|
assignment.setAssignTime(now);
|
|
|
- assignment.setAssignType("0"); // 拆单类型
|
|
|
+ assignment.setAssignType("0");
|
|
|
assignment.setRemark(bo.getRemark());
|
|
|
- baseMapper.insert(assignment);
|
|
|
+ assignment.setAssigneeId(key.id());
|
|
|
+ assignment.setAssigneeType(key.type());
|
|
|
+ baseMapper.insert(assignment); // 假设 baseMapper 是 OrderAssignmentMapper
|
|
|
}
|
|
|
|
|
|
- // 5. 更新父订单状态:标记为已拆分
|
|
|
+ Map<String, Object> stats = orderProductMapper.getAssignmentStats(parentId);
|
|
|
+ Long total = (Long) stats.getOrDefault("total", 0L);
|
|
|
+ Long assigned = (Long) stats.getOrDefault("assigned", 0L);
|
|
|
+
|
|
|
+ String parentAssignmentStatus;
|
|
|
+ if (assigned == 0) {
|
|
|
+ parentAssignmentStatus = OrderAssignStatus.WAIT_ASSIGN.getCode(); // 待分配
|
|
|
+ } else if (assigned.equals(total)) {
|
|
|
+ parentAssignmentStatus = OrderAssignStatus.ASSIGNED.getCode(); // 已分配
|
|
|
+ } else {
|
|
|
+ parentAssignmentStatus = OrderAssignStatus.PARTIALLY_ASSIGNED.getCode();// 部分分配
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 更新父订单状态
|
|
|
OrderMain updateParent = new OrderMain();
|
|
|
updateParent.setId(parentId);
|
|
|
- updateParent.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode());
|
|
|
updateParent.setSplitStatus(OrderSplitStatus.SPLITED.getCode()); // 已拆分
|
|
|
- // 注意:父订单 platformCode 保持不变(作为入口)
|
|
|
+ updateParent.setAssignmentStatus(parentAssignmentStatus);
|
|
|
+ updateParent.setUpdateTime(now);
|
|
|
orderMainMapper.updateById(updateParent);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private List<Long> parseOrderIds(String orderIdsStr) {
|
|
|
if (StringUtils.contains(orderIdsStr, ",")) {
|
|
|
return Arrays.stream(orderIdsStr.split(","))
|
|
|
@@ -301,38 +322,56 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
}
|
|
|
|
|
|
|
|
|
- private OrderMain copyParentToChild(OrderMain parent, String targetPlatform) {
|
|
|
+ private OrderMain copyParentToChild(OrderMain parent, Long assigneeId, String assigneeType) {
|
|
|
OrderMain child = new OrderMain();
|
|
|
String childSeqKey = "child_order_seq:" + parent.getOrderNo();
|
|
|
String paddedSeq = SequenceUtils.nextPaddedIdStr(childSeqKey, Duration.ofDays(90), 2);
|
|
|
+
|
|
|
child.setParentOrderId(parent.getId());
|
|
|
- child.setIsSplitChild("0");
|
|
|
- child.setPlatformCode(targetPlatform);
|
|
|
+ child.setIsSplitChild("0"); // 0=是子订单
|
|
|
child.setOrderNo(parent.getOrderNo() + paddedSeq);
|
|
|
child.setCompanyId(parent.getCompanyId());
|
|
|
child.setCustomerId(parent.getCustomerId());
|
|
|
- child.setExpectedDeliveryTime(parent.getExpectedDeliveryTime());
|
|
|
+ child.setCustomerCode(parent.getCustomerCode());
|
|
|
+ child.setUserId(parent.getUserId());
|
|
|
+ child.setUserNo(parent.getUserNo());
|
|
|
child.setShippingAddressId(parent.getShippingAddressId());
|
|
|
child.setWarehouseId(parent.getWarehouseId());
|
|
|
+ child.setExpectedDeliveryTime(parent.getExpectedDeliveryTime());
|
|
|
+ child.setPurchaseReason(parent.getPurchaseReason());
|
|
|
child.setInvoiceType(parent.getInvoiceType());
|
|
|
child.setPayType(parent.getPayType());
|
|
|
+ child.setCreditLimit(parent.getCreditLimit());
|
|
|
+ child.setBusinessStaff(parent.getBusinessStaff());
|
|
|
+ child.setCustomerService(parent.getCustomerService());
|
|
|
+ child.setBusinessDept(parent.getBusinessDept());
|
|
|
+ child.setUserDept(parent.getUserDept());
|
|
|
child.setTotalAmount(parent.getTotalAmount());
|
|
|
child.setPayableAmount(parent.getPayableAmount());
|
|
|
child.setShippingFee(parent.getShippingFee());
|
|
|
child.setOrderSource(parent.getOrderSource());
|
|
|
- child.setBusinessStaff(parent.getBusinessStaff());
|
|
|
- child.setBusinessDept(parent.getBusinessDept());
|
|
|
- child.setPurchaseReason(parent.getPurchaseReason());
|
|
|
- child.setCreditLimit(parent.getCreditLimit());
|
|
|
- child.setOrderStatus(parent.getOrderStatus());
|
|
|
- child.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode());
|
|
|
- child.setSplitStatus("0");
|
|
|
- child.setRemark(parent.getRemark());
|
|
|
+ child.setOrderStatus(parent.getOrderStatus()); // 继承状态,后续由子订单独立流转
|
|
|
+ child.setOrderTime(parent.getOrderTime());
|
|
|
+ child.setConfirmTime(parent.getConfirmTime());
|
|
|
+ child.setDeliveryType(parent.getDeliveryType());
|
|
|
+ child.setExpenseType(parent.getExpenseType());
|
|
|
child.setCheckStatus(parent.getCheckStatus());
|
|
|
+ child.setDataSource(parent.getDataSource());
|
|
|
+ child.setRemark(parent.getRemark());
|
|
|
+ child.setAttachmentPath(parent.getAttachmentPath());
|
|
|
+
|
|
|
+ // 新增分配字段
|
|
|
+ child.setAssigneeId(assigneeId);
|
|
|
+ child.setAssigneeType(assigneeType);
|
|
|
+
|
|
|
+ // 子订单状态初始化
|
|
|
+ child.setSplitStatus(OrderSplitStatus.WAIT_SPLIT.getCode()); // 子订单默认未拆(通常不再拆)
|
|
|
+ child.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode()); // 已分配
|
|
|
+ child.setStatus("0"); // 正常
|
|
|
+ child.setDelFlag("0");
|
|
|
child.setCreateTime(new Date());
|
|
|
child.setUpdateTime(new Date());
|
|
|
- child.setDataSource(parent.getDataSource());
|
|
|
- child.setDeliveryType(parent.getDeliveryType());
|
|
|
+
|
|
|
return child;
|
|
|
}
|
|
|
|