JobService.java 15 KB

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