JobController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.yihu.wlyy.web.quota;
  2. import com.yihu.wlyy.job.FollowupPlanJob;
  3. import com.yihu.wlyy.job.QuartzHelper;
  4. import com.yihu.wlyy.job.consult.ConsultCleanerJob;
  5. import com.yihu.wlyy.job.consult.FamousConsultTimesJob;
  6. import com.yihu.wlyy.service.app.disease.PatientDiseaseService;
  7. import com.yihu.wlyy.service.app.scheduling.DoctorWorkTimeService;
  8. import com.yihu.wlyy.service.app.statistics.StatisticsService;
  9. import com.yihu.wlyy.service.quota.JobService;
  10. import com.yihu.wlyy.web.BaseController;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.data.redis.core.StringRedisTemplate;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import static com.yihu.wlyy.job.consult.ConsultCleanerJob.ConsultTerminatorJobKey;
  23. /**
  24. * 任务启动
  25. *
  26. * @author chenweida
  27. */
  28. @RestController
  29. @RequestMapping(value = "/job", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  30. @Api(description = "后台-任务控制")
  31. public class JobController extends BaseController {
  32. private final JobService jobService;
  33. private final PatientDiseaseService diseaseService;
  34. private final StringRedisTemplate redisTemplate;
  35. private final QuartzHelper quartzHelper;
  36. private final DoctorWorkTimeService workTimeService;
  37. private final StatisticsService statisticsService;
  38. @Autowired
  39. public JobController(StatisticsService statisticsService, JobService jobService, StringRedisTemplate redisTemplate, DoctorWorkTimeService workTimeService, PatientDiseaseService diseaseService, QuartzHelper quartzHelper) {
  40. this.statisticsService = statisticsService;
  41. this.jobService = jobService;
  42. this.redisTemplate = redisTemplate;
  43. this.workTimeService = workTimeService;
  44. this.diseaseService = diseaseService;
  45. this.quartzHelper = quartzHelper;
  46. }
  47. /**
  48. * 启动任务
  49. *
  50. * @param id id
  51. * @return
  52. */
  53. @RequestMapping(value = "startNowById", method = RequestMethod.GET)
  54. public String startNowById(String id) {
  55. try {
  56. jobService.startNowById(id);
  57. return success("启动成功!");
  58. } catch (Exception e) {
  59. error(e);
  60. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  61. }
  62. }
  63. /**
  64. * 生成过去几天的数据
  65. *
  66. * @param day
  67. * @return
  68. */
  69. @RequestMapping(value = "productDataByDay", method = RequestMethod.GET)
  70. public String productDataByDay(Integer day) {
  71. try {
  72. jobService.productDataByDay(day);
  73. return success("启动成功!");
  74. } catch (Exception e) {
  75. error(e);
  76. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  77. }
  78. }
  79. /**
  80. * 生成过去某一天的全部的数据
  81. *
  82. * @param day
  83. * @return
  84. */
  85. @RequestMapping(value = "productDataByOneDay", method = RequestMethod.GET)
  86. public String productDataByOneDay(String day) {
  87. try {
  88. jobService.productDataByOneDay(day);
  89. return success("启动成功!");
  90. } catch (Exception e) {
  91. error(e);
  92. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  93. }
  94. }
  95. /**
  96. * 生成过去某一天的某一个指标的数据
  97. *
  98. * @param day
  99. * @return
  100. */
  101. @RequestMapping(value = "productDataByOneDayWithId", method = RequestMethod.GET)
  102. public String productDataByOneDayWithId(String day, String id) {
  103. try {
  104. jobService.productDataByOneDayWithId(day, id);
  105. return success("启动成功!");
  106. } catch (Exception e) {
  107. error(e);
  108. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  109. }
  110. }
  111. /**
  112. * 生成过去某几天的某一个指标的数据
  113. *
  114. * @param day
  115. * @return
  116. */
  117. @RequestMapping(value = "productDataByDayAndId", method = RequestMethod.GET)
  118. public String productDataByDayAndId(Integer day, String id) {
  119. try {
  120. jobService.productDataByDayAndId(day, id);
  121. return success("启动成功!");
  122. } catch (Exception e) {
  123. error(e);
  124. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  125. }
  126. }
  127. /**
  128. * 启动任务
  129. *
  130. * @param id id
  131. * @return
  132. */
  133. @RequestMapping(value = "startById", method = RequestMethod.GET)
  134. public String startById(String id) {
  135. try {
  136. jobService.startById(id);
  137. return success("启动成功!");
  138. } catch (Exception e) {
  139. error(e);
  140. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  141. }
  142. }
  143. /**
  144. * 停止任务
  145. *
  146. * @param id id
  147. * @return
  148. */
  149. @RequestMapping(value = "stopById", method = RequestMethod.GET)
  150. public String stopById(String id) {
  151. try {
  152. jobService.stopById(id);
  153. return success("停止成功!");
  154. } catch (Exception e) {
  155. error(e);
  156. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  157. }
  158. }
  159. /**
  160. * 停止所有任务
  161. *
  162. * @return
  163. */
  164. @RequestMapping(value = "stopAll", method = RequestMethod.GET)
  165. public String stopAll() {
  166. try {
  167. jobService.stopAll();
  168. return success("停止成功!");
  169. } catch (Exception e) {
  170. error(e);
  171. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  172. }
  173. }
  174. /**
  175. * 启动所有任务
  176. *
  177. * @return
  178. */
  179. @RequestMapping(value = "startAll", method = RequestMethod.GET)
  180. public String startAll() {
  181. try {
  182. jobService.startAll();
  183. return success("启动成功!");
  184. } catch (Exception e) {
  185. error(e);
  186. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  187. }
  188. }
  189. @RequestMapping(value = "startaaaa", method = RequestMethod.GET)
  190. public String startaaaa() {
  191. try {
  192. jobService.startaaaa();
  193. return success("启动成功!");
  194. } catch (Exception e) {
  195. error(e);
  196. return invalidUserException(e, -1, "启动失败:" + e.getMessage());
  197. }
  198. }
  199. /**
  200. * 将患者疾病推送到redis
  201. *
  202. * @return
  203. */
  204. @RequestMapping(value = "/disease/to_redis", method = RequestMethod.GET)
  205. public String updateToRedid() {
  206. try {
  207. diseaseService.updateToRedis();
  208. return write(200, "更新成功");
  209. } catch (Exception e) {
  210. return error(-1, "更新失败");
  211. }
  212. }
  213. /**
  214. * 将患者疾病到数据库
  215. *
  216. * @return
  217. */
  218. @RequestMapping(value = "/disease/to_disease", method = RequestMethod.GET)
  219. public String updateToDisease() {
  220. try {
  221. diseaseService.updateToDisease();
  222. return write(200, "更新成功");
  223. } catch (Exception e) {
  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. /******************************************** 随访计划任务 **********************************************************************/
  305. @RequestMapping(value = "/followup/planJob", method = RequestMethod.POST)
  306. @ApiOperation("随访计划消息发送任务")
  307. public String addFollowPlanJob() {
  308. try {
  309. String jobKey = "followup-plan";
  310. if (!quartzHelper.isExistJob(jobKey)) {
  311. quartzHelper.addJob(FollowupPlanJob.class, "0 0/5 0 * * ?", jobKey, new HashMap<String, Object>());
  312. return write(200, "启动成功");
  313. } else {
  314. return write(200, "任务已存在");
  315. }
  316. } catch (Exception e) {
  317. error(e);
  318. return error(-1, "启动失败");
  319. }
  320. }
  321. }