QuartzController.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.jiayue.ipfcst.console.controller;
  2. import com.jiayue.ipfcst.aop.SaveValidate;
  3. import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
  4. import com.jiayue.ipfcst.common.data.entity.Quartz;
  5. import com.jiayue.ipfcst.console.service.InitJobClassService;
  6. import com.jiayue.ipfcst.console.service.QuartzService;
  7. import com.jiayue.ipfcst.console.service.SysParameterService;
  8. import lombok.SneakyThrows;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.*;
  12. import java.sql.SQLException;
  13. import java.util.Map;
  14. /**
  15. * 定时控制类
  16. *
  17. * @author bizy
  18. * @version 1.0
  19. * @since 2018/10/22 11:29
  20. */
  21. @RestController
  22. @Slf4j
  23. public class QuartzController {
  24. @Autowired
  25. QuartzService quartzService;
  26. @Autowired
  27. InitJobClassService initJobClassService;
  28. @Autowired
  29. SysParameterService sysParameterService;
  30. /**
  31. * 添加定时任务
  32. *
  33. * @param qt 定时任务对象
  34. * @return 状态bean
  35. */
  36. @SaveValidate
  37. @SneakyThrows
  38. @PostMapping(value = "quartzs")
  39. public ResponseVO addQuartz(@RequestBody final Quartz qt) {
  40. final Quartz quartz = quartzService.findByClass(qt.getExecuteClass());
  41. if (null != quartz) {
  42. return ResponseVO.fail("添加失败,任务已存在!");
  43. } else {
  44. final boolean flag = quartzService.save(qt);
  45. if (flag) {
  46. return ResponseVO.success("添加成功!");
  47. } else {
  48. return ResponseVO.fail("添加失败!");
  49. }
  50. }
  51. }
  52. /**
  53. * 修改定时任务状态
  54. *
  55. * @param qt 定时任务对象
  56. * @return 状态bean
  57. */
  58. @SaveValidate
  59. @SneakyThrows
  60. @PutMapping(value = "quartzs")
  61. public ResponseVO stateQuartz(@RequestBody final Quartz qt) {
  62. final boolean flag = quartzService.save(qt);
  63. if (flag) {
  64. return ResponseVO.success("修改成功!");
  65. } else {
  66. return ResponseVO.success("修改成功!");
  67. }
  68. }
  69. /**
  70. * 删除定时任务
  71. *
  72. * @param ids
  73. * @return
  74. */
  75. @SneakyThrows
  76. @RequestMapping(value = "quartzs/delete/{ids}", method = RequestMethod.DELETE)
  77. public ResponseVO deleteQuartzById(@PathVariable final String ids) {
  78. if (quartzService.deleteById(ids)) {
  79. return ResponseVO.success("删除成功!");
  80. }
  81. return ResponseVO.fail("删除失败!没有查询到id对应的定时任务");
  82. }
  83. /**
  84. * 根据jobName模糊查询
  85. *
  86. * @param page 页数
  87. * @param size 每页几条
  88. * @param jobName jobName
  89. * @return Map<String, Object>
  90. */
  91. @SneakyThrows
  92. @GetMapping(value = "quartzs/{page}/{size}/{jobName}/{jobType}")
  93. public ResponseVO getQuartzs(
  94. @PathVariable final Integer page, @PathVariable final Integer size, @PathVariable String jobName,
  95. @PathVariable String jobType) {
  96. if (jobName.equals("1")) {
  97. jobName = "";
  98. }
  99. if (jobType.equals("1")) {
  100. jobType = "";
  101. }
  102. final Map<String, Object> map = quartzService.getQuartz(jobName, jobType, page, size);
  103. return ResponseVO.success(map);
  104. }
  105. @SneakyThrows
  106. @GetMapping(value = "initJobClasses/initJob")
  107. public ResponseVO initJobClass() throws SQLException {
  108. Boolean b = quartzService.initJobClass();
  109. if (!b) {
  110. return ResponseVO.fail("初始化失败,请检查sql文件是否存在");
  111. }
  112. return ResponseVO.success("初始化成功!");
  113. }
  114. }