fix(firstleg): make loading evidence upload replayable
This commit is contained in:
+33
-2
@@ -25,6 +25,7 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.io.IOException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
@@ -44,9 +45,13 @@ public class CabinetLoadingEvidenceService {
|
||||
|
||||
@Transactional
|
||||
public EvidenceList upsert(long taskId, String key, UpsertRequest request, long operatorId, String operatorName) {
|
||||
return upsert(taskId, key, request, operatorId, operatorName, hash(request));
|
||||
}
|
||||
|
||||
private EvidenceList upsert(long taskId, String key, UpsertRequest request, long operatorId,
|
||||
String operatorName, String requestHash) {
|
||||
Scope scope = scope(taskId, request.warehouseId(), operatorId, true);
|
||||
requireKey(key);
|
||||
String requestHash = hash(request);
|
||||
CabinetLoadingEvidenceEntity replay = mapper.byOperation(taskId, key);
|
||||
if (replay != null) {
|
||||
if (!Objects.equals(requestHash, replay.getRequestHash())) throw conflict("相同幂等键的请求内容不一致");
|
||||
@@ -87,12 +92,21 @@ 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 requestHash = uploadHash(warehouseId, expectedVersion, evidenceType, sealNo, remark, 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 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);
|
||||
uploaded.fileId(), sealNo, remark), operatorId, operatorName, requestHash);
|
||||
} catch (FeignException exception) {
|
||||
throw new BizException(ResultCode.SERVICE_UNAVAILABLE, "文件服务暂不可用");
|
||||
}
|
||||
@@ -181,6 +195,23 @@ public class CabinetLoadingEvidenceService {
|
||||
objectMapper.writeValueAsString(value).getBytes(StandardCharsets.UTF_8))); }
|
||||
catch (JsonProcessingException | NoSuchAlgorithmException exception) { throw new IllegalStateException(exception); }
|
||||
}
|
||||
private static String uploadHash(long warehouseId, int expectedVersion, String evidenceType,
|
||||
String sealNo, String remark, MultipartFile multipart) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
String metadata = warehouseId + "|" + expectedVersion + "|" + evidenceType + "|"
|
||||
+ Objects.toString(sealNo, "") + "|" + Objects.toString(remark, "") + "|"
|
||||
+ Objects.toString(multipart.getOriginalFilename(), "") + "|"
|
||||
+ Objects.toString(multipart.getContentType(), "") + "|" + multipart.getSize() + "|";
|
||||
digest.update(metadata.getBytes(StandardCharsets.UTF_8));
|
||||
digest.update(multipart.getBytes());
|
||||
return HexFormat.of().formatHex(digest.digest());
|
||||
} 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();
|
||||
|
||||
Reference in New Issue
Block a user