This commit is contained in:
2026-08-07 18:44:51 +08:00
parent 1a76602872
commit 37fec1e012
24 changed files with 601 additions and 24 deletions
@@ -151,9 +151,11 @@ public class PlatformInboundMeasurementController {
@BusinessAudit(event = "INBOUND_WEIGHT_OCR_CONFIRM", resourceType = "INBOUND_WEIGHT_RESULT",
resourceId = "#resultId", description = "确认 OCR 重量并累加实际总毛重")
@Operation(summary = "确认 OCR 重量", description = "统一换算为 KG 并重算所有已确认图片的总重量。")
@Operation(summary = "确认 OCR 重量",
description = "可在同一事务中携带人工修改值并确认;统一换算为 KG 后重算所有已确认图片的总重量。")
@PostMapping("/weight-results/{resultId}/confirm")
@PreAuthorize("hasAuthority('platform:firstleg:inbound:weight:confirm')")
@PreAuthorize("hasAuthority('platform:firstleg:inbound:weight:confirm')"
+ " and (!#request.hasAdjustment() or hasAuthority('platform:firstleg:inbound:weight:edit'))")
public ApiResponse<InboundMeasurementResponses.WeightResult> confirmWeightResult(
@AuthenticationPrincipal AuthenticatedUser user,
@RequestParam long tenantId,
@@ -94,7 +94,13 @@ public final class InboundMeasurementRequests {
}
public record ConfirmWeightResultRequest(
@NotNull @PositiveOrZero Integer version
@NotNull @PositiveOrZero Integer version,
@DecimalMin("0.001") @Digits(integer = 15, fraction = 3) BigDecimal adjustedWeightValue,
@Size(max = 8) String adjustedWeightUnit,
@Size(max = 500) String adjustmentReason
) {
public boolean hasAdjustment() {
return adjustedWeightValue != null || adjustedWeightUnit != null || adjustmentReason != null;
}
}
}
@@ -156,13 +156,22 @@ public class InboundMeasurementService {
InboundOrderEntity inbound = requireInbound(tenantId, inboundId);
InboundWeightResultEntity weight = requireWeight(tenantId, inboundId, resultId);
requireVersion(request.version(), weight.getVersion());
String adjustedUnit = normalizeUnit(request.adjustedWeightUnit());
applyWeightCorrection(weight, request.version(), request.adjustedWeightValue(),
request.adjustedWeightUnit(), request.adjustmentReason(), operatorId, operatorName);
return toWeight(weight);
}
private int applyWeightCorrection(
InboundWeightResultEntity weight, int expectedVersion, BigDecimal adjustedWeightValue,
String adjustedWeightUnit, String adjustmentReason, long operatorId, String operatorName) {
requireCompleteAdjustment(adjustedWeightValue, adjustedWeightUnit, adjustmentReason);
String adjustedUnit = normalizeUnit(adjustedWeightUnit);
BigDecimal beforeValue = effectiveValue(weight);
String beforeUnit = effectiveUnit(weight);
weight.setAdjustedWeightValue(request.adjustedWeightValue());
weight.setAdjustedWeightValue(adjustedWeightValue);
weight.setAdjustedWeightUnit(adjustedUnit);
weight.setAdjustmentReason(request.adjustmentReason());
weight.setAdjustmentReason(adjustmentReason.trim());
weight.setAdjustedBy(operatorId);
weight.setAdjustedAt(LocalDateTime.now());
weight.setFinalWeightKg(null);
@@ -173,10 +182,12 @@ public class InboundMeasurementService {
if (!repository.updateWeight(weight)) {
throw concurrentModification();
}
insertRevision(weight, request.version() + 1, "CORRECT", beforeValue, beforeUnit,
request.adjustedWeightValue(), adjustedUnit, null, request.adjustmentReason(),
int correctedVersion = expectedVersion + 1;
weight.setVersion(correctedVersion);
insertRevision(weight, correctedVersion, "CORRECT", beforeValue, beforeUnit,
adjustedWeightValue, adjustedUnit, null, adjustmentReason.trim(),
operatorId, operatorName);
return toWeight(weight);
return correctedVersion;
}
@Transactional
@@ -186,7 +197,12 @@ public class InboundMeasurementService {
InboundOrderEntity inbound = requireInbound(tenantId, inboundId);
InboundWeightResultEntity weight = requireWeight(tenantId, inboundId, resultId);
requireVersion(request.version(), weight.getVersion());
if (WEIGHT_CONFIRMED.equals(weight.getRecognitionStatus())) {
int currentVersion = request.version();
if (request.hasAdjustment()) {
currentVersion = applyWeightCorrection(weight, currentVersion,
request.adjustedWeightValue(), request.adjustedWeightUnit(),
request.adjustmentReason(), operatorId, operatorName);
} else if (WEIGHT_CONFIRMED.equals(weight.getRecognitionStatus())) {
return toWeight(weight);
}
BigDecimal value = effectiveValue(weight);
@@ -204,7 +220,9 @@ public class InboundMeasurementService {
if (!repository.updateWeight(weight)) {
throw concurrentModification();
}
insertRevision(weight, request.version() + 1, "CONFIRM", value, unit, value, unit,
int confirmedVersion = currentVersion + 1;
weight.setVersion(confirmedVersion);
insertRevision(weight, confirmedVersion, "CONFIRM", value, unit, value, unit,
finalKg, null, operatorId, operatorName);
recalculateConfirmedWeight(inbound, operatorId);
return toWeight(weight);
@@ -235,6 +253,13 @@ public class InboundMeasurementService {
return isPositive(total) ? total : null;
}
private void requireCompleteAdjustment(BigDecimal value, String unit, String reason) {
if (!isPositive(value) || !StringUtils.hasText(unit) || !StringUtils.hasText(reason)) {
throw new BizException(ResultCode.VALIDATION_FAILED,
"人工修改重量时必须同时填写有效重量、单位和修改原因");
}
}
private void insertRevision(InboundWeightResultEntity weight, int revisionNo, String action,
BigDecimal beforeValue, String beforeUnit,
BigDecimal afterValue, String afterUnit, BigDecimal finalWeightKg,