From 8de89de837a00f53169a74249545350e2c1f4767 Mon Sep 17 00:00:00 2001 From: skyyemperor Date: Sat, 10 Dec 2022 21:40:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=87=E4=BB=B6=E5=8D=95=E4=B8=AA?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: skyyemperor --- .../biology/controller/JobController.java | 106 +++++++++++++++--- .../core/data/enums/JobStatusEnum.java | 26 +++-- .../core/data/vo/result/error/JobError.java | 1 + .../com/weilab/biology/mapper/JobMapper.java | 1 + .../weilab/biology/service/JobService.java | 15 +-- src/main/resources/mapper/JobMapper.xml | 3 + 6 files changed, 117 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/weilab/biology/controller/JobController.java b/src/main/java/com/weilab/biology/controller/JobController.java index 7302622..25752b4 100644 --- a/src/main/java/com/weilab/biology/controller/JobController.java +++ b/src/main/java/com/weilab/biology/controller/JobController.java @@ -2,14 +2,18 @@ package com.weilab.biology.controller; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.weilab.biology.core.data.dto.JobDto; import com.weilab.biology.core.data.enums.JobStatusEnum; +import com.weilab.biology.core.data.po.Job; import com.weilab.biology.core.data.vo.result.CommonError; import com.weilab.biology.core.data.vo.result.Result; import com.weilab.biology.core.data.vo.result.error.JobError; import com.weilab.biology.core.validation.EnumValidation; +import com.weilab.biology.mapper.JobMapper; import com.weilab.biology.service.JobService; import com.weilab.biology.util.FileUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -19,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.BufferedInputStream; import java.io.IOException; +import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; @@ -30,11 +35,15 @@ public class JobController { @Autowired private JobService jobService; + @Autowired + private JobMapper jobMapper; + @Value("${biology.request-path}") private String requestPath; @PostMapping("/submit") - public Result submit(@RequestParam(required = false) String dataStr, + public Result submit(@RequestParam(required = false) Long jobId, + @RequestParam(required = false) String dataStr, @RequestParam(required = false) MultipartFile dataFile, @RequestParam String param, @RequestParam String mail, @@ -59,30 +68,29 @@ public class JobController { @RequestParam(required = false) MultipartFile file18, @RequestParam(required = false) MultipartFile file19, @RequestParam(required = false) MultipartFile file20) { - if (dataFile == null && StringUtils.isBlank(dataStr)) + if (dataFile == null && StringUtils.isBlank(dataStr)) { return Result.getResult(JobError.PARAM_CAN_NOT_BE_EMPTY); + } - JSONObject obj = null; + // 解析param字符串为map对象 + JSONObject reqParamMap = null; try { - obj = JSON.parseObject(param); + reqParamMap = JSON.parseObject(param); } catch (Exception e) { return Result.getResult(CommonError.PARAM_WRONG); } - BufferedInputStream dataStream = null; - if (dataFile != null) { - dataStream = FileUtil.getInputStream(FileUtils.multipartToFile(dataFile)); - } - + // 写入file文件,并将文件路径放入paramMap try { List files = Arrays.asList(file1, file2, file3, file4, file5, file6, file7, file8, file9, file10, file11, file12, file13, file14, file15, file16, file17, file18, file19, file20); for (MultipartFile file : files) { if (file != null) { - String filePath = requestPath + FileUtil.FILE_SEPARATOR + "file" + FileUtil.FILE_SEPARATOR + IdUtil.fastUUID() + "." + FileUtil.extName(file.getOriginalFilename()); + String filePath = requestPath + FileUtil.FILE_SEPARATOR + "file" + FileUtil.FILE_SEPARATOR + + IdUtil.fastUUID() + "." + FileUtil.extName(file.getOriginalFilename()); FileUtil.writeFromStream(file.getInputStream(), filePath); - obj.put(file.getName(), filePath); + reqParamMap.put(file.getName(), filePath); } } } catch (IOException e) { @@ -90,7 +98,78 @@ public class JobController { return Result.getResult(JobError.FILE_READ_FAIL); } - return jobService.submit(dataStr, dataStream, obj, mail, type); + // 将请求数据file转为InputStream + BufferedInputStream dataStream = null; + if (dataFile != null) { + dataStream = FileUtil.getInputStream(FileUtils.multipartToFile(dataFile)); + } + + Job job = null; + if (jobId != null) { + // jobId不为空,从数据库获取已创建的任务 + job = jobMapper.selectById(jobId); + if (job == null) { + return Result.getResult(JobError.JOB_NOT_FOUND); + } + } else { + job = new Job(); + } + + if (!StrUtil.isEmpty(job.getParam())) { + JSONObject params = JSON.parseObject(job.getParam()); + params.putAll(reqParamMap); + reqParamMap = params; + job.setParam(JSON.toJSONString(reqParamMap)); + } + + job.setData(""); + job.setMail(mail); + job.setStatus(JobStatusEnum.WAIT.getKey()); + job.setRequestTime(LocalDateTime.now()); + job.setType(type); + + return jobService.submit(dataStr, dataStream, reqParamMap, job); + } + + @PostMapping("/create") + public Result createJob() { + Job job = new Job(); + job.setParam("{}"); + job.setStatus(JobStatusEnum.CREATING.getKey()); + job.setRequestTime(LocalDateTime.now()); + jobMapper.insert(job); + return Result.success(JobDto.parseJob(job)); + } + + @PostMapping("/file/upload") + public Result uploadFile(@RequestParam Long jobId, + @RequestParam String fileKey, + @RequestParam MultipartFile file) { + Job job = jobMapper.selectById(jobId); + if (job == null || !job.getStatus().equals(JobStatusEnum.CREATING.getKey())) { + return Result.getResult(JobError.JOB_NOT_FOUND); + } + + JSONObject params = JSON.parseObject(job.getParam()); + try { + if (file != null) { + String extName = FileUtil.extName(file.getOriginalFilename()); + if (!StrUtil.isEmpty(extName)) { + extName = StrUtil.DOT + extName; + } + String filePath = requestPath + FileUtil.FILE_SEPARATOR + jobId + FileUtil.FILE_SEPARATOR + + IdUtil.fastUUID() + extName; + FileUtil.writeFromStream(file.getInputStream(), filePath); + params.put(fileKey, filePath); + + job.setParam(JSON.toJSONString(params)); + jobMapper.updateById(job); + return Result.success(job); + } + } catch (Exception e) { + e.printStackTrace(); + } + return Result.fail(); } @PostMapping("/status/update") @@ -124,8 +203,9 @@ public class JobController { @GetMapping("/list/v2") public Result getJobList(@RequestParam(required = false) Integer type, + @RequestParam(defaultValue = "true") Boolean filterCreating, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "100") Integer size) { - return jobService.getJobList(type, page, size); + return jobService.getJobList(type, filterCreating, page, size); } } diff --git a/src/main/java/com/weilab/biology/core/data/enums/JobStatusEnum.java b/src/main/java/com/weilab/biology/core/data/enums/JobStatusEnum.java index 63c9dc2..bcfa664 100644 --- a/src/main/java/com/weilab/biology/core/data/enums/JobStatusEnum.java +++ b/src/main/java/com/weilab/biology/core/data/enums/JobStatusEnum.java @@ -2,41 +2,45 @@ package com.weilab.biology.core.data.enums; import lombok.Getter; +import java.util.HashMap; +import java.util.Map; + /** * 校区的枚举类 */ @Getter public enum JobStatusEnum { WAIT(0, "waiting"), - REQED(3, "requested"), RUNNING(1, "running"), SUCCESS(2, "success"), + REQED(3, "requested"), FAIL(-1, "failed"), TIMEOUT(-2, "timeout"), + CREATING(-3, "creating"), ; private final Integer key; private final String remark; + private final static Map HOLDER = new HashMap<>(); + + static { + for (JobStatusEnum e : JobStatusEnum.values()) { + HOLDER.put(e.key, e); + } + } + private JobStatusEnum(Integer key, String remark) { this.key = key; this.remark = remark; } public static String getRemark(Integer key) { - for (JobStatusEnum enums : JobStatusEnum.values()) { - if (enums.key.equals(key)) - return enums.getRemark(); - } - return null; + return HOLDER.get(key).remark; } public static JobStatusEnum getEnumByKey(Integer key) { - for (JobStatusEnum enums : JobStatusEnum.values()) { - if (enums.key.equals(key)) - return enums; - } - return null; + return HOLDER.get(key); } } diff --git a/src/main/java/com/weilab/biology/core/data/vo/result/error/JobError.java b/src/main/java/com/weilab/biology/core/data/vo/result/error/JobError.java index 44e7f01..0347ae3 100644 --- a/src/main/java/com/weilab/biology/core/data/vo/result/error/JobError.java +++ b/src/main/java/com/weilab/biology/core/data/vo/result/error/JobError.java @@ -10,6 +10,7 @@ public enum JobError implements ResultError { PARAM_CAN_NOT_BE_EMPTY(40100, "文本框和文件不能同时为空"), FILE_READ_FAIL(40101, "文件读取出错"), STATUS_UPDATE_FAIL(40102,"当前状态下不允许更新为指定状态"), + JOB_NOT_FOUND(40103,"当前状态下不允许更新为指定状态"), ; private int code; diff --git a/src/main/java/com/weilab/biology/mapper/JobMapper.java b/src/main/java/com/weilab/biology/mapper/JobMapper.java index 08d9a94..e2ea8d1 100644 --- a/src/main/java/com/weilab/biology/mapper/JobMapper.java +++ b/src/main/java/com/weilab/biology/mapper/JobMapper.java @@ -21,6 +21,7 @@ public interface JobMapper extends BaseMapper { List selectJobList(@Param("type") Integer type); List selectJobListByPage(@Param("type") Integer type, + @Param("filterCreating") Boolean filterCreating, @Param("offset") Integer offset, @Param("count") Integer count); diff --git a/src/main/java/com/weilab/biology/service/JobService.java b/src/main/java/com/weilab/biology/service/JobService.java index ea9b26b..fc93f59 100644 --- a/src/main/java/com/weilab/biology/service/JobService.java +++ b/src/main/java/com/weilab/biology/service/JobService.java @@ -80,18 +80,11 @@ public class JobService { /** * 提交job - * - * @param dataStr 文本内容 - * @param param 其他参数(json格式) - * @param mail 邮箱 - * @return - * @throws Exception */ @Transactional - public Result submit(String dataStr, BufferedInputStream dataStream, JSONObject param, String mail, Integer type) { - Job job = new Job("", JSON.toJSONString(param), mail, JobStatusEnum.WAIT.getKey(), LocalDateTime.now(), type); + public Result submit(String dataStr, BufferedInputStream dataStream, JSONObject param, Job job) { jobMapper.insert(job); - sendEmail(job, JobStatusEnum.WAIT, mail); + sendEmail(job, JobStatusEnum.WAIT, job.getMail()); try { //将请求数据写入本地文件,之后向python传递文件路径参数 @@ -172,8 +165,8 @@ public class JobService { return Result.success(jobs.stream().map(JobLessDto::parseJob).collect(Collectors.toList())); } - public Result getJobList(Integer type, Integer page, Integer size) { - List jobs = jobMapper.selectJobListByPage(type, (page - 1) * size, size); + public Result getJobList(Integer type, Boolean filterCreating, Integer page, Integer size) { + List jobs = jobMapper.selectJobListByPage(type, filterCreating, (page - 1) * size, size); return Result.success(jobs.stream().map(JobLessDto::parseJob).collect(Collectors.toList())); } diff --git a/src/main/resources/mapper/JobMapper.xml b/src/main/resources/mapper/JobMapper.xml index c8e5020..8acf17a 100644 --- a/src/main/resources/mapper/JobMapper.xml +++ b/src/main/resources/mapper/JobMapper.xml @@ -63,6 +63,9 @@ AND type = #{type} + + AND `status` != '${@com.weilab.biology.core.data.enums.JobStatusEnum@CREATING.getKey()}' + ORDER BY job_id DESC LIMIT #{offset}, #{count}