JobController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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(value = "/job", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  29. @Api(description = "后台-任务控制")
  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 day
  114. * @return
  115. */
  116. @RequestMapping(value = "productDataByDayAndId", method = RequestMethod.GET)
  117. public String productDataByDayAndId(Integer day, String id) {
  118. try {
  119. jobService.productDataByDayAndId(day, 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 = "startById", method = RequestMethod.GET)
  133. public String startById(String id) {
  134. try {
  135. jobService.startById(id);
  136. return success("启动成功!");
  137. } catch (Exception e) {
  138. error(e);
  139. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  140. }
  141. }
  142. /**
  143. * 停止任务
  144. *
  145. * @param id id
  146. * @return
  147. */
  148. @RequestMapping(value = "stopById", method = RequestMethod.GET)
  149. public String stopById(String id) {
  150. try {
  151. jobService.stopById(id);
  152. return success("停止成功!");
  153. } catch (Exception e) {
  154. error(e);
  155. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  156. }
  157. }
  158. /**
  159. * 停止所有任务
  160. *
  161. * @return
  162. */
  163. @RequestMapping(value = "stopAll", method = RequestMethod.GET)
  164. public String stopAll() {
  165. try {
  166. jobService.stopAll();
  167. return success("停止成功!");
  168. } catch (Exception e) {
  169. error(e);
  170. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  171. }
  172. }
  173. /**
  174. * 启动判断的任务
  175. *
  176. * @return
  177. */
  178. @RequestMapping(value = "startCheckSignJob", method = RequestMethod.GET)
  179. public String startCheckSignJob() {
  180. try {
  181. jobService.startCheckSignJob();
  182. return success("启动成功!");
  183. } catch (Exception e) {
  184. error(e);
  185. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  186. }
  187. }
  188. /**
  189. * 停止判断的任务
  190. *
  191. * @return
  192. */
  193. @RequestMapping(value = "stopCheckSignJob", method = RequestMethod.GET)
  194. public String stopCheckSignJob() {
  195. try {
  196. jobService.stopCheckSignJob();
  197. return success("停止成功!");
  198. } catch (Exception e) {
  199. error(e);
  200. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  201. }
  202. }
  203. /**
  204. * 启动所有任务
  205. *
  206. * @return
  207. */
  208. @RequestMapping(value = "startAll", method = RequestMethod.GET)
  209. public String startAll() {
  210. try {
  211. jobService.startAll();
  212. return success("启动成功!");
  213. } catch (Exception e) {
  214. error(e);
  215. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  216. }
  217. }
  218. @RequestMapping(value = "startaaaa", method = RequestMethod.GET)
  219. public String startaaaa() {
  220. try {
  221. jobService.startaaaa();
  222. return success("启动成功!");
  223. } catch (Exception e) {
  224. error(e);
  225. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  226. }
  227. }
  228. /**
  229. * 将患者疾病推送到redis
  230. *
  231. * @return
  232. */
  233. @RequestMapping(value = "/disease/to_redis", method = RequestMethod.GET)
  234. public String updateToRedid() {
  235. try {
  236. diseaseService.updateToRedis();
  237. return write(200, "更新成功");
  238. } catch (Exception e) {
  239. return error(-1, "更新失败");
  240. }
  241. }
  242. /**
  243. * 将患者疾病到数据库
  244. *
  245. * @return
  246. */
  247. @RequestMapping(value = "/disease/to_disease", method = RequestMethod.GET)
  248. public String updateToDisease() {
  249. try {
  250. diseaseService.updateToDisease();
  251. return write(200, "更新成功");
  252. } catch (Exception e) {
  253. e.printStackTrace();
  254. return error(-1, "更新失败");
  255. }
  256. }
  257. /**
  258. * 从Redis查询患者疾病
  259. *
  260. * @param patient
  261. * @return
  262. */
  263. @RequestMapping(value = "/disease/patient", method = RequestMethod.GET)
  264. public String getDiseaseFromRedis(String patient) {
  265. try {
  266. return write(200, "查询成功", "data", redisTemplate.opsForValue().get("disease:" + patient));
  267. } catch (Exception e) {
  268. e.printStackTrace();
  269. return error(-1, "查询失败");
  270. }
  271. }
  272. /**
  273. * 开始名医咨询剩余次数统计任务
  274. *
  275. * @return
  276. */
  277. @RequestMapping(value = "/famous_doctor/start_job", method = RequestMethod.GET)
  278. public String startConsultTimesJob() {
  279. try {
  280. if (!quartzHelper.isExistJob("famous-doctor-times")) {
  281. quartzHelper.addJob(FamousConsultTimesJob.class, "0 0 0 * * ?", "famous-doctor-times", new HashMap<String, Object>());
  282. return write(200, "启动成功");
  283. } else {
  284. return write(200, "任务已存在");
  285. }
  286. } catch (Exception e) {
  287. error(e);
  288. return error(-1, "启动失败");
  289. }
  290. }
  291. /**
  292. * 名医咨询剩余次数手动更新
  293. *
  294. * @return
  295. */
  296. @RequestMapping(value = "/famous_doctor/times_update", method = RequestMethod.GET)
  297. public String famousConsultTimeUpdate() {
  298. try {
  299. workTimeService.consultTimesRemain(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
  300. return write(200, "更新成功");
  301. } catch (Exception e) {
  302. error(e);
  303. return error(-1, "更新失败");
  304. }
  305. }
  306. /**
  307. * 更新人口数据到redis
  308. *
  309. * @return
  310. */
  311. public String peopleNumToRedis() {
  312. try {
  313. statisticsService.peopleNumToRedis();
  314. return write(200, "更新成功");
  315. } catch (Exception e) {
  316. error(e);
  317. return error(-1, "更新失败");
  318. }
  319. }
  320. /**
  321. * 添加患者咨询超时未响应关闭任务。
  322. */
  323. @RequestMapping(value = "/consult_auto_termination", method = RequestMethod.POST)
  324. @ApiOperation("添加超时咨询自动关闭任务")
  325. public String addConsultJob() {
  326. try {
  327. quartzHelper.startNow(ConsultCleanerJob.class, ConsultTerminatorJobKey, null);
  328. return write(200, "");
  329. } catch (Exception e) {
  330. error(e);
  331. return error(-1, e.getMessage());
  332. }
  333. }
  334. }