重构这段代码 private void automaticDispatch(JointCase jointCase) { if (EmptyUtils.isNotEmpty(jointCase) && EmptyUtils.isNotEmpty(jointCase.getCaseSmallType())) { BaseHandleTimeDetailsResponse baseHandleTimeDetailsResponse = baseHandleTimeHelper.getBaseHandleTimeDetailsResponse(jointCase.getCaseSmallType()); if (EmptyUtils.isNotEmpty(baseHandleTimeDetailsResponse) && DispatchTypeEnum.AUTOMATIC_DISPATCH_TYPE.getCode().equals(baseHandleTimeDetailsResponse.getDispatchType()) && CollectionTypeEnum.SELF_EXAMINATION_TYPE.getCode() != jointCase.getCollectionType()) { int handleTime = baseHandleTimeDetailsResponse.getHandleDays() * JointCommandConstants.HOURS * JointCommandConstants.MINUTE * JointCommandConstants.SECOND + baseHandleTimeDetailsResponse.getHandleHours() * JointCommandConstants.MINUTE * JointCommandConstants.SECOND + baseHandleTimeDetailsResponse.getHandleMinute() * JointCommandConstants.SECOND; List<String> assistDeptCodes = ClassCastUtils.toList(baseHandleTimeDetailsResponse.getAssistDeptCode(), SymbolConstants.COMMA.getSymbol()); JointCommandCaseDispatchAddRequest dispatchAddRequest = JointCommandCaseDispatchAddRequest.builder().jointCaseId(jointCase.getId()).dispatchRemarks(JointCommandConstants.DISPATCH_SLOGAN) .handleTime(handleTime).mianDeptCode(baseHandleTimeDetailsResponse.getMainDeptCode()).assistDeptCodeList(assistDeptCodes).version(GeneralConstants.NUMBER_ZERO).build(); approvalProcessService.submitFirstDispatch(dispatchAddRequest); } } }
时间: 2024-03-22 07:41:16 浏览: 55
这段代码的主要问题在于可读性较差,存在一些魔法数字和硬编码的部分,同时也有一些过长的方法调用链,影响了代码的可维护性和可扩展性。下面是一个重构后的代码示例:
```java
private void automaticDispatch(JointCase jointCase) {
if (jointCase == null || jointCase.getCaseSmallType() == null) {
return;
}
BaseHandleTimeDetailsResponse handleTimeDetails = baseHandleTimeHelper.getBaseHandleTimeDetailsResponse(jointCase.getCaseSmallType());
if (handleTimeDetails == null || !DispatchTypeEnum.AUTOMATIC_DISPATCH_TYPE.getCode().equals(handleTimeDetails.getDispatchType())) {
return;
}
if (CollectionTypeEnum.SELF_EXAMINATION_TYPE.getCode() == jointCase.getCollectionType()) {
return;
}
int handleTimeInSeconds = convertHandleTimeToSeconds(handleTimeDetails);
List<String> assistDeptCodes = convertAssistDeptCodesToList(handleTimeDetails.getAssistDeptCode());
JointCommandCaseDispatchAddRequest dispatchRequest = JointCommandCaseDispatchAddRequest.builder()
.jointCaseId(jointCase.getId())
.dispatchRemarks(JointCommandConstants.DISPATCH_SLOGAN)
.handleTime(handleTimeInSeconds)
.mianDeptCode(handleTimeDetails.getMainDeptCode())
.assistDeptCodeList(assistDeptCodes)
.version(GeneralConstants.NUMBER_ZERO)
.build();
approvalProcessService.submitFirstDispatch(dispatchRequest);
}
private int convertHandleTimeToSeconds(BaseHandleTimeDetailsResponse handleTimeDetails) {
int handleDaysInSeconds = handleTimeDetails.getHandleDays() * JointCommandConstants.HOURS * JointCommandConstants.MINUTE * JointCommandConstants.SECOND;
int handleHoursInSeconds = handleTimeDetails.getHandleHours() * JointCommandConstants.MINUTE * JointCommandConstants.SECOND;
int handleMinutesInSeconds = handleTimeDetails.getHandleMinute() * JointCommandConstants.SECOND;
return handleDaysInSeconds + handleHoursInSeconds + handleMinutesInSeconds;
}
private List<String> convertAssistDeptCodesToList(String assistDeptCodes) {
if (StringUtils.isBlank(assistDeptCodes)) {
return Collections.emptyList();
}
return Arrays.asList(assistDeptCodes.split(SymbolConstants.COMMA.getSymbol()));
}
```
重构后的代码中,我们将原来的多个 if 判断合并成了一个顺序执行的逻辑,这样可以提高代码的可读性。我们还将一些常量提取成了静态变量或方法,这样可以避免硬编码和魔法数字。此外,我们还将一些过长的方法调用链拆分成了多个方法,提高了代码的可维护性和可扩展性。
阅读全文