|
|
@@ -26,6 +26,7 @@ import org.dromara.order.domain.OrderProduct;
|
|
|
import org.dromara.order.domain.OrderProductAssignRule;
|
|
|
import org.dromara.order.domain.bo.OrderAssignmentBo;
|
|
|
import org.dromara.order.domain.bo.OrderSplitAssignBo;
|
|
|
+import org.dromara.order.domain.dto.AssignmentStatsDto;
|
|
|
import org.dromara.order.domain.vo.OrderAssignmentVo;
|
|
|
import org.dromara.order.domain.vo.OrderMainVo;
|
|
|
import org.dromara.order.mapper.OrderAssignmentMapper;
|
|
|
@@ -35,6 +36,7 @@ import org.dromara.order.service.IOrderAssignmentService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.time.Duration;
|
|
|
import java.util.*;
|
|
|
import java.util.function.Function;
|
|
|
@@ -208,11 +210,11 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
if (parentOrder == null) {
|
|
|
throw new ServiceException("父订单不存在");
|
|
|
}
|
|
|
- if ("1".equals(parentOrder.getSplitStatus())) { // 已拆分
|
|
|
+ /* if (OrderSplitStatus.SPLITED.getCode().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()
|
|
|
@@ -223,12 +225,12 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
if (item == null || !parentId.equals(item.getOrderId())) {
|
|
|
throw new ServiceException("商品行不属于该订单或不存在");
|
|
|
}
|
|
|
- if ("1".equals(item.getAssignmentStatus())) { // 已分配
|
|
|
+ if (OrderAssignStatus.ASSIGNED.getCode().equals(item.getAssignmentStatus())) {
|
|
|
throw new ServiceException("商品 [" + item.getProductName() + "] 已分配,不可重复操作");
|
|
|
}
|
|
|
- // 校验 assigneeType 合法性
|
|
|
String type = rule.getAssigneeType();
|
|
|
- if (!AssigneeTypeConstants.SUPPLIER.getCode().equals(type) && !AssigneeTypeConstants.PARTNER.getCode().equals(type)) {
|
|
|
+ if (!AssigneeTypeConstants.SUPPLIER.getCode().equals(type) &&
|
|
|
+ !AssigneeTypeConstants.PARTNER.getCode().equals(type)) {
|
|
|
throw new ServiceException("分配对象类型不支持: " + type);
|
|
|
}
|
|
|
}
|
|
|
@@ -239,51 +241,118 @@ public class OrderAssignmentServiceImpl extends ServiceImpl<OrderAssignmentMappe
|
|
|
Map<AssigneeKey, List<OrderProductAssignRule>> assigneeGroups = rules.stream()
|
|
|
.collect(Collectors.groupingBy(rule -> new AssigneeKey(rule.getAssigneeId(), rule.getAssigneeType())));
|
|
|
|
|
|
- // 4. 为每个分配对象创建子订单
|
|
|
+ // 4. 为每个分配对象创建子订单 + 商品副本
|
|
|
for (Map.Entry<AssigneeKey, List<OrderProductAssignRule>> entry : assigneeGroups.entrySet()) {
|
|
|
AssigneeKey key = entry.getKey();
|
|
|
List<OrderProductAssignRule> groupRules = entry.getValue();
|
|
|
|
|
|
- // 创建子订单
|
|
|
+ // 创建子订单(从主订单拷贝必要字段)
|
|
|
OrderMain childOrder = copyParentToChild(parentOrder, key.id(), key.type());
|
|
|
orderMainMapper.insert(childOrder);
|
|
|
+ Long childOrderId = childOrder.getId();
|
|
|
+
|
|
|
+ // 准备子订单商品列表(副本)
|
|
|
+ List<OrderProduct> childProducts = new ArrayList<>();
|
|
|
+ List<Long> parentItemIdsToUpdate = new ArrayList<>();
|
|
|
+
|
|
|
+ for (OrderProductAssignRule rule : groupRules) {
|
|
|
+ OrderProduct parentItem = itemMap.get(rule.getItemId());
|
|
|
+
|
|
|
+ // 1. 收集主订单商品ID,用于后续批量更新分配状态
|
|
|
+ parentItemIdsToUpdate.add(parentItem.getId());
|
|
|
+
|
|
|
+ // 2. 创建子订单商品副本
|
|
|
+ OrderProduct childProduct = new OrderProduct();
|
|
|
+
|
|
|
+ // 继承租户信息
|
|
|
+ childProduct.setTenantId(parentItem.getTenantId());
|
|
|
+
|
|
|
+ // 关联信息
|
|
|
+ childProduct.setOrderId(childOrderId); // 归属子订单
|
|
|
+ childProduct.setOriginalItemId(parentItem.getId()); // 指向主订单商品
|
|
|
+
|
|
|
+ // 订单基础信息
|
|
|
+ childProduct.setOrderNo(childOrder.getOrderNo()); // 子订单编号
|
|
|
+ childProduct.setDataSource(parentItem.getDataSource());
|
|
|
+
|
|
|
+ // 商品核心信息
|
|
|
+ childProduct.setProductId(parentItem.getProductId());
|
|
|
+ childProduct.setProductNo(parentItem.getProductNo());
|
|
|
+ childProduct.setProductName(parentItem.getProductName());
|
|
|
+ childProduct.setProductUnitId(parentItem.getProductUnitId());
|
|
|
+ childProduct.setProductUnit(parentItem.getProductUnit());
|
|
|
+ childProduct.setProductImage(parentItem.getProductImage());
|
|
|
+
|
|
|
+ // 价格信息
|
|
|
+ childProduct.setPlatformPrice(parentItem.getPlatformPrice());
|
|
|
+ childProduct.setMarketPrice(parentItem.getMarketPrice());
|
|
|
+ childProduct.setMemberPrice(parentItem.getMemberPrice());
|
|
|
+ childProduct.setPurchasingPrice(parentItem.getPurchasingPrice());
|
|
|
+ childProduct.setMaxPurchasePrice(parentItem.getMaxPurchasePrice());
|
|
|
+ childProduct.setMinSellingPrice(parentItem.getMinSellingPrice());
|
|
|
+ childProduct.setTaxRate(parentItem.getTaxRate());
|
|
|
+ childProduct.setOrderPrice(parentItem.getOrderPrice());
|
|
|
+ childProduct.setSubtotal(parentItem.getSubtotal());
|
|
|
+
|
|
|
+ // 数量信息(注意:履约数量应重置)
|
|
|
+ childProduct.setOrderQuantity(parentItem.getOrderQuantity());
|
|
|
+ childProduct.setMinOrderQuantity(parentItem.getMinOrderQuantity());
|
|
|
+ childProduct.setUnsentQuantity(parentItem.getOrderQuantity()); // 未发货 = 总数
|
|
|
+ childProduct.setReturnAmount(BigDecimal.ZERO);
|
|
|
+
|
|
|
+ // 状态与标志
|
|
|
+ childProduct.setAssignmentStatus(OrderAssignStatus.ASSIGNED.getCode()); // 已分配
|
|
|
+
|
|
|
+ // 时间 & 其他
|
|
|
+ childProduct.setPreDeliveryDate(parentItem.getPreDeliveryDate());
|
|
|
+ childProduct.setRemark(parentItem.getRemark());
|
|
|
+
|
|
|
+ childProducts.add(childProduct);
|
|
|
+ }
|
|
|
|
|
|
- // 更新商品:指向子订单 + 标记已分配
|
|
|
- List<Long> groupItemIds = groupRules.stream().map(OrderProductAssignRule::getItemId).collect(Collectors.toList());
|
|
|
- orderProductMapper.updateForSplitAssign(
|
|
|
- groupItemIds,
|
|
|
- childOrder.getId(),
|
|
|
- OrderAssignStatus.ASSIGNED.getCode() // assignmentStatus = 已分配
|
|
|
- );
|
|
|
+ // 批量插入子订单商品
|
|
|
+ if (!childProducts.isEmpty()) {
|
|
|
+ orderProductMapper.insertBatch(childProducts);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量更新主订单商品:标记已分配 + 记录分配到的子订单ID
|
|
|
+ if (!parentItemIdsToUpdate.isEmpty()) {
|
|
|
+ orderProductMapper.updateAssignmentInfo(
|
|
|
+ parentItemIdsToUpdate,
|
|
|
+ OrderAssignStatus.ASSIGNED.getCode(),
|
|
|
+ childOrderId,
|
|
|
+ now
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
- // 插入分配记录
|
|
|
+ // 插入分配记录(分配动作日志)
|
|
|
OrderAssignment assignment = new OrderAssignment();
|
|
|
- assignment.setOrderId(childOrder.getId());
|
|
|
+ assignment.setOrderId(childOrderId);
|
|
|
assignment.setOrderNo(childOrder.getOrderNo());
|
|
|
assignment.setPlatformBefore(parentOrder.getPlatformCode());
|
|
|
assignment.setAssignedBy(operatorId);
|
|
|
assignment.setAssignTime(now);
|
|
|
- assignment.setAssignType("0");
|
|
|
+ assignment.setAssignType("0"); // 可按需定义
|
|
|
assignment.setRemark(bo.getRemark());
|
|
|
assignment.setAssigneeId(key.id());
|
|
|
assignment.setAssigneeType(key.type());
|
|
|
- baseMapper.insert(assignment); // 假设 baseMapper 是 OrderAssignmentMapper
|
|
|
+ baseMapper.insert(assignment); // 假设是 OrderAssignmentMapper
|
|
|
}
|
|
|
|
|
|
- Map<String, Object> stats = orderProductMapper.getAssignmentStats(parentId);
|
|
|
- Long total = (Long) stats.getOrDefault("total", 0L);
|
|
|
- Long assigned = (Long) stats.getOrDefault("assigned", 0L);
|
|
|
+ // 5. 更新主订单的拆分状态和分配状态
|
|
|
+ AssignmentStatsDto stats = orderProductMapper.getAssignmentStats(parentId);
|
|
|
+ Long total = stats.getTotal();
|
|
|
+ Long assigned = stats.getAssigned();
|
|
|
|
|
|
String parentAssignmentStatus;
|
|
|
if (assigned == 0) {
|
|
|
- parentAssignmentStatus = OrderAssignStatus.WAIT_ASSIGN.getCode(); // 待分配
|
|
|
- } else if (assigned.equals(total)) {
|
|
|
- parentAssignmentStatus = OrderAssignStatus.ASSIGNED.getCode(); // 已分配
|
|
|
+ parentAssignmentStatus = OrderAssignStatus.WAIT_ASSIGN.getCode();
|
|
|
+ } else if (assigned == total) {
|
|
|
+ parentAssignmentStatus = OrderAssignStatus.ASSIGNED.getCode();
|
|
|
} else {
|
|
|
- parentAssignmentStatus = OrderAssignStatus.PARTIALLY_ASSIGNED.getCode();// 部分分配
|
|
|
+ parentAssignmentStatus = OrderAssignStatus.PARTIALLY_ASSIGNED.getCode();
|
|
|
}
|
|
|
|
|
|
- // 5. 更新父订单状态
|
|
|
OrderMain updateParent = new OrderMain();
|
|
|
updateParent.setId(parentId);
|
|
|
updateParent.setSplitStatus(OrderSplitStatus.SPLITED.getCode()); // 已拆分
|