package com.yihu.wlyy.job.check; import com.yihu.wlyy.entity.job.QuartzJobConfig; import com.yihu.wlyy.entity.patient.SignFamily; import com.yihu.wlyy.entity.statistics.WlyyQuota; import com.yihu.wlyy.entity.statistics.WlyyQuotaResult; import com.yihu.wlyy.job.QuartzHelper; import com.yihu.wlyy.job.SignJob; import com.yihu.wlyy.repository.job.QuartzJobConfigDao; import com.yihu.wlyy.repository.patient.SignFamilyDao; import com.yihu.wlyy.repository.statistics.QuotaDao; import com.yihu.wlyy.repository.statistics.WlyyQuotaResultDao; import com.yihu.wlyy.util.DateUtil; import com.yihu.wlyy.web.quota.WlyyJobConfigVO; import com.yihu.wlyy.web.quota.WlyyQuotaVO; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import org.springframework.web.context.support.SpringBeanAutowiringSupport; import java.util.*; /** * Created by Administrator on 2016.10.11. * 判断签约的数据对不对 不对的话 添加任务重新生成签约数据 */ @Component public class CheckSignJob implements Job{ public static String jobKey="CHECK_SIGN_JOB"; public static String cron="0 0 12 * * ?"; @Autowired private WlyyQuotaResultDao wlyyQuotaResultDao; @Autowired private SignFamilyDao signFamilyDao; @Autowired private QuartzHelper quartzHelper; @Autowired private QuartzJobConfigDao wlyyJobConfigDao; @Autowired private QuotaDao quotaDao; @Transactional public void execute(JobExecutionContext context) throws JobExecutionException { try{ SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); //得到需要检验的统计 List wlyyQuotaResults=getNeedCheckData(); //检验统计数据和签约数据是否一致 key:yes 是需要 no:是不需要 Map> wlyyQuotaResultsMap=checkWlyyQuotaResult(wlyyQuotaResults); //更新不需要下一次统计的任务 upDataNoNeedCheckQuato(wlyyQuotaResultsMap.get("no")); //添加需要重新统计的指标 addNeedCheckQuato(wlyyQuotaResultsMap.get("yes")); }catch (Exception e){ e.printStackTrace(); } } private void addNeedCheckQuato(List yes)throws Exception { if(yes.size()>0){ for(WlyyQuotaResult wlyyQuotaResult:yes){ QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(wlyyQuotaResult.getId()); if (quartzJobConfig == null) { throw new Exception("id不存在"); } WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId()); WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO(); WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO(); BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO); BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO); Map params = new HashMap(); params.put("quota", wlyyQuotaVO); params.put("jobConfig", wlyyJobConfigVO); String now=getNextDate(wlyyQuotaResult.getQuotaDate()); String yesterday=wlyyQuotaResult.getQuotaDate(); //往quartz框架添加任务 params.put("now", now); params.put("yesterday", yesterday); String jobKey="checkSignQuartz:"+wlyyQuotaResult.getQuotaDate()+":"+wlyyQuotaResult.getQuatoCode(); if(!quartzHelper.isExistJob(jobKey)){ quartzHelper.startAt(weeHours(new Date(),1),SignJob.class,jobKey,params); //重新生成性别 疾病 扣费 和 年龄的统计 startJob("6",yesterday,now); startJob("7",yesterday,now); startJob("8",yesterday,now); startJob("12",yesterday,now); } } } } private void startJob(String id, String yesterday, String now) throws Exception{ QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id); if (quartzJobConfig == null) { throw new Exception("id不存在"); } WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId()); WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO(); WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO(); BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO); BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO); Map params = new HashMap(); params.put("quota", wlyyQuotaVO); params.put("jobConfig", wlyyJobConfigVO); //往quartz框架添加任务 params.put("now", now); params.put("yesterday", yesterday); String jobKey="checkSignQuartz:"+yesterday+":"+wlyyQuota.getId(); if(!quartzHelper.isExistJob(jobKey)){ quartzHelper.startAt(weeHours(new Date(),1),getClassById(id),jobKey,params); } } private Class getClassById(String id) { switch (id){ case "6":{ return SignJob.class; } case "7":{ return SignJob.class; } case "8":{ return SignJob.class; } case "12":{ return SignJob.class; } } return null; } /** * 更新不需要下一次统计的任务 更新qkdoctorJobName字段为1 * @param noNeedCheckQuatos */ private void upDataNoNeedCheckQuato(List noNeedCheckQuatos) { if(noNeedCheckQuatos.size()>0){ for(WlyyQuotaResult wlyyQuotaResult:noNeedCheckQuatos){ wlyyQuotaResult.setQkdoctorJobName("1"); } wlyyQuotaResultDao.save(noNeedCheckQuatos); } } /** * 检验统计数据和签约数据是否一致 key:yes 是需要 no:是不需要 * @param wlyyQuotaResults * @return */ private Map> checkWlyyQuotaResult(List wlyyQuotaResults) { Map> returnMap=new HashMap>(); List yes=new ArrayList();//需要重新统计的 List no=new ArrayList();//不需要重新统计 for(WlyyQuotaResult wlyyQuotaResult:wlyyQuotaResults){ String date= wlyyQuotaResult.getQuotaDate();//格式 2016-10-20 String nextDate=getNextDate(date); List signFamilys = signFamilyDao.findByJiatingSignYesterdayExpensesStatus(date, nextDate); if(signFamilys.size()!=Integer.valueOf(wlyyQuotaResult.getResult())){ //如果数目不相等 说明需要重新统计 yes.add(wlyyQuotaResult); }else{ //如果数目相等 说明需要不需要重新统计 no.add(wlyyQuotaResult); } } returnMap.put("yes",yes); returnMap.put("no",no); return returnMap; } private String getNextDate(String date) { Date dateTime= DateUtil.strToDateShort(date); Calendar calendar = new GregorianCalendar(); calendar.setTime(dateTime); calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 Date nextDateTime=calendar.getTime(); return DateUtil.dateToStrShort(nextDateTime); } /** * 得到需要检验的统计 * @return */ private List getNeedCheckData() { List wlyyQuotaResults=wlyyQuotaResultDao.findByLevel1TypeAndQkdoctorJobName("4","1"); return wlyyQuotaResults; } /** * 获取当前时间所在的凌晨时间 * 凌晨 * @param date * @flag 0 返回yyyy-MM-dd 00:00:00日期
* 1 返回yyyy-MM-dd 23:59:59日期 * @return */ private Date weeHours(Date date, int flag) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int hour = cal.get(Calendar.HOUR_OF_DAY); int minute = cal.get(Calendar.MINUTE); int second = cal.get(Calendar.SECOND); //时分秒(毫秒数) long millisecond = hour*60*60*1000 + minute*60*1000 + second*1000; //凌晨00:00:00 cal.setTimeInMillis(cal.getTimeInMillis()-millisecond); if (flag == 0) { return cal.getTime(); } else if (flag == 1) { //凌晨23:59:59 cal.setTimeInMillis(cal.getTimeInMillis()+23*60*60*1000 + 59*60*1000 + 59*1000); } return cal.getTime(); } }