优化这段代码( ApplyFinanceDTO dto = new ApplyFinanceDTO(); dto.setId(applyNo); Result<ApplyFinanceVO> financeResult = applyFeign.selectInfoById(dto); if(financeResult == null || !financeResult.isSuccess()){ throw new BusinessException("融资申请信息获取失败"); } ApplyFinanceVO financeVO = financeResult.getResult(); QueryWrapper<ContractTemplate> wrapper = new QueryWrapper<>(); wrapper .eq(financeVO.getBusinessType() != null, "BUSINESS_TYPE", financeVO.getBusinessType()) .eq(financeVO.getFundType() != null, "EXCLUSIVE_CODE", financeVO.getFundType()) .eq( "TEMPLATE_STATUS", InitConstants.STRING_1) .or(queryWrapper -> queryWrapper.eq( "IS_UNIVERSAL", InitConstants.STRING_1).eq( "TEMPLATE_STATUS", InitConstants.STRING_1)); wrapper.orderByAsc("ID", "TEMPLATE_ID"); List<ContractTemplate> contractTemplates = contractTemplateService.getBaseMapper().selectList(wrapper); CollectContractParam contractParam = new CollectContractParam(applyNo, contractTemplates, null); List<TemplateParam> templateParamAndValue = createContractService.getTemplateParamAndValue(contractParam); contractParam.setParams(templateParamAndValue); pdfUtil.createContractAssignment(contractParam); return Constant.SUCCESS;)
时间: 2023-06-07 08:09:10 浏览: 156
别再用 BeanUtils 了,这款 PO VO DTO 转换神器不香么?.zip
以下是优化后的代码:
ApplyFinanceDTO dto = new ApplyFinanceDTO();
dto.setId(applyNo);
Result<ApplyFinanceVO> financeResult = applyFeign.selectInfoById(dto);
if (financeResult == null || !financeResult.isSuccess()) {
throw new BusinessException("融资申请信息获取失败");
}
ApplyFinanceVO financeVO = financeResult.getResult();
QueryWrapper<ContractTemplate> wrapper = new QueryWrapper<>();
wrapper.eq(financeVO.getBusinessType() != null, "BUSINESS_TYPE", financeVO.getBusinessType())
.eq(financeVO.getFundType() != null, "EXCLUSIVE_CODE", financeVO.getFundType())
.eq("TEMPLATE_STATUS", InitConstants.STRING_1)
.or(queryWrapper -> queryWrapper.eq("IS_UNIVERSAL", InitConstants.STRING_1).eq("TEMPLATE_STATUS", InitConstants.STRING_1))
.orderByAsc("ID", "TEMPLATE_ID");
List<ContractTemplate> contractTemplates = contractTemplateService.list(wrapper);
CollectContractParam contractParam = new CollectContractParam(applyNo, contractTemplates, null);
List<TemplateParam> templateParamAndValue = createContractService.getTemplateParamAndValue(contractParam);
contractParam.setParams(templateParamAndValue);
pdfUtil.createContractAssignment(contractParam);
return Constant.SUCCESS;
优化方法:
1. 使用 list() 方法代替 selectList() 方法,从而避免基类中的两次无效 count() 查询
2. 删除 wrapper.eq() 方法中的显式非空检查,因为 QueryWrapper 内部自动处理为 IS NOT NULL
3. 将 orderByAsc() 方法放在 QueryWrapper 链式调用的最后,以提高代码可读性
阅读全文