JobService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. package com.yihu.wlyy.statistics.service;
  2. import com.yihu.wlyy.statistics.dao.DoctorPatientGroupInfoDao;
  3. import com.yihu.wlyy.statistics.dao.QuartzJobConfigDao;
  4. import com.yihu.wlyy.statistics.dao.SignFamilyDao;
  5. import com.yihu.wlyy.statistics.job.check.CheckSignJob;
  6. import com.yihu.wlyy.statistics.job.message.FollowupPlanJob;
  7. import com.yihu.wlyy.statistics.job.message.HealthMessageJob;
  8. import com.yihu.wlyy.statistics.job.message.NoticeJob;
  9. import com.yihu.wlyy.statistics.model.job.QuartzJobConfig;
  10. import com.yihu.wlyy.statistics.util.QuartzHelper;
  11. import com.yihu.wlyy.statistics.vo.WlyyJobConfigVO;
  12. import org.springframework.beans.BeanUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.scheduling.annotation.Async;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import org.springframework.util.StringUtils;
  19. import java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.*;
  22. /**
  23. * @author chenweida
  24. */
  25. @Service
  26. public class JobService {
  27. @Autowired
  28. private QuartzHelper quartzHelper;
  29. @Autowired
  30. private QuartzJobConfigDao wlyyJobConfigDao;
  31. @Autowired
  32. private SignFamilyDao signFamilyDao;
  33. @Autowired
  34. private DoctorPatientGroupInfoDao doctorPatientGroupInfoDao;
  35. @Transactional
  36. public void stopById(String id) throws Exception {
  37. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "1");
  38. if (quartzJobConfig != null) {
  39. for (int j = 1; j <= 2; j++) {
  40. quartzHelper.removeJob(quartzJobConfig.getId() + "-" + j);
  41. quartzJobConfig.setStatus("0");
  42. }
  43. } else {
  44. throw new Exception("任务已经停止");
  45. }
  46. }
  47. @Transactional
  48. public void startById(String id) throws Exception {
  49. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "0");
  50. if (quartzJobConfig != null) {
  51. startOneJob(quartzJobConfig);
  52. } else {
  53. throw new Exception("任务已经启动");
  54. }
  55. }
  56. @Transactional
  57. public void stopAll() throws Exception {
  58. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("1");
  59. if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
  60. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  61. for (int j = 1; j <= 2; j++) {
  62. quartzHelper.removeJob(quartzJobConfig.getId() + "-" + j);
  63. quartzJobConfig.setStatus("0");
  64. }
  65. }
  66. } else {
  67. throw new Exception("任务已经全部停止");
  68. }
  69. }
  70. @Transactional
  71. public void startAll() throws Exception {
  72. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("0");
  73. if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
  74. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  75. startOneJob(quartzJobConfig);
  76. }
  77. } else {
  78. throw new Exception("任务已经全部启动");
  79. }
  80. }
  81. /**
  82. * 启动单个任务
  83. *
  84. * @param quartzJobConfig
  85. * @throws Exception
  86. */
  87. private void startOneJob(QuartzJobConfig quartzJobConfig) throws Exception {
  88. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  89. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  90. Map<String, Object> params = new HashMap<String, Object>();
  91. params.put("jobConfig", wlyyJobConfigVO);
  92. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  93. for (int j = 1; j <= 2; j++) {
  94. //往quartz框架添加任务
  95. quartzHelper.addJob(getRightClass(quartzJobConfig), quartzJobConfig.getQuartzCron(), quartzJobConfig.getId() + "-" + j, params);
  96. quartzJobConfig.setStatus("1");//设置任务状态是启动 }
  97. }
  98. }
  99. }
  100. public void startNowById(String id) throws Exception {
  101. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findOne(id);
  102. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  103. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  104. Map<String, String> params = new HashMap<String, String>();
  105. params.put("jobConfig", wlyyJobConfigVO.getId());
  106. for (int i = 1; i <= 2; i++) {
  107. params.put("timeLevel", i + "");
  108. //往quartz框架添加任务
  109. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  110. quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
  111. }
  112. }
  113. }
  114. public void productDataByDay(Integer day) throws Exception {
  115. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByIds();
  116. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  117. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  118. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  119. Map<String, String> params = new HashMap<String, String>();
  120. params.put("jobConfig", wlyyJobConfigVO.getId());
  121. for (int i = 1; i <= day; i++) {
  122. for (int j = 1; j <= 2; j++) {
  123. params.put("timeLevel", j + "");
  124. //往quartz框架添加任务
  125. params.put("startTime", getYesterday(0 - i - 1));
  126. params.put("endTime", getYesterday(0 - i));
  127. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  128. quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
  129. Thread.sleep(6000L);
  130. }
  131. }
  132. }
  133. }
  134. }
  135. public static String getYesterday(Integer day) {
  136. Calendar cal = Calendar.getInstance();
  137. cal.add(Calendar.DATE, day);
  138. String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
  139. return yesterday;
  140. }
  141. public void productDataByOneDay(String yesterday) throws Exception {
  142. SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
  143. Date date = dataSimple.parse(yesterday);
  144. if (date == null) {
  145. throw new Exception("时间格式错误");
  146. }
  147. Calendar calendar = new GregorianCalendar();
  148. calendar.setTime(date);
  149. calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
  150. Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
  151. String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
  152. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByIds();
  153. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  154. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  155. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  156. Map<String, String> params = new HashMap<String, String>();
  157. params.put("jobConfig", wlyyJobConfigVO.getId());
  158. //往quartz框架添加任务
  159. params.put("startTime", daybefore);
  160. params.put("endTime", yesterday);
  161. for (int j = 1; j <= 2; j++) {
  162. params.put("timeLevel", j + "");
  163. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  164. quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
  165. Thread.sleep(6000L);
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * @param quartzJobConfig
  172. * @return
  173. * @throws ClassNotFoundException
  174. */
  175. private Class getRightClass(QuartzJobConfig quartzJobConfig) throws ClassNotFoundException {
  176. return Class.forName(quartzJobConfig.getJobClass());
  177. }
  178. public void productDataByOneDayWithId(String yesterday, String id) throws Exception {
  179. SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
  180. Date date = dataSimple.parse(yesterday);
  181. if (date == null) {
  182. throw new Exception("时间格式错误");
  183. }
  184. Calendar calendar = new GregorianCalendar();
  185. calendar.setTime(date);
  186. calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
  187. Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
  188. String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
  189. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  190. if (quartzJobConfig == null) {
  191. throw new Exception("id不存在");
  192. }
  193. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  194. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  195. Map<String, String> params = new HashMap<String, String>();
  196. params.put("jobConfig", wlyyJobConfigVO.getId());
  197. //往quartz框架添加任务
  198. params.put("startTime", daybefore);
  199. params.put("endTime", yesterday);
  200. for (int j = 1; j <= 2; j++) {
  201. params.put("timeLevel", j + "");
  202. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  203. quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
  204. Thread.sleep(6000L);
  205. }
  206. }
  207. }
  208. @Transactional
  209. @Async("dbExtractExecutor")
  210. public void startaaaa() throws Exception {
  211. quartzHelper.startNow(HealthMessageJob.class, UUID.randomUUID().toString().replace("-", ""), new HashMap<>());
  212. }
  213. public void productDataByDayAndId(Integer day, String id) throws Exception {
  214. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  215. if (quartzJobConfig == null) {
  216. throw new Exception("id不存在");
  217. }
  218. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  219. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  220. Map<String, String> params = new HashMap<String, String>();
  221. params.put("jobConfig", wlyyJobConfigVO.getId());
  222. for (int i = 1; i <= day; i++) {
  223. //往quartz框架添加任务
  224. params.put("startTime", getYesterday(0 - i - 1));
  225. params.put("endTime", getYesterday(0 - i));
  226. for (int j = 1; j <= 2; j++) {
  227. params.put("timeLevel", j + "");
  228. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  229. quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
  230. Thread.sleep(6000L);
  231. }
  232. }
  233. }
  234. }
  235. public void startCheckSignJob() throws Exception {
  236. if (!quartzHelper.isExistJob(CheckSignJob.jobKey)) {
  237. quartzHelper.addJob(CheckSignJob.class, CheckSignJob.cron, CheckSignJob.jobKey, new HashMap<>());
  238. }
  239. }
  240. public void stopCheckSignJob() throws Exception {
  241. if (quartzHelper.isExistJob(CheckSignJob.jobKey)) {
  242. quartzHelper.removeJob(CheckSignJob.jobKey);
  243. }
  244. }
  245. public void productDataByDayToDay(String start, String end) throws Exception {
  246. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  247. Date startDate = sdf.parse(start);
  248. Date endDate = sdf.parse(end);
  249. if (startDate.after(endDate)) {
  250. throw new Exception("日期参数错误");
  251. }
  252. int day = daysBetween(startDate, endDate);
  253. for (int i = 0; i < day; i++) {
  254. productDataByOneDay(getYesterday(i, startDate));
  255. }
  256. }
  257. public static String getYesterday(Integer day, Date startDate) {
  258. Calendar cal = Calendar.getInstance();
  259. cal.setTime(startDate);
  260. cal.add(Calendar.DAY_OF_MONTH, day);
  261. String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
  262. return yesterday;
  263. }
  264. public static int daysBetween(Date smdate, Date bdate) throws ParseException {
  265. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  266. smdate = sdf.parse(sdf.format(smdate));
  267. bdate = sdf.parse(sdf.format(bdate));
  268. Calendar cal = Calendar.getInstance();
  269. cal.setTime(smdate);
  270. long time1 = cal.getTimeInMillis();
  271. cal.setTime(bdate);
  272. long time2 = cal.getTimeInMillis();
  273. long between_days = (time2 - time1) / (1000 * 3600 * 24);
  274. return Integer.parseInt(String.valueOf(between_days));
  275. }
  276. public void productDataByDayToDayAndId(String start, String end, String id) throws Exception {
  277. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  278. Date startDate = sdf.parse(start);
  279. Date endDate = sdf.parse(end);
  280. if (startDate.after(endDate)) {
  281. throw new Exception("日期参数错误");
  282. }
  283. int day = daysBetween(startDate, endDate);
  284. for (int i = 0; i < day; i++) {
  285. productDataByOneDayWithId(getYesterday(i, startDate), id);
  286. }
  287. }
  288. public void startHealthMessageJob() throws Exception {
  289. if (!quartzHelper.isExistJob(HealthMessageJob.jobKey)) {
  290. quartzHelper.addJob(HealthMessageJob.class, HealthMessageJob.cron, HealthMessageJob.jobKey, new HashMap<>());
  291. } else {
  292. throw new Exception("已经启动");
  293. }
  294. }
  295. public void stopHealthMessageJob() throws Exception {
  296. if (quartzHelper.isExistJob(HealthMessageJob.jobKey)) {
  297. quartzHelper.removeJob(HealthMessageJob.jobKey);
  298. } else {
  299. throw new Exception("已经停止");
  300. }
  301. }
  302. public void productHealthDataByOneDay(String day) throws Exception {
  303. SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
  304. Date date = dataSimple.parse(day);
  305. if (date == null) {
  306. throw new Exception("时间格式错误");
  307. }
  308. Calendar calendar = new GregorianCalendar();
  309. calendar.setTime(date);
  310. calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
  311. Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
  312. String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
  313. Map<String, String> params = new HashMap<String, String>();
  314. //往quartz框架添加任务
  315. params.put("now", yesterday);
  316. params.put("yesterday", day);
  317. quartzHelper.startNow(HealthMessageJob.class, HealthMessageJob.jobKey + UUID.randomUUID().toString().replace("-", ""), params);
  318. //Thread.sleep(40000L);
  319. }
  320. public void productHealthDataByDayToDay(String start, String end) throws Exception {
  321. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  322. Date startDate = sdf.parse(start);
  323. Date endDate = sdf.parse(end);
  324. if (startDate.after(endDate)) {
  325. throw new Exception("日期参数错误");
  326. }
  327. int day = daysBetween(startDate, endDate);
  328. for (int i = 0; i < day; i++) {
  329. productHealthDataByOneDay(getYesterday(i, startDate));
  330. }
  331. }
  332. public static void main(String[] args) throws Exception {
  333. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  334. Date startDate = sdf.parse("2016-10-20");
  335. Date endDate = sdf.parse("2016-10-28");
  336. System.out.println(daysBetween(startDate, endDate));
  337. System.out.println(getYesterday(0, startDate));
  338. }
  339. public void startNoticeJob() throws Exception {
  340. if (!quartzHelper.isExistJob(NoticeJob.jobKey)) {
  341. quartzHelper.addJob(NoticeJob.class, NoticeJob.jobCron, NoticeJob.jobKey, new HashMap<>());
  342. // quartzHelper.startNow(NoticeJob.class, UUID.randomUUID().toString().replace("-", ""), new HashMap<>());
  343. } else {
  344. throw new Exception("已经启动");
  345. }
  346. }
  347. public void stopNoticeJob() throws Exception {
  348. if (quartzHelper.isExistJob(NoticeJob.jobKey)) {
  349. quartzHelper.removeJob(NoticeJob.jobKey);
  350. } else {
  351. throw new Exception("已经停止");
  352. }
  353. }
  354. /*******************************
  355. * 随访计划任务
  356. *******************************************************/
  357. @Value("${spring.followupMessage.jobId}")
  358. private String followupJob;
  359. @Value("${spring.followupMessage.cron}")
  360. private String followupJobCron;
  361. public void startFollowupPlantJob() throws Exception {
  362. if (!quartzHelper.isExistJob(followupJob)) {
  363. quartzHelper.addJob(FollowupPlanJob.class, followupJobCron, followupJob, new HashMap<>());
  364. } else {
  365. throw new Exception("已经启动");
  366. }
  367. }
  368. public void stopFollowupPlantJob() throws Exception {
  369. if (quartzHelper.isExistJob(followupJob)) {
  370. quartzHelper.removeJob(followupJob);
  371. } else {
  372. throw new Exception("已经停止");
  373. }
  374. }
  375. public void startNoticeJobNow() throws Exception {
  376. quartzHelper.startNow(NoticeJob.class, UUID.randomUUID().toString(), new HashMap<>());
  377. }
  378. public void startHealthMessageJobNow() throws Exception {
  379. quartzHelper.startNow(HealthMessageJob.class, UUID.randomUUID().toString(), new HashMap<>());
  380. }
  381. }