优化下面的代码 public Page<FinanceCodeDTO> selectFinanceCodePage(FinanceCodeListVO financeCodeListVO) { //根据类型关联查询数据信息 Page<FinanceCodeDTO> page = new Page<>(financeCodeListVO.getPageNo(),financeCodeListVO.getPageSize()); Page<FinanceCodeDTO> dtoPage = financeCodeMapper.financeCodeList(page, financeCodeListVO); List<FinanceCodeDTO> dtos = dtoPage.getRecords(); List<String> contractIds = dtos.stream().map(FinanceCodePO::getContractIds).collect(Collectors.toList()); List<Integer> idList = new ArrayList<>(); contractIds.forEach(ids -> { if (ToolUtil.isNotEmpty(ids) && !ToolUtil.equals("[]",ids)){ List<Integer> list = JSONObject.parseArray(ids, Integer.class); idList.addAll(list); } }); if (ToolUtil.isNotEmpty(idList)){ CustomerContractPO customerContractPO = new CustomerContractPO(); List<CustomerContractPO> customerContractPOS = customerContractPO.selectList(new QueryWrapper<>(customerContractPO).lambda().in(CustomerContractPO::getCustomerContractId, idList)); Map<Integer, String> map = customerContractPOS.stream().collect(Collectors.toMap(CustomerContractPO::getCustomerContractId, CustomerContractPO::getContractName)); dtos.forEach(dto ->{ if (ToolUtil.isNotEmpty(dto.getContractIds())&& !ToolUtil.equals("[]",dto.getContractIds())){ List<Integer> list = JSONObject.parseArray(dto.getContractIds(), Integer.class); List<String> names = new ArrayList<>(); list.forEach(id ->{ names.add(map.get(id)); }); dto.setContractName(String.join(",",names)); } }); dtoPage.setRecords(dtos); } return dtoPage ; }
时间: 2023-03-14 10:01:23 浏览: 125
&& ids.contains(",")) { String[] idsArr = ids.split(","); Arrays.stream(idsArr).forEach(id -> { idList.add(Integer.parseInt(id)); }); } }); List<ContractDTO> contractDTOList = contractMapper.selectContractByIdList(idList); dtos.forEach(dto -> { dto.setContractList(contractDTOList); }); return dtoPage; }答:根据给定的代码,优化的思路是把contractIds通过Arrays.stream()和Integer.parseInt()转换为idList,并通过contractMapper.selectContractByIdList()获取contractDTOList,最后把contractDTOList赋给dtos中每个dto对象。
阅读全文