纳尼,传列表还要传删除掉列表数据???
根据某属性判断集合的交集,差集,并集
需求描述–前端传一个银行列表,需要和原来的银行列表对比,判断出最终的列表增删改的状态,这里简单用一幅图描述一下:前端传的银行列表是newBankList ,数据库里的银行列表是oldBanList ,银行对象是PublicEmpBankParamDTO,唯一主键是empBankacctId
![]()
搞清楚需求下面直接上代码:
List<PublicEmpBankParamDTO> oldBanList = resDTO.getPublicEmpBankDTOS();
List<PublicEmpBankParamDTO> newBankList = modifyInsuEmpInfoReqDto.getPublicEmpBankParamDTOList();
//先判断改的部分
//交集
List<PublicEmpBankParamDTO> updateBankList = new ArrayList<>();
//差集1
List<PublicEmpBankParamDTO> insertBankList = new ArrayList<>();
//差集2
List<PublicEmpBankParamDTO> deleteBankList = new ArrayList<>();
if (!CollectionUtils.isEmpty(newBankList) && !CollectionUtils.isEmpty(oldBanList)) {
//根据empBankacctId 判断赋予银行的操作状态
List<String> empbackIds = oldBanList.stream().map(PublicEmpBankParamDTO::getEmpBankacctId).collect(Collectors.toList());
updateBankList = newBankList.stream().filter(m -> empbackIds.contains(m.getEmpBankacctId())).collect(Collectors.toList());
}
List<String> updatebackIds = new ArrayList<>();
if (!CollectionUtils.isEmpty(updateBankList)) {
updatebackIds = updateBankList.stream().map(PublicEmpBankParamDTO::getEmpBankacctId).collect(Collectors.toList());
updateBankList.stream().forEach(m -> {
m.setDataChgType("3");//修改
});
}
//再判断增的
if (!CollectionUtils.isEmpty(newBankList)) {
if (!CollectionUtils.isEmpty(updatebackIds)) {
List<String> finalUpdatebackIds = updatebackIds;
insertBankList = newBankList.stream().filter(m -> !finalUpdatebackIds.contains(m.getEmpBankacctId())).collect(Collectors.toList());
} else {
insertBankList = newBankList;
}
}
//最后判断删除的
if (!CollectionUtils.isEmpty(oldBanList)) {
if (!CollectionUtils.isEmpty(updatebackIds)) {
List<String> finalUpdatebackIds = updatebackIds;
deleteBankList = oldBanList.stream().filter(m -> !finalUpdatebackIds.contains(m.getEmpBankacctId())).collect(Collectors.toList());
} else {
deleteBankList = oldBanList;
}
}
//处理新增和删除的
if (!CollectionUtils.isEmpty(insertBankList)) {
insertBankList.stream().forEach(m -> {
m.setDataChgType("1");//新增
});
//这里up主懒得new对象了,直接用交集对象上了,把差集都加上就是并集啦
updateBankList.addAll(insertBankList);
}
if (!CollectionUtils.isEmpty(deleteBankList)) {
deleteBankList.stream().forEach(m -> {
m.setDataChgType("2");//删除
});
//这里up主懒得new对象了,直接用交集对象上了,把差集都加上就是并集啦
updateBankList.addAll(deleteBankList);
}