CheckSignJob.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.yihu.wlyy.job.check;
  2. import com.yihu.wlyy.entity.job.QuartzJobConfig;
  3. import com.yihu.wlyy.entity.patient.SignFamily;
  4. import com.yihu.wlyy.entity.statistics.WlyyQuota;
  5. import com.yihu.wlyy.entity.statistics.WlyyQuotaResult;
  6. import com.yihu.wlyy.job.QuartzHelper;
  7. import com.yihu.wlyy.job.SignJob;
  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.repository.statistics.WlyyQuotaResultDao;
  12. import com.yihu.wlyy.util.DateUtil;
  13. import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
  14. import com.yihu.wlyy.web.quota.WlyyQuotaVO;
  15. import org.quartz.Job;
  16. import org.quartz.JobExecutionContext;
  17. import org.quartz.JobExecutionException;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.StringUtils;
  23. import org.springframework.web.context.support.SpringBeanAutowiringSupport;
  24. import java.util.*;
  25. /**
  26. * Created by Administrator on 2016.10.11.
  27. * 判断签约的数据对不对 不对的话 添加任务重新生成签约数据
  28. */
  29. @Component
  30. public class CheckSignJob implements Job{
  31. public static String jobKey="CHECK_SIGN_JOB";
  32. public static String cron="0 0 12 * * ?";
  33. @Autowired
  34. private WlyyQuotaResultDao wlyyQuotaResultDao;
  35. @Autowired
  36. private SignFamilyDao signFamilyDao;
  37. @Autowired
  38. private QuartzHelper quartzHelper;
  39. @Autowired
  40. private QuartzJobConfigDao wlyyJobConfigDao;
  41. @Autowired
  42. private QuotaDao quotaDao;
  43. @Transactional
  44. public void execute(JobExecutionContext context) throws JobExecutionException {
  45. try{
  46. SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
  47. //得到需要检验的统计
  48. List<WlyyQuotaResult> wlyyQuotaResults=getNeedCheckData();
  49. //检验统计数据和签约数据是否一致 key:yes 是需要 no:是不需要
  50. Map<String,List<WlyyQuotaResult>> wlyyQuotaResultsMap=checkWlyyQuotaResult(wlyyQuotaResults);
  51. //更新不需要下一次统计的任务
  52. upDataNoNeedCheckQuato(wlyyQuotaResultsMap.get("no"));
  53. //添加需要重新统计的指标
  54. addNeedCheckQuato(wlyyQuotaResultsMap.get("yes"));
  55. }catch (Exception e){
  56. e.printStackTrace();
  57. }
  58. }
  59. private void addNeedCheckQuato(List<WlyyQuotaResult> yes)throws Exception {
  60. if(yes.size()>0){
  61. for(WlyyQuotaResult wlyyQuotaResult:yes){
  62. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(wlyyQuotaResult.getId());
  63. if (quartzJobConfig == null) {
  64. throw new Exception("id不存在");
  65. }
  66. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  67. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  68. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  69. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  70. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  71. Map<String, Object> params = new HashMap<String, Object>();
  72. params.put("quota", wlyyQuotaVO);
  73. params.put("jobConfig", wlyyJobConfigVO);
  74. String now=getNextDate(wlyyQuotaResult.getQuotaDate());
  75. String yesterday=wlyyQuotaResult.getQuotaDate();
  76. //往quartz框架添加任务
  77. params.put("now", now);
  78. params.put("yesterday", yesterday);
  79. String jobKey="checkSignQuartz:"+wlyyQuotaResult.getQuotaDate()+":"+wlyyQuotaResult.getQuatoCode();
  80. if(!quartzHelper.isExistJob(jobKey)){
  81. quartzHelper.startAt(weeHours(new Date(),1),SignJob.class,jobKey,params);
  82. //重新生成性别 疾病 扣费 和 年龄的统计
  83. startJob("6",yesterday,now);
  84. startJob("7",yesterday,now);
  85. startJob("8",yesterday,now);
  86. startJob("12",yesterday,now);
  87. }
  88. }
  89. }
  90. }
  91. private void startJob(String id, String yesterday, String now) throws Exception{
  92. QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id);
  93. if (quartzJobConfig == null) {
  94. throw new Exception("id不存在");
  95. }
  96. WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
  97. WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
  98. WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
  99. BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
  100. BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
  101. Map<String, Object> params = new HashMap<String, Object>();
  102. params.put("quota", wlyyQuotaVO);
  103. params.put("jobConfig", wlyyJobConfigVO);
  104. //往quartz框架添加任务
  105. params.put("now", now);
  106. params.put("yesterday", yesterday);
  107. String jobKey="checkSignQuartz:"+yesterday+":"+wlyyQuota.getId();
  108. if(!quartzHelper.isExistJob(jobKey)){
  109. quartzHelper.startAt(weeHours(new Date(),1),getClassById(id),jobKey,params);
  110. }
  111. }
  112. private Class getClassById(String id) {
  113. switch (id){
  114. case "6":{
  115. return SignJob.class;
  116. }
  117. case "7":{
  118. return SignJob.class;
  119. }
  120. case "8":{
  121. return SignJob.class;
  122. }
  123. case "12":{
  124. return SignJob.class;
  125. }
  126. }
  127. return null;
  128. }
  129. /**
  130. * 更新不需要下一次统计的任务 更新qkdoctorJobName字段为1
  131. * @param noNeedCheckQuatos
  132. */
  133. private void upDataNoNeedCheckQuato(List<WlyyQuotaResult> noNeedCheckQuatos) {
  134. if(noNeedCheckQuatos.size()>0){
  135. for(WlyyQuotaResult wlyyQuotaResult:noNeedCheckQuatos){
  136. wlyyQuotaResult.setQkdoctorJobName("1");
  137. }
  138. wlyyQuotaResultDao.save(noNeedCheckQuatos);
  139. }
  140. }
  141. /**
  142. * 检验统计数据和签约数据是否一致 key:yes 是需要 no:是不需要
  143. * @param wlyyQuotaResults
  144. * @return
  145. */
  146. private Map<String,List<WlyyQuotaResult>> checkWlyyQuotaResult(List<WlyyQuotaResult> wlyyQuotaResults) {
  147. Map<String,List<WlyyQuotaResult>> returnMap=new HashMap<String,List<WlyyQuotaResult>>();
  148. List<WlyyQuotaResult> yes=new ArrayList<WlyyQuotaResult>();//需要重新统计的
  149. List<WlyyQuotaResult> no=new ArrayList<WlyyQuotaResult>();//不需要重新统计
  150. for(WlyyQuotaResult wlyyQuotaResult:wlyyQuotaResults){
  151. String date= wlyyQuotaResult.getQuotaDate();//格式 2016-10-20
  152. String nextDate=getNextDate(date);
  153. List<SignFamily> signFamilys = signFamilyDao.findByJiatingSignYesterdayExpensesStatus(date, nextDate);
  154. if(signFamilys.size()!=Integer.valueOf(wlyyQuotaResult.getResult())){
  155. //如果数目不相等 说明需要重新统计
  156. yes.add(wlyyQuotaResult);
  157. }else{
  158. //如果数目相等 说明需要不需要重新统计
  159. no.add(wlyyQuotaResult);
  160. }
  161. }
  162. returnMap.put("yes",yes);
  163. returnMap.put("no",no);
  164. return returnMap;
  165. }
  166. private String getNextDate(String date) {
  167. Date dateTime= DateUtil.strToDateShort(date);
  168. Calendar calendar = new GregorianCalendar();
  169. calendar.setTime(dateTime);
  170. calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
  171. Date nextDateTime=calendar.getTime();
  172. return DateUtil.dateToStrShort(nextDateTime);
  173. }
  174. /**
  175. * 得到需要检验的统计
  176. * @return
  177. */
  178. private List<WlyyQuotaResult> getNeedCheckData() {
  179. List<WlyyQuotaResult> wlyyQuotaResults=wlyyQuotaResultDao.findByLevel1TypeAndQkdoctorJobName("4","1");
  180. return wlyyQuotaResults;
  181. }
  182. /**
  183. * 获取当前时间所在的凌晨时间
  184. * 凌晨
  185. * @param date
  186. * @flag 0 返回yyyy-MM-dd 00:00:00日期<br>
  187. * 1 返回yyyy-MM-dd 23:59:59日期
  188. * @return
  189. */
  190. private Date weeHours(Date date, int flag) {
  191. Calendar cal = Calendar.getInstance();
  192. cal.setTime(date);
  193. int hour = cal.get(Calendar.HOUR_OF_DAY);
  194. int minute = cal.get(Calendar.MINUTE);
  195. int second = cal.get(Calendar.SECOND);
  196. //时分秒(毫秒数)
  197. long millisecond = hour*60*60*1000 + minute*60*1000 + second*1000;
  198. //凌晨00:00:00
  199. cal.setTimeInMillis(cal.getTimeInMillis()-millisecond);
  200. if (flag == 0) {
  201. return cal.getTime();
  202. } else if (flag == 1) {
  203. //凌晨23:59:59
  204. cal.setTimeInMillis(cal.getTimeInMillis()+23*60*60*1000 + 59*60*1000 + 59*1000);
  205. }
  206. return cal.getTime();
  207. }
  208. }