JobService.java 14 KB

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