进入用友后,第一次的代码
优化前:
优化后:
优化前:
优化后:
/**
* 复制盘点计划
* @param planid
* @return
* @author xinqq
* @create 2019-12-06 17:00
*/
public Plan copyPlan(String planid){
if(StringUtils.isBlank(planid))
throwBusinessException(I18nEnum.COMMON4);
Plan oldPlan = queryService.queryPlanById(planid);
Plan copyPlan = new Plan();
BeanUtils.copyProperties(oldPlan,copyPlan,"id","name","version","state","creatorname","archivetime");
copyPlan.setName(oldPlan.getName()+ Toolkit.dateToStr(new Date(),"yyyyMMddhhmmss"));
copyPlan.setState(OtrConstant.PlanType.NEW.getCode());
copyPlan.setVersion(OtrConstant.GLOBAL_SCHEME_VERSION);
copyPlan.setCreatorname(StaffHelper.queryStaffNameByUserId(InvocationInfoProxy.getUserid()));
genericService.insert(copyPlan);
String copyPlanId = copyPlan.getId();
List<PlanGroup> oldPlanGroups = groupService.queryPlanGroupByPlanId(planid);
List<PlanObject> objs = new ArrayList<>();
oldPlanGroups.forEach(group->{
PlanGroup newGroup = new PlanGroup();
BeanUtils.copyProperties(group,newGroup,"id","planid");
newGroup.setPlanid(copyPlanId);
genericService.insert(newGroup);
Condition cond = new Condition();
cond.addExpression("planid",planid);
cond.addExpression("plangroupid",group.getId());
group.setPlanObjects(objService.queryPlanObjectListByCond(cond,null));
group.getPlanObjects().forEach(obj->{
PlanObject newObj = new PlanObject();
BeanUtils.copyProperties(obj,newObj,"id","planid","plangroupid","userid");
newObj.setPlangroupid(newGroup.getId());
newObj.setPlanid(copyPlanId);
newObj.setUserid(InvocationInfoProxy.getUserid());
objs.add(newObj);
});
});
genericService.insertBatch(objs);
copyPlan.setPlanobjects(objs);
return copyPlan;
}
第二次代码
优化前:
优化后:
优化前:
问题:假如有100(大量)的entry,则forEach中就需要与数据库交互200次,严重影响性能。
改为:先批量查出来,再一一设置对应
优化后:
@Override
public List<Entry> queryEntryListByCond(Condition cond, Sorter sorter, Boolean wrapClassifyNameAndEvaluateGrades) {
List<Entry> entries = genericService.find(Entry.class, cond, sorter);
if(CollectionUtils.isEmpty(entries) || !wrapClassifyNameAndEvaluateGrades){
return entries;
}
//设置分类名称
List<String> classifyIds = entries.stream().map(item -> item.getClassifyid()).collect(Collectors.toList());
Map<String, EntryClassify> mapByClassifyId = entryClassifyService.queryClassifyMapByIds(classifyIds);
entries.forEach( item ->{
if(mapByClassifyId.get(item.getClassifyid()) != null){
item.setClassifyname(mapByClassifyId.get(item.getClassifyid()).getName());
}
});
//设置评价等级
List<String> entryIds = entries.stream().map(item -> item.getId()).collect(Collectors.toList());
Map<String, List<EvaluateGrade>> mapByEntryId = queryEvaluateGradeByEntryId(entryIds);
entries.forEach( item -> {
if(mapByEntryId.get(item.getId()) != null){
item.setEvaluateGrades(mapByEntryId.get(item.getId()));
}
});
return entries;
}
//通过entryids查询EvaluateGrades
private Map<String, List<EvaluateGrade>> queryEvaluateGradeByEntryId(List<String> entryids){
Condition cond = ConditionUtils.buildInExpression(null, entryids, "entryid");
//评价等级暂时按照创建时间倒叙排列
Sorter sorter = ConditionUtils.buildSorter4CreationtimeDesc();
return evaluateGradeService.queryListByCond(cond, sorter)
.stream().collect(Collectors.groupingBy(EvaluateGrade::getEntryid));
}
而且优化后,queryEntryListByCond()yCond()方法完全可以代替queryEntryById()方法
像下面这样:
优化前:
优化后:
@ApiOperation(value = "查看词条详情")
@GetMapping("/queryDetail/{id}")
public MessageResult queryEntryDetail(@PathVariable("id") String id){
Condition cond = new Condition();
cond.addExpression("id",id);
return Toolkit.getResponseBody(settingService.queryEntryListByCond(cond, null, true),I18nEnum.QUERY);
}
@ApiOperation(value = "浏览词条")
@GetMapping("/queryEntrys")
public MessageResult queryEntrys(@RequestParam String entryClassifyId){
Condition cond = new Condition();
cond.addExpression("classifyid",entryClassifyId);
Sorter sorter = ConditionUtils.buildSorter4CreationtimeDesc();
return Toolkit.getResponseBody(settingService.queryEntryListByCond(cond, sorter, false),I18nEnum.QUERY);
}
来源:CSDN
作者:xq_zzu
链接:https://blog.csdn.net/xq_zzu/article/details/103622450