JobController.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 = "startAll", method = RequestMethod.GET)
  179. public String startAll() {
  180. try {
  181. jobService.startAll();
  182. return success("启动成功!");
  183. } catch (Exception e) {
  184. error(e);
  185. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  186. }
  187. }
  188. @RequestMapping(value = "startaaaa", method = RequestMethod.GET)
  189. public String startaaaa() {
  190. try {
  191. jobService.startaaaa();
  192. return success("启动成功!");
  193. } catch (Exception e) {
  194. error(e);
  195. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  196. }
  197. }
  198. /**
  199. * 将患者疾病推送到redis
  200. *
  201. * @return
  202. */
  203. @RequestMapping(value = "/disease/to_redis", method = RequestMethod.GET)
  204. public String updateToRedid() {
  205. try {
  206. diseaseService.updateToRedis();
  207. return write(200, "更新成功");
  208. } catch (Exception e) {
  209. return error(-1, "更新失败");
  210. }
  211. }
  212. /**
  213. * 将患者疾病到数据库
  214. *
  215. * @return
  216. */
  217. @RequestMapping(value = "/disease/to_disease", method = RequestMethod.GET)
  218. public String updateToDisease() {
  219. try {
  220. diseaseService.updateToDisease();
  221. return write(200, "更新成功");
  222. } catch (Exception e) {
  223. e.printStackTrace();
  224. return error(-1, "更新失败");
  225. }
  226. }
  227. /**
  228. * 从Redis查询患者疾病
  229. *
  230. * @param patient
  231. * @return
  232. */
  233. @RequestMapping(value = "/disease/patient", method = RequestMethod.GET)
  234. public String getDiseaseFromRedis(String patient) {
  235. try {
  236. return write(200, "查询成功", "data", redisTemplate.opsForValue().get("disease:" + patient));
  237. } catch (Exception e) {
  238. e.printStackTrace();
  239. return error(-1, "查询失败");
  240. }
  241. }
  242. /**
  243. * 开始名医咨询剩余次数统计任务
  244. *
  245. * @return
  246. */
  247. @RequestMapping(value = "/famous_doctor/start_job", method = RequestMethod.GET)
  248. public String startConsultTimesJob() {
  249. try {
  250. if (!quartzHelper.isExistJob("famous-doctor-times")) {
  251. quartzHelper.addJob(FamousConsultTimesJob.class, "0 0 0 * * ?", "famous-doctor-times", new HashMap<String, Object>());
  252. return write(200, "启动成功");
  253. } else {
  254. return write(200, "任务已存在");
  255. }
  256. } catch (Exception e) {
  257. error(e);
  258. return error(-1, "启动失败");
  259. }
  260. }
  261. /**
  262. * 名医咨询剩余次数手动更新
  263. *
  264. * @return
  265. */
  266. @RequestMapping(value = "/famous_doctor/times_update", method = RequestMethod.GET)
  267. public String famousConsultTimeUpdate() {
  268. try {
  269. workTimeService.consultTimesRemain(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
  270. return write(200, "更新成功");
  271. } catch (Exception e) {
  272. error(e);
  273. return error(-1, "更新失败");
  274. }
  275. }
  276. /**
  277. * 更新人口数据到redis
  278. *
  279. * @return
  280. */
  281. public String peopleNumToRedis() {
  282. try {
  283. statisticsService.peopleNumToRedis();
  284. return write(200, "更新成功");
  285. } catch (Exception e) {
  286. error(e);
  287. return error(-1, "更新失败");
  288. }
  289. }
  290. /**
  291. * 添加患者咨询超时未响应关闭任务。
  292. */
  293. @RequestMapping(value = "/consult_auto_termination", method = RequestMethod.POST)
  294. @ApiOperation("添加超时咨询自动关闭任务")
  295. public String addConsultJob() {
  296. try {
  297. quartzHelper.startNow(ConsultCleanerJob.class, ConsultTerminatorJobKey, null);
  298. return write(200, "");
  299. } catch (Exception e) {
  300. error(e);
  301. return error(-1, e.getMessage());
  302. }
  303. }
  304. }