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();
|
||||
|
||||
+36
@@ -14,11 +14,18 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -98,6 +105,35 @@ class CabinetLoadingEvidenceServiceTest {
|
||||
verify(mapper).insert(org.mockito.ArgumentMatchers.any(CabinetLoadingEvidenceEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void multipartReplayUsesContentHashBeforeUploadingAnotherFile() {
|
||||
var multipart = new MockMultipartFile("file", "photo.png", "image/png", new byte[] {1, 2, 3});
|
||||
var upload = new LoadingEvidenceFileClient.UploadResult(301L, "PLATFORM", 0L,
|
||||
"LOADING_PHOTO_STEP_1", "99", "photo.png", "image/png", 3L);
|
||||
var metadata = new LoadingEvidenceFileClient.FileMetadata(301L, "PLATFORM", 0L,
|
||||
"LOADING_PHOTO_STEP_1", "99", "photo.png", "image/png");
|
||||
when(files.upload(anyString(), anyLong(), anyLong(), anyString(), any()))
|
||||
.thenReturn(com.baiyue.common.core.api.ApiResponse.success(upload));
|
||||
when(files.validate(anyString(), anyLong(), anyString(), anyLong()))
|
||||
.thenReturn(com.baiyue.common.core.api.ApiResponse.success(metadata));
|
||||
when(mapper.byContainer(99L)).thenReturn(List.of());
|
||||
|
||||
service.upload(20L, 8L, "same-key", 0, "PHOTO_STEP_1", null, "remark",
|
||||
multipart, 9L, "operator");
|
||||
|
||||
ArgumentCaptor<CabinetLoadingEvidenceEntity> inserted =
|
||||
ArgumentCaptor.forClass(CabinetLoadingEvidenceEntity.class);
|
||||
verify(mapper).insert(inserted.capture());
|
||||
when(mapper.byOperation(20L, "same-key")).thenReturn(inserted.getValue());
|
||||
when(mapper.byContainer(99L)).thenReturn(List.of(inserted.getValue()));
|
||||
|
||||
var replay = service.upload(20L, 8L, "same-key", 0, "PHOTO_STEP_1", null, "remark",
|
||||
multipart, 9L, "operator");
|
||||
|
||||
assertThat(replay.evidences()).hasSize(1);
|
||||
verify(files, times(1)).upload(anyString(), anyLong(), anyLong(), anyString(), any());
|
||||
}
|
||||
|
||||
private static CabinetLoadingEvidenceEntity evidence(String type) {
|
||||
CabinetLoadingEvidenceEntity value = new CabinetLoadingEvidenceEntity();
|
||||
value.setEvidenceType(type); value.setStatus("ACTIVE"); value.setContainerBookingId(99L);
|
||||
|
||||
Reference in New Issue
Block a user