优化这段代码if (typeLists.remove(TransTradeType.TI.getValue())) { hqlBuilder.append(" (t1.type2 = 'TI' and t1.externalApplicationNo like 'BONUS%' and t1.externalCashRef not like 'DIV&BONUS%') "); if (typeLists.size() > 0) { hqlBuilder.append(" or "); } } if (typeLists.remove(TransTradeType.DR.getValue())) { hqlBuilder.append(" (t1.type2 = 'DR' and t1.externalApplicationNo like 'SCRIP DIV%' and t1.externalCashRef not like 'DIV&BONUS%') "); if (typeLists.size() > 0) { hqlBuilder.append(" or "); } } if (typeLists.remove(TransTradeType.DV.getValue())) { hqlBuilder.append(" (t1.type2 = 'DV' and t1.externalApplicationNo not like 'DIV&BONUS%')"); if (typeLists.size() > 0) { hqlBuilder.append(" or "); } }
时间: 2023-12-12 08:02:44 浏览: 88
优化代码
可以将这段代码进行重构,使用一个 Map 存储 TransTradeType 和对应的 HQL 查询语句,然后遍历 typeLists,判断是否包含该 TransTradeType,如果包含就将对应的 HQL 查询语句添加到 StringBuilder 中。如下所示:
```
Map<TransTradeType, String> hqlMap = new HashMap<>();
hqlMap.put(TransTradeType.TI, " (t1.type2 = 'TI' and t1.externalApplicationNo like 'BONUS%' and t1.externalCashRef not like 'DIV&BONUS%') ");
hqlMap.put(TransTradeType.DR, " (t1.type2 = 'DR' and t1.externalApplicationNo like 'SCRIP DIV%' and t1.externalCashRef not like 'DIV&BONUS%') ");
hqlMap.put(TransTradeType.DV, " (t1.type2 = 'DV' and t1.externalApplicationNo not like 'DIV&BONUS%')");
for (TransTradeType type : typeLists) {
if (hqlMap.containsKey(type)) {
hqlBuilder.append(hqlMap.get(type));
if (typeLists.size() > 1) {
hqlBuilder.append(" or ");
}
}
}
```
这样可以减少代码的重复性,提高代码的可读性和可维护性。
阅读全文