优化代码 if (user.getTenantId().equals(project.getTenantId())) { List<DepositRecord> recordList = depositRecordService.list(new QueryWrapper<DepositRecord>() .eq("assets_id", assetsId) .eq("user_id", userId)); // 判断是一次性付款还是分期付款 recordList.forEach(e -> { if (recordList.size() == 1) { if (project.getTheTone() != null && e.getAmount() == project.getTheTone()) { context.put("tradeMethod", "一次性付款"); } if (project.getTheTone() == null && e.getAmount() == dealRecord.getDealPrice()) { context.put("tradeMethod", "一次性付款"); } context.put("tradeMethod", "分期付款"); } else { context.put("tradeMethod", "分期付款"); } }); } else if (user.getTenantId().equals(project.getAgencyTenantId())) { Enroll enrollServiceOne = enrollService.getOne(new QueryWrapper<Enroll>().eq("id", dealRecord.getEnrollId())); List<DepositRecord> recordList = depositRecordService.list(new QueryWrapper<DepositRecord>() .eq("assets_id", assetsId) .eq("user_id", enrollServiceOne.getUserId())); // 判断是一次性付款还是分期付款 recordList.forEach(e -> { if (recordList.size() == 1) { if (project.getTheTone() != null && e.getAmount() == project.getTheTone()) { context.put("tradeMethod", "一次性付款"); } if (project.getTheTone() == null && e.getAmount() == dealRecord.getDealPrice()) { context.put("tradeMethod", "一次性付款"); } context.put("tradeMethod", "分期付款");
时间: 2023-12-14 20:40:27 浏览: 89
可以对代码进行如下优化:
1. 提取出重复的逻辑,避免代码冗余。将获取 DepositRecord 列表以及判断付款方式的逻辑抽取出来,放到一个方法中。这个方法可以接收一个 DepositRecord 列表和 project 对象作为参数,返回一个字符串类型的付款方式。
2. 简化 if/else 的嵌套结构。可以使用 return 语句避免过深的嵌套,提高代码可读性。
优化后的代码如下:
```
String tradeMethod = getTradeMethod(user, project, dealRecord, assetsId);
context.put("tradeMethod", tradeMethod);
// ...
private String getTradeMethod(User user, Project project, DealRecord dealRecord, Long assetsId) {
List<DepositRecord> recordList = depositRecordService.list(new QueryWrapper<DepositRecord>()
.eq("assets_id", assetsId)
.eq("user_id", user.getTenantId().equals(project.getTenantId()) ? userId : enrollService.getOne(new QueryWrapper<Enroll>().eq("id", dealRecord.getEnrollId())).getUserId()));
if (recordList.size() == 1) {
if (project.getTheTone() != null && recordList.get(0).getAmount() == project.getTheTone()) {
return "一次性付款";
}
if (project.getTheTone() == null && recordList.get(0).getAmount() == dealRecord.getDealPrice()) {
return "一次性付款";
}
}
return "分期付款";
}
```
这样,代码就更加清晰简洁了。
阅读全文