优化这段代码: //获取慢病信息 List<SlowDiseasePatInfo> slowDiseasePattInfosList = new ArrayList<>(); slowDiseasePattInfosList.add(new SlowDiseasePatInfo(slowDiseaseId,"普通慢病报卡")); Result result = detail(slowDiseasePattInfosList); if (null == result.getData()){ return ResultGenerator.genFailedResult("未查到需要上报的数据"); } List<SlowDiseasePatientInfo> list = (List<SlowDiseasePatientInfo>)result.getData(); String outpatNum = list.get(0).getOutpatNum().isEmpty() ? list.get(0).getInpatNum() : list.get(0).getOutpatNum(); //获取患者的院区 String hospitalCode = emrSlowDiseaseDao.getHospitalCode(outpatNum); String unitCode = ConstantEnum.GW_CZ.getValue().equals(hospitalCode) ? ConstantEnum.GW_CZDWBM.getValue() : ConstantEnum.GW_YHDWBM.getValue(); String regCode = ConstantEnum.GW_CZ.getValue().equals(hospitalCode) ? ConstantEnum.GW_CZZCM.getValue() : ConstantEnum.GW_YHZCM.getValue(); Map<String,Object> reqMap = new HashMap<>(); reqMap.put("slowInfo",list.get(0)); reqMap.put("unitCode",unitCode); reqMap.put("regCode",regCode); String reqInfo = MessageUtil.select(reqMap, "/velocity/slowdisease.vm"); Document document = DocumentHelper.parseText(StringEscapeUtils.unescapeJava(reqInfo.replace("xmlns=\"urn:hl7-org:v3\"", ""))); reqInfo = XmlUtils.documentToStr(document); logger.info("请求慢病接口开始{}", reqInfo); String res = HttpClientUtil.sendWebServiceForLis(webservice + "SlowSfqUploadService/SlowSfqUploadService?WSDL", reqInfo.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","")); logger.info("请求慢病接口结束{}", res); return ResultGenerator.genOkResult(res);
时间: 2024-03-11 15:46:40 浏览: 67
可以尝试进行以下优化:
1. 将获取慢病信息的代码封装到一个方法中,让代码更加清晰易读。
2. 使用 Optional 类对可能为空的数据进行处理,避免空指针异常。
3. 使用字符串模板替换字符串拼接,让代码更加简洁。
4. 对于多次使用的常量,可以将其定义为常量或枚举类型,提高代码的可读性和可维护性。
5. 将请求WebService的代码封装到一个工具类中,提高代码的可复用性和可测试性。
下面是代码优化后的示例:
```java
public Result getSlowDiseaseInfo(String slowDiseaseId) throws Exception {
// 获取慢病信息
List<SlowDiseasePatInfo> slowDiseasePatInfosList = new ArrayList<>();
slowDiseasePatInfosList.add(new SlowDiseasePatInfo(slowDiseaseId, "普通慢病报卡"));
Result result = detail(slowDiseasePatInfosList);
Optional<List<SlowDiseasePatientInfo>> optional = Optional.ofNullable((List<SlowDiseasePatientInfo>) result.getData());
List<SlowDiseasePatientInfo> list = optional.orElseThrow(() -> new RuntimeException("未查到需要上报的数据"));
// 获取患者的院区
String outpatNum = Optional.ofNullable(list.get(0).getOutpatNum()).orElse(list.get(0).getInpatNum());
String hospitalCode = emrSlowDiseaseDao.getHospitalCode(outpatNum);
String unitCode = ConstantEnum.GW_CZ.getValue().equals(hospitalCode) ? ConstantEnum.GW_CZDWBM.getValue() : ConstantEnum.GW_YHDWBM.getValue();
String regCode = ConstantEnum.GW_CZ.getValue().equals(hospitalCode) ? ConstantEnum.GW_CZZCM.getValue() : ConstantEnum.GW_YHZCM.getValue();
// 封装请求参数
Map<String, Object> reqMap = new HashMap<>();
reqMap.put("slowInfo", list.get(0));
reqMap.put("unitCode", unitCode);
reqMap.put("regCode", regCode);
String reqInfo = MessageUtil.select(reqMap, "/velocity/slowdisease.vm");
// 发送WebService请求
String reqBody = String.format("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v3=\"urn:hl7-org:v3\"><soapenv:Header/><soapenv:Body>%s</soapenv:Body></soapenv:Envelope>", reqInfo);
String res = HttpClientUtil.sendWebServiceForLis(webservice + "SlowSfqUploadService/SlowSfqUploadService?WSDL", reqBody);
return ResultGenerator.genOkResult(res);
}
```
阅读全文