JobService.java 17 KB

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