package com.ruoyi.project.tr.HiddenDangerCheckJob.util; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.ScheduleConstants; import com.ruoyi.common.constant.TrHiddenDangerCheckConstants; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.ExceptionUtil; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.common.utils.security.ShiroUtils; import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.doublePrevention.entity.PreventRiskJobAndMeasure; import com.ruoyi.doublePrevention.service.RiskService; import com.ruoyi.framework.web.service.JpushService; import com.ruoyi.project.enumerate.TrRiskTypeEnum; import com.ruoyi.project.monitor.job.domain.JobLog; import com.ruoyi.project.monitor.job.service.IJobLogService; import com.ruoyi.project.tr.HiddenDangerCheckJob.domain.HiddenDangerCheckJob; import com.ruoyi.project.tr.baseCheckPoint.domain.BaseCheckPoint; import com.ruoyi.project.tr.baseCheckPoint.mapper.BaseCheckPointMapper; import com.ruoyi.project.tr.baseCheckPoint.service.IBaseCheckPointService; import com.ruoyi.project.tr.hiddenDangerCheck.domain.HiddenDangerCheck; import com.ruoyi.project.tr.hiddenDangerCheck.service.IHiddenDangerCheckService; import com.ruoyi.project.tr.hiddenDangerCheckPoint.domain.HiddenDangerCheckPoint; import com.ruoyi.project.tr.hiddenDangerCheckPoint.service.IHiddenDangerCheckPointService; import com.ruoyi.project.tr.riskCheckPoint.domain.RiskCheckPoint; import com.ruoyi.project.tr.riskCheckPoint.service.IRiskCheckPointService; import com.ruoyi.project.tr.riskEvaluationPlan.domain.RiskEvaluationPlan; import com.ruoyi.project.tr.riskEvaluationPlan.service.IRiskEvaluationPlanService; import com.ruoyi.project.tr.riskList.domain.RiskList; import com.ruoyi.project.tr.riskList.service.IRiskListService; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.support.SpringBeanAutowiringSupport; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 抽象quartz调用 * * @author ruoyi */ public abstract class AbstractQuartzJob implements org.quartz.Job { private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class); JpushService jpushService; RiskService riskService; IRiskListService iRiskListService; /** * 线程本地变量 */ private static ThreadLocal threadLocal = new ThreadLocal<>(); @Override public void execute(JobExecutionContext context) throws JobExecutionException { HiddenDangerCheckJob job = new HiddenDangerCheckJob(); BeanUtils.copyBeanProp(job, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES)); try { before(context, job); if (job != null) { doExecute(context, job); } createData(context, job);//生成数据 // todo-2022 调用方法 定时隐患排查任务生成 after(context, job, null); } catch (Exception e) { log.error("任务执行异常 - :", e); after(context, job, e); } } /** * 执行前 * * @param context 工作执行上下文对象 */ protected void before(JobExecutionContext context, HiddenDangerCheckJob job) { threadLocal.set(new Date()); } /** * 执行后 * * @param context 工作执行上下文对象 * @param job 系统计划任务 */ protected void after(JobExecutionContext context, HiddenDangerCheckJob job, Exception e) { Date startTime = threadLocal.get(); threadLocal.remove(); final JobLog jobLog = new JobLog(); jobLog.setJobName(job.getJobName()); jobLog.setJobGroup(job.getJobGroup()); jobLog.setInvokeTarget(job.getInvokeTarget()); jobLog.setStartTime(startTime); jobLog.setEndTime(new Date()); System.out.println(startTime); System.out.println(jobLog.getEndTime()); long runMs = jobLog.getEndTime().getTime() - jobLog.getStartTime().getTime(); // todo -2022 检查此处异常来源 jobLog.setJobMessage(jobLog.getJobName() + " 总共耗时:" + runMs + "毫秒"); if (e != null) { jobLog.setStatus(Constants.FAIL); String errorMsg = StringUtils.substring(ExceptionUtil.getExceptionMessage(e), 0, 2000); jobLog.setExceptionInfo(errorMsg); } else { jobLog.setStatus(Constants.SUCCESS); } // 写入数据库当中 SpringUtils.getBean(IJobLogService.class).addJobLog(jobLog); } /** * todo-2022 定时隐患排查任务生成 * */ //生成定时隐患排查数据 @Transactional public void createData(JobExecutionContext context, HiddenDangerCheckJob job) { Date startTime = threadLocal.get(); //比较当前时间与执行开始时间 long difference = job.getStartTime().getTime() - new Date().getTime(); if (difference <= 0) { final HiddenDangerCheck hdc = new HiddenDangerCheck(); hdc.setJobId(job.getJobId()); hdc.setJobName(job.getJobName()); hdc.setJobGroup(job.getJobGroup()); hdc.setInvokeTarget(job.getInvokeTarget()); long runMs = new Date().getTime() - startTime.getTime(); hdc.setJobMessage(hdc.getJobName() + " 总共耗时:" + runMs + "毫秒"); hdc.setStatus(Constants.SUCCESS);//执行状态 hdc.setTroubleshootTypeId(job.getTroubleshootTypeId());//隐患排查类型id hdc.setTroubleshootTypeName(job.getTroubleshootTypeName());//隐患排查类型名称 hdc.setScheduleCreateUserId(job.getCreateUserId());//定时任务创建人ID hdc.setScheduleCreateUserName(job.getCreateUserName());//定时任务创建人名称 hdc.setScheduleCheckUserId(job.getExecuteUserId());//设置核查执行人ID hdc.setScheduleCheckUserName(job.getExecuteUserName());//设置核查执行人名称 hdc.setScheduleCheckStatus(TrHiddenDangerCheckConstants.CHECK_STATUS_NOT_CHECK);//排查状态(待排查) // hdc.setDangerSources(TrHiddenDangerCheckConstants.DANGER_SOURCES_SCHEDULE_PRODUCE);//隐患来源(定时器任务生成) hdc.setCreateBy(job.getCreateBy());//创建者 hdc.setCreateTime(DateUtils.getNowDate());//设置创建时间 //排查方式(1基础清单排查 2选择风险单元清单排查) if (!StringUtils.isEmpty(job.getCheckType())) { hdc.setCheckType(job.getCheckType());//设置排查方式 if ("1".equals(job.getCheckType())) { hdc.setRiskId(job.getRiskId() != null ? Long.valueOf(job.getRiskId()) : -1);//风险单元ID // todo-2022 , 基础排除清单名称-》改用基础清单表中信息 “需要改动”(现在的情况是,清单调整后,依然使用第一次新建时读取的job的名字) // RiskList infoByRiskId = iRiskListService.getInfoByRiskId(job.getRiskId()); hdc.setRiskName(job.getRiskName());//风险单元名称 hdc.setDangerPlaceId(job.getRiskPlaceId());//风险单元地点ID hdc.setDangerPlaceName(job.getRiskPlaceName());//风险单元地点名称 hdc.setDangerDeptId(job.getRiskDeptId());//隐患责任部门ID hdc.setDangerDeptName(job.getRiskDeptName());//隐患责任部门名称 hdc.setRiskType(job.getRiskType());//风险单元类型 // //1、校验是否有管控措施 // PreventRiskJobAndMeasure jobAndMeasure = riskService.getJobAndMeasure(job.getJobId()); // if (jobAndMeasure == null ){ // throw new RuntimeException("空任务,不调度"); // } // HiddenDangerCheck(隐患排查)写入数据库当中 SpringUtils.getBean(IHiddenDangerCheckService.class).insertHiddenDangerCheck(hdc); //HiddenDangerCheckPoint(隐患排查检查点)写入数据库当中 hdc.setCompanyId(job.getCompanyId()); insertHiddenDangerCheckPoint(hdc); System.out.println("【-】开始调用方法,处理任务附属表"); // todo-2022 插入 任务附属表 写到此处 hdc.getCheckId(); int result = SpringUtils.getBean(RiskService.class).insertDangerCheckLog(hdc.getCheckId(), hdc); // int result = riskService.insertDangerCheckLog(hdc.getCheckId(), hdc); if (result < 1){ throw new RuntimeException("定时任务生产失败"); } System.out.println("【-】开始调用方法,任务附属表处理完成"); } else if ("2".equals(job.getCheckType())) { String[] riskIdArray = job.getRiskId().split(","); String[] riskNameArray = job.getRiskName().split(","); String[] riskPlaceId = job.getRiskPlaceId().split(","); String[] riskPlaceName = job.getRiskPlaceName().split(","); String[] riskDeptIdArray = job.getRiskDeptId().split(","); String[] riskDeptNameArray = job.getRiskDeptName().split(","); String[] riskTypeArray = job.getRiskType().split(","); for (int i = 0; i < riskIdArray.length; ++i) { hdc.setRiskId(riskIdArray[i] != null ? Long.valueOf(riskIdArray[i]) : -1);//风险单元ID hdc.setRiskName(riskNameArray[i]);//风险单元名称 hdc.setDangerPlaceId(riskPlaceId[i]);//风险单元地点ID hdc.setDangerPlaceName(riskPlaceName[i]);//风险单元地点名称 hdc.setDangerDeptId(riskDeptIdArray[i]);//隐患责任部门ID hdc.setDangerDeptName(riskDeptNameArray[i]);//隐患责任部门名称 hdc.setRiskType(riskTypeArray[i]);//风险单元类型 hdc.setCheckId(null); // HiddenDangerCheck insertHiddenDangerCheckObject = new HiddenDangerCheck(); // HiddenDangerCheck(隐患排查)写入数据库当中 SpringUtils.getBean(IHiddenDangerCheckService.class).insertHiddenDangerCheck(hdc); //HiddenDangerCheckPoint(隐患排查检查点)写入数据库当中 hdc.setCompanyId(job.getCompanyId()); insertHiddenDangerCheckPoint(hdc); System.out.println("【-】开始调用方法,处理任务附属表"); // todo-2022 插入 任务附属表 写到此处 hdc.getCheckId(); int result = SpringUtils.getBean(RiskService.class).insertDangerCheckLog(hdc.getCheckId(), hdc); // int result = riskService.insertDangerCheckLog(hdc.getCheckId(), hdc); if (result < 1){ throw new RuntimeException("定时任务生产失败"); } System.out.println("【-】开始调用方法,任务附属表处理完成"); } } scheduleJPush(hdc);//定时任务 推送 } } } /** * todo-2022 生成核查任务时,添加任务的核查点信息 * */ //生成检查点信息 @Transactional public void insertHiddenDangerCheckPoint(HiddenDangerCheck job) { if(!StringUtils.isEmpty(job.getRiskType())) { //风险单元类型为基础清单 if(TrRiskTypeEnum.BASE.getCode().toString().equals(job.getRiskType())) { BaseCheckPoint bcpQuery = new BaseCheckPoint(); bcpQuery.setRiskId(job.getRiskId()); List baseCheckPointList = SpringUtils.getBean(IBaseCheckPointService.class).selectBaseCheckPointList(bcpQuery); baseCheckPointList.forEach(bcp -> { if (bcp != null) { HiddenDangerCheckPoint hdcp = new HiddenDangerCheckPoint(); hdcp.setCreateBy(job.getCreateBy());//创建者 hdcp.setCreateTime(DateUtils.getNowDate());//设置创建时间 hdcp.setCheckPointId(bcp.getCheckPointId()); hdcp.setCheckId(job.getCheckId()); hdcp.setScheduleCheckStatus(TrHiddenDangerCheckConstants.CHECK_STATUS_NOT_CHECK);//排查状态(待排查) hdcp.setId(null); hdcp.setCompanyId(job.getCompanyId()); // hdcp.setDangerSources(TrHiddenDangerCheckConstants.DANGER_SOURCES_SCHEDULE_PRODUCE);//隐患来源(定时器任务生成) // 写入数据库当中 SpringUtils.getBean(IHiddenDangerCheckPointService.class).insertHiddenDangerCheckPoint(hdcp); hdcp.getId(); // todo-2022 现场核查清单对应的核查点信息 System.out.println("【***】开始调用方法,处理核查点附属表"); int result = SpringUtils.getBean(RiskService.class).insertCheckAndMeasure(hdcp.getId(), hdcp); // int result = riskService.insertCheckAndMeasure(hdcp.getId(), hdcp); if (result < 1){ throw new RuntimeException("【***】保存核查点关联信息失败"); } } }); }else{ //查询该风险单元最近的评价计划 RiskEvaluationPlan plan = SpringUtils.getBean(IRiskEvaluationPlanService.class).getRiskEvaluationPlanByRecent(job.getRiskId()); RiskCheckPoint rcpQuery = new RiskCheckPoint(); rcpQuery.setPlanId(plan.getPlanId()); List riskCheckPointList = SpringUtils.getBean(IRiskCheckPointService.class).selectRiskCheckPointList(rcpQuery); riskCheckPointList.forEach(rcp -> { if (rcp != null) { HiddenDangerCheckPoint hdcp = new HiddenDangerCheckPoint(); hdcp.setCreateBy(job.getCreateBy());//创建者 hdcp.setCreateTime(DateUtils.getNowDate());//设置创建时间 hdcp.setCheckPointId(rcp.getCheckPointId()); hdcp.setCheckId(job.getCheckId()); hdcp.setScheduleCheckStatus(TrHiddenDangerCheckConstants.CHECK_STATUS_NOT_CHECK);//排查状态(待排查) hdcp.setId(null); hdcp.setCompanyId(job.getCompanyId()); // hdcp.setDangerSources(TrHiddenDangerCheckConstants.DANGER_SOURCES_SCHEDULE_PRODUCE);//隐患来源(定时器任务生成) // 写入数据库当中 SpringUtils.getBean(IHiddenDangerCheckPointService.class).insertHiddenDangerCheckPoint(hdcp); // todo-2022 现场核查清单对应的核查点信息 System.out.println("【***】开始调用方法,处理核查点附属表"); int result = SpringUtils.getBean(RiskService.class).insertCheckAndMeasure(hdcp.getId(), hdcp); // int result = riskService.insertCheckAndMeasure(hdcp.getId(), hdcp); if (result < 1){ throw new RuntimeException("【***】保存核查点关联信息失败"); } } }); } } } //定时任务 推送 /** * @param hiddenDangerCheck */ public void scheduleJPush(HiddenDangerCheck hiddenDangerCheck) { String title = "您有一项定时任务待执行"; String content = "【隐患排查定时任务】您有一项定时任务待执行"; Map extrasMap = new HashMap(); extrasMap.put("message", "这是一个定时任务通知"); extrasMap.put("methodType", "goToSchedule"); extrasMap.put("hiddenDangerCheck", JSONObject.toJSONString(hiddenDangerCheck)); SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); jpushService = SpringContextUtils.getBean(JpushService.class); //大文本通知栏样式 jpushService.sendPushByAndroidBigText(title, content, 1, content, extrasMap, hiddenDangerCheck.getScheduleCheckUserId().toString()); } /** * 执行方法,由子类重载 * * @param context 工作执行上下文对象 * @param job 系统计划任务 * @throws Exception 执行过程中的异常 */ protected abstract void doExecute(JobExecutionContext context, HiddenDangerCheckJob job) throws Exception; }