public TableDataInfo list(TAllParam tAllParam) { startPage(); List<TAllParam> list = tAllParamService.list(new LambdaQueryWrapper<TAllParam>() .like(StringUtils.isNotBlank(tAllParam.getMsc()),TAllParam::getMsc,StringUtils.isNotBlank(tAllParam.getMsc())?tAllParam.getMsc().toUpperCase():"") .like(StringUtils.isNotBlank(tAllParam.getClxh()),TAllParam::getClxh,tAllParam.getClxh()) .like(StringUtils.isNotBlank(tAllParam.getHbpl()),TAllParam::getHbpl(),tAllParam.getHbpl()) .like(StringUtils.isNotBlank(tAllParam.getVehRlzl()),TAllParam::getVehRlzl(),tAllParam.getVehRlzl()) .like(StringUtils.isNotBlank(tAllParam.getClyyxzcbh()),TAllParam::getClyyxzcbh,tAllParam.getClyyxzcbh()) .like(StringUtils.isNotBlank(tAllParam.getVehZbzl()),TAllParam::getVehZbzl(),tAllParam.getVehZbzl()) ); // 遍历List,根据MSC匹配查询T_VEHCERT_PRINT表和T_LOCK_VIN表 for (TAllParam param : list) { String msc = param.getMsc(); List<String> vinList = vehcertPrintService.getVinByMsc(msc); // 根据MSC查询对应的VIN列表 boolean isBlc = false; // 是否为保留车的标志位 for (String vin : vinList) { if (StringUtils.isNotBlank(vin) && lockVinService.isVinExisted(vin)) { isBlc = true; break; } } param.setBlc(isBlc ? "是" : ""); } // 如果查询条件中有blc字段,则按照该字段进行匹配查询 if (StringUtils.isNotBlank(tAllParam.getBlc())) { list = list.stream().filter(param -> tAllParam.getBlc().equals(param.getBlc())).collect(Collectors.toList()); } return getDataTable(list); }能不能优化代码,这样在循环中调用查询方法,每循环一次都要去查询对应的表一次,很拖时间,页面半天出不来
时间: 2024-03-16 07:47:44 浏览: 105
可以优化代码,可以考虑将查询操作放在循环外面,避免每次循环都去查询,可以考虑使用 Map 来存储 msc 和对应的 VIN 列表,然后在循环中根据 msc 取出对应的 VIN 列表进行操作。修改后的代码如下所示:
```
public TableDataInfo list(TAllParam tAllParam) {
startPage();
List<TAllParam> list = tAllParamService.list(new LambdaQueryWrapper<TAllParam>()
.like(StringUtils.isNotBlank(tAllParam.getMsc()),TAllParam::getMsc,StringUtils.isNotBlank(tAllParam.getMsc())?tAllParam.getMsc().toUpperCase():"")
.like(StringUtils.isNotBlank(tAllParam.getClxh()),TAllParam::getClxh,tAllParam.getClxh())
.like(StringUtils.isNotBlank(tAllParam.getHbpl()),TAllParam::getHbpl(),tAllParam.getHbpl())
.like(StringUtils.isNotBlank(tAllParam.getVehRlzl()),TAllParam::getVehRlzl(),tAllParam.getVehRlzl())
.like(StringUtils.isNotBlank(tAllParam.getClyyxzcbh()),TAllParam::getClyyxzcbh,tAllParam.getClyyxzcbh())
.like(StringUtils.isNotBlank(tAllParam.getVehZbzl()),TAllParam::getVehZbzl(),tAllParam.getVehZbzl())
);
// 遍历List,根据MSC匹配查询T_VEHCERT_PRINT表和T_LOCK_VIN表
Map<String, List<String>> mscToVinListMap = new HashMap<>();
for (TAllParam param : list) {
String msc = param.getMsc();
if (!mscToVinListMap.containsKey(msc)) {
List<String> vinList = vehcertPrintService.getVinByMsc(msc);
mscToVinListMap.put(msc, vinList);
}
List<String> vinList = mscToVinListMap.get(msc);
boolean isBlc = false; // 是否为保留车的标志位
for (String vin : vinList) {
if (StringUtils.isNotBlank(vin) && lockVinService.isVinExisted(vin)) {
isBlc = true;
break;
}
}
param.setBlc(isBlc ? "是" : "");
}
// 如果查询条件中有blc字段,则按照该字段进行匹配查询
if (StringUtils.isNotBlank(tAllParam.getBlc())) {
list = list.stream().filter(param -> tAllParam.getBlc().equals(param.getBlc())).collect(Collectors.toList());
}
return getDataTable(list);
}
```
通过以上优化,可以避免在循环中重复查询,提高代码的执行效率。
阅读全文