你能帮我把下面的代码优化一下并给出示例吗?private String getContent(CommonTaskSaveDTO taskSaveDTO, TaskGroup group, StringBuilder sb) { String name = getStr(group.getName()); Field[] fields = taskSaveDTO.getClass().getDeclaredFields(); if (!isEqual(taskSaveDTO.getName(),name)) { sb.append("任务名称:").append(name).append(","); sb.append("任务名称:").append(taskSaveDTO.getName()).append(","); } if (taskSaveDTO.getTaskValidTime().compareTo(group.getTaskValidTime()) != 0) { sb.append("任务生效时间:").append(group.getTaskValidTime()).append(","); sb.append("任务生效时间:").append(taskSaveDTO.getTaskValidTime()).append(","); } if (taskSaveDTO.getTaskInvalidTime().compareTo(group.getTaskInvalidTime()) != 0) { sb.append("任务失效时间:").append(group.getTaskInvalidTime()).append(","); sb.append("任务生效时间:").append(taskSaveDTO.getTaskInvalidTime()).append(","); } String info = group.getInfo(); if (!taskSaveDTO.getInfo().equals(info)) { sb.append("任务说明:").append(getStr(info)).append(","); sb.append("任务说明:").append(taskSaveDTO.getInfo()).append(","); } //查询任务组和任务关联表 TaskGroupListDetailDTO detailDTO = taskGroupRelationService.queryTaskGroupRelationDetailList(group.getCode()); if (detailDTO == null) { return sb.deleteCharAt(sb.length() - 1).toString(); } String taskPurpose = CollectionUtils.isEmpty(taskSaveDTO.getTaskPurposes()) ? "" : String.join(",", taskSaveDTO.getTaskPurposes()); if (!taskPurpose.equals(detailDTO.getTaskPurpose())) { String newPurpose = getPurpose(taskPurpose); String oldPurpose = getPurpose(detailDTO.getTaskPurpose()); sb.append("任务目的:").append(getStr(oldPurpose)).append(","); sb.append("任务目的:").append(newPurpose).append(","); } //获取到用户身份 String join = StringUtils.join(taskSaveDTO.getUserIdentityList(), ","); String userIdentity = detailDTO.getUserIdentity(); if (!join.equals(userIdentity)) { sb.append("用户身份:").append("(").append(getStr(userIdentity)).append(")").append(","); sb.append("用户身份:").append("(").append(join).append(")").append(","); } String appCode = StringUtils.join(taskSaveDTO.getAppCodes(), ","); String appCode1 = detailDTO.getAppCode(); if (!appCode.equals(appCode1)) { sb.append("归属平台:").append("(").append(getStr(appCode1)).append(")").append(","); sb.append("归属平台:").append("(").append(appCode).append(")").append(","); } String chanel = StringUtils.join(taskSaveDTO.getChannels(), ","); String channel = detailDTO.getChannel(); if (!chanel.equals(channel)) { sb.append("归属渠道:").append("(").append(getStr(channel)).append(")").append(","); sb.append("归属渠道:").append("(").append(chanel).append(")").append(","); } String department = StringUtils.join(taskSaveDTO.getDepartments(), ","); String department1 = detailDTO.getDepartment(); if (!department.equals(department1)) { sb.append("归属渠道:").append("(").append(getStr(department1)).append(")").append(","); sb.append("归属渠道:").append("(").append(department).append(")").append(","); } if (!taskSaveDTO.getTaskState().equals(detailDTO.getTaskState())) { sb.append("启用/禁用:").append(Objects.requireNonNull(CommonBusinessEnum.TaskStateEnum.ofByValue(detailDTO.getTaskState())).getName()).append(","); sb.append("启用/禁用:").append(Objects.requireNonNull(CommonBusinessEnum.TaskStateEnum.ofByValue(taskSaveDTO.getTaskState())).getName()).append(","); } if(taskSaveDTO.getType().equals(CommonBusinessEnum.TaskTypeEnum.YDWZ.getValue())){ //判断阅读文章 readeRule(taskSaveDTO, sb, detailDTO); }else { //判断签到规则 checkRule(taskSaveDTO, sb, detailDTO); } if (sb.length() != 0) { return sb.deleteCharAt(sb.length() - 1).toString(); } return ""; }
时间: 2023-02-09 16:56:05 浏览: 107
感谢你的提问。我们可以对这段代码进行以下优化:
1. 使用 Java 的反射机制来简化代码。可以将任务信息存储在一个 Map 中,然后使用反射来访问这些信息。这样可以减少很多冗余的代码。
2. 将对比的属性和描述存储在数组中,这样可以使用循环简化代码。
3. 使用 StringBuilder 的 append 方法来组装字符串,这样可以减少字符串的拼接操作。
以下是优化后的代码示例:
```
private String getContent(CommonTaskSaveDTO taskSaveDTO, TaskGroup group, StringBuilder sb) {
Map<String, Object> taskInfo = new HashMap<>();
taskInfo.put("name", getStr(group.getName()));
taskInfo.put("taskValidTime", group.getTaskValidTime());
taskInfo.put("taskInvalidTime", group.getTaskInvalidTime());
taskInfo.put("info", getStr(group.getInfo()));
taskInfo.put("taskPurpose", getPurpose(detailDTO.getTaskPurpose()));
taskInfo.put("userIdentity", getStr(detailDTO.getUserIdentity()));
taskInfo.put("appCode", getStr(detailDTO.getAppCode()));
taskInfo.put("channel", getStr(detailDTO.getChannel()));
String[] descriptions = {"任务名称", "任务生效时间", "任务失效时间", "任务说明", "任务目的", "用户身份", "归属平台", "渠道"};
String[] properties = {"name", "taskValidTime", "taskInvalidTime", "info", "taskPurpose", "userIdentity", "appCode", "channel"};
for (int i = 0; i < descriptions.length; i++) {
String property = properties[i];
Object value1 = taskInfo.get(property);
Object value2 = taskSaveDTO.getClass().getDeclaredField(property).get(taskSaveDTO);
if (!value1.equals(value2)) {
sb.append(descriptions[i]).append(":").append(
阅读全文