fix: scope smart loading recommendations

This commit is contained in:
2026-09-14 17:22:09 +08:00
parent 8a1e587ee4
commit 64bddb1f81
10 changed files with 199 additions and 44 deletions
@@ -76,7 +76,8 @@ public final class SmartLoadingDtos { private SmartLoadingDtos(){}
public record RemoveGroupRequest(@NotNull @Min(0) Integer expectedPlanVersion,
@NotBlank @Size(max=128) String operationId) {}
public record RecommendRequest(@NotNull @Min(0) Integer expectedVersion,
@NotBlank @Size(max=128) String operationId) {}
@NotBlank @Size(max=128) String operationId,
@Positive Long customerId, @Size(max=64) String inboundCode) {}
public record AdjustRequest(@NotNull @Positive Integer packageCount,@NotNull @Min(0) Integer expectedPlanVersion,
@NotNull @Min(0) Integer expectedItemVersion,@NotBlank @Size(max=128) String operationId) {}
public record DeleteRequest(@NotNull @Min(0) Integer expectedPlanVersion,
@@ -106,9 +106,14 @@ public class SmartLoadingService {
return grouped.values().stream().flatMap(details -> inboundCandidates(details).stream()).toList();
}
private List<InboundCandidate> recommendationCandidates(ContainerBookingEntity booking) {
private List<InboundCandidate> recommendationCandidates(ContainerBookingEntity booking,
Long customerId, String inboundCode) {
List<Candidate> values = repository.candidates(null).stream()
.filter(candidate -> sameChannel(booking, candidate) && sameCustomerScope(booking, candidate)).toList();
.filter(candidate -> sameChannel(booking, candidate) && sameCustomerScope(booking, candidate))
.filter(candidate -> customerId == null || Objects.equals(customerId, candidate.accountId()))
.filter(candidate -> !StringUtils.hasText(inboundCode)
|| inboundCode.trim().equals(candidate.inboundCode()))
.toList();
Map<Long, List<Candidate>> grouped = enrichColoadCandidates(booking,
repository.planByBooking(booking.getId()), values).stream()
.collect(java.util.stream.Collectors.groupingBy(Candidate::inboundId, LinkedHashMap::new,
@@ -442,6 +447,11 @@ public class SmartLoadingService {
@BusinessAudit(event = "SMART_LOADING_PALLET_ASSIGN", resourceType = "LOADING_PLAN",
resourceId = "#planId", description = "按已封托托盘分配智能装柜明细")
public Plan assignPallet(long planId, AssignPalletRequest request, long operatorId, String operatorName) {
return assignPallet(planId, request, operatorId, operatorName, true);
}
private Plan assignPallet(long planId, AssignPalletRequest request,
long operatorId, String operatorName, boolean recalculateFees) {
String operation = operation(request.operationId());
String requestHash = sha256("ASSIGN_PALLET|" + planId + '|' + request.palletId() + '|'
+ request.expectedVersion());
@@ -475,7 +485,10 @@ public class SmartLoadingService {
json(values("palletId", request.palletId(), "version", request.expectedVersion())),
json(values("requestHash", requestHash, "palletNo", first.getPalletNo(), "lineCount", lines.size())),
operatorId, operatorName);
recalculateAfterRelationshipChange(assigned.getContainerBookingId(), Set.of(first.getAccountId()), operation);
if (recalculateFees) {
recalculateAfterRelationshipChange(assigned.getContainerBookingId(),
Set.of(first.getAccountId()), operation);
}
return view(assigned, requireBooking(assigned.getContainerBookingId()));
}
@@ -901,14 +914,18 @@ public class SmartLoadingService {
resourceId = "#planId", description = "执行智能装柜推荐")
public Plan recommend(long planId, RecommendRequest request, long operatorId, String operatorName) {
String operation = operation(request.operationId());
String requestHash = sha256("RECOMMEND|" + planId + '|' + request.expectedVersion());
String scopedInboundCode = trim(request.inboundCode());
String requestHash = sha256("RECOMMEND|" + planId + '|' + request.expectedVersion()
+ '|' + request.customerId() + '|' + scopedInboundCode);
Plan replay = replay(operation, planId, "SMART_RECOMMEND", null, requestHash);
if (replay != null) return replay;
LoadingPlanEntity plan = editing(planId, request.expectedVersion(), operatorId, operatorName);
ContainerBookingEntity booking = requireBooking(plan.getContainerBookingId());
Set<Long> visitedGroups = new HashSet<>();
Set<Long> visitedPallets = new HashSet<>();
for (InboundCandidate candidate : recommendationCandidates(booking)) {
Set<Long> affectedCustomers = new HashSet<>();
for (InboundCandidate candidate : recommendationCandidates(booking,
request.customerId(), scopedInboundCode)) {
if (candidate.coLoadGroupId() != null) {
if (!visitedGroups.add(candidate.coLoadGroupId()) || StringUtils.hasText(candidate.blockReason())) {
continue;
@@ -943,7 +960,8 @@ public class SmartLoadingService {
plan = repository.plan(planId);
assignPallet(planId, new AssignPalletRequest(candidate.palletId(), plan.getVersion(),
"REC-PAL-" + sha256(operation + '|' + candidate.palletId()).substring(0, 48)),
operatorId, operatorName);
operatorId, operatorName, false);
affectedCustomers.add(candidate.accountId());
continue;
}
int count = fittingBoxes(booking, repository.items(planId), candidate.availablePackageCount(),
@@ -952,12 +970,15 @@ public class SmartLoadingService {
plan = repository.plan(planId);
assignInbound(planId, new AssignInboundRequest(candidate.inboundId(), count,
plan.getVersion(), "REC-INB-" + sha256(operation + '|' + candidate.inboundId())
.substring(0, 48)), operatorId, operatorName);
.substring(0, 48)), operatorId, operatorName, false, null, false);
affectedCustomers.add(candidate.accountId());
}
}
recalculateAfterRelationshipChange(booking.getId(), affectedCustomers, operation);
LoadingPlanEntity recommended = repository.plan(planId);
history(planId, null, "SMART_RECOMMEND", plan.getPlanStatus(), recommended.getPlanStatus(), operation,
json(values("version", request.expectedVersion())),
json(values("version", request.expectedVersion(), "customerId", request.customerId(),
"inboundCode", scopedInboundCode)),
json(values("requestHash", requestHash, "version", recommended.getVersion())),
operatorId, operatorName);
return view(recommended, requireBooking(plan.getContainerBookingId()));
@@ -678,6 +678,20 @@ paths:
responses:
'200': {description: 成功、计划不存在或计划已进入预装流程}
'403': {description: 无智能装柜查询权限}
/api/platform/v1/first-leg/smart-loading/plans/{planId}/recommend:
post:
summary: 按当前筛选范围执行智能装柜推荐
description: >-
需要 platform:firstleg:smart-loading:assign 权限。customerId 与 inboundCode 均为可选推荐范围;
传入时只推荐同时命中的候选,避免把同渠道历史候选批量加入当前柜。未传时按柜次渠道推荐全部可用候选。
一件代发仍按每个已封托托盘整体加入,不得按箱拆分;同一次推荐完成后按受影响客户统一重算费用。
parameters:
- {name: planId, in: path, required: true, schema: {type: string, pattern: '^[1-9][0-9]*$'}}
requestBody:
required: true
content: {application/json: {schema: {$ref: '#/components/schemas/SmartLoadingRecommendRequest'}}}
responses:
'200': {description: 成功或业务失败,data 为最新 SmartLoadingPlan}
/api/platform/v1/first-leg/smart-loading/plans/{planId}/assignments:
post:
summary: 向预装计划新增入库单明细
@@ -2304,6 +2318,14 @@ components:
packageCount: {type: integer, minimum: 1, description: 待审核订单本次占用的申报箱数}
expectedVersion: {type: integer, minimum: 0, description: 当前 planVersion}
operationId: {type: string, minLength: 1, maxLength: 128}
SmartLoadingRecommendRequest:
type: object
required: [expectedVersion, operationId]
properties:
expectedVersion: {type: integer, minimum: 0, description: 当前 planVersion}
operationId: {type: string, minLength: 1, maxLength: 128}
customerId: {type: string, pattern: '^[1-9][0-9]*$', nullable: true, description: 当前页面客户筛选;传入时限制推荐范围}
inboundCode: {type: string, minLength: 1, maxLength: 64, nullable: true, description: 当前页面入仓码筛选;传入时限制推荐范围}
SmartLoadingAdjustRequest:
type: object
required: [packageCount, expectedPlanVersion, expectedItemVersion, operationId]