| @@ -2,14 +2,18 @@ package com.weilab.biology.controller; | |||||||
|  |  | ||||||
| import cn.hutool.core.io.FileUtil; | import cn.hutool.core.io.FileUtil; | ||||||
| import cn.hutool.core.util.IdUtil; | import cn.hutool.core.util.IdUtil; | ||||||
|  | import cn.hutool.core.util.StrUtil; | ||||||
| import com.alibaba.fastjson.JSON; | import com.alibaba.fastjson.JSON; | ||||||
| import com.alibaba.fastjson.JSONObject; | import com.alibaba.fastjson.JSONObject; | ||||||
| import com.baomidou.mybatisplus.core.toolkit.StringUtils; | 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.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.CommonError; | ||||||
| import com.weilab.biology.core.data.vo.result.Result; | import com.weilab.biology.core.data.vo.result.Result; | ||||||
| import com.weilab.biology.core.data.vo.result.error.JobError; | import com.weilab.biology.core.data.vo.result.error.JobError; | ||||||
| import com.weilab.biology.core.validation.EnumValidation; | import com.weilab.biology.core.validation.EnumValidation; | ||||||
|  | import com.weilab.biology.mapper.JobMapper; | ||||||
| import com.weilab.biology.service.JobService; | import com.weilab.biology.service.JobService; | ||||||
| import com.weilab.biology.util.FileUtils; | import com.weilab.biology.util.FileUtils; | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| @@ -19,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile; | |||||||
|  |  | ||||||
| import java.io.BufferedInputStream; | import java.io.BufferedInputStream; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.time.LocalDateTime; | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| @@ -30,11 +35,15 @@ public class JobController { | |||||||
|     @Autowired |     @Autowired | ||||||
|     private JobService jobService; |     private JobService jobService; | ||||||
|  |  | ||||||
|  |     @Autowired | ||||||
|  |     private JobMapper jobMapper; | ||||||
|  |  | ||||||
|     @Value("${biology.request-path}") |     @Value("${biology.request-path}") | ||||||
|     private String requestPath; |     private String requestPath; | ||||||
|  |  | ||||||
|     @PostMapping("/submit") |     @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(required = false) MultipartFile dataFile, | ||||||
|                          @RequestParam String param, |                          @RequestParam String param, | ||||||
|                          @RequestParam String mail, |                          @RequestParam String mail, | ||||||
| @@ -59,30 +68,29 @@ public class JobController { | |||||||
|                          @RequestParam(required = false) MultipartFile file18, |                          @RequestParam(required = false) MultipartFile file18, | ||||||
|                          @RequestParam(required = false) MultipartFile file19, |                          @RequestParam(required = false) MultipartFile file19, | ||||||
|                          @RequestParam(required = false) MultipartFile file20) { |                          @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); |             return Result.getResult(JobError.PARAM_CAN_NOT_BE_EMPTY); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         JSONObject obj = null; |         // 解析param字符串为map对象 | ||||||
|  |         JSONObject reqParamMap = null; | ||||||
|         try { |         try { | ||||||
|             obj = JSON.parseObject(param); |             reqParamMap = JSON.parseObject(param); | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             return Result.getResult(CommonError.PARAM_WRONG); |             return Result.getResult(CommonError.PARAM_WRONG); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         BufferedInputStream dataStream = null; |         // 写入file文件,并将文件路径放入paramMap | ||||||
|         if (dataFile != null) { |  | ||||||
|             dataStream = FileUtil.getInputStream(FileUtils.multipartToFile(dataFile)); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             List<MultipartFile> files = Arrays.asList(file1, file2, file3, file4, file5, |             List<MultipartFile> files = Arrays.asList(file1, file2, file3, file4, file5, | ||||||
|                     file6, file7, file8, file9, file10, file11, file12, file13, file14, file15, |                     file6, file7, file8, file9, file10, file11, file12, file13, file14, file15, | ||||||
|                     file16, file17, file18, file19, file20); |                     file16, file17, file18, file19, file20); | ||||||
|             for (MultipartFile file : files) { |             for (MultipartFile file : files) { | ||||||
|                 if (file != null) { |                 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); |                     FileUtil.writeFromStream(file.getInputStream(), filePath); | ||||||
|                     obj.put(file.getName(), filePath); |                     reqParamMap.put(file.getName(), filePath); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
| @@ -90,7 +98,78 @@ public class JobController { | |||||||
|             return Result.getResult(JobError.FILE_READ_FAIL); |             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") |     @PostMapping("/status/update") | ||||||
| @@ -124,8 +203,9 @@ public class JobController { | |||||||
|  |  | ||||||
|     @GetMapping("/list/v2") |     @GetMapping("/list/v2") | ||||||
|     public Result getJobList(@RequestParam(required = false) Integer type, |     public Result getJobList(@RequestParam(required = false) Integer type, | ||||||
|  |                              @RequestParam(defaultValue = "true") Boolean filterCreating, | ||||||
|                              @RequestParam(defaultValue = "1") Integer page, |                              @RequestParam(defaultValue = "1") Integer page, | ||||||
|                              @RequestParam(defaultValue = "100") Integer size) { |                              @RequestParam(defaultValue = "100") Integer size) { | ||||||
|         return jobService.getJobList(type, page, size); |         return jobService.getJobList(type, filterCreating, page, size); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,41 +2,45 @@ package com.weilab.biology.core.data.enums; | |||||||
|  |  | ||||||
| import lombok.Getter; | import lombok.Getter; | ||||||
|  |  | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 校区的枚举类 |  * 校区的枚举类 | ||||||
|  */ |  */ | ||||||
| @Getter | @Getter | ||||||
| public enum JobStatusEnum { | public enum JobStatusEnum { | ||||||
|     WAIT(0, "waiting"), |     WAIT(0, "waiting"), | ||||||
|     REQED(3, "requested"), |  | ||||||
|     RUNNING(1, "running"), |     RUNNING(1, "running"), | ||||||
|     SUCCESS(2, "success"), |     SUCCESS(2, "success"), | ||||||
|  |     REQED(3, "requested"), | ||||||
|     FAIL(-1, "failed"), |     FAIL(-1, "failed"), | ||||||
|     TIMEOUT(-2, "timeout"), |     TIMEOUT(-2, "timeout"), | ||||||
|  |     CREATING(-3, "creating"), | ||||||
|     ; |     ; | ||||||
|  |  | ||||||
|     private final Integer key; |     private final Integer key; | ||||||
|     private final String remark; |     private final String remark; | ||||||
|  |  | ||||||
|  |     private final static Map<Integer, JobStatusEnum> HOLDER = new HashMap<>(); | ||||||
|  |  | ||||||
|  |     static { | ||||||
|  |         for (JobStatusEnum e : JobStatusEnum.values()) { | ||||||
|  |             HOLDER.put(e.key, e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private JobStatusEnum(Integer key, String remark) { |     private JobStatusEnum(Integer key, String remark) { | ||||||
|         this.key = key; |         this.key = key; | ||||||
|         this.remark = remark; |         this.remark = remark; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static String getRemark(Integer key) { |     public static String getRemark(Integer key) { | ||||||
|         for (JobStatusEnum enums : JobStatusEnum.values()) { |         return HOLDER.get(key).remark; | ||||||
|             if (enums.key.equals(key)) |  | ||||||
|                 return enums.getRemark(); |  | ||||||
|         } |  | ||||||
|         return null; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static JobStatusEnum getEnumByKey(Integer key) { |     public static JobStatusEnum getEnumByKey(Integer key) { | ||||||
|         for (JobStatusEnum enums : JobStatusEnum.values()) { |         return HOLDER.get(key); | ||||||
|             if (enums.key.equals(key)) |  | ||||||
|                 return enums; |  | ||||||
|         } |  | ||||||
|         return null; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ public enum JobError implements ResultError { | |||||||
|     PARAM_CAN_NOT_BE_EMPTY(40100, "文本框和文件不能同时为空"), |     PARAM_CAN_NOT_BE_EMPTY(40100, "文本框和文件不能同时为空"), | ||||||
|     FILE_READ_FAIL(40101, "文件读取出错"), |     FILE_READ_FAIL(40101, "文件读取出错"), | ||||||
|     STATUS_UPDATE_FAIL(40102,"当前状态下不允许更新为指定状态"), |     STATUS_UPDATE_FAIL(40102,"当前状态下不允许更新为指定状态"), | ||||||
|  |     JOB_NOT_FOUND(40103,"当前状态下不允许更新为指定状态"), | ||||||
|     ; |     ; | ||||||
|  |  | ||||||
|     private int code; |     private int code; | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ public interface JobMapper extends BaseMapper<Job> { | |||||||
|     List<Job> selectJobList(@Param("type") Integer type); |     List<Job> selectJobList(@Param("type") Integer type); | ||||||
|  |  | ||||||
|     List<Job> selectJobListByPage(@Param("type") Integer type, |     List<Job> selectJobListByPage(@Param("type") Integer type, | ||||||
|  |                                   @Param("filterCreating")  Boolean filterCreating, | ||||||
|                                   @Param("offset") Integer offset, |                                   @Param("offset") Integer offset, | ||||||
|                                   @Param("count") Integer count); |                                   @Param("count") Integer count); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -80,18 +80,11 @@ public class JobService { | |||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 提交job |      * 提交job | ||||||
|      * |  | ||||||
|      * @param dataStr 文本内容 |  | ||||||
|      * @param param   其他参数(json格式) |  | ||||||
|      * @param mail    邮箱 |  | ||||||
|      * @return |  | ||||||
|      * @throws Exception |  | ||||||
|      */ |      */ | ||||||
|     @Transactional |     @Transactional | ||||||
|     public Result submit(String dataStr, BufferedInputStream dataStream, JSONObject param, String mail, Integer type) { |     public Result submit(String dataStr, BufferedInputStream dataStream, JSONObject param, Job job) { | ||||||
|         Job job = new Job("", JSON.toJSONString(param), mail, JobStatusEnum.WAIT.getKey(), LocalDateTime.now(), type); |  | ||||||
|         jobMapper.insert(job); |         jobMapper.insert(job); | ||||||
|         sendEmail(job, JobStatusEnum.WAIT, mail); |         sendEmail(job, JobStatusEnum.WAIT, job.getMail()); | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             //将请求数据写入本地文件,之后向python传递文件路径参数 |             //将请求数据写入本地文件,之后向python传递文件路径参数 | ||||||
| @@ -172,8 +165,8 @@ public class JobService { | |||||||
|         return Result.success(jobs.stream().map(JobLessDto::parseJob).collect(Collectors.toList())); |         return Result.success(jobs.stream().map(JobLessDto::parseJob).collect(Collectors.toList())); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public Result getJobList(Integer type, Integer page, Integer size) { |     public Result getJobList(Integer type, Boolean filterCreating, Integer page, Integer size) { | ||||||
|         List<Job> jobs = jobMapper.selectJobListByPage(type, (page - 1) * size, size); |         List<Job> jobs = jobMapper.selectJobListByPage(type, filterCreating, (page - 1) * size, size); | ||||||
|         return Result.success(jobs.stream().map(JobLessDto::parseJob).collect(Collectors.toList())); |         return Result.success(jobs.stream().map(JobLessDto::parseJob).collect(Collectors.toList())); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -63,6 +63,9 @@ | |||||||
|             <if test="type != null"> |             <if test="type != null"> | ||||||
|                 AND type = #{type} |                 AND type = #{type} | ||||||
|             </if> |             </if> | ||||||
|  |             <if test="filterCreating"> | ||||||
|  |                 AND `status` != '${@com.weilab.biology.core.data.enums.JobStatusEnum@CREATING.getKey()}' | ||||||
|  |             </if> | ||||||
|         </where> |         </where> | ||||||
|         ORDER BY job_id DESC |         ORDER BY job_id DESC | ||||||
|             LIMIT #{offset}, #{count} |             LIMIT #{offset}, #{count} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user