From d9a0236d961eff353ed783f7f7f97f075b7f385a Mon Sep 17 00:00:00 2001 From: qyx <565485304@qq.com> Date: Tue, 15 Sep 2020 09:50:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9B=B8=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91,=E8=BF=9B=E8=A1=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../taskrecord/rest/TaskRecordController.java | 22 +++++-- .../zhengjie/task/MergeRecordFilesTask.java | 64 +++++++++++++++++-- .../java/me/zhengjie/task/SendRecordTask.java | 19 +++--- 3 files changed, 88 insertions(+), 17 deletions(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/taskrecord/rest/TaskRecordController.java b/eladmin-system/src/main/java/me/zhengjie/modules/taskrecord/rest/TaskRecordController.java index e8faccd..20e60c4 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/taskrecord/rest/TaskRecordController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/taskrecord/rest/TaskRecordController.java @@ -119,7 +119,7 @@ public class TaskRecordController { @Log("新建课包任务") @ApiOperation("新建课包任务") // @PreAuthorize("@el.check('taskRecord:add')") - @AnonymousAccess + @AnonymousAccess // fixme 需要测试完成后进行去除和使用上面的权限注解 public ResponseEntity buildTask(@RequestBody TaskRecordBuildVO taskRecordBuildVO){ TaskRecord resources = taskRecordBuildVO.getResources(); // 任务名称不能重复 @@ -157,7 +157,7 @@ public class TaskRecordController { @ApiOperation("发送课包任务") // @PreAuthorize("@el.check('taskRecord:list')") @PostMapping(value = "/sendTask") - @AnonymousAccess + @AnonymousAccess // fixme 需要测试完成后进行去除和使用上面的权限注解 public ResponseEntity sendTask(@RequestBody TaskRecordSendVO taskRecordSendVO){ // 参数校验 Integer id = taskRecordSendVO.getId(); @@ -181,7 +181,7 @@ public class TaskRecordController { @ApiOperation("发送课包数目校验") // @PreAuthorize("@el.check('taskRecord:list')") @PostMapping(value = "/checkSendTotal") - @AnonymousAccess + @AnonymousAccess // fixme 需要测试完成后进行去除和使用上面的权限注解 public ResponseEntity checkSendTotal(@RequestParam(value = "sendTotal", defaultValue = "0") Long sendTotal, @RequestParam(value = "id")Integer id){ if (sendTotal == null || id == null){ @@ -201,7 +201,13 @@ public class TaskRecordController { private boolean isMatchCheckNum(TaskRecordDto dto, Long sendTotal){ Long total = dto.getTotal(); + if (total == null){ + total = 0L; + } Long send = dto.getSendTotal(); + if (send == null){ + send = 0L; + } if ((sendTotal + send) < total){ return Boolean.TRUE; } @@ -218,16 +224,22 @@ public class TaskRecordController { @ApiOperation("任务课包任务合并") // @PreAuthorize("@el.check('taskRecord:list')") @PostMapping(value = "/mergeRecord") - @AnonymousAccess + @AnonymousAccess // fixme 需要测试完成后进行去除和使用上面的权限注解 public ResponseEntity mergeRecord(@RequestBody TaskRecordMergeVO taskRecordMergeVO){ + // 参数校验 List idList = taskRecordMergeVO.getIdList(); String type = taskRecordMergeVO.getType(); TaskRecord taskRecord = taskRecordMergeVO.getTaskRecord(); if (CollectionUtils.isEmpty(idList) || StringUtils.isBlank(type) || taskRecord == null){ return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.EMPTY_ARGUMENT), HttpStatus.OK); } + // 任务名称不能重复 + String taskName = taskRecord.getTaskName(); + if (!checkTaskNameForBuild(taskName)){ + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_NAME_IS_EXIST), HttpStatus.OK); + } mergeRecordFilesTask.doRunTask(idList, type, taskRecord); - return new ResponseEntity<>(HttpStatus.OK); + return new ResponseEntity<>(CommonResponse.createBySuccess(), HttpStatus.OK); } // ================ 自定义功能实现 end ================ } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/task/MergeRecordFilesTask.java b/eladmin-system/src/main/java/me/zhengjie/task/MergeRecordFilesTask.java index c0a5a4e..896fe94 100644 --- a/eladmin-system/src/main/java/me/zhengjie/task/MergeRecordFilesTask.java +++ b/eladmin-system/src/main/java/me/zhengjie/task/MergeRecordFilesTask.java @@ -1,5 +1,7 @@ package me.zhengjie.task; +import cn.hutool.system.OsInfo; +import cn.hutool.system.SystemUtil; import lombok.extern.slf4j.Slf4j; import me.zhengjie.modules.taskrecord.domain.TaskRecord; import me.zhengjie.modules.taskrecord.service.TaskRecordService; @@ -7,6 +9,7 @@ import me.zhengjie.modules.taskrecord.service.dto.TaskRecordDto; import me.zhengjie.utils.FileUtil; import me.zhengjie.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; @@ -18,6 +21,7 @@ import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.time.LocalDateTime; import java.time.ZoneOffset; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -32,7 +36,7 @@ public class MergeRecordFilesTask { private static final String INTERSECTION_TAG = "intersection"; /** - * 两个集合操作 - 交集 -(listA ∪ listB) + * 两个集合操作 - 并集 -(listA ∪ listB) */ private static final String UNION_TAG = "union"; @@ -46,6 +50,22 @@ public class MergeRecordFilesTask { */ private static final int SUCESS_BUILD_TAG = 1; + /** + * Windows环境下的路径信息 + */ + @Value(value = "${file.windows.path}") + private String windowsFilePath; + /** + * Windows环境下的路径信息 + */ + @Value(value = "${file.linux.path}") + private String linuxFilePath; + /** + * Windows环境下的路径信息 + */ + @Value(value = "${file.mac.path}") + private String macFilePath; + @Autowired private TaskRecordService taskRecordService; @@ -90,7 +110,8 @@ public class MergeRecordFilesTask { ); // 把合并后的list建立任务写入到文件中 Path path = null; - String filePath = FileUtil.buildOnlyFileNameRule(); + buildFileWritePath(taskRecord); + String filePath = taskRecord.getLocalFilePath(); try { if (StringUtils.isNotBlank(filePath)){ path = Files.write(Paths.get(filePath), tempList, StandardOpenOption.CREATE_NEW); @@ -101,8 +122,8 @@ public class MergeRecordFilesTask { } // 更新课包记录信息 if (path != null){ - taskRecord.setLocalFilePath(filePath); taskRecord.setIsBuild(SUCESS_BUILD_TAG); + taskRecord.setTotal((long) tempList.size()); TaskRecordDto taskRecordDto = taskRecordService.create(taskRecord); if (taskRecordDto == null){ log.error("========== [Insert merge File Task Info fail, please check out path, the path is : {} ] ==========", filePath); @@ -111,9 +132,39 @@ public class MergeRecordFilesTask { tempList.clear(); } + /** + * 解析文件然后入库 + * + * @param taskRecord 需要构建的任务包体 + */ + private void buildFileWritePath(TaskRecord taskRecord){ + // 获取环境配置信息 + OsInfo osInfo = SystemUtil.getOsInfo(); + String fullPath; + if (osInfo.isWindows()){ + FileUtil.mkdir(windowsFilePath); + // 构建存储文件 + fullPath = windowsFilePath + FileUtil.buildOnlyFileNameRule(); + }else if (osInfo.isLinux()){ + FileUtil.mkdir(linuxFilePath); + // 构建存储文件 + fullPath = linuxFilePath + FileUtil.buildOnlyFileNameRule(); + }else if (osInfo.isMac()){ + FileUtil.mkdir(macFilePath); + // 构建存储文件 + fullPath = macFilePath + FileUtil.buildOnlyFileNameRule(); + }else { + return; + } + taskRecord.setLocalFilePath(fullPath); + } + + + private void mergeEachCollect(List list, String tag) { if (CollectionUtils.isEmpty(tempList)){ + tempList = new ArrayList<>(); tempList.addAll(list); return; } @@ -128,7 +179,12 @@ public class MergeRecordFilesTask { } // 差 if (StringUtils.equalsIgnoreCase(SUBTRACTION_TAG, tag)){ - tempList = tempList.stream().filter(item -> !list.contains(item)).collect(Collectors.toList()); + // fixme 差集的时候注意大小集合的区别 大 - 小 + if (tempList.size() > list.size()){ + tempList = tempList.stream().filter(item -> !list.contains(item)).collect(Collectors.toList()); + }else { + tempList = list.stream().filter(item -> !tempList.contains(item)).collect(Collectors.toList()); + } } } } diff --git a/eladmin-system/src/main/java/me/zhengjie/task/SendRecordTask.java b/eladmin-system/src/main/java/me/zhengjie/task/SendRecordTask.java index dd27ed2..4786c9c 100644 --- a/eladmin-system/src/main/java/me/zhengjie/task/SendRecordTask.java +++ b/eladmin-system/src/main/java/me/zhengjie/task/SendRecordTask.java @@ -75,19 +75,20 @@ public class SendRecordTask { // 准备所有需要发送任务 TaskRecordDto taskRecordDto = taskRecordService.findById(id); if (BUILD_STATUS == taskRecordDto.getIsBuild() && StringUtils.isNotBlank(taskRecordDto.getLocalFilePath())){ - String localFilePath = taskRecordDto.getLocalFilePath(); - if (StringUtils.isBlank(localFilePath)){ - log.error("============== [ localFilePath is Empty, please check taskRecordDto : {} ]==============", taskRecordDto.toString()); - return; - } + String localFilePath = taskRecordDto.getLocalFilePath(); + if (StringUtils.isBlank(localFilePath)){ + log.error("============== [ localFilePath is Empty, please check taskRecordDto : {} ]==============", taskRecordDto.toString()); + return; + } try { // tips : 发送前需要调用前端接口进行发送数目合法性校验 List lines = Files.readAllLines(Paths.get(localFilePath)); // 限制发送次数 为limit以下 Long sendTotal = taskRecordDto.getSendTotal(); - if (sendTotal <= 0 ){ - return; + if (sendTotal == null){ + sendTotal = 0L; } + List collect = lines.stream() .skip(sendTotal) .limit(limit - sendTotal) @@ -98,7 +99,8 @@ public class SendRecordTask { // 对发送后的记录进行更新 TaskRecord taskRecord = new TaskRecord(); BeanUtils.copyProperties(taskRecordDto, taskRecord); - taskRecord.setSendTotal(taskRecordDto.getSendTotal() + limit); + taskRecord.setId(id); + taskRecord.setSendTotal(sendTotal + limit); taskRecord.setIsSend(FINISH_SEND_TAG); taskRecordService.update(taskRecord); } catch (IOException e) { @@ -113,6 +115,7 @@ public class SendRecordTask { list->{ // TODO: 2020/9/10 0010 调用推送地址进行推送 String jsonStr = JSON.toJSONString(list); + System.out.println(jsonStr); if (StringUtils.isNotBlank(jsonStr)){ int count = 1; // 失败重发请求3次