public boolean relation(Long projectId, List<BsMemberEntity> members) {
//1)、获取原关联数据
List<ProProjectAuthorEntity> oldList = this.findByProjectId(projectId);
List<Long> oldMemberIds = oldList.stream().map(item -> {
return item.getMemberId();
}).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(members)) {
List<ProProjectAuthorEntity> newList = members.stream().map(item -> {
ProProjectAuthorEntity entity;
//是否是原有存在的人员
if (CollectionUtil.contains(oldMemberIds, item.getId())) {
entity = oldList.stream().filter(el -> el.getMemberId().equals(item.getId())).findFirst().get();
} else {
entity = new ProProjectAuthorEntity();
entity.setProjectId(projectId);
entity.setMemberId(item.getId());
entity.setUserId(item.getUserId());
}
entity.setPower(item.getDescription());//此处使用人员描述字段代传
return entity;
}).collect(Collectors.toList());
//1)、保存数据
this.saveOrUpdateBatch(newList);
//2)、去除差异数据
Collection<Long> subtract = CollectionUtil.subtract(oldList, newList).stream().map(item -> {
return item.getId();
}).collect(Collectors.toList());
this.removeBatchByIds(subtract);
}else{
this.removeBatchByIds(oldList);
}
return true;
}
标签:java,stream,删除,差异,oldList,getId,entity,item,return
From: https://www.cnblogs.com/linhan8888/p/18086671