JobController.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package com.yihu.wlyy.web.quota;
  2. import com.yihu.wlyy.job.QuartzHelper;
  3. import com.yihu.wlyy.job.consult.ConsultCleanerJob;
  4. import com.yihu.wlyy.job.consult.FamousConsultTimesJob;
  5. import com.yihu.wlyy.service.app.disease.PatientDiseaseService;
  6. import com.yihu.wlyy.service.app.scheduling.DoctorWorkTimeService;
  7. import com.yihu.wlyy.service.app.statistics.StatisticsService;
  8. import com.yihu.wlyy.service.quota.JobService;
  9. import com.yihu.wlyy.web.BaseController;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.redis.core.StringRedisTemplate;
  14. import org.springframework.http.MediaType;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. import java.util.HashMap;
  21. import static com.yihu.wlyy.job.consult.ConsultCleanerJob.ConsultTerminatorJobKey;
  22. /**
  23. * 任务启动
  24. *
  25. * @author chenweida
  26. */
  27. @RestController
  28. @RequestMapping("/job")
  29. @Api(description = "后台-任务控制", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  30. public class JobController extends BaseController {
  31. private final JobService jobService;
  32. private final PatientDiseaseService diseaseService;
  33. private final StringRedisTemplate redisTemplate;
  34. private final QuartzHelper quartzHelper;
  35. private final DoctorWorkTimeService workTimeService;
  36. private final StatisticsService statisticsService;
  37. @Autowired
  38. public JobController(StatisticsService statisticsService, JobService jobService, StringRedisTemplate redisTemplate, DoctorWorkTimeService workTimeService, PatientDiseaseService diseaseService, QuartzHelper quartzHelper) {
  39. this.statisticsService = statisticsService;
  40. this.jobService = jobService;
  41. this.redisTemplate = redisTemplate;
  42. this.workTimeService = workTimeService;
  43. this.diseaseService = diseaseService;
  44. this.quartzHelper = quartzHelper;
  45. }
  46. /**
  47. * 启动任务
  48. *
  49. * @param id id
  50. * @return
  51. */
  52. @RequestMapping(value = "startNowById", method = RequestMethod.GET)
  53. public String startNowById(String id) {
  54. try {
  55. jobService.startNowById(id);
  56. return success("启动成功!");
  57. } catch (Exception e) {
  58. error(e);
  59. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  60. }
  61. }
  62. /**
  63. * 生成过去几天的数据
  64. *
  65. * @param day
  66. * @return
  67. */
  68. @RequestMapping(value = "productDataByDay", method = RequestMethod.GET)
  69. public String productDataByDay(Integer day) {
  70. try {
  71. jobService.productDataByDay(day);
  72. return success("启动成功!");
  73. } catch (Exception e) {
  74. error(e);
  75. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  76. }
  77. }
  78. /**
  79. * 生成过去某一天的全部的数据
  80. *
  81. * @param day
  82. * @return
  83. */
  84. @RequestMapping(value = "productDataByOneDay", method = RequestMethod.GET)
  85. public String productDataByOneDay(String day) {
  86. try {
  87. jobService.productDataByOneDay(day);
  88. return success("启动成功!");
  89. } catch (Exception e) {
  90. error(e);
  91. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  92. }
  93. }
  94. /**
  95. * 生成过去某一天的某一个指标的数据
  96. *
  97. * @param day
  98. * @return
  99. */
  100. @RequestMapping(value = "productDataByOneDayWithId", method = RequestMethod.GET)
  101. public String productDataByOneDayWithId(String day, String id) {
  102. try {
  103. jobService.productDataByOneDayWithId(day, id);
  104. return success("启动成功!");
  105. } catch (Exception e) {
  106. error(e);
  107. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  108. }
  109. }
  110. /**
  111. * 启动任务
  112. *
  113. * @param id id
  114. * @return
  115. */
  116. @RequestMapping(value = "startById", method = RequestMethod.GET)
  117. public String startById(String id) {
  118. try {
  119. jobService.startById(id);
  120. return success("启动成功!");
  121. } catch (Exception e) {
  122. error(e);
  123. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  124. }
  125. }
  126. /**
  127. * 停止任务
  128. *
  129. * @param id id
  130. * @return
  131. */
  132. @RequestMapping(value = "stopById", method = RequestMethod.GET)
  133. public String stopById(String id) {
  134. try {
  135. jobService.stopById(id);
  136. return success("停止成功!");
  137. } catch (Exception e) {
  138. error(e);
  139. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  140. }
  141. }
  142. /**
  143. * 停止所有任务
  144. *
  145. * @return
  146. */
  147. @RequestMapping(value = "stopAll", method = RequestMethod.GET)
  148. public String stopAll() {
  149. try {
  150. jobService.stopAll();
  151. return success("停止成功!");
  152. } catch (Exception e) {
  153. error(e);
  154. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  155. }
  156. }
  157. /**
  158. * 启动所有任务
  159. *
  160. * @return
  161. */
  162. @RequestMapping(value = "startAll", method = RequestMethod.GET)
  163. public String startAll() {
  164. try {
  165. jobService.startAll();
  166. return success("启动成功!");
  167. } catch (Exception e) {
  168. error(e);
  169. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  170. }
  171. }
  172. @RequestMapping(value = "startaaaa", method = RequestMethod.GET)
  173. public String startaaaa() {
  174. try {
  175. jobService.startaaaa();
  176. return success("启动成功!");
  177. } catch (Exception e) {
  178. error(e);
  179. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  180. }
  181. }
  182. /**
  183. * 将患者疾病推送到redis
  184. *
  185. * @return
  186. */
  187. @RequestMapping(value = "/disease/to_redis", method = RequestMethod.GET)
  188. public String updateToRedid() {
  189. try {
  190. diseaseService.updateToRedis();
  191. return write(200, "更新成功");
  192. } catch (Exception e) {
  193. return error(-1, "更新失败");
  194. }
  195. }
  196. /**
  197. * 将患者疾病到数据库
  198. *
  199. * @return
  200. */
  201. @RequestMapping(value = "/disease/to_disease", method = RequestMethod.GET)
  202. public String updateToDisease() {
  203. try {
  204. diseaseService.updateToDisease();
  205. return write(200, "更新成功");
  206. } catch (Exception e) {
  207. e.printStackTrace();
  208. return error(-1, "更新失败");
  209. }
  210. }
  211. /**
  212. * 从Redis查询患者疾病
  213. *
  214. * @param patient
  215. * @return
  216. */
  217. @RequestMapping(value = "/disease/patient", method = RequestMethod.GET)
  218. public String getDiseaseFromRedis(String patient) {
  219. try {
  220. return write(200, "查询成功", "data", redisTemplate.opsForValue().get("disease:" + patient));
  221. } catch (Exception e) {
  222. e.printStackTrace();
  223. return error(-1, "查询失败");
  224. }
  225. }
  226. /**
  227. * 开始名医咨询剩余次数统计任务
  228. *
  229. * @return
  230. */
  231. @RequestMapping(value = "/famous_doctor/start_job", method = RequestMethod.GET)
  232. public String startConsultTimesJob() {
  233. try {
  234. if (!quartzHelper.isExistJob("famous-doctor-times")) {
  235. quartzHelper.addJob(FamousConsultTimesJob.class, "0 0 0 * * ?", "famous-doctor-times", new HashMap<String, Object>());
  236. return write(200, "启动成功");
  237. } else {
  238. return write(200, "任务已存在");
  239. }
  240. } catch (Exception e) {
  241. error(e);
  242. return error(-1, "启动失败");
  243. }
  244. }
  245. /**
  246. * 名医咨询剩余次数手动更新
  247. *
  248. * @return
  249. */
  250. @RequestMapping(value = "/famous_doctor/times_update", method = RequestMethod.GET)
  251. public String famousConsultTimeUpdate() {
  252. try {
  253. workTimeService.consultTimesRemain(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
  254. return write(200, "更新成功");
  255. } catch (Exception e) {
  256. error(e);
  257. return error(-1, "更新失败");
  258. }
  259. }
  260. /**
  261. * 更新人口数据到redis
  262. *
  263. * @return
  264. */
  265. public String peopleNumToRedis() {
  266. try {
  267. statisticsService.peopleNumToRedis();
  268. return write(200, "更新成功");
  269. } catch (Exception e) {
  270. error(e);
  271. return error(-1, "更新失败");
  272. }
  273. }
  274. /**
  275. * 添加患者咨询超时未响应关闭任务。
  276. */
  277. @RequestMapping(value = "/consult_auto_termination", method = RequestMethod.POST)
  278. @ApiOperation("添加超时咨询自动关闭任务")
  279. public String addConsultJob() {
  280. try {
  281. quartzHelper.startNow(ConsultCleanerJob.class, ConsultTerminatorJobKey, null);
  282. return write(200, "");
  283. } catch (Exception e) {
  284. error(e);
  285. return error(-1, e.getMessage());
  286. }
  287. }
  288. }