|
|
|
@@ -4,20 +4,21 @@ import com.baiyue.common.core.api.ResultCode;
|
|
|
|
|
import com.baiyue.common.core.exception.BizException;
|
|
|
|
|
import com.baiyue.order.dto.OrderSplitDtos;
|
|
|
|
|
import com.baiyue.order.entity.OrderEntity;
|
|
|
|
|
import com.baiyue.order.entity.OrderFulfillmentSplitEntity;
|
|
|
|
|
import com.baiyue.order.mapper.OrderFulfillmentSplitMapper;
|
|
|
|
|
import com.baiyue.order.mapper.OrderMapper;
|
|
|
|
|
import com.baiyue.order.entity.OrderFulfillmentSplitEntity;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/** 订单域父子单/柜次数量契约。仅生成一层 CHILD,不复制原订单身份。 */
|
|
|
|
|
/** 订单域父子单/柜次数量契约。新模型只创建余量子单,来源订单承接本次实装。 */
|
|
|
|
|
@Service
|
|
|
|
|
public class OrderFulfillmentSplitService {
|
|
|
|
|
@Resource private OrderFulfillmentSplitMapper splits;
|
|
|
|
@@ -39,67 +40,165 @@ public class OrderFulfillmentSplitService {
|
|
|
|
|
requireMatchingReplay(replay, hash);
|
|
|
|
|
return response(req, splits.lockOrder(req.tenantId(), req.parentOrderId()), replay);
|
|
|
|
|
}
|
|
|
|
|
OrderEntity parent = splits.lockOrder(req.tenantId(), req.parentOrderId());
|
|
|
|
|
|
|
|
|
|
OrderEntity root = splits.lockOrder(req.tenantId(), req.parentOrderId());
|
|
|
|
|
if (root == null) throw new BizException(ResultCode.NOT_FOUND, "根订单不存在或不属于该客户");
|
|
|
|
|
replay = splits.findByIdempotency(req.tenantId(), req.parentOrderId(), req.idempotencyKey());
|
|
|
|
|
if (!replay.isEmpty()) {
|
|
|
|
|
requireMatchingReplay(replay, hash);
|
|
|
|
|
return response(req, parent, replay);
|
|
|
|
|
return response(req, root, replay);
|
|
|
|
|
}
|
|
|
|
|
if (parent == null) throw new BizException(ResultCode.NOT_FOUND, "父订单不存在或不属于该客户");
|
|
|
|
|
if (!"MASTER".equals(parent.getOrderRole())) throw new BizException(ResultCode.CONFLICT, "子订单不能继续拆分");
|
|
|
|
|
requireUnconsumedSource(req, parent);
|
|
|
|
|
if (req.expectedParentVersion()!=null && parent.getVersion()!=null
|
|
|
|
|
&& !req.expectedParentVersion().equals(parent.getVersion()))
|
|
|
|
|
if (!"MASTER".equals(root.getOrderRole())) throw new BizException(ResultCode.CONFLICT, "根订单层级非法");
|
|
|
|
|
if (req.expectedParentVersion() != null && root.getVersion() != null
|
|
|
|
|
&& !req.expectedParentVersion().equals(root.getVersion()))
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "父订单版本已变化,请刷新后重试");
|
|
|
|
|
int parentVersion = parent.getVersion() == null ? 0 : parent.getVersion();
|
|
|
|
|
int next = Optional.ofNullable(splits.maxSplitSequence(req.tenantId(), req.parentOrderId())).orElse(0) + 1;
|
|
|
|
|
List<OrderFulfillmentSplitEntity> rows = new ArrayList<>();
|
|
|
|
|
OrderSplitDtos.Child loaded = createChild(req, parent, next++, "LOADED", req.loadedPackageCount(), hash, operatorId, rows);
|
|
|
|
|
OrderSplitDtos.Child remainder = req.remainingPackageCount() > 0
|
|
|
|
|
? createChild(req, parent, next, "REMAINDER", req.remainingPackageCount(), hash, operatorId, rows) : null;
|
|
|
|
|
for (OrderFulfillmentSplitEntity row : rows) splits.insert(row);
|
|
|
|
|
int updated = splits.incrementParentVersion(parent.getTenantId(), parent.getId(), parentVersion, operatorId);
|
|
|
|
|
if (updated != 1) throw new BizException(ResultCode.CONFLICT, "父订单版本已变化,请刷新后重试");
|
|
|
|
|
parent.setVersion(parentVersion + 1);
|
|
|
|
|
return new OrderSplitDtos.Response(parent.getId(), parent.getOrderNo(), parent.getVersion(), loaded, remainder,
|
|
|
|
|
req.sourcePackageCount(), req.loadedPackageCount(), req.remainingPackageCount(), req.loadingPlanId(), req.containerBookingId(), req.idempotencyKey(), LocalDateTime.now());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderSplitDtos.Tree tree(long tenantId, long parentId) {
|
|
|
|
|
OrderEntity parent = splits.findOrder(tenantId, parentId);
|
|
|
|
|
if (parent == null) throw new BizException(ResultCode.NOT_FOUND, "订单不存在");
|
|
|
|
|
if (!"MASTER".equals(parent.getOrderRole())) parentId = Optional.ofNullable(parent.getParentOrderId()).orElse(parentId);
|
|
|
|
|
parent = splits.findOrder(tenantId, parentId);
|
|
|
|
|
List<OrderFulfillmentSplitEntity> rows = splits.findByParent(tenantId, parentId);
|
|
|
|
|
Map<Long,OrderEntity> children = new HashMap<>();
|
|
|
|
|
for (OrderFulfillmentSplitEntity row: rows) { OrderEntity child=orders.selectById(row.getChildOrderId()); if(child!=null&&tenantId==child.getTenantId()) children.put(child.getId(),child); }
|
|
|
|
|
List<OrderSplitDtos.Child> result = rows.stream().map(r -> { OrderEntity c=children.get(r.getChildOrderId()); return new OrderSplitDtos.Child(r.getChildOrderId(), c==null?null:c.getOrderNo(), c==null?null:c.getSplitSequence(), r.getSplitRole(), "LOADED".equals(r.getSplitRole())?r.getLoadedPackageCount():r.getRemainingPackageCount(), r.getSourceOrderId(), r.getLoadingPlanId(), r.getContainerBookingId()); }).toList();
|
|
|
|
|
int allocated = rows.stream().filter(r -> "LOADED".equals(r.getSplitRole())).mapToInt(OrderFulfillmentSplitEntity::getLoadedPackageCount).sum();
|
|
|
|
|
return new OrderSplitDtos.Tree(parent.getId(), parent.getOrderNo(), parent.getVersion(), result, allocated, Math.max(0, rows.stream().mapToInt(OrderFulfillmentSplitEntity::getSourcePackageCount).findFirst().orElse(0)-allocated));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void requireUnconsumedSource(OrderSplitDtos.CreateRequest request, OrderEntity parent) {
|
|
|
|
|
if (!request.parentOrderId().equals(request.sourceOrderId())) {
|
|
|
|
|
OrderFulfillmentSplitEntity source = splits.lockSourceSlice(request.tenantId(),
|
|
|
|
|
request.parentOrderId(), request.sourceOrderId());
|
|
|
|
|
if (source == null || !"REMAINDER".equals(source.getSplitRole())) {
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "来源订单必须是未消费的余量子订单");
|
|
|
|
|
}
|
|
|
|
|
if (!Objects.equals(source.getRemainingPackageCount(), request.sourcePackageCount())) {
|
|
|
|
|
OrderFulfillmentSplitEntity activeRemainder = splits.lockActiveRemainder(req.tenantId(), root.getId());
|
|
|
|
|
OrderEntity source = root;
|
|
|
|
|
if (!req.parentOrderId().equals(req.sourceOrderId())) {
|
|
|
|
|
source = splits.lockOrder(req.tenantId(), req.sourceOrderId());
|
|
|
|
|
if (source == null || !"CHILD".equals(source.getOrderRole())
|
|
|
|
|
|| !Objects.equals(source.getParentOrderId(), root.getId()))
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "来源订单必须是根订单下的余量子订单");
|
|
|
|
|
OrderFulfillmentSplitEntity sourceLedger = splits.lockSourceSlice(req.tenantId(), root.getId(), source.getId());
|
|
|
|
|
if (sourceLedger == null || !"REMAINDER".equals(sourceLedger.getSplitRole())
|
|
|
|
|
|| !Objects.equals(sourceLedger.getRemainingPackageCount(), req.sourcePackageCount()))
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "余量子订单箱数与本次来源箱数不一致");
|
|
|
|
|
}
|
|
|
|
|
if (activeRemainder == null || !Objects.equals(activeRemainder.getChildOrderId(), source.getId()))
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "来源订单不是当前有效余量子订单");
|
|
|
|
|
} else if (activeRemainder != null) {
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "根订单已有有效余量,必须从当前余量子订单继续装柜");
|
|
|
|
|
}
|
|
|
|
|
if (splits.consumedSourceCount(request.tenantId(), parent.getId(), request.sourceOrderId()) != 0) {
|
|
|
|
|
if (splits.consumedSourceCount(req.tenantId(), root.getId(), source.getId()) != 0)
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "来源订单已经完成履约拆分,不能重复消费");
|
|
|
|
|
|
|
|
|
|
int rootVersion = root.getVersion() == null ? 0 : root.getVersion();
|
|
|
|
|
int sequence = Optional.ofNullable(splits.maxSplitSequence(req.tenantId(), root.getId())).orElse(0) + 1;
|
|
|
|
|
List<OrderFulfillmentSplitEntity> rows = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// 来源订单本身承接本次实装;该台账行保留 LOADED 语义以兼容旧消费者。
|
|
|
|
|
OrderSplitDtos.Child loaded = new OrderSplitDtos.Child(source.getId(), source.getOrderNo(), source.getSplitSequence(),
|
|
|
|
|
"LOADED", req.loadedPackageCount(), source.getId(), req.loadingPlanId(), req.containerBookingId());
|
|
|
|
|
rows.add(ledger(req, root, source, source.getId(), "LOADED", hash, operatorId));
|
|
|
|
|
|
|
|
|
|
OrderSplitDtos.Child remainder = null;
|
|
|
|
|
if (req.remainingPackageCount() > 0) {
|
|
|
|
|
OrderEntity child = cloneRemainder(req, root, sequence, operatorId);
|
|
|
|
|
remainder = new OrderSplitDtos.Child(child.getId(), child.getOrderNo(), sequence, "REMAINDER",
|
|
|
|
|
req.remainingPackageCount(), source.getId(), req.loadingPlanId(), req.containerBookingId());
|
|
|
|
|
rows.add(ledger(req, root, source, child.getId(), "REMAINDER", hash, operatorId));
|
|
|
|
|
}
|
|
|
|
|
for (OrderFulfillmentSplitEntity row : rows) {
|
|
|
|
|
if (splits.insert(row) != 1) throw new BizException(ResultCode.CONFLICT, "订单拆分台账写入失败");
|
|
|
|
|
}
|
|
|
|
|
if (splits.incrementParentVersion(root.getTenantId(), root.getId(), rootVersion, operatorId) != 1)
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "父订单版本已变化,请刷新后重试");
|
|
|
|
|
return new OrderSplitDtos.Response(root.getId(), root.getOrderNo(), rootVersion + 1, loaded, remainder,
|
|
|
|
|
req.sourcePackageCount(), req.loadedPackageCount(), req.remainingPackageCount(), req.loadingPlanId(),
|
|
|
|
|
req.containerBookingId(), req.idempotencyKey(), LocalDateTime.now());
|
|
|
|
|
}
|
|
|
|
|
private OrderSplitDtos.Child createChild(OrderSplitDtos.CreateRequest r, OrderEntity p, int seq, String role, int count, String hash, long op, List<OrderFulfillmentSplitEntity> rows) {
|
|
|
|
|
OrderEntity c = new OrderEntity(); c.setId(IdWorker.getId()); c.setTenantId(p.getTenantId()); c.setOrderNo(p.getOrderNo()+"-"+seq); c.setOrderRole("CHILD"); c.setParentOrderId(p.getId()); c.setRootOrderId(p.getId()); c.setSplitSequence(seq); c.setStatus(p.getStatus()); c.setCustomerId(p.getCustomerId()); c.setCustomerName(p.getCustomerName()); c.setTransportMode(p.getTransportMode()); c.setTradeTerm(p.getTradeTerm()); c.setDestinationCountryCode(p.getDestinationCountryCode()); c.setDestinationWarehouseId(p.getDestinationWarehouseId()); c.setDestinationWarehouseName(p.getDestinationWarehouseName()); c.setReceivingWarehouseId(p.getReceivingWarehouseId()); c.setReceivingWarehouseName(p.getReceivingWarehouseName()); c.setChannelCode(p.getChannelCode()); c.setChannelName(p.getChannelName()); c.setDestinationType(p.getDestinationType()); c.setCreatedBy(op); c.setUpdatedBy(op); orders.insert(c);
|
|
|
|
|
OrderFulfillmentSplitEntity row=new OrderFulfillmentSplitEntity(); row.setId(IdWorker.getId()); row.setTenantId(r.tenantId()); row.setParentOrderId(p.getId()); row.setSourceOrderId(r.sourceOrderId()); row.setChildOrderId(c.getId()); row.setLoadingPlanId(r.loadingPlanId()); row.setContainerBookingId(r.containerBookingId()); row.setSourcePackageCount(r.sourcePackageCount()); row.setLoadedPackageCount(r.loadedPackageCount()); row.setRemainingPackageCount(r.remainingPackageCount()); row.setSourceDetailJson(sourceDetails(r)); row.setOperationId(Optional.ofNullable(r.operationId()).orElse("split-"+r.idempotencyKey())); row.setIdempotencyKey(r.idempotencyKey()); row.setRequestHash(hash); row.setSplitRole(role); row.setExpectedParentVersion(Optional.ofNullable(r.expectedParentVersion()).orElse(p.getVersion()==null?0:p.getVersion())); row.setCreatedBy(op); row.setCreatedByName(r.operatorName()); row.setCreatedAt(LocalDateTime.now()); rows.add(row); return new OrderSplitDtos.Child(c.getId(), c.getOrderNo(), seq, role, count, r.sourceOrderId(), r.loadingPlanId(), r.containerBookingId());
|
|
|
|
|
|
|
|
|
|
public OrderSplitDtos.Tree tree(long tenantId, long orderId) {
|
|
|
|
|
OrderEntity requested = splits.findOrder(tenantId, orderId);
|
|
|
|
|
if (requested == null) throw new BizException(ResultCode.NOT_FOUND, "订单不存在");
|
|
|
|
|
long rootId = "MASTER".equals(requested.getOrderRole()) ? requested.getId()
|
|
|
|
|
: Optional.ofNullable(requested.getRootOrderId()).orElse(requested.getParentOrderId());
|
|
|
|
|
OrderEntity root = splits.findOrder(tenantId, rootId);
|
|
|
|
|
List<OrderFulfillmentSplitEntity> rows = splits.findByParent(tenantId, rootId);
|
|
|
|
|
Map<Long, OrderEntity> ordersById = new HashMap<>();
|
|
|
|
|
for (OrderFulfillmentSplitEntity row : rows) {
|
|
|
|
|
OrderEntity child = orders.selectById(row.getChildOrderId());
|
|
|
|
|
if (child != null && tenantId == child.getTenantId()) ordersById.put(child.getId(), child);
|
|
|
|
|
}
|
|
|
|
|
List<OrderSplitDtos.Child> fragments = rows.stream().map(r -> {
|
|
|
|
|
OrderEntity child = ordersById.get(r.getChildOrderId());
|
|
|
|
|
int count = "LOADED".equals(r.getSplitRole()) ? r.getLoadedPackageCount() : r.getRemainingPackageCount();
|
|
|
|
|
return new OrderSplitDtos.Child(r.getChildOrderId(), child == null ? null : child.getOrderNo(),
|
|
|
|
|
child == null ? null : child.getSplitSequence(), r.getSplitRole(), count,
|
|
|
|
|
r.getSourceOrderId(), r.getLoadingPlanId(), r.getContainerBookingId());
|
|
|
|
|
}).toList();
|
|
|
|
|
int cumulativeLoaded = rows.stream().filter(r -> "LOADED".equals(r.getSplitRole()))
|
|
|
|
|
.mapToInt(OrderFulfillmentSplitEntity::getLoadedPackageCount).sum();
|
|
|
|
|
int original = rows.stream().map(OrderFulfillmentSplitEntity::getSourcePackageCount)
|
|
|
|
|
.filter(Objects::nonNull).max(Integer::compareTo).orElse(0);
|
|
|
|
|
Set<Long> consumed = new HashSet<>();
|
|
|
|
|
rows.stream().filter(r -> "LOADED".equals(r.getSplitRole())).map(OrderFulfillmentSplitEntity::getSourceOrderId)
|
|
|
|
|
.forEach(consumed::add);
|
|
|
|
|
int currentRemainder = rows.stream().filter(r -> "REMAINDER".equals(r.getSplitRole())
|
|
|
|
|
&& !consumed.contains(r.getChildOrderId()))
|
|
|
|
|
.mapToInt(OrderFulfillmentSplitEntity::getRemainingPackageCount).sum();
|
|
|
|
|
return new OrderSplitDtos.Tree(root.getId(), root.getOrderNo(), root.getVersion(), fragments,
|
|
|
|
|
cumulativeLoaded, currentRemainder, original, cumulativeLoaded, currentRemainder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OrderEntity cloneRemainder(OrderSplitDtos.CreateRequest r, OrderEntity root, int sequence, long operatorId) {
|
|
|
|
|
OrderEntity child = new OrderEntity();
|
|
|
|
|
child.setId(IdWorker.getId()); child.setTenantId(root.getTenantId()); child.setOrderNo(root.getOrderNo() + "-" + sequence);
|
|
|
|
|
child.setOrderRole("CHILD"); child.setParentOrderId(root.getId()); child.setRootOrderId(root.getId()); child.setSplitSequence(sequence);
|
|
|
|
|
child.setStatus(root.getStatus()); child.setCustomerId(root.getCustomerId()); child.setCustomerName(root.getCustomerName());
|
|
|
|
|
child.setTransportMode(root.getTransportMode()); child.setTradeTerm(root.getTradeTerm());
|
|
|
|
|
child.setDestinationCountryCode(root.getDestinationCountryCode()); child.setDestinationWarehouseId(root.getDestinationWarehouseId());
|
|
|
|
|
child.setDestinationWarehouseName(root.getDestinationWarehouseName()); child.setReceivingWarehouseId(root.getReceivingWarehouseId());
|
|
|
|
|
child.setReceivingWarehouseName(root.getReceivingWarehouseName()); child.setChannelCode(root.getChannelCode());
|
|
|
|
|
child.setChannelName(root.getChannelName()); child.setDestinationType(root.getDestinationType());
|
|
|
|
|
child.setCreatedBy(operatorId); child.setUpdatedBy(operatorId);
|
|
|
|
|
if (orders.insert(child) != 1) throw new BizException(ResultCode.CONFLICT, "余量子订单创建失败");
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OrderFulfillmentSplitEntity ledger(OrderSplitDtos.CreateRequest r, OrderEntity root, OrderEntity source,
|
|
|
|
|
long childId, String role, String hash, long operatorId) {
|
|
|
|
|
OrderFulfillmentSplitEntity row = new OrderFulfillmentSplitEntity();
|
|
|
|
|
row.setId(IdWorker.getId()); row.setTenantId(r.tenantId()); row.setParentOrderId(root.getId()); row.setSourceOrderId(source.getId());
|
|
|
|
|
row.setChildOrderId(childId); row.setLoadingPlanId(r.loadingPlanId()); row.setContainerBookingId(r.containerBookingId());
|
|
|
|
|
row.setSourcePackageCount(r.sourcePackageCount()); row.setLoadedPackageCount(r.loadedPackageCount()); row.setRemainingPackageCount(r.remainingPackageCount());
|
|
|
|
|
row.setSourceDetailJson(sourceDetails(r)); row.setOperationId(Optional.ofNullable(r.operationId()).orElse("split-" + r.idempotencyKey()));
|
|
|
|
|
row.setIdempotencyKey(r.idempotencyKey()); row.setRequestHash(hash); row.setSplitRole(role);
|
|
|
|
|
row.setExpectedParentVersion(Optional.ofNullable(r.expectedParentVersion()).orElse(root.getVersion() == null ? 0 : root.getVersion()));
|
|
|
|
|
row.setCreatedBy(operatorId); row.setCreatedByName(r.operatorName()); row.setCreatedAt(LocalDateTime.now());
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validate(OrderSplitDtos.CreateRequest r) {
|
|
|
|
|
if (r == null || r.tenantId() == null || r.parentOrderId() == null || r.sourceOrderId() == null
|
|
|
|
|
|| r.loadingPlanId() == null || r.containerBookingId() == null || r.sourcePackageCount() == null
|
|
|
|
|
|| r.loadedPackageCount() == null || r.remainingPackageCount() == null || r.idempotencyKey() == null
|
|
|
|
|
|| r.idempotencyKey().isBlank() || r.lines() == null || r.lines().isEmpty())
|
|
|
|
|
throw new BizException(ResultCode.BAD_REQUEST, "拆分请求字段不完整");
|
|
|
|
|
if (r.loadedPackageCount() < 1 || r.remainingPackageCount() < 0
|
|
|
|
|
|| r.loadedPackageCount() + r.remainingPackageCount() != r.sourcePackageCount())
|
|
|
|
|
throw new BizException(ResultCode.VALIDATION_FAILED, "装柜数量与剩余数量必须守恒");
|
|
|
|
|
if (r.lines().stream().anyMatch(line -> line == null || line.sourceDetailId() == null || line.sourceDetailId() <= 0
|
|
|
|
|
|| line.packageCount() == null || line.packageCount() <= 0)
|
|
|
|
|
|| r.lines().stream().map(OrderSplitDtos.QuantityLine::sourceDetailId).distinct().count() != r.lines().size()
|
|
|
|
|
|| r.lines().stream().mapToInt(OrderSplitDtos.QuantityLine::packageCount).sum() != r.sourcePackageCount())
|
|
|
|
|
throw new BizException(ResultCode.VALIDATION_FAILED, "来源明细箱数必须唯一且合计等于来源箱数");
|
|
|
|
|
}
|
|
|
|
|
private void requireMatchingReplay(List<OrderFulfillmentSplitEntity> rows, String hash) {
|
|
|
|
|
if (rows.stream().anyMatch(row -> !Objects.equals(hash, row.getRequestHash())))
|
|
|
|
|
throw new BizException(ResultCode.CONFLICT, "幂等键对应的请求内容不一致");
|
|
|
|
|
}
|
|
|
|
|
private String sourceDetails(OrderSplitDtos.CreateRequest request) {
|
|
|
|
|
try { return json.writeValueAsString(request.lines()); }
|
|
|
|
|
catch (Exception exception) { throw new BizException(ResultCode.INTERNAL_ERROR, "无法保存来源明细快照"); }
|
|
|
|
|
}
|
|
|
|
|
private String hash(Object value) {
|
|
|
|
|
try { byte[] digest = MessageDigest.getInstance("SHA-256").digest(json.writeValueAsString(value).getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
StringBuilder result = new StringBuilder(); for (byte b : digest) result.append(String.format("%02x", b)); return result.toString();
|
|
|
|
|
} catch (Exception exception) { throw new BizException("无法生成请求指纹"); }
|
|
|
|
|
}
|
|
|
|
|
private OrderSplitDtos.Response response(OrderSplitDtos.CreateRequest r, OrderEntity root, List<OrderFulfillmentSplitEntity> rows) {
|
|
|
|
|
OrderSplitDtos.Child loaded = null, remainder = null;
|
|
|
|
|
for (OrderFulfillmentSplitEntity row : rows) {
|
|
|
|
|
OrderEntity child = orders.selectById(row.getChildOrderId());
|
|
|
|
|
OrderSplitDtos.Child value = new OrderSplitDtos.Child(row.getChildOrderId(), child == null ? null : child.getOrderNo(),
|
|
|
|
|
child == null ? null : child.getSplitSequence(), row.getSplitRole(),
|
|
|
|
|
"LOADED".equals(row.getSplitRole()) ? row.getLoadedPackageCount() : row.getRemainingPackageCount(),
|
|
|
|
|
row.getSourceOrderId(), row.getLoadingPlanId(), row.getContainerBookingId());
|
|
|
|
|
if ("LOADED".equals(row.getSplitRole())) loaded = value; else remainder = value;
|
|
|
|
|
}
|
|
|
|
|
return new OrderSplitDtos.Response(root.getId(), root.getOrderNo(), root.getVersion(), loaded, remainder,
|
|
|
|
|
r.sourcePackageCount(), r.loadedPackageCount(), r.remainingPackageCount(), r.loadingPlanId(), r.containerBookingId(),
|
|
|
|
|
r.idempotencyKey(), rows.get(0).getCreatedAt());
|
|
|
|
|
}
|
|
|
|
|
private void validate(OrderSplitDtos.CreateRequest r) { if(r==null||r.tenantId()==null||r.parentOrderId()==null||r.sourceOrderId()==null||r.loadingPlanId()==null||r.containerBookingId()==null||r.sourcePackageCount()==null||r.loadedPackageCount()==null||r.remainingPackageCount()==null||r.idempotencyKey()==null||r.idempotencyKey().isBlank()||r.lines()==null||r.lines().isEmpty()) throw new BizException(ResultCode.BAD_REQUEST,"拆分请求字段不完整"); if(r.loadedPackageCount()<1||r.remainingPackageCount()<0||r.loadedPackageCount()+r.remainingPackageCount()!=r.sourcePackageCount()) throw new BizException(ResultCode.VALIDATION_FAILED,"装柜数量与剩余数量必须守恒"); if(r.lines().stream().anyMatch(line -> line==null||line.sourceDetailId()==null||line.sourceDetailId()<=0||line.packageCount()==null||line.packageCount()<=0)||r.lines().stream().map(OrderSplitDtos.QuantityLine::sourceDetailId).distinct().count()!=r.lines().size()||r.lines().stream().mapToInt(OrderSplitDtos.QuantityLine::packageCount).sum()!=r.sourcePackageCount()) throw new BizException(ResultCode.VALIDATION_FAILED,"来源明细箱数必须唯一且合计等于来源箱数"); }
|
|
|
|
|
private void requireMatchingReplay(List<OrderFulfillmentSplitEntity> rows, String hash) { if(rows.stream().anyMatch(row -> !Objects.equals(hash,row.getRequestHash()))) throw new BizException(ResultCode.CONFLICT,"幂等键对应的请求内容不一致"); }
|
|
|
|
|
private String sourceDetails(OrderSplitDtos.CreateRequest request) { try { return json.writeValueAsString(request.lines()); } catch(Exception exception) { throw new BizException(ResultCode.INTERNAL_ERROR,"无法保存来源明细快照"); } }
|
|
|
|
|
private String hash(Object o) { try { byte[] b=json.writeValueAsString(o).getBytes(StandardCharsets.UTF_8); byte[] d=MessageDigest.getInstance("SHA-256").digest(b); StringBuilder s=new StringBuilder(); for(byte x:d)s.append(String.format("%02x",x)); return s.toString(); } catch(Exception e){throw new BizException("无法生成请求指纹");} }
|
|
|
|
|
private OrderSplitDtos.Response response(OrderSplitDtos.CreateRequest r, OrderEntity p, List<OrderFulfillmentSplitEntity> rows) { OrderSplitDtos.Child l=null,m=null; for(var x:rows){OrderEntity c=orders.selectById(x.getChildOrderId()); var z=new OrderSplitDtos.Child(x.getChildOrderId(),c==null?null:c.getOrderNo(),c==null?null:c.getSplitSequence(),x.getSplitRole(),"LOADED".equals(x.getSplitRole())?x.getLoadedPackageCount():x.getRemainingPackageCount(),x.getSourceOrderId(),x.getLoadingPlanId(),x.getContainerBookingId()); if("LOADED".equals(x.getSplitRole()))l=z;else m=z;} return new OrderSplitDtos.Response(p.getId(),p.getOrderNo(),p.getVersion(),l,m,r.sourcePackageCount(),r.loadedPackageCount(),r.remainingPackageCount(),r.loadingPlanId(),r.containerBookingId(),r.idempotencyKey(),rows.get(0).getCreatedAt()); }
|
|
|
|
|
}
|
|
|
|
|