fix: make loading evidence replay idempotent

This commit is contained in:
2026-09-01 22:59:50 +08:00
parent d965a82cfd
commit e19224c4b7
3 changed files with 87 additions and 6 deletions
@@ -24,6 +24,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -46,7 +47,11 @@ public class CabinetLoadingEvidenceService {
public EvidenceList upsert(long taskId, String key, UpsertRequest request, long operatorId, String operatorName) {
Scope scope = scope(taskId, request.warehouseId(), operatorId, true);
requireKey(key);
String requestHash = hash(request);
return persist(scope, taskId, key, request, hash(request), operatorId, operatorName);
}
private EvidenceList persist(Scope scope, long taskId, String key, UpsertRequest request,
String requestHash, long operatorId, String operatorName) {
CabinetLoadingEvidenceEntity replay = mapper.byOperation(taskId, key);
if (replay != null) {
if (!Objects.equals(requestHash, replay.getRequestHash())) throw conflict("相同幂等键的请求内容不一致");
@@ -87,12 +92,23 @@ public class CabinetLoadingEvidenceService {
String evidenceType, String sealNo, String remark, MultipartFile multipart,
long operatorId, String operatorName) {
Scope scope = scope(taskId, warehouseId, operatorId, true);
requireKey(key);
String type = evidenceType.trim();
if (!LoadingEvidenceType.VALUES.contains(type)) throw bad("不支持的装柜证据类型");
String normalizedSealNo = LoadingEvidenceType.SEAL.equals(type) ? normalizedSeal(sealNo) : null;
String requestHash = hash(new UploadHashRequest(warehouseId, expectedVersion, type,
normalizedSealNo, remark, multipartHash(multipart)));
CabinetLoadingEvidenceEntity replay = mapper.byOperation(taskId, key);
if (replay != null) {
if (!Objects.equals(requestHash, replay.getRequestHash())) throw conflict("相同幂等键的请求内容不一致");
return view(scope);
}
try {
var response = files.upload(internalToken, operatorId, scope.containerBookingId(), evidenceType, multipart);
var response = files.upload(internalToken, operatorId, scope.containerBookingId(), type, multipart);
var uploaded = response == null ? null : response.data();
if (uploaded == null) throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "装柜证据文件上传失败");
return upsert(taskId, key, new UpsertRequest(warehouseId, expectedVersion, evidenceType,
uploaded.fileId(), sealNo, remark), operatorId, operatorName);
return persist(scope, taskId, key, new UpsertRequest(warehouseId, expectedVersion, type,
uploaded.fileId(), normalizedSealNo, remark), requestHash, operatorId, operatorName);
} catch (FeignException exception) {
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "文件服务暂不可用");
}
@@ -181,6 +197,15 @@ public class CabinetLoadingEvidenceService {
objectMapper.writeValueAsString(value).getBytes(StandardCharsets.UTF_8))); }
catch (JsonProcessingException | NoSuchAlgorithmException exception) { throw new IllegalStateException(exception); }
}
private static String multipartHash(MultipartFile file) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(file.getBytes()));
} catch (IOException exception) {
throw bad("装柜证据文件读取失败");
} catch (NoSuchAlgorithmException exception) {
throw new IllegalStateException(exception);
}
}
private static String normalizedSeal(String value) {
if (!StringUtils.hasText(value)) throw bad("铅封证据必须填写锁码");
return value.trim();
@@ -189,5 +214,7 @@ public class CabinetLoadingEvidenceService {
private static BizException bad(String message) { return new BizException(ResultCode.BAD_REQUEST, message); }
private static BizException conflict(String message) { return new BizException(ResultCode.CONFLICT, message); }
private static String id(Long value) { return value == null ? null : value.toString(); }
private record UploadHashRequest(long warehouseId, int expectedVersion, String evidenceType,
String sealNo, String remark, String fileHash) {}
private record Scope(LoadingPickTaskEntity task, long containerBookingId) {}
}