JobService.java 16 KB

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