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

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

@@ -21,6 +21,15 @@ public interface ICcdiAssetInfoService {
*/
List<CcdiAssetInfo> selectByFamilyId(String familyId);
/**
* 按归属键查询资产列表
*
* @param familyId 归属员工证件号
* @param personId 资产实际持有人证件号
* @return 资产列表
*/
List<CcdiAssetInfo> selectByFamilyIdAndPersonId(String familyId, String personId);
/**
* 按归属员工身份证号覆盖资产列表
*
@@ -29,6 +38,15 @@ public interface ICcdiAssetInfoService {
*/
void replaceByFamilyId(String familyId, List<CcdiAssetInfoDTO> assetInfoList);
/**
* 按归属键覆盖资产列表
*
* @param familyId 归属员工证件号
* @param personId 资产实际持有人证件号
* @param assetInfoList 资产列表
*/
void replaceByFamilyIdAndPersonId(String familyId, String personId, List<CcdiAssetInfoDTO> assetInfoList);
/**
* 删除单个员工资产
*
@@ -37,6 +55,15 @@ public interface ICcdiAssetInfoService {
*/
int deleteByFamilyId(String familyId);
/**
* 按归属键删除资产
*
* @param familyId 归属员工证件号
* @param personId 资产实际持有人证件号
* @return 影响行数
*/
int deleteByFamilyIdAndPersonId(String familyId, String personId);
/**
* 批量删除员工资产
*

View File

@@ -30,7 +30,7 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* 员工资产信息异步导入服务层处理
* 亲属资产信息异步导入服务层处理
*
* @author ruoyi
* @date 2026-03-12
@@ -97,10 +97,10 @@ public class CcdiAssetInfoImportServiceImpl implements ICcdiAssetInfoImportServi
validateExcel(excel);
Set<String> familyIds = ownerMap.get(excel.getPersonId());
if (familyIds == null || familyIds.isEmpty()) {
throw new RuntimeException("未找到资产归属员工");
throw new RuntimeException("未找到亲属资产归属员工");
}
if (familyIds.size() > 1) {
throw new RuntimeException("资产归属员工不唯一");
throw new RuntimeException("亲属资产归属员工不唯一");
}
CcdiAssetInfo assetInfo = new CcdiAssetInfo();
@@ -164,8 +164,7 @@ public class CcdiAssetInfoImportServiceImpl implements ICcdiAssetInfoImportServi
private Map<String, Set<String>> buildOwnerMap(List<String> personIds) {
Map<String, Set<String>> result = new LinkedHashMap<>();
mergeOwnerMappings(result, assetInfoMapper.selectOwnerByEmployeeIdCards(personIds));
mergeOwnerMappings(result, assetInfoMapper.selectOwnerByFamilyRelationIdCards(personIds));
mergeOwnerMappings(result, assetInfoMapper.selectOwnerCandidatesByRelationCertNos(personIds));
return result;
}
@@ -185,7 +184,7 @@ public class CcdiAssetInfoImportServiceImpl implements ICcdiAssetInfoImportServi
private void validateExcel(CcdiAssetInfoExcel excel) {
if (StringUtils.isEmpty(excel.getPersonId())) {
throw new RuntimeException("资产实际持有人身份证号不能为空");
throw new RuntimeException("关系人证件号不能为空");
}
if (StringUtils.isEmpty(excel.getAssetMainType())) {
throw new RuntimeException("资产大类不能为空");

View File

@@ -30,22 +30,21 @@ public class CcdiAssetInfoServiceImpl implements ICcdiAssetInfoService {
return assetInfoMapper.selectByFamilyId(familyId);
}
@Override
public List<CcdiAssetInfo> selectByFamilyIdAndPersonId(String familyId, String personId) {
return assetInfoMapper.selectByFamilyIdAndPersonId(familyId, personId);
}
@Override
@Transactional
public void replaceByFamilyId(String familyId, List<CcdiAssetInfoDTO> assetInfoList) {
assetInfoMapper.deleteByFamilyId(familyId);
if (assetInfoList == null || assetInfoList.isEmpty()) {
return;
}
replaceAssets(familyId, familyId, assetInfoList, true);
}
List<CcdiAssetInfo> saveList = assetInfoList.stream()
.filter(item -> !isEmptyRow(item))
.map(item -> toEntity(familyId, item))
.toList();
if (!saveList.isEmpty()) {
assetInfoMapper.insertBatch(saveList);
}
@Override
@Transactional
public void replaceByFamilyIdAndPersonId(String familyId, String personId, List<CcdiAssetInfoDTO> assetInfoList) {
replaceAssets(familyId, personId, assetInfoList, false);
}
@Override
@@ -53,6 +52,11 @@ public class CcdiAssetInfoServiceImpl implements ICcdiAssetInfoService {
return assetInfoMapper.deleteByFamilyId(familyId);
}
@Override
public int deleteByFamilyIdAndPersonId(String familyId, String personId) {
return assetInfoMapper.deleteByFamilyIdAndPersonId(familyId, personId);
}
@Override
public int deleteByFamilyIds(List<String> familyIds) {
if (familyIds == null || familyIds.isEmpty()) {
@@ -61,16 +65,39 @@ public class CcdiAssetInfoServiceImpl implements ICcdiAssetInfoService {
return assetInfoMapper.deleteByFamilyIds(familyIds);
}
private CcdiAssetInfo toEntity(String familyId, CcdiAssetInfoDTO dto) {
private void replaceAssets(String familyId, String personId, List<CcdiAssetInfoDTO> assetInfoList, boolean deleteByFamilyOnly) {
if (deleteByFamilyOnly) {
assetInfoMapper.deleteByFamilyId(familyId);
} else {
assetInfoMapper.deleteByFamilyIdAndPersonId(familyId, personId);
}
if (assetInfoList == null || assetInfoList.isEmpty()) {
return;
}
List<CcdiAssetInfo> saveList = assetInfoList.stream()
.filter(item -> !isEmptyRow(item))
.map(item -> {
validateAsset(item);
return toEntity(familyId, personId, item);
})
.toList();
if (!saveList.isEmpty()) {
assetInfoMapper.insertBatch(saveList);
}
}
private CcdiAssetInfo toEntity(String familyId, String personId, CcdiAssetInfoDTO dto) {
CcdiAssetInfo assetInfo = new CcdiAssetInfo();
BeanUtils.copyProperties(dto, assetInfo);
assetInfo.setFamilyId(familyId);
assetInfo.setPersonId(personId);
return assetInfo;
}
private boolean isEmptyRow(CcdiAssetInfoDTO dto) {
return StringUtils.isEmpty(dto.getPersonId())
&& StringUtils.isEmpty(dto.getAssetMainType())
return StringUtils.isEmpty(dto.getAssetMainType())
&& StringUtils.isEmpty(dto.getAssetSubType())
&& StringUtils.isEmpty(dto.getAssetName())
&& dto.getCurrentValue() == null
@@ -81,4 +108,22 @@ public class CcdiAssetInfoServiceImpl implements ICcdiAssetInfoService {
&& dto.getValuationDate() == null
&& StringUtils.isEmpty(dto.getRemarks());
}
private void validateAsset(CcdiAssetInfoDTO dto) {
if (StringUtils.isEmpty(dto.getAssetMainType())) {
throw new RuntimeException("资产大类不能为空");
}
if (StringUtils.isEmpty(dto.getAssetSubType())) {
throw new RuntimeException("资产小类不能为空");
}
if (StringUtils.isEmpty(dto.getAssetName())) {
throw new RuntimeException("资产名称不能为空");
}
if (dto.getCurrentValue() == null) {
throw new RuntimeException("当前估值不能为空");
}
if (StringUtils.isEmpty(dto.getAssetStatus())) {
throw new RuntimeException("资产状态不能为空");
}
}
}

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 + "名称");