HashMap<String, Object> paramMap = new HashMap<>(); // status值为1-配药,2-发药,3-退药,4-取消配药 String status = prescInformation.getString("status"); Date datatime = prescInformation.getDate("datetime"); if("1/2/3".contains(status)) { logger.info("1-配药,2-发药,3-退药,4-取消配药---根据状态设置回写内容:status:"+status); paramMap.put("datetime", datatime); paramMap.put("personId", prescInformation.getString("personId")); paramMap.put("dispensaryId", prescInformation.getString("dispensaryId")); }else if ("4".equals(status)){ // 取消配药 logger.info("1-配药,2-发药,3-退药,4-取消配药---根据状态设置回写内容:status:"+status); logger.info("根据状态设置回写内容:status:"+status); paramMap.put("datetime", ""); paramMap.put("personId", ""); paramMap.put("dispensaryId", ""); } logger.info("开始写入数据到emr.patient_presc,prescNumList:"+JSON.toJSONString(prescNumList)+",paramMap:"+JSON.toJSONString(paramMap)+",status:"+status); this.prescriptionMapper.writePrescription(prescNumList, paramMap, status); if ("3".equals(status)){ logger.info("========回写退药数量!--prescNumList:"+JSON.toJSONString(prescNumList)+",paramMap:"+JSON.toJSONString(paramMap)+",status:"+status); this.prescriptionMapper.writePrescDrug(prescNumList, paramMap, status); }
时间: 2024-03-17 15:47:16 浏览: 101
java 读取csv 并转为Map<String,Object>
这段代码可以进行以下优化:
1. 将字符串"1/2/3"提取为一个常量,这样可以提高代码的可读性。
2. 将重复的日志语句提取为一个方法,避免重复的代码。
3. 使用 switch 语句代替 if-else 语句,这可以提高代码的可读性和性能。
4. 对于 "3" 的情况,可以将写入 emr.patient_presc 和 emr.patient_presc_drug 的操作封装到一个方法中,避免重复的代码。
下面是优化后的代码:
```java
HashMap<String, Object> paramMap = new HashMap<>();
String status = prescInformation.getString("status");
Date datatime = prescInformation.getDate("datetime");
final String DISPENSED_STATUS = "1/2/3";
if (DISPENSED_STATUS.contains(status)) {
logger.info("1-配药,2-发药,3-退药,4-取消配药---根据状态设置回写内容:status:" + status);
paramMap.put("datetime", datatime);
paramMap.put("personId", prescInformation.getString("personId"));
paramMap.put("dispensaryId", prescInformation.getString("dispensaryId"));
} else if ("4".equals(status)) {
logger.info("1-配药,2-发药,3-退药,4-取消配药---根据状态设置回写内容:status:" + status);
logger.info("根据状态设置回写内容:status:" + status);
paramMap.put("datetime", "");
paramMap.put("personId", "");
paramMap.put("dispensaryId", "");
}
writePrescription(prescNumList, paramMap, status);
if ("3".equals(status)) {
logger.info("========回写退药数量!--prescNumList:" + JSON.toJSONString(prescNumList) + ",paramMap:" + JSON.toJSONString(paramMap) + ",status:" + status);
writePrescDrug(prescNumList, paramMap, status);
}
private void writePrescription(List<String> prescNumList, Map<String, Object> paramMap, String status) {
logger.info("开始写入数据到emr.patient_presc,prescNumList:" + JSON.toJSONString(prescNumList) + ",paramMap:" + JSON.toJSONString(paramMap) + ",status:" + status);
prescriptionMapper.writePrescription(prescNumList, paramMap, status);
}
private void writePrescDrug(List<String> prescNumList, Map<String, Object> paramMap, String status) {
logger.info("开始写入数据到emr.patient_presc_drug,prescNumList:" + JSON.toJSONString(prescNumList) + ",paramMap:" + JSON.toJSONString(paramMap) + ",status:" + status);
prescriptionMapper.writePrescDrug(prescNumList, paramMap, status);
}
```
这样,代码就更加简洁易读了。
阅读全文