优化这段代码: //不是管理员,先判断用户是否为运营商,需要运营商id作为查询条件 if (RoleCodeEnum.OPERATOR.getCode().equals(code)) { LambdaQueryWrapper<OperatorDO> queryWrapper2 = new LambdaQueryWrapper<>(); queryWrapper2.eq(OperatorDO::getOperatorUserId, memberUserId); OperatorDO operatorDO = operatorMapper.selectOne(queryWrapper2); LambdaQueryWrapper<AgentDO> queryWrapper3 = new LambdaQueryWrapper<>(); //根据运营商id查其下所有代理商 queryWrapper3.eq(AgentDO::getOperatorId, operatorDO.getId()); List<AgentDO> agentDOS = agentMapper.selectList(queryWrapper3); //查询每个代理商下面的门店集合并将门店id封装到storeIdList中,需要代理商id作为查询条件 for (int i = 0; i < agentDOS.size(); i++) { LambdaQueryWrapper<StoreDO> queryWrapper4 = new LambdaQueryWrapper<>(); queryWrapper4.eq(StoreDO::getAgentId, agentDOS.get(i).getId()); List<StoreDO> storeDOS = storeMapper.selectList(queryWrapper4); for (int j = 0; j < storeDOS.size(); j++) { storeIdList.add(storeDOS.get(j).getId()); } } return batteryMapper.pageJoin(queryReqVO, BatteryRespVO.class, buildQueryWrapper(buildJoinWrapper(new MPJLambdaWrapper<BatteryDO>()).in(BatteryDO::getStoreId, storeIdList), queryReqVO)); }
时间: 2024-04-20 13:25:20 浏览: 182
优化后的代码如下:
```java
if (RoleCodeEnum.OPERATOR.getCode().equals(code)) {
List<Long> storeIdList = operatorMapper.selectStoreIdsByUserId(memberUserId);
return batteryMapper.pageJoin(queryReqVO, BatteryRespVO.class,
buildQueryWrapper(buildJoinWrapper(new MPJLambdaWrapper<BatteryDO>()).in(BatteryDO::getStoreId, storeIdList), queryReqVO));
}
```
优化思路:
1. 使用 `operatorMapper.selectStoreIdsByUserId(memberUserId)` 一次性查询出所有的门店ID,避免多次循环查询。
2. 直接将门店ID列表传入查询条件中,不再使用循环添加到 `storeIdList` 中。
阅读全文