代码优化 public Result phmOperate(Operation<PhmOperation,PhmCause> operation) { HlwExmMessage msg = messageService.queryMsgById(operation.getMsgId()); if (msg == null||!HlwExmMessage.FORWARD_DOC_TO_PHM.equals(msg.getForward())) { return new Result<>(Status.ERROR, "被处置消息无效!"); } if (msg.getOperateMsgId() != null) { return new Result<>(Status.UN_ACCESS_OPERATION, "消息已处置,重复处置无效!"); } HlwExmMessage<DocOperateMsg> msg2 = messageService.queryDocToPhmMsg(msg.getTaskId()); Integer msgId2 = msg2 == null ? null : msg2.getMsgId(); if (!Objects.equals(msg.getMsgId(), msgId2)) { //当前被处置消息与任务最后一条消息不是同一条消息, //原因:第一次审查通过后,药师正准备提交审方结果时,医生重新修改医嘱提交,造成药师审核的医嘱信息已过期。 // 最终会形成一个审方任务干预记录中有两个及以上药师审核通过,也有可能药师不通过,造成审方流程错乱 return new Result(Status.UN_ACCESS_OPERATION, "被处置消息已过期,处置无效,医生已修改医嘱,请刷新任务!"); } List<String> operations = msg.getOperations(); PhmOperation opCode = operation.getOperation(); if (!operations.contains(opCode.name())) {//校验操作是否合法。每个消息可处置方式不同: return new Result<>(Status.ERROR, "非法处置:当前消息可选择的处置方式" + operations.toString()); } return operate(operation, msg, opCode) ? Result.SUCCESS : new Result<>(Status.UN_ACCESS_OPERATION, "当次就诊已作废,处置失败!"); } //返回false 任务作废 private boolean operate(Operation<PhmOperation, PhmCause> operation, HlwExmMessage msg, PhmOperation code) { ExmTask task = taskService.queryExmTask(msg.getMsgId()); if(ExmState.TASK_ABOLISHED.equals(task.getState())){ return false; } ExmState taskState = OperateUtil.getTaskState(code, msg.getMsgType()); task.setState(taskState); task.setPhmOperateTime(DateUtil.getCurrDateStr(DateUtil.YYYY_MM_DD_HH_MM_SS)); ParamVersion paramVersion = paramVersionService.queryLastVersionByTaskId(task.getId(), msg.getParamVersionId()); HlwExmMessage<PhmOperateMsg> phmToDocMsg = HlwExmMessageUtil.buildPhmToDocMsg(operation, task, paramVersion,msg.getMsgType()); taskService.updateExmTask(task); phmToDocMsg.setCtrMaxLv(msg.getCtrMaxLv()); messageService.addMessage(phmToDocMsg); messageService.updateOperateMsg(operation.getMsgId(),phmToDocMsg.getMsgId()); if (taskState.isFinished()){//任务结束。转存最终结果表 storeFinalData(paramVersion); } return true; }
时间: 2024-01-21 11:18:46 浏览: 131
可以尝试对代码进行以下优化:
1. 建议在方法开头进行参数的非空校验,避免在后续代码中引起空指针异常。
2. 可以将以下代码块提取为方法,增加代码的可读性和复用性:
```java
HlwExmMessage<DocOperateMsg> msg2 = messageService.queryDocToPhmMsg(msg.getTaskId());
Integer msgId2 = msg2 == null ? null : msg2.getMsgId();
if (!Objects.equals(msg.getMsgId(), msgId2)) {
return new Result(Status.UN_ACCESS_OPERATION, "被处置消息已过期,处置无效,医生已修改医嘱,请刷新任务!");
}
```
3. 将以下代码块中的三个方法调用放在同一个事务中,确保代码的原子性:
```java
taskService.updateExmTask(task);
phmToDocMsg.setCtrMaxLv(msg.getCtrMaxLv());
messageService.addMessage(phmToDocMsg);
```
4. 可以对以下代码块进行优化,使用更加简洁的方式进行判断:
```java
if (ExmState.TASK_ABOLISHED.equals(task.getState())) {
return false;
}
```
可以改为:
```java
return !ExmState.TASK_ABOLISHED.equals(task.getState());
```
5. 可以考虑使用枚举来代替字符串,增加代码的可读性和可维护性。比如:
```java
return new Result<>(Status.ERROR, "非法处置:当前消息可选择的处置方式" + operations.toString());
```
可以改为:
```java
return new Result<>(Status.ERROR, String.format("非法处置:当前消息可选择的处置方式%s", operations.toString()));
```
阅读全文