帮我优化这段代码 private Map<String, Object> boTOMap(DownloadEpidHfmCaseSevereDeathTableVO tableVO){ this.setSonList(tableVO); List<Map<String, Object>> listMap = CodeToStringUtil.dataObjectProcessing(this.getDataObjectProcessingUtilBO(Arrays.asList(tableVO), true, chinesePattern, kg)); if(CollectionUtils.isNotEmpty(listMap)){ Map<String, Object> objectMap = listMap.get(0); //疫苗 List<EpidHfmCaseSevereDeathTestTableVO> selfTestTableVOS = tableVO.getTestTableVOS().stream().filter(item -> item.getJcdx() == DetectionObjectEnum.SELF).collect(Collectors.toList()); List<EpidHfmCaseSevereDeathTestTableVO> familyMembersTestTableVOS = tableVO.getTestTableVOS().stream().filter(item -> item.getJcdx() == DetectionObjectEnum.FAMILY_MEMBERS).collect(Collectors.toList()); if(CollectionUtils.isNotEmpty(selfTestTableVOS)){ objectMap.put("selfTestTable", this.testTableToMap(selfTestTableVOS)); } if(CollectionUtils.isNotEmpty(familyMembersTestTableVOS)){ objectMap.put("familyMembersTestTable", this.testTableToMap(familyMembersTestTableVOS)); } if(CollectionUtils.isNotEmpty(tableVO.getVaccinateTableVOS())){ objectMap.put("vaccinateTable", this.vaccinateTableToMap(tableVO.getVaccinateTableVOS())); } List<Map<String, Object>> dateList = epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableDateList1(tableVO.getId()); dateList.addAll(epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableDateList2(tableVO.getId(), 7-dateList.size())); if(dateList.size() < 7){ int size = dateList.size(); for (int i = 0; i < (7-size); i++) { Map<String, Object> m = new HashMap<>(); m.put("date", "-"); dateList.add(m); } } objectMap.put("titleList", dateList); if(CollectionUtils.isNotEmpty(tableVO.getComplicationTableVOS())){ List<Map<String, Object>> complicationList = null; Map<ComplicationTypeEnum, List<EpidHfmCaseSevereDeathComplicationTableVO>> listMap1 = tableVO.getComplicationTableVOS().stream().collect(Collectors.groupingBy(EpidHfmCaseSevereDeathComplicationTableVO::getType)); int num = 0; for (int j = 0; j < ComplicationTypeEnum.values().length; j++) { complicationList = new ArrayList<>(); List<Map<String, Object>> bfzmcList = epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableBfzmcList( ComplicationTypeEnum.values()[j], tableVO.getId()); for (Map<String, Object> bfzmc : bfzmcList) { Map<String, Object> complication = new HashMap<>(); complication.put("bfzmc7", bfzmc.get("bfzmc")); for (int i = 0; i < dateList.size(); i++) { num = 0; for (EpidHfmCaseSevereDeathComplicationTableVO complicationTableVO : listMap1.get(ComplicationTypeEnum.values()[j])) { if(complicationTableVO.getDate().equals(dateList.get(i).get("title")) && complicationTableVO.getBfzmc().equals(bfzmc.get("bfzmc"))){ complication.put("bfzmc"+i, StringUtils.isNotBlank(complicationTableVO.getJg()) ? complicationTableVO.getJg() : ""); num = 1; break; } } if(num == 0){ complication.put("bfzmc"+i, ""); } } complicationList.add(complication); } objectMap.put("complicationType"+j, complicationList); } } return objectMap; } return null; }
时间: 2024-03-28 20:40:32 浏览: 98
封装ResultSet结果集
首先,这段代码缺少注释,不容易理解代码的作用。建议加入注释,增加可读性。
其次,可以将一些重复的代码抽取成方法,提高代码复用性和可维护性。例如:
1. 抽取出获取测试表和疫苗表的方法:
```
private void addTestTableAndVaccinateTable(Map<String, Object> objectMap, DownloadEpidHfmCaseSevereDeathTableVO tableVO) {
List<EpidHfmCaseSevereDeathTestTableVO> selfTestTableVOS = tableVO.getTestTableVOS().stream().filter(item -> item.getJcdx() == DetectionObjectEnum.SELF).collect(Collectors.toList());
List<EpidHfmCaseSevereDeathTestTableVO> familyMembersTestTableVOS = tableVO.getTestTableVOS().stream().filter(item -> item.getJcdx() == DetectionObjectEnum.FAMILY_MEMBERS).collect(Collectors.toList());
if(CollectionUtils.isNotEmpty(selfTestTableVOS)){
objectMap.put("selfTestTable", this.testTableToMap(selfTestTableVOS));
}
if(CollectionUtils.isNotEmpty(familyMembersTestTableVOS)){
objectMap.put("familyMembersTestTable", this.testTableToMap(familyMembersTestTableVOS));
}
if(CollectionUtils.isNotEmpty(tableVO.getVaccinateTableVOS())){
objectMap.put("vaccinateTable", this.vaccinateTableToMap(tableVO.getVaccinateTableVOS()));
}
}
private Map<String, Object> testTableToMap(List<EpidHfmCaseSevereDeathTestTableVO> testTableVOS) {
// TODO: 实现将测试表转为 Map 的逻辑
}
private Map<String, Object> vaccinateTableToMap(List<EpidHfmCaseSevereDeathVaccinateTableVO> vaccinateTableVOS) {
// TODO: 实现将疫苗表转为 Map 的逻辑
}
```
2. 抽取出获取并补全并发症表格的方法:
```
private void addComplicationTable(Map<String, Object> objectMap, DownloadEpidHfmCaseSevereDeathTableVO tableVO, List<Map<String, Object>> dateList) {
if(CollectionUtils.isNotEmpty(tableVO.getComplicationTableVOS())){
List<Map<String, Object>> complicationList = null;
Map<ComplicationTypeEnum, List<EpidHfmCaseSevereDeathComplicationTableVO>> listMap1 = tableVO.getComplicationTableVOS().stream().collect(Collectors.groupingBy(EpidHfmCaseSevereDeathComplicationTableVO::getType));
int num = 0;
for (int j = 0; j < ComplicationTypeEnum.values().length; j++) {
complicationList = new ArrayList<>();
List<Map<String, Object>> bfzmcList = epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableBfzmcList(ComplicationTypeEnum.values()[j], tableVO.getId());
for (Map<String, Object> bfzmc : bfzmcList) {
Map<String, Object> complication = new HashMap<>();
complication.put("bfzmc7", bfzmc.get("bfzmc"));
for (int i = 0; i < dateList.size(); i++) {
num = 0;
for (EpidHfmCaseSevereDeathComplicationTableVO complicationTableVO : listMap1.get(ComplicationTypeEnum.values()[j])) {
if(complicationTableVO.getDate().equals(dateList.get(i).get("title")) && complicationTableVO.getBfzmc().equals(bfzmc.get("bfzmc"))){
complication.put("bfzmc"+i, StringUtils.isNotBlank(complicationTableVO.getJg()) ? complicationTableVO.getJg() : "");
num = 1;
break;
}
}
if(num == 0){
complication.put("bfzmc"+i, "");
}
}
complicationList.add(complication);
}
objectMap.put("complicationType"+j, complicationList);
}
}
}
```
这样,原来的代码可以简化为:
```
private Map<String, Object> boTOMap(DownloadEpidHfmCaseSevereDeathTableVO tableVO) {
this.setSonList(tableVO);
List<Map<String, Object>> listMap = CodeToStringUtil.dataObjectProcessing(this.getDataObjectProcessingUtilBO(Arrays.asList(tableVO), true, chinesePattern, kg));
if(CollectionUtils.isNotEmpty(listMap)){
Map<String, Object> objectMap = listMap.get(0);
addTestTableAndVaccinateTable(objectMap, tableVO);
List<Map<String, Object>> dateList = epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableDateList1(tableVO.getId());
dateList.addAll(epidHfmCaseSevereDeathComplicationTableDao.getComplicationTableDateList2(tableVO.getId(), 7-dateList.size()));
if(dateList.size() < 7){
int size = dateList.size();
for (int i = 0; i < (7-size); i++) {
Map<String, Object> m = new HashMap<>();
m.put("date", "-");
dateList.add(m);
}
}
objectMap.put("titleList", dateList);
addComplicationTable(objectMap, tableVO, dateList);
return objectMap;
}
return null;
}
```
阅读全文