概述
问题说明:
假如一个项目包含多个研发阶段,每次添加项目的时候,设置该项目的研发阶段,
研发阶段的修改,新增,删除调用同一个接口,如何做判断
思路
分别获取原有的研发阶段和新准备添加的研发阶段,分别定义两个集合。
然后利用jdk1.8新特性中的lam进行判断
如下所示:
List<ProStage> updateOldList=proStageListNew.stream()
.filter(item -> proStagesOld.stream()
.map(e -> e.getId())
.collect(Collectors.toList())
.contains(item.getId()))
.collect(Collectors.toList());
for (ProStage proStage1 : updateOldList){
System.out.println("要修改的:"+proStage1.getId()+"内容:"+proStage1.getContent());
proStageService.updateProStage(proStage1);
}
具体实现方法
//从数据库取的数据
List<ProStage> proStagesOld = proStageService.selectList(new QueryWrapper<ProStage>().lambda().eq(ProStage::getProjectId, project.getId()));
//从前端修改的数据
List<ProStage> proStageListNew = project.getProStageList();
//如果之前和现在都有研发阶段
if(proStageListNew!=null && proStagesOld!=null){
//修改的
List<ProStage> updateOldList = proStageListNew.stream()
.filter(item -> proStagesOld.stream()
.map(e -> e.getId())
.collect(Collectors.toList())
.contains(item.getId()))
.collect(Collectors.toList());
for (ProStage proStage1 : updateOldList){
System.out.println("要修改的:"+proStage1.getId()+"内容:"+proStage1.getContent());
proStageService.updateProStage(proStage1);
}
//删除的
List<ProStage> deleteOldList = proStagesOld.stream()
.filter(item -> !proStageListNew.stream()
.map(e -> e.getId())
.collect(Collectors.toList())
.contains(item.getId()))
.collect(Collectors.toList());
for (ProStage proStage2 : deleteOldList){
System.out.println("要删除的:"+proStage2.getId()+"内容:"+proStage2.getContent());
//删除研发阶段
proStageService.delProStage(proStage2);
}
//新的
List<ProStage> newList = proStageListNew.stream()
.filter(item -> !proStagesOld.stream()
.map(e -> e.getId())
.collect(Collectors.toList())
.contains(item.getId()))
.collect(Collectors.toList());
for (ProStage proStage3 : newList){
System.out.println("要新增的:"+proStage3.getId()+"内容:"+proStage3.getContent());
ProStage proStage=new ProStage();
BeanCopierUtils.copy(proStage3,proStage);
proStage.setId(UuidUtil.getUuidFor32());
proStage.setIsValid(WhetherEnum.NO.getCode());
proStage.setProjectId(project.getId());
proStage.setCreationUserId(userDto.getId());
proStage.setCreationTime(LocalDateTime.now());
proStageService.insert(proStage);
}
//如果原来没有研发阶段
}else if(proStagesOld==null && proStageListNew!=null){
for (ProStage proStage3 : proStageListNew){
ProStage proStage=new ProStage();
BeanCopierUtils.copy(proStage3,proStage);
proStage.setId(UuidUtil.getUuidFor32());
proStage.setIsValid(WhetherEnum.NO.getCode());
proStage.setProjectId(project.getId());
proStage.setCreationUserId(userDto.getId());
proStage.setCreationTime(LocalDateTime.now());
proStageService.insert(proStage);
}
//如果原来有研发阶段现在没有,则删除之前的研发阶段
}else if(proStagesOld!=null && proStageListNew==null){
for (ProStage proStage2 : proStagesOld){
//删除研发阶段
proStageService.delProStage(proStage2);
}
}
//======================================