贴皮文件上传修正

This commit is contained in:
gaoqr
2026-07-08 16:48:33 +08:00
parent 74c794253b
commit bec9f04c86
5 changed files with 48 additions and 10 deletions
@@ -28,6 +28,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@@ -116,8 +117,9 @@ public class VeneerController {
@PostMapping("/pic") @PostMapping("/pic")
@Operation(summary = "上传贴皮照片") @Operation(summary = "上传贴皮照片")
@OperateLog(logArgs = false, type = CREATE) @OperateLog(logArgs = false, type = CREATE)
public CommonResult<Long> uploadVeneerPic(@RequestPart(value = "file") MultipartFile file) throws IOException { public CommonResult<Long> uploadVeneerPic(@RequestParam("id") Long id,
return success(veneerService.uploadPic(file)); @RequestPart(value = "file") MultipartFile file) throws IOException {
return success(veneerService.uploadPic(id, file));
} }
@PostMapping("/defect") @PostMapping("/defect")
@@ -50,10 +50,11 @@ public interface VeneerService {
/** /**
* 上传贴皮照片 * 上传贴皮照片
* *
* @param file * @param id 贴皮id
* @param file 贴皮图片
* @return * @return
*/ */
Long uploadPic(MultipartFile file) throws IOException; Long uploadPic(Long id, MultipartFile file) throws IOException;
/** /**
* 贴皮导入模版下载 * 贴皮导入模版下载
@@ -127,7 +127,11 @@ public class VeneerServiceImpl implements VeneerService {
} }
@Override @Override
public Long uploadPic(MultipartFile file) throws IOException { public Long uploadPic(Long id, MultipartFile file) throws IOException {
if (ObjectUtil.isNull(validateExist(id))) {
throw new ServiceException(VENEER_NOT_EXIST);
}
Long organId = SecurityFrameworkUtils.getUserOrganId(); Long organId = SecurityFrameworkUtils.getUserOrganId();
// 获取组织名称 // 获取组织名称
@@ -138,7 +142,11 @@ public class VeneerServiceImpl implements VeneerService {
} else { } else {
organizationDTO = details.getData(); organizationDTO = details.getData();
} }
return fileApi.createFileAndReturnId(file.getBytes(), String.format("%s-veneer-pic-file-%s.%s", organizationDTO.getName(), LocalDateTimeUtil.format(LocalDateTime.now(), PURE_DATETIME_PATTERN), getFileExtension(file.getOriginalFilename()))); Long fileId = fileApi.createFileAndReturnId(file.getBytes(), String.format("%s-veneer-pic-file-%s.%s", organizationDTO.getName(), LocalDateTimeUtil.format(LocalDateTime.now(), PURE_DATETIME_PATTERN), getFileExtension(file.getOriginalFilename())));
veneerMapper.update(new LambdaUpdateWrapper<VeneerDO>()
.eq(VeneerDO::getId, id)
.set(VeneerDO::getFileId, fileId));
return fileId;
} }
@Override @Override
@@ -161,10 +161,11 @@ class VeneerControllerTest extends BaseWebUnitTest {
"file", "a.jpg", "image/jpeg", "123".getBytes() "file", "a.jpg", "image/jpeg", "123".getBytes()
); );
when(veneerService.uploadPic(any())).thenReturn(10L); when(veneerService.uploadPic(eq(1L), any())).thenReturn(10L);
mockMvc.perform(multipart("/executor/veneer/pic") mockMvc.perform(multipart("/executor/veneer/pic")
.file(file)) .file(file)
.param("id", "1"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.data").value(10L)); .andExpect(jsonPath("$.data").value(10L));
} }
@@ -259,6 +259,9 @@ class VeneerServiceImplTest extends BaseMockitoUnitTest {
@Test @Test
@WithMockLoginUser(organId = 1L) @WithMockLoginUser(organId = 1L)
void uploadPic_success() throws Exception { void uploadPic_success() throws Exception {
VeneerDO veneerDO = new VeneerDO();
veneerDO.setId(1L);
when(veneerMapper.selectById(1L)).thenReturn(veneerDO);
// mock organApi // mock organApi
OrganizationDTO dto = new OrganizationDTO(); OrganizationDTO dto = new OrganizationDTO();
@@ -279,7 +282,7 @@ class VeneerServiceImplTest extends BaseMockitoUnitTest {
"test-content".getBytes() "test-content".getBytes()
); );
Long fileId = veneerService.uploadPic(file); Long fileId = veneerService.uploadPic(1L, file);
assertEquals(100L, fileId); assertEquals(100L, fileId);
@@ -288,11 +291,16 @@ class VeneerServiceImplTest extends BaseMockitoUnitTest {
any(byte[].class), any(byte[].class),
argThat(name -> name.contains("测试组织-veneer-pic-file-")) argThat(name -> name.contains("测试组织-veneer-pic-file-"))
); );
verify(veneerMapper).update(argThat((LambdaUpdateWrapper<VeneerDO> wrapper) ->
wrapper.getSqlSegment().contains("id") && wrapper.getSqlSet().contains("file_id")));
} }
@Test @Test
@WithMockLoginUser(organId = 1L) @WithMockLoginUser(organId = 1L)
void uploadPic_fail_when_organ_api_error() { void uploadPic_fail_when_organ_api_error() {
VeneerDO veneerDO = new VeneerDO();
veneerDO.setId(1L);
when(veneerMapper.selectById(1L)).thenReturn(veneerDO);
when(organApi.getOrganDetails(1L)) when(organApi.getOrganDetails(1L))
.thenReturn(CommonResult.error(500, "组织服务异常")); .thenReturn(CommonResult.error(500, "组织服务异常"));
@@ -306,13 +314,31 @@ class VeneerServiceImplTest extends BaseMockitoUnitTest {
ServiceException ex = assertThrows( ServiceException ex = assertThrows(
ServiceException.class, ServiceException.class,
() -> veneerService.uploadPic(file) () -> veneerService.uploadPic(1L, file)
); );
assertEquals(500, ex.getCode()); assertEquals(500, ex.getCode());
assertEquals("组织服务异常", ex.getMessage()); assertEquals("组织服务异常", ex.getMessage());
verify(fileApi, never()).createFileAndReturnId(any(), any()); verify(fileApi, never()).createFileAndReturnId(any(), any());
verify(veneerMapper, never()).update(any());
}
@Test
void uploadPic_fail_when_veneer_not_exists() {
MockMultipartFile file = new MockMultipartFile(
"file",
"test.jpg",
"image/jpeg",
"content".getBytes()
);
assertServiceException(() -> veneerService.uploadPic(999L, file),
VENEER_NOT_EXIST);
verify(organApi, never()).getOrganDetails(anyLong());
verify(fileApi, never()).createFileAndReturnId(any(), any());
verify(veneerMapper, never()).update(any());
} }
@Test @Test