实现亲属资产服务与导入服务

This commit is contained in:
wkc
2026-03-13 09:47:58 +08:00
parent 70bdce7bda
commit e36f13b6b5
6 changed files with 152 additions and 65 deletions

View File

@@ -82,19 +82,17 @@ class CcdiAssetInfoImportServiceImplTest {
}
@Test
void importAssetInfoAsync_shouldResolveFamilyIdFromEmployeeIdCard() {
void importAssetInfoAsync_shouldResolveFamilyIdFromFamilyRelationForRelativeAsset() {
CcdiAssetInfoExcel excel = buildExcel("320101199001010011", "房产");
when(redisTemplate.opsForHash()).thenReturn(hashOperations);
when(assetInfoMapper.selectOwnerByEmployeeIdCards(List.of("320101199001010011")))
.thenReturn(List.of(owner("320101199001010011", "320101199001010011")));
when(assetInfoMapper.selectOwnerByFamilyRelationIdCards(List.of("320101199001010011")))
.thenReturn(List.of());
when(assetInfoMapper.selectOwnerCandidatesByRelationCertNos(List.of("320101199001010011")))
.thenReturn(List.of(owner("320101199001010011", "320101199009090099")));
service.importAssetInfoAsync(List.of(excel), "task-1", "tester");
ArgumentCaptor<List<CcdiAssetInfo>> captor = ArgumentCaptor.forClass(List.class);
verify(assetInfoMapper).insertBatch(captor.capture());
assertEquals("320101199001010011", captor.getValue().get(0).getFamilyId());
assertEquals("320101199009090099", captor.getValue().get(0).getFamilyId());
assertEquals("320101199001010011", captor.getValue().get(0).getPersonId());
}
@@ -102,9 +100,7 @@ class CcdiAssetInfoImportServiceImplTest {
void importAssetInfoAsync_shouldResolveFamilyIdFromFamilyRelationIdCard() {
CcdiAssetInfoExcel excel = buildExcel("320101199201010022", "车辆");
when(redisTemplate.opsForHash()).thenReturn(hashOperations);
when(assetInfoMapper.selectOwnerByEmployeeIdCards(List.of("320101199201010022")))
.thenReturn(List.of());
when(assetInfoMapper.selectOwnerByFamilyRelationIdCards(List.of("320101199201010022")))
when(assetInfoMapper.selectOwnerCandidatesByRelationCertNos(List.of("320101199201010022")))
.thenReturn(List.of(owner("320101199201010022", "320101199001010011")));
service.importAssetInfoAsync(List.of(excel), "task-2", "tester");
@@ -122,17 +118,15 @@ class CcdiAssetInfoImportServiceImplTest {
when(redisTemplate.opsForHash()).thenReturn(hashOperations);
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
when(assetInfoMapper.selectOwnerByEmployeeIdCards(List.of("320101199001010011", "320101199001010099")))
.thenReturn(List.of(owner("320101199001010011", "320101199001010011")));
when(assetInfoMapper.selectOwnerByFamilyRelationIdCards(List.of("320101199001010011", "320101199001010099")))
.thenReturn(List.of());
when(assetInfoMapper.selectOwnerCandidatesByRelationCertNos(List.of("320101199001010011", "320101199001010099")))
.thenReturn(List.of(owner("320101199001010011", "320101199009090099")));
service.importAssetInfoAsync(List.of(good, bad), "task-3", "tester");
ArgumentCaptor<List<CcdiAssetInfo>> insertCaptor = ArgumentCaptor.forClass(List.class);
verify(assetInfoMapper).insertBatch(insertCaptor.capture());
assertEquals(1, insertCaptor.getValue().size());
assertEquals("320101199001010011", insertCaptor.getValue().get(0).getFamilyId());
assertEquals("320101199009090099", insertCaptor.getValue().get(0).getFamilyId());
ArgumentCaptor<Object> failureCaptor = ArgumentCaptor.forClass(Object.class);
verify(valueOperations).set(eq("import:assetInfo:task-3:failures"), failureCaptor.capture(), eq(7L), eq(TimeUnit.DAYS));
@@ -140,7 +134,7 @@ class CcdiAssetInfoImportServiceImplTest {
assertEquals(1, failures.size());
AssetImportFailureVO failure = (AssetImportFailureVO) failures.get(0);
assertEquals("320101199001010099", failure.getPersonId());
assertTrue(failure.getErrorMessage().contains("未找到资产归属员工"));
assertTrue(failure.getErrorMessage().contains("未找到亲属资产归属员工"));
}
@Test
@@ -148,13 +142,11 @@ class CcdiAssetInfoImportServiceImplTest {
CcdiAssetInfoExcel excel = buildExcel("320101199201010022", "车辆");
when(redisTemplate.opsForHash()).thenReturn(hashOperations);
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
when(assetInfoMapper.selectOwnerByEmployeeIdCards(List.of("320101199201010022")))
when(assetInfoMapper.selectOwnerCandidatesByRelationCertNos(List.of("320101199201010022")))
.thenReturn(List.of(
owner("320101199201010022", "320101199001010011"),
owner("320101199201010022", "320101199001010033")
));
when(assetInfoMapper.selectOwnerByFamilyRelationIdCards(List.of("320101199201010022")))
.thenReturn(List.of());
service.importAssetInfoAsync(List.of(excel), "task-4", "tester");
@@ -162,7 +154,7 @@ class CcdiAssetInfoImportServiceImplTest {
ArgumentCaptor<Object> failureCaptor = ArgumentCaptor.forClass(Object.class);
verify(valueOperations).set(eq("import:assetInfo:task-4:failures"), failureCaptor.capture(), eq(7L), eq(TimeUnit.DAYS));
AssetImportFailureVO failure = (AssetImportFailureVO) ((List<?>) failureCaptor.getValue()).get(0);
assertTrue(failure.getErrorMessage().contains("资产归属员工不唯一"));
assertTrue(failure.getErrorMessage().contains("亲属资产归属员工不唯一"));
}
@Test
@@ -183,7 +175,7 @@ class CcdiAssetInfoImportServiceImplTest {
));
AssetImportFailureVO failureVO = new AssetImportFailureVO();
failureVO.setPersonId("320101199001010099");
failureVO.setErrorMessage("未找到资产归属员工");
failureVO.setErrorMessage("未找到亲属资产归属员工");
when(valueOperations.get("import:assetInfo:task-5:failures")).thenReturn(List.of(failureVO));
ImportStatusVO statusVO = service.getImportStatus("task-5");

View File

@@ -6,18 +6,23 @@ import com.ruoyi.info.collection.mapper.CcdiAssetInfoMapper;
import com.ruoyi.info.collection.service.impl.CcdiAssetInfoServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.invocation.Invocation;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -42,43 +47,64 @@ class CcdiAssetInfoServiceImplTest {
}
@Test
void replaceByFamilyId_shouldDeleteThenInsertNormalizedRows() {
CcdiAssetInfoDTO selfOwnedAsset = buildDto("320101199001010011", "房产");
CcdiAssetInfoDTO familyOwnedAsset = buildDto("320101199201010022", "车辆");
void replaceByFamilyIdAndPersonId_shouldDeleteThenInsertNormalizedRows() throws Exception {
CcdiAssetInfoDTO familyOwnedAsset = buildDto("房产");
CcdiAssetInfoDTO anotherAsset = buildDto("车辆");
service.replaceByFamilyId("320101199001010011", List.of(selfOwnedAsset, familyOwnedAsset));
Method method = CcdiAssetInfoServiceImpl.class.getMethod(
"replaceByFamilyIdAndPersonId", String.class, String.class, List.class);
method.invoke(service, "320101199001010011", "A123456789", List.of(familyOwnedAsset, anotherAsset));
verify(assetInfoMapper).deleteByFamilyId("320101199001010011");
Invocation deleteInvocation = mockingDetails(assetInfoMapper).getInvocations().stream()
.filter(invocation -> "deleteByFamilyIdAndPersonId".equals(invocation.getMethod().getName()))
.findFirst()
.orElseThrow();
assertEquals("320101199001010011", deleteInvocation.getArguments()[0]);
assertEquals("A123456789", deleteInvocation.getArguments()[1]);
ArgumentCaptor<List<CcdiAssetInfo>> captor = ArgumentCaptor.forClass(List.class);
verify(assetInfoMapper).insertBatch(captor.capture());
List<CcdiAssetInfo> savedList = captor.getValue();
assertEquals(2, savedList.size());
assertEquals("320101199001010011", savedList.get(0).getFamilyId());
assertEquals("320101199001010011", savedList.get(0).getPersonId());
assertEquals("A123456789", savedList.get(0).getPersonId());
assertEquals("320101199001010011", savedList.get(1).getFamilyId());
assertEquals("320101199201010022", savedList.get(1).getPersonId());
assertEquals("A123456789", savedList.get(1).getPersonId());
assertEquals("房产", savedList.get(0).getAssetMainType());
assertEquals("车辆", savedList.get(1).getAssetMainType());
}
@Test
void replaceByFamilyId_shouldIgnoreEmptyRows() {
void replaceByFamilyIdAndPersonId_shouldIgnoreEmptyRows() throws Exception {
CcdiAssetInfoDTO emptyRow = new CcdiAssetInfoDTO();
service.replaceByFamilyId("320101199001010011", List.of(emptyRow));
Method method = CcdiAssetInfoServiceImpl.class.getMethod(
"replaceByFamilyIdAndPersonId", String.class, String.class, List.class);
method.invoke(service, "320101199001010011", "A123456789", List.of(emptyRow));
verify(assetInfoMapper).deleteByFamilyId("320101199001010011");
Invocation deleteInvocation = mockingDetails(assetInfoMapper).getInvocations().stream()
.filter(invocation -> "deleteByFamilyIdAndPersonId".equals(invocation.getMethod().getName()))
.findFirst()
.orElseThrow();
assertEquals("320101199001010011", deleteInvocation.getArguments()[0]);
assertEquals("A123456789", deleteInvocation.getArguments()[1]);
verify(assetInfoMapper, never()).insertBatch(anyList());
}
@Test
void deleteByFamilyId_shouldDelegateToMapper() {
when(assetInfoMapper.deleteByFamilyId("320101199001010011")).thenReturn(1);
void replaceByFamilyIdAndPersonId_shouldValidateRequiredFields() throws Exception {
CcdiAssetInfoDTO invalid = new CcdiAssetInfoDTO();
invalid.setAssetMainType("房产");
invalid.setAssetSubType("商品房");
invalid.setAssetName("测试房产");
invalid.setAssetStatus("正常");
int result = service.deleteByFamilyId("320101199001010011");
Method method = CcdiAssetInfoServiceImpl.class.getMethod(
"replaceByFamilyIdAndPersonId", String.class, String.class, List.class);
InvocationTargetException exception = assertThrows(InvocationTargetException.class,
() -> method.invoke(service, "320101199001010011", "A123456789", List.of(invalid)));
assertEquals(1, result);
assertEquals("当前估值不能为空", exception.getCause().getMessage());
}
@Test
@@ -92,9 +118,8 @@ class CcdiAssetInfoServiceImplTest {
verify(assetInfoMapper).deleteByFamilyIds(eq(familyIds));
}
private CcdiAssetInfoDTO buildDto(String personId, String assetMainType) {
private CcdiAssetInfoDTO buildDto(String assetMainType) {
CcdiAssetInfoDTO dto = new CcdiAssetInfoDTO();
dto.setPersonId(personId);
dto.setAssetMainType(assetMainType);
dto.setAssetSubType(assetMainType + "小类");
dto.setAssetName(assetMainType + "名称");

View File

@@ -56,8 +56,8 @@ class CcdiBaseStaffServiceImplTest {
addDTO.setPhone("13812345678");
addDTO.setStatus("0");
addDTO.setAssetInfoList(List.of(
buildAssetDto("320101199001010011", "房产"),
buildAssetDto("320101199201010022", "车辆")
buildAssetDto("房产"),
buildAssetDto("车辆")
));
when(baseStaffMapper.selectById(1001L)).thenReturn(null);
@@ -71,8 +71,8 @@ class CcdiBaseStaffServiceImplTest {
verify(assetInfoService).replaceByFamilyId(eq("320101199001010011"), captor.capture());
List<CcdiAssetInfoDTO> savedAssets = captor.getValue();
assertEquals(2, savedAssets.size());
assertEquals("320101199001010011", savedAssets.get(0).getPersonId());
assertEquals("320101199201010022", savedAssets.get(1).getPersonId());
assertEquals("房产", savedAssets.get(0).getAssetMainType());
assertEquals("车辆", savedAssets.get(1).getAssetMainType());
}
@Test
@@ -88,7 +88,7 @@ class CcdiBaseStaffServiceImplTest {
editDTO.setIdCard("320101199001010011");
editDTO.setPhone("13812345678");
editDTO.setStatus("0");
editDTO.setAssetInfoList(List.of(buildAssetDto("320101199201010022", "车辆")));
editDTO.setAssetInfoList(List.of(buildAssetDto("车辆")));
when(baseStaffMapper.selectById(1001L)).thenReturn(existing);
when(baseStaffMapper.selectCount(any())).thenReturn(0L);
@@ -114,7 +114,7 @@ class CcdiBaseStaffServiceImplTest {
editDTO.setIdCard("320101199001010011");
editDTO.setPhone("13812345678");
editDTO.setStatus("0");
editDTO.setAssetInfoList(List.of(buildAssetDto("320101199201010022", "车辆")));
editDTO.setAssetInfoList(List.of(buildAssetDto("车辆")));
when(baseStaffMapper.selectById(1001L)).thenReturn(existing);
when(baseStaffMapper.selectCount(any())).thenReturn(0L);
@@ -172,9 +172,8 @@ class CcdiBaseStaffServiceImplTest {
verify(assetInfoService).deleteByFamilyIds(List.of("320101199001010011", "320101199001010022"));
}
private CcdiAssetInfoDTO buildAssetDto(String personId, String assetMainType) {
private CcdiAssetInfoDTO buildAssetDto(String assetMainType) {
CcdiAssetInfoDTO dto = new CcdiAssetInfoDTO();
dto.setPersonId(personId);
dto.setAssetMainType(assetMainType);
dto.setAssetSubType(assetMainType + "小类");
dto.setAssetName(assetMainType + "名称");