JobService.java 19 KB

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