JobService.java 14 KB

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