JobService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package com.yihu.wlyy.service.quota;
  2. import com.yihu.wlyy.entity.doctor.team.sign.DoctorPatientGroupInfo;
  3. import com.yihu.wlyy.entity.job.QuartzJobConfig;
  4. import com.yihu.wlyy.entity.patient.SignFamily;
  5. import com.yihu.wlyy.entity.statistics.WlyyQuota;
  6. import com.yihu.wlyy.job.QuartzHelper;
  7. import com.yihu.wlyy.repository.doctor.DoctorPatientGroupInfoDao;
  8. import com.yihu.wlyy.repository.job.QuartzJobConfigDao;
  9. import com.yihu.wlyy.repository.patient.SignFamilyDao;
  10. import com.yihu.wlyy.repository.statistics.QuotaDao;
  11. import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
  12. import com.yihu.wlyy.web.quota.WlyyQuotaVO;
  13. import org.springframework.beans.BeanUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import org.springframework.util.StringUtils;
  18. import java.text.SimpleDateFormat;
  19. import java.util.*;
  20. /**
  21. * @author chenweida
  22. */
  23. @Service
  24. public class JobService {
  25. @Autowired
  26. private QuartzHelper quartzHelper;
  27. @Autowired
  28. private QuartzJobConfigDao wlyyJobConfigDao;
  29. @Autowired
  30. private QuotaDao quotaDao;
  31. @Autowired
  32. private SignFamilyDao signFamilyDao;
  33. @Autowired
  34. private DoctorPatientGroupInfoDao doctorPatientGroupInfoDao;
  35. @Transactional
  36. public void stopById(String id) throws Exception {
  37. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "1");
  38. if (quartzJobConfig != null) {
  39. quartzHelper.removeJob(quartzJobConfig.getId());
  40. quartzJobConfig.setStatus("0");
  41. } else {
  42. throw new Exception("任务已经停止");
  43. }
  44. }
  45. @Transactional
  46. public void startById(String id) throws Exception {
  47. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "0");
  48. if (quartzJobConfig != null) {
  49. startOneJob(quartzJobConfig);
  50. } else {
  51. throw new Exception("任务已经启动");
  52. }
  53. }
  54. @Transactional
  55. public void stopAll() throws Exception {
  56. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("1");
  57. if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
  58. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  59. quartzHelper.removeJob(quartzJobConfig.getId());
  60. quartzJobConfig.setStatus("0");
  61. }
  62. } else {
  63. throw new Exception("任务已经全部停止");
  64. }
  65. }
  66. @Transactional
  67. public void startAll() throws Exception {
  68. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("0");
  69. if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
  70. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  71. startOneJob(quartzJobConfig);
  72. }
  73. } else {
  74. throw new Exception("任务已经全部启动");
  75. }
  76. }
  77. /**
  78. * 启动单个任务
  79. *
  80. * @param quartzJobConfig
  81. * @throws Exception
  82. */
  83. private void startOneJob(QuartzJobConfig quartzJobConfig) throws Exception {
  84. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  85. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  86. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  87. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  88. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  89. Map<String, Object> params = new HashMap<String, Object>();
  90. params.put("quota", wlyyQuotaVO);
  91. params.put("jobConfig", wlyyJobConfigVO);
  92. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  93. //往quartz框架添加任务
  94. quartzHelper.addJob(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getQuartzCron(), quartzJobConfig.getId(), params);
  95. quartzJobConfig.setStatus("1");//设置任务状态是启动
  96. }
  97. }
  98. public void startNowById(String id) throws Exception {
  99. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findOne(id);
  100. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  101. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  102. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  103. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  104. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  105. Map<String, Object> params = new HashMap<String, Object>();
  106. params.put("quota", wlyyQuotaVO);
  107. params.put("jobConfig", wlyyJobConfigVO);
  108. //往quartz框架添加任务
  109. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  110. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId()+ UUID.randomUUID(), params);
  111. }
  112. }
  113. public void productDataByDay(Integer day) throws Exception {
  114. List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByIds();
  115. for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
  116. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  117. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  118. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  119. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  120. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  121. Map<String, Object> params = new HashMap<String, Object>();
  122. params.put("quota", wlyyQuotaVO);
  123. params.put("jobConfig", wlyyJobConfigVO);
  124. for (int i = 1; i <= day; i++) {
  125. //往quartz框架添加任务
  126. params.put("now", getYesterday(0 - i + 1));
  127. params.put("yesterday", getYesterday(0 - i));
  128. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  129. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId() + UUID.randomUUID(), params);
  130. Thread.sleep(15000L);
  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(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId() + UUID.randomUUID(), params);
  167. Thread.sleep(15000L);
  168. }
  169. }
  170. }
  171. public void productDataByOneDayWithId(String yesterday, String id) throws Exception {
  172. SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
  173. Date date = dataSimple.parse(yesterday);
  174. if (date == null) {
  175. throw new Exception("时间格式错误");
  176. }
  177. Calendar calendar = new GregorianCalendar();
  178. calendar.setTime(date);
  179. calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
  180. Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
  181. String now = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
  182. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  183. if (quartzJobConfig == null) {
  184. throw new Exception("id不存在");
  185. }
  186. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  187. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  188. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  189. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  190. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  191. Map<String, Object> params = new HashMap<String, Object>();
  192. params.put("quota", wlyyQuotaVO);
  193. params.put("jobConfig", wlyyJobConfigVO);
  194. //往quartz框架添加任务
  195. params.put("now", now);
  196. params.put("yesterday", yesterday);
  197. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  198. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId() + UUID.randomUUID(), params);
  199. Thread.sleep(15000L);
  200. }
  201. }
  202. @Transactional
  203. public void startaaaa() {
  204. List<SignFamily> s = signFamilyDao.findByDate("2016-08-05 15:40:00");
  205. for (SignFamily ss : s) {
  206. List<DoctorPatientGroupInfo> DoctorPatientGroupInfos = doctorPatientGroupInfoDao.findByPatient(ss.getPatient());
  207. if (DoctorPatientGroupInfos != null && DoctorPatientGroupInfos.size() == 0) {
  208. DoctorPatientGroupInfo qkDoctorPatientGroupInfo = new DoctorPatientGroupInfo();
  209. qkDoctorPatientGroupInfo.setCzrq(new Date());
  210. qkDoctorPatientGroupInfo.setQyrq(ss.getApplyDate());
  211. qkDoctorPatientGroupInfo.setGroup("2");
  212. qkDoctorPatientGroupInfo.setPatient(ss.getPatient());
  213. qkDoctorPatientGroupInfo.setPartAmount(0);
  214. qkDoctorPatientGroupInfo.setStatus(ss.getStatus() > 0 ? 1 : 0);
  215. if (ss.getStatus() < 0) {
  216. qkDoctorPatientGroupInfo.setDqrq(ss.getApplyUnsignDate());
  217. }
  218. qkDoctorPatientGroupInfo.setSignType("1");
  219. qkDoctorPatientGroupInfo.setDoctor(ss.getDoctor());
  220. doctorPatientGroupInfoDao.save(qkDoctorPatientGroupInfo);
  221. qkDoctorPatientGroupInfo = new DoctorPatientGroupInfo();
  222. qkDoctorPatientGroupInfo.setCzrq(new Date());
  223. qkDoctorPatientGroupInfo.setQyrq(ss.getApplyDate());
  224. qkDoctorPatientGroupInfo.setGroup("2");
  225. qkDoctorPatientGroupInfo.setPatient(ss.getPatient());
  226. qkDoctorPatientGroupInfo.setPartAmount(0);
  227. qkDoctorPatientGroupInfo.setStatus(ss.getStatus() > 0 ? 1 : 0);
  228. if (ss.getStatus() < 0) {
  229. qkDoctorPatientGroupInfo.setDqrq(ss.getApplyUnsignDate());
  230. }
  231. qkDoctorPatientGroupInfo.setSignType("1");
  232. qkDoctorPatientGroupInfo.setDoctor(ss.getDoctorHealth());
  233. doctorPatientGroupInfoDao.save(qkDoctorPatientGroupInfo);
  234. }
  235. }
  236. s = signFamilyDao.findByDate("2016-08-16 00:00:00");
  237. }
  238. public void productDataByDayAndId(Integer day, String id) throws Exception{
  239. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  240. if(quartzJobConfig==null){
  241. throw new Exception("id不存在");
  242. }
  243. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  244. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  245. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  246. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  247. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  248. Map<String, Object> params = new HashMap<String, Object>();
  249. params.put("quota", wlyyQuotaVO);
  250. params.put("jobConfig", wlyyJobConfigVO);
  251. for (int i = 1; i <= day; i++) {
  252. //往quartz框架添加任务
  253. params.put("now", getYesterday(0 - i + 1));
  254. params.put("yesterday", getYesterday(0 - i));
  255. if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
  256. quartzHelper.startNow(Class.forName(quartzJobConfig.getJobClass()), quartzJobConfig.getId() + UUID.randomUUID(), params);
  257. Thread.sleep(15000L);
  258. }
  259. }
  260. }
  261. }