fix(firstleg): reprice container fee on loading changes
This commit is contained in:
@@ -74,6 +74,8 @@ public class LoadingListService {
|
||||
@Resource
|
||||
private LoadingCompletedOutboxService loadingCompletedOutboxService;
|
||||
@Resource
|
||||
private FbaPricingService fbaPricingService;
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
@Resource
|
||||
private LoadingDocumentFileClient documentFiles;
|
||||
@@ -501,6 +503,7 @@ public class LoadingListService {
|
||||
|
||||
AggregateRow before = requirePlan(planId);
|
||||
List<Link> links = loadingPickService.requirePreloadConfirmationReady(planId);
|
||||
requireContainerFeesReadyForPreloadConfirmation(links, operationId);
|
||||
if (repository.touchForPreloadConfirmation(planId, request.expectedVersion(),
|
||||
operatorId, trim(operatorName)) != 1) {
|
||||
throw conflict("预装计划已被其他操作修改,请刷新后重试");
|
||||
@@ -610,6 +613,30 @@ public class LoadingListService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-review/pre-measurement members may be reserved in an editing plan. Confirmation is the
|
||||
* hard boundary: every customer group in the cabinet must have a ready, freshly recomputed fee.
|
||||
*/
|
||||
private void requireContainerFeesReadyForPreloadConfirmation(List<Link> links, String operationId) {
|
||||
Map<Long, Set<Long>> tenantsByContainer = new LinkedHashMap<>();
|
||||
for (Link link : links) {
|
||||
if (link.containerBookingId() == null || link.tenantId() == null) {
|
||||
throw conflict("预装关联缺少柜次或客户归属,不能确认预装");
|
||||
}
|
||||
tenantsByContainer.computeIfAbsent(link.containerBookingId(), ignored -> new java.util.LinkedHashSet<>())
|
||||
.add(link.tenantId());
|
||||
}
|
||||
for (Map.Entry<Long, Set<Long>> entry : tenantsByContainer.entrySet()) {
|
||||
for (Long tenantId : entry.getValue()) {
|
||||
var result = fbaPricingService.recalculateUnlockedContainerCustomerFees(entry.getKey(), tenantId,
|
||||
sha256("CONFIRM_PRELOAD_FEE|" + operationId + '|' + entry.getKey() + '|' + tenantId));
|
||||
if (result.status() == FbaPricingService.ContainerFeeRecalculationStatus.NOT_READY) {
|
||||
throw conflict("柜内存在尚未完成初始费用的入仓单,不能确认预装");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateRange(java.time.LocalDateTime from, java.time.LocalDateTime to) {
|
||||
if (from != null && to != null && from.isAfter(to)) {
|
||||
throw badRequest("创建开始时间不能晚于结束时间");
|
||||
|
||||
@@ -62,6 +62,7 @@ public class SmartLoadingService {
|
||||
@Resource private LoadingModelBridgeService loadingModelBridge;
|
||||
@Resource private SplitPreloadService splitPreloadService;
|
||||
@Resource private ColoadGroupAssignmentPort coloadGroups;
|
||||
@Resource private FbaPricingService fbaPricingService;
|
||||
@Resource private PriceBookQueryUseCase priceBooks;
|
||||
@Resource private FirstlegDocumentNumberGenerator documentNumbers;
|
||||
@Resource private ObjectMapper objectMapper;
|
||||
@@ -418,11 +419,16 @@ public class SmartLoadingService {
|
||||
@BusinessAudit(event = "SMART_LOADING_ITEM_ASSIGN", resourceType = "LOADING_PLAN",
|
||||
resourceId = "#planId", description = "分配智能装柜明细")
|
||||
public Plan assign(long planId, AssignRequest request, long operatorId, String operatorName) {
|
||||
return assign(planId, request, operatorId, operatorName, false);
|
||||
return assign(planId, request, operatorId, operatorName, false, true);
|
||||
}
|
||||
|
||||
private Plan assign(long planId, AssignRequest request, long operatorId, String operatorName,
|
||||
boolean wholeGroupAssignment) {
|
||||
return assign(planId, request, operatorId, operatorName, wholeGroupAssignment, false);
|
||||
}
|
||||
|
||||
private Plan assign(long planId, AssignRequest request, long operatorId, String operatorName,
|
||||
boolean wholeGroupAssignment, boolean recalculateFees) {
|
||||
String operation = operation(request.operationId());
|
||||
String requestHash = sha256("ASSIGN|" + planId + '|'
|
||||
+ request.inboundId() + '|' + request.inboundDetailId() + '|' + request.packageCount()
|
||||
@@ -475,6 +481,9 @@ public class SmartLoadingService {
|
||||
loadingModelBridge.bindIncrementalAllocation(planId, candidate.accountId(), candidate.inboundId(),
|
||||
operatorId, operatorName, operation);
|
||||
loadingPickService.sync(planId, changed);
|
||||
if (recalculateFees) {
|
||||
recalculateAfterRelationshipChange(booking.getId(), Set.of(candidate.accountId()), operation);
|
||||
}
|
||||
return view(repository.plan(planId), booking);
|
||||
}
|
||||
|
||||
@@ -488,7 +497,7 @@ public class SmartLoadingService {
|
||||
resourceId = "#planId", description = "按入仓码分配智能装柜箱数")
|
||||
public Plan assignInbound(long planId, AssignInboundRequest request,
|
||||
long operatorId, String operatorName) {
|
||||
return assignInbound(planId, request, operatorId, operatorName, false);
|
||||
return assignInbound(planId, request, operatorId, operatorName, false, null, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -568,17 +577,24 @@ public class SmartLoadingService {
|
||||
loadingModelBridge.bindIncrementalAllocation(planId, request.customerId(), inboundId,
|
||||
operatorId, operatorName, operation);
|
||||
created.forEach(item -> loadingPickService.sync(planId, item));
|
||||
recalculateAfterRelationshipChange(booking.getId(), Set.of(request.customerId()), operation);
|
||||
return view(repository.plan(planId), booking);
|
||||
}
|
||||
|
||||
private Plan assignInbound(long planId, AssignInboundRequest request,
|
||||
long operatorId, String operatorName, boolean wholeGroupAssignment) {
|
||||
return assignInbound(planId, request, operatorId, operatorName, wholeGroupAssignment, null);
|
||||
return assignInbound(planId, request, operatorId, operatorName, wholeGroupAssignment, null, false);
|
||||
}
|
||||
|
||||
private Plan assignInbound(long planId, AssignInboundRequest request,
|
||||
long operatorId, String operatorName, boolean wholeGroupAssignment,
|
||||
List<Candidate> frozenDetails) {
|
||||
return assignInbound(planId, request, operatorId, operatorName, wholeGroupAssignment, frozenDetails, false);
|
||||
}
|
||||
|
||||
private Plan assignInbound(long planId, AssignInboundRequest request,
|
||||
long operatorId, String operatorName, boolean wholeGroupAssignment,
|
||||
List<Candidate> frozenDetails, boolean recalculateFees) {
|
||||
String operation = operation(request.operationId());
|
||||
String requestHash = sha256("ASSIGN_INBOUND|" + planId + '|' + request.inboundId() + '|'
|
||||
+ request.packageCount() + '|' + request.expectedVersion());
|
||||
@@ -592,8 +608,13 @@ public class SmartLoadingService {
|
||||
if (!wholeGroupAssignment && projection != null && Set.of("PREPARING", "WAIT_MEASURE",
|
||||
"MEASURING", "MEASURE_PENDING", "MEASURE_REJECTED", "WAIT_PRICING",
|
||||
"FEE_CONFIRMING", "INBOUNDING").contains(projection.getProcessStatus())) {
|
||||
return assignDeclaredInbound(planId, request, projection, operation, requestHash,
|
||||
Plan assigned = assignDeclaredInbound(planId, request, projection, operation, requestHash,
|
||||
operatorId, operatorName);
|
||||
if (recalculateFees) {
|
||||
recalculateAfterRelationshipChange(requireBooking(repository.plan(planId).getContainerBookingId()).getId(),
|
||||
Set.of(projection.getTenantId()), operation);
|
||||
}
|
||||
return assigned;
|
||||
}
|
||||
LoadingPlanEntity plan = editing(planId, request.expectedVersion(), operatorId, operatorName);
|
||||
ContainerBookingEntity booking = requireBooking(plan.getContainerBookingId());
|
||||
@@ -626,6 +647,9 @@ public class SmartLoadingService {
|
||||
json(values("requestHash", requestHash, "packageCount", request.packageCount(),
|
||||
"specificationLineCount", sequence, "cartonIdentityStatus", "UNVERIFIED")),
|
||||
operatorId, operatorName);
|
||||
if (recalculateFees) {
|
||||
recalculateAfterRelationshipChange(booking.getId(), Set.of(first.accountId()), operation);
|
||||
}
|
||||
return view(assigned, booking);
|
||||
}
|
||||
|
||||
@@ -756,6 +780,8 @@ public class SmartLoadingService {
|
||||
json(values("requestHash", requestHash, "groupId", locked.groupId(),
|
||||
"memberCount", locked.members().size(), "groupTotalBoxes", locked.fullPackageCount(),
|
||||
"candidateInboundCount", memberCandidates.size())), operatorId, operatorName);
|
||||
recalculateAfterRelationshipChange(booking.getId(), locked.members().stream()
|
||||
.map(Member::tenantId).collect(java.util.stream.Collectors.toCollection(LinkedHashSet::new)), operation);
|
||||
return view(assigned, booking);
|
||||
}
|
||||
|
||||
@@ -795,6 +821,8 @@ public class SmartLoadingService {
|
||||
json(values("version", request.expectedPlanVersion(), "groupId", groupId)),
|
||||
json(values("requestHash", requestHash, "groupId", groupId, "deleted", true)),
|
||||
operatorId, operatorName);
|
||||
recalculateAfterRelationshipChange(booking.getId(), group.members().stream()
|
||||
.map(Member::tenantId).collect(java.util.stream.Collectors.toCollection(LinkedHashSet::new)), operation);
|
||||
return view(repository.plan(planId), booking);
|
||||
}
|
||||
|
||||
@@ -979,6 +1007,7 @@ public class SmartLoadingService {
|
||||
history(planId, itemId, "REMOVE_ITEM", plan.getPlanStatus(), plan.getPlanStatus(), operation,
|
||||
json(values("version", request.expectedPlanVersion(), "packageCount", item.getPackageCount())),
|
||||
json(values("requestHash", requestHash, "deleted", true)), operatorId, operatorName);
|
||||
recalculateAfterRelationshipChange(plan.getContainerBookingId(), Set.of(item.getAccountId()), operation);
|
||||
return view(repository.plan(planId), requireBooking(plan.getContainerBookingId()));
|
||||
}
|
||||
|
||||
@@ -1057,6 +1086,10 @@ public class SmartLoadingService {
|
||||
json(values("version", request.expectedPlanVersion(), "itemCount", items.size())),
|
||||
json(values("requestHash", requestHash, "removedItemCount", removable.size(),
|
||||
"removedGroupCount", removableGroupIds.size())), operatorId, operatorName);
|
||||
if (!removable.isEmpty()) {
|
||||
recalculateAfterRelationshipChange(booking.getId(), removable.stream().map(LoadingPlanItemEntity::getAccountId)
|
||||
.collect(java.util.stream.Collectors.toCollection(LinkedHashSet::new)), operation);
|
||||
}
|
||||
return view(repository.plan(planId), booking);
|
||||
}
|
||||
|
||||
@@ -1526,6 +1559,17 @@ public class SmartLoadingService {
|
||||
private static void requireSameCustomerScope(ContainerBookingEntity booking, Candidate candidate) {
|
||||
if (!sameCustomerScope(booking, candidate)) throw forbidden();
|
||||
}
|
||||
/**
|
||||
* A draft may legitimately contain pre-review or pre-measurement members without a fee snapshot.
|
||||
* The pricing port reports that as NOT_READY, while any sent/customer-confirmed member fails and
|
||||
* rolls this relation mutation back with the surrounding transaction.
|
||||
*/
|
||||
private void recalculateAfterRelationshipChange(long containerBookingId, Set<Long> tenantIds,
|
||||
String operation) {
|
||||
tenantIds.stream().filter(Objects::nonNull).filter(tenantId -> tenantId > 0).sorted()
|
||||
.forEach(tenantId -> fbaPricingService.recalculateUnlockedContainerCustomerFees(
|
||||
containerBookingId, tenantId, sha256("SMART_LOADING_RELATION|" + operation + '|' + tenantId)));
|
||||
}
|
||||
private void touch(LoadingPlanEntity p,long op,String name){if(repository.touch(p.getId(),p.getVersion(),op,name)!=1)throw conflict("计划已被并发修改");}
|
||||
private Plan replay(String operation, long planId, String action, Long itemId, String requestHash) {
|
||||
LoadingPlanHistoryEntity history = repository.operation(operation);
|
||||
|
||||
Reference in New Issue
Block a user