优化以下代码: List<String> areaNos = this.getShopAreaNosInTenant(tenantId, shopId); if (shopId<0||(!CollectionUtils.isEmpty(areaNos)&&areaNos.contains("#"))){ return new Page<>(new ArrayList<>(), new PageInfo(pageRequest.getPage(), pageRequest.getSize()), 0); } //先查询常购清单 List<GoodsCollect> goodsCollectList = this.goodsCollectRepository.select(new GoodsCollect().setTenantId(tenantId).setShopId(shopId)); if (CollectionUtils.isEmpty(goodsCollectList)){ return new Page<>(new ArrayList<>(), new PageInfo(pageRequest.getPage(), pageRequest.getSize()), 0); } List<Long> supplyGoodsId = goodsCollectList.stream().map(GoodsCollect::getSupplierGoodsId).collect(Collectors.toList()); request.setSupplyGoodsIds(supplyGoodsId); //常购清单列表-一品多商加云仓全量展示 Page<GoodsEsNewDoc> goodsEsNewDocs = this.goodsEsService.searchPage(tenantId, pageRequest.getPage(), pageRequest.getSize(), request); List<AppGoodsVO> appGoodsVOS = this.wrapEsGoods(tenantId, areaNos, goodsEsNewDocs.getContent(), shopId, request.getTerminalEnum()); return new Page<>(appGoodsVOS, new PageInfo(pageRequest.getPage(), pageRequest.getSize()), goodsEsNewDocs.getTotalElements());
时间: 2024-03-15 21:46:31 浏览: 182
优化代码
可以优化的地方如下:
1. 首先,可以将 List<String> areaNos 的判断逻辑放到 getShopAreaNosInTenant 方法中,使得该方法返回一个 boolean 值,来表示是否需要进行下一步的查询。然后在主方法中根据返回值判断是否需要返回空列表。
2. 在判断常购清单是否为空时,可以使用 goodsCollectList.isEmpty() 代替 CollectionUtils.isEmpty(goodsCollectList)。
3. 在将 goodsCollectList 转换为 supplyGoodsId 时,可以使用方法引用的方式代替 lambda 表达式,如:goodsCollectList.stream().map(GoodsCollect::getSupplierGoodsId)。
优化后的代码如下:
```
boolean needEmptyList = this.isShopAreaNosInvalid(tenantId, shopId);
if (shopId < 0 || needEmptyList) {
return new Page<>(new ArrayList<>(), new PageInfo(pageRequest.getPage(), pageRequest.getSize()), 0);
}
List<GoodsCollect> goodsCollectList = this.goodsCollectRepository.select(new GoodsCollect().setTenantId(tenantId).setShopId(shopId));
if (goodsCollectList.isEmpty()) {
return new Page<>(new ArrayList<>(), new PageInfo(pageRequest.getPage(), pageRequest.getSize()), 0);
}
List<Long> supplyGoodsId = goodsCollectList.stream().map(GoodsCollect::getSupplierGoodsId).collect(Collectors.toList());
request.setSupplyGoodsIds(supplyGoodsId);
Page<GoodsEsNewDoc> goodsEsNewDocs = this.goodsEsService.searchPage(tenantId, pageRequest.getPage(), pageRequest.getSize(), request);
List<AppGoodsVO> appGoodsVOS = this.wrapEsGoods(tenantId, areaNos, goodsEsNewDocs.getContent(), shopId, request.getTerminalEnum());
return new Page<>(appGoodsVOS, new PageInfo(pageRequest.getPage(), pageRequest.getSize()), goodsEsNewDocs.getTotalElements());
private boolean isShopAreaNosInvalid(Long tenantId, Long shopId) {
List<String> areaNos = this.getShopAreaNosInTenant(tenantId, shopId);
return shopId < 0 || (!CollectionUtils.isEmpty(areaNos) && areaNos.contains("#"));
}
```
阅读全文