123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- package com.yihu.wlyy.statistics.service;
- import com.yihu.wlyy.statistics.dao.DoctorPatientGroupInfoDao;
- import com.yihu.wlyy.statistics.dao.QuartzJobConfigDao;
- import com.yihu.wlyy.statistics.dao.QuotaDao;
- import com.yihu.wlyy.statistics.dao.SignFamilyDao;
- import com.yihu.wlyy.statistics.etl.model.CacheModel;
- import com.yihu.wlyy.statistics.etl.mycache.CachePool;
- import com.yihu.wlyy.statistics.job.business.QuartzHelper;
- import com.yihu.wlyy.statistics.job.cache.CacheCleanJob;
- import com.yihu.wlyy.statistics.job.check.CheckSignJob;
- import com.yihu.wlyy.statistics.job.message.HealthMessageJob;
- import com.yihu.wlyy.statistics.model.doctor.DoctorPatientGroupInfo;
- import com.yihu.wlyy.statistics.model.job.QuartzJobConfig;
- import com.yihu.wlyy.statistics.model.job.WlyyQuota;
- import com.yihu.wlyy.statistics.model.signfamily.SignFamily;
- import com.yihu.wlyy.statistics.vo.WlyyJobConfigVO;
- import com.yihu.wlyy.statistics.vo.WlyyQuotaVO;
- import org.quartz.SchedulerException;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.StringUtils;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * @author chenweida
- */
- @Service
- public class JobService {
- @Autowired
- private QuartzHelper quartzHelper;
- @Autowired
- private QuartzJobConfigDao wlyyJobConfigDao;
- @Autowired
- private QuotaDao quotaDao;
- @Autowired
- private SignFamilyDao signFamilyDao;
- @Autowired
- private DoctorPatientGroupInfoDao doctorPatientGroupInfoDao;
- @Autowired
- private CachePool cachePool;
- @Transactional
- public void stopById(String id) throws Exception {
- QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "1");
- if (quartzJobConfig != null) {
- quartzHelper.removeJob(quartzJobConfig.getId());
- quartzJobConfig.setStatus("0");
- } else {
- throw new Exception("任务已经停止");
- }
- }
- @Transactional
- public void startById(String id) throws Exception {
- QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findById(id, "0");
- if (quartzJobConfig != null) {
- startOneJob(quartzJobConfig);
- } else {
- throw new Exception("任务已经启动");
- }
- }
- @Transactional
- public void stopAll() throws Exception {
- List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("1");
- if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
- for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
- quartzHelper.removeJob(quartzJobConfig.getId());
- quartzJobConfig.setStatus("0");
- }
- } else {
- throw new Exception("任务已经全部停止");
- }
- }
- @Transactional
- public void startAll() throws Exception {
- List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByAll("0");
- if (quartzJobConfigs != null && quartzJobConfigs.size() > 0) {
- for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
- startOneJob(quartzJobConfig);
- }
- } else {
- throw new Exception("任务已经全部启动");
- }
- }
- /**
- * 启动单个任务
- *
- * @param quartzJobConfig
- * @throws Exception
- */
- private void startOneJob(QuartzJobConfig quartzJobConfig) throws Exception {
- WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
- WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
- WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
- BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
- BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("quota", wlyyQuotaVO);
- params.put("jobConfig", wlyyJobConfigVO);
- if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
- //往quartz框架添加任务
- quartzHelper.addJob(getRightClass(quartzJobConfig), quartzJobConfig.getQuartzCron(), quartzJobConfig.getId(), params);
- quartzJobConfig.setStatus("1");//设置任务状态是启动
- }
- }
- public void startNowById(String id) throws Exception {
- QuartzJobConfig quartzJobConfig = wlyyJobConfigDao.findOne(id);
- WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
- WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
- WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
- BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
- BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("quota", wlyyQuotaVO);
- params.put("jobConfig", wlyyJobConfigVO);
- //往quartz框架添加任务
- if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
- quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId()+ UUID.randomUUID().toString().replace("-",""), params);
- }
- }
- public void productDataByDay(Integer day) throws Exception {
- List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByIds();
- for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
- WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
- WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
- WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
- BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
- BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("quota", wlyyQuotaVO);
- params.put("jobConfig", wlyyJobConfigVO);
- for (int i = 1; i <= day; i++) {
- //往quartz框架添加任务
- params.put("daybefore", getYesterday(0 - i-1 ));
- params.put("yesterday", getYesterday(0 - i));
- if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
- quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-",""), params);
- Thread.sleep(20000L);
- }
- }
- }
- }
- public static String getYesterday(Integer day) {
- Calendar cal = Calendar.getInstance();
- cal.add(Calendar.DATE, day);
- String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
- return yesterday;
- }
- public void productDataByOneDay(String yesterday) throws Exception {
- SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
- Date date = dataSimple.parse(yesterday);
- if (date == null) {
- throw new Exception("时间格式错误");
- }
- Calendar calendar = new GregorianCalendar();
- calendar.setTime(date);
- calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
- Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
- String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
- List<QuartzJobConfig> quartzJobConfigs = wlyyJobConfigDao.findByIds();
- for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
- WlyyQuota wlyyQuota = quotaDao.findOne(quartzJobConfig.getQuotaId());
- WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
- WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
- BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
- BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("quota", wlyyQuotaVO);
- params.put("jobConfig", wlyyJobConfigVO);
- //往quartz框架添加任务
- params.put("daybefore", daybefore);
- params.put("yesterday", yesterday);
- if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
- quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-",""), params);
- Thread.sleep(20000L);
- }
- }
- }
- /**
- *
- * @param quartzJobConfig
- * @return
- * @throws ClassNotFoundException
- */
- private Class getRightClass(QuartzJobConfig quartzJobConfig) throws ClassNotFoundException {
- return Class.forName(quartzJobConfig.getJobClass());
- }
- public void productDataByOneDayWithId(String yesterday, String id) throws Exception {
- SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
- Date date = dataSimple.parse(yesterday);
- if (date == null) {
- throw new Exception("时间格式错误");
- }
- Calendar calendar = new GregorianCalendar();
- calendar.setTime(date);
- calendar.add(calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
- Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
- String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
- 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<String, Object> params = new HashMap<String, Object>();
- params.put("quota", wlyyQuotaVO);
- params.put("jobConfig", wlyyJobConfigVO);
- //往quartz框架添加任务
- params.put("daybefore", daybefore);
- params.put("yesterday", yesterday);
- if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
- quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-",""), params);
- }
- }
- @Transactional
- @Async("dbExtractExecutor")
- public void startaaaa() throws Exception{
- quartzHelper.startNow(HealthMessageJob.class,UUID.randomUUID().toString().replace("-",""),new HashMap<>());
- }
- public void productDataByDayAndId(Integer day, String id) 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<String, Object> params = new HashMap<String, Object>();
- params.put("quota", wlyyQuotaVO);
- params.put("jobConfig", wlyyJobConfigVO);
- for (int i = 1; i <= day; i++) {
- //往quartz框架添加任务
- params.put("daybefore", getYesterday(0 - i -1));
- params.put("yesterday", getYesterday(0 - i));
- if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
- quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-",""), params);
- Thread.sleep(20000L);
- }
- }
- }
- public void startCheckSignJob() throws Exception{
- if(!quartzHelper.isExistJob(CheckSignJob.jobKey)){
- quartzHelper.addJob(CheckSignJob.class,CheckSignJob.cron,CheckSignJob.jobKey,new HashMap<>());
- }
- }
- public void stopCheckSignJob()throws Exception {
- if(quartzHelper.isExistJob(CheckSignJob.jobKey)){
- quartzHelper.removeJob(CheckSignJob.jobKey);
- }
- }
- public void productDataByDayToDay(String start, String end) throws Exception {
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
- Date startDate=sdf.parse(start);
- Date endDate=sdf.parse(end);
- if(startDate.after(endDate)){
- throw new Exception("日期参数错误");
- }
- int day=daysBetween(startDate,endDate);
- for(int i=0;i<day;i++){
- productDataByOneDay(getYesterday(i,startDate));
- }
- }
- public static String getYesterday(Integer day,Date startDate) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(startDate);
- cal.add(Calendar.DAY_OF_MONTH, day);
- String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
- return yesterday;
- }
- public static int daysBetween(Date smdate,Date bdate) throws ParseException
- {
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
- smdate=sdf.parse(sdf.format(smdate));
- bdate=sdf.parse(sdf.format(bdate));
- Calendar cal = Calendar.getInstance();
- cal.setTime(smdate);
- long time1 = cal.getTimeInMillis();
- cal.setTime(bdate);
- long time2 = cal.getTimeInMillis();
- long between_days=(time2-time1)/(1000*3600*24);
- return Integer.parseInt(String.valueOf(between_days));
- }
- public static void main(String[] args) {
- System.out.println(getYesterday(0,new Date()));
- }
- public void productDataByDayToDayAndId(String start, String end, String id) throws Exception {
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
- Date startDate=sdf.parse(start);
- Date endDate=sdf.parse(end);
- if(startDate.after(endDate)){
- throw new Exception("日期参数错误");
- }
- int day=daysBetween(startDate,endDate);
- for(int i=0;i<day;i++){
- productDataByOneDayWithId(getYesterday(i,startDate),id);
- }
- }
- public void startCleanCacheJob() throws Exception {
- if(!quartzHelper.isExistJob(CacheCleanJob.jobKey)){
- quartzHelper.addJob(CacheCleanJob.class,CacheCleanJob.cron,CacheCleanJob.jobKey,new HashMap<>());
- }
- }
- public void stopCleanCacheJob()throws Exception {
- if(quartzHelper.isExistJob(CacheCleanJob.jobKey)){
- quartzHelper.removeJob(CacheCleanJob.jobKey);
- }
- }
- public void cleanCache() {
- CachePool.cleanAllCache();
- }
- public String seeCache() {
- Map<String, CacheModel> cacheModesCache= CachePool.getArriveSignFamilyExpenseStatus1Map();
- Map<String, String> patientGroupCache=CachePool.getPatientGroup();
- Map<String, String> healthGroupCache=CachePool.getHealthGroup();
- Map<String, List<String>> diseaseGroupCache=CachePool.getDiseaseGroup();
- String returnMessage=" 签约缓存:缓存存在"+cacheModesCache.size()+"天的缓存,";
- for(Map.Entry<String, CacheModel> entry:cacheModesCache.entrySet()){
- returnMessage+=entry.getKey()+",";
- }
- returnMessage+="patientGroupCache"+(patientGroupCache.size()>0?"有缓存":"没有缓存");
- returnMessage+="healthGroupCache"+(healthGroupCache.size()>0?"有缓存":"没有缓存");
- returnMessage+="diseaseGroupCache"+(diseaseGroupCache.size()>0?"有缓存":"没有缓存");
- return returnMessage;
- }
- public void startHealthMessageJob() throws Exception {
- if(!quartzHelper.isExistJob(HealthMessageJob.jobKey)){
- quartzHelper.addJob(HealthMessageJob.class,HealthMessageJob.cron,HealthMessageJob.jobKey,new HashMap<>());
- }
- }
- public void stopHealthMessageJob()throws Exception {
- if(quartzHelper.isExistJob(HealthMessageJob.jobKey)){
- quartzHelper.removeJob(HealthMessageJob.jobKey);
- }
- }
- }
|