JobService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. *
  185. * @param quartzJobConfig
  186. * @return
  187. * @throws ClassNotFoundException
  188. */
  189. private Class getRightClass(QuartzJobConfig quartzJobConfig) throws ClassNotFoundException {
  190. return Class.forName(quartzJobConfig.getJobClass());
  191. }
  192. public void productDataByOneDayWithId(String yesterday, String id) throws Exception {
  193. SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
  194. Date date = dataSimple.parse(yesterday);
  195. if (date == null) {
  196. throw new Exception("时间格式错误");
  197. }
  198. Calendar calendar = new GregorianCalendar();
  199. calendar.setTime(date);
  200. calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
  201. Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
  202. String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
  203. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  204. if (quartzJobConfig == null) {
  205. throw new Exception("id不存在");
  206. }
  207. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  208. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  209. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  210. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  211. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  212. Map<String, Object> params = new HashMap<String, Object>();
  213. params.put("quota", wlyyQuotaVO);
  214. params.put("jobConfig", wlyyJobConfigVO);
  215. //往quartz框架添加任务
  216. params.put("daybefore", daybefore);
  217. params.put("yesterday", yesterday);
  218. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  219. quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-",""), params);
  220. Thread.sleep(20000L);
  221. }
  222. }
  223. @Transactional
  224. @Async("dbExtractExecutor")
  225. public void startaaaa() throws Exception{
  226. quartzHelper.startNow(HealthMessageJob.class,UUID.randomUUID().toString().replace("-",""),new HashMap<>());
  227. }
  228. public void productDataByDayAndId(Integer day, String id) throws Exception{
  229. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  230. if(quartzJobConfig==null){
  231. throw new Exception("id不存在");
  232. }
  233. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  234. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  235. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  236. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  237. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  238. Map<String, Object> params = new HashMap<String, Object>();
  239. params.put("quota", wlyyQuotaVO);
  240. params.put("jobConfig", wlyyJobConfigVO);
  241. for (int i = 1; i <= day; i++) {
  242. //往quartz框架添加任务
  243. params.put("daybefore", getYesterday(0 - i -1));
  244. params.put("yesterday", getYesterday(0 - i));
  245. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  246. quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-",""), params);
  247. Thread.sleep(20000L);
  248. }
  249. }
  250. }
  251. public void startCheckSignJob() throws Exception{
  252. if(!quartzHelper.isExistJob(CheckSignJob.jobKey)){
  253. quartzHelper.addJob(CheckSignJob.class,CheckSignJob.cron,CheckSignJob.jobKey,new HashMap<>());
  254. }
  255. }
  256. public void stopCheckSignJob()throws Exception {
  257. if(quartzHelper.isExistJob(CheckSignJob.jobKey)){
  258. quartzHelper.removeJob(CheckSignJob.jobKey);
  259. }
  260. }
  261. public void productDataByDayToDay(String start, String end) throws Exception {
  262. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  263. Date startDate=sdf.parse(start);
  264. Date endDate=sdf.parse(end);
  265. if(startDate.after(endDate)){
  266. throw new Exception("日期参数错误");
  267. }
  268. int day=daysBetween(startDate,endDate);
  269. for(int i=0;i<day;i++){
  270. productDataByOneDay(getYesterday(i,startDate));
  271. }
  272. }
  273. public static String getYesterday(Integer day,Date startDate) {
  274. Calendar cal = Calendar.getInstance();
  275. cal.setTime(startDate);
  276. cal.add(Calendar.DAY_OF_MONTH, day);
  277. String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
  278. return yesterday;
  279. }
  280. public static int daysBetween(Date smdate,Date bdate) throws ParseException
  281. {
  282. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  283. smdate=sdf.parse(sdf.format(smdate));
  284. bdate=sdf.parse(sdf.format(bdate));
  285. Calendar cal = Calendar.getInstance();
  286. cal.setTime(smdate);
  287. long time1 = cal.getTimeInMillis();
  288. cal.setTime(bdate);
  289. long time2 = cal.getTimeInMillis();
  290. long between_days=(time2-time1)/(1000*3600*24);
  291. return Integer.parseInt(String.valueOf(between_days));
  292. }
  293. public void productDataByDayToDayAndId(String start, String end, String id) throws Exception {
  294. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  295. Date startDate=sdf.parse(start);
  296. Date endDate=sdf.parse(end);
  297. if(startDate.after(endDate)){
  298. throw new Exception("日期参数错误");
  299. }
  300. int day=daysBetween(startDate,endDate);
  301. for(int i=0;i<day;i++){
  302. productDataByOneDayWithId(getYesterday(i,startDate),id);
  303. }
  304. }
  305. public void startCleanCacheJob() throws Exception {
  306. if(!quartzHelper.isExistJob(CacheCleanJob.jobKey)){
  307. quartzHelper.addJob(CacheCleanJob.class,CacheCleanJob.cron,CacheCleanJob.jobKey,new HashMap<>());
  308. }else{
  309. throw new Exception("已经启动");
  310. }
  311. }
  312. public void stopCleanCacheJob()throws Exception {
  313. if(quartzHelper.isExistJob(CacheCleanJob.jobKey)){
  314. quartzHelper.removeJob(CacheCleanJob.jobKey);
  315. }else{
  316. throw new Exception("已经停止");
  317. }
  318. }
  319. public void cleanCache() {
  320. CachePool.cleanAllCache();
  321. }
  322. public String seeCache() {
  323. Map<String, CacheModel> cacheModesCache= CachePool.getArriveSignFamilyExpenseStatus1Map();
  324. Map<String, String> patientGroupCache=CachePool.getPatientGroup();
  325. Map<String, String> healthGroupCache=CachePool.getHealthGroup();
  326. Map<String, List<String>> diseaseGroupCache=CachePool.getDiseaseGroup();
  327. String returnMessage=" 签约缓存:缓存存在"+cacheModesCache.size()+"天的缓存,";
  328. for(Map.Entry<String, CacheModel> entry:cacheModesCache.entrySet()){
  329. returnMessage+=entry.getKey()+",";
  330. }
  331. returnMessage+="patientGroupCache"+(patientGroupCache.size()>0?"有缓存":"没有缓存");
  332. returnMessage+="healthGroupCache"+(healthGroupCache.size()>0?"有缓存":"没有缓存");
  333. returnMessage+="diseaseGroupCache"+(diseaseGroupCache.size()>0?"有缓存":"没有缓存");
  334. return returnMessage;
  335. }
  336. public void startHealthMessageJob() throws Exception {
  337. if(!quartzHelper.isExistJob(HealthMessageJob.jobKey)){
  338. quartzHelper.addJob(HealthMessageJob.class,HealthMessageJob.cron,HealthMessageJob.jobKey,new HashMap<>());
  339. }else{
  340. throw new Exception("已经启动");
  341. }
  342. }
  343. public void stopHealthMessageJob()throws Exception {
  344. if(quartzHelper.isExistJob(HealthMessageJob.jobKey)){
  345. quartzHelper.removeJob(HealthMessageJob.jobKey);
  346. }else{
  347. throw new Exception("已经停止");
  348. }
  349. }
  350. public void productHealthDataByOneDay(String day)throws Exception {
  351. SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
  352. Date date = dataSimple.parse(day);
  353. if (date == null) {
  354. throw new Exception("时间格式错误");
  355. }
  356. Calendar calendar = new GregorianCalendar();
  357. calendar.setTime(date);
  358. calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
  359. Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
  360. String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
  361. Map<String, Object> params = new HashMap<String, Object>();
  362. //往quartz框架添加任务
  363. params.put("now", yesterday);
  364. params.put("yesterday", day);
  365. quartzHelper.startNow(HealthMessageJob.class, HealthMessageJob.jobKey + UUID.randomUUID().toString().replace("-",""), params);
  366. Thread.sleep(20000L);
  367. }
  368. public void productHealthDataByDayToDay(String start, String end) throws Exception{
  369. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  370. Date startDate=sdf.parse(start);
  371. Date endDate=sdf.parse(end);
  372. if(startDate.after(endDate)){
  373. throw new Exception("日期参数错误");
  374. }
  375. int day=daysBetween(startDate,endDate);
  376. for(int i=0;i<day;i++){
  377. productHealthDataByOneDay(getYesterday(i,startDate));
  378. }
  379. }
  380. public static void main(String[] args) throws Exception{
  381. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  382. Date startDate=sdf.parse("2016-10-20");
  383. Date endDate=sdf.parse("2016-10-28");
  384. System.out.println(daysBetween(startDate,endDate));
  385. System.out.println(getYesterday(0,startDate));
  386. }
  387. public void startEveryDayReportJob()throws Exception {
  388. if(!quartzHelper.isExistJob(ReportAllLogJob.jobKey)){
  389. quartzHelper.addJob(ReportAllLogJob.class,ReportAllLogJob.cron,ReportAllLogJob.jobKey,new HashMap<>());
  390. }else{
  391. throw new Exception("已经启动");
  392. }
  393. }
  394. public void stopEveryDayReportJob() throws Exception{
  395. if(quartzHelper.isExistJob(ReportAllLogJob.jobKey)){
  396. quartzHelper.removeJob(ReportAllLogJob.jobKey);
  397. }else{
  398. throw new Exception("已经停止");
  399. }
  400. }
  401. public void startNoticeJob() throws Exception {
  402. if(!quartzHelper.isExistJob(NoticeJob.jobKey)){
  403. quartzHelper.addJob(NoticeJob.class,NoticeJob.jobCron,NoticeJob.jobKey,new HashMap<>());
  404. }else{
  405. throw new Exception("已经启动");
  406. }
  407. }
  408. public void stopNoticeJob()throws Exception {
  409. if(quartzHelper.isExistJob(NoticeJob.jobKey)){
  410. quartzHelper.removeJob(NoticeJob.jobKey);
  411. }else{
  412. throw new Exception("已经停止");
  413. }
  414. }
  415. }