package com.ruoyi.project.tr.HiddenDangerCheckJob.controller; import com.github.pagehelper.util.StringUtil; import com.ruoyi.common.exception.job.TaskException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.doublePrevention.entity.PreventReportConfig; import com.ruoyi.doublePrevention.entity.PreventRiskJobAndMeasure; import com.ruoyi.doublePrevention.enums.SyncEnum; import com.ruoyi.doublePrevention.service.RiskService; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; import com.ruoyi.framework.web.controller.BaseController; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.project.enumerate.TrRiskTypeEnum; import com.ruoyi.project.system.dept.domain.Dept; import com.ruoyi.project.system.dept.service.IDeptService; import com.ruoyi.project.system.user.domain.User; import com.ruoyi.project.system.user.service.IUserService; import com.ruoyi.project.tr.HiddenDangerCheckJob.domain.HiddenDangerCheckJob; import com.ruoyi.project.tr.HiddenDangerCheckJob.service.IHiddenDangerCheckJobService; import com.ruoyi.project.tr.region.domain.Region; import com.ruoyi.project.tr.region.service.IRegionService; import com.ruoyi.project.tr.riskList.domain.RiskList; import com.ruoyi.project.tr.riskList.service.IRiskListService; import com.ruoyi.project.tr.troubleshootType.domain.TroubleshootType; import com.ruoyi.project.tr.troubleshootType.service.ITroubleshootTypeService; import org.quartz.SchedulerException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; /** * 调度任务信息操作处理 * * @author ruoyi */ @Controller @RequestMapping("/tr/hiddenDangerCheckJob") public class HiddenDangerCheckJobController extends BaseController { private String prefix = "tr/hiddenDangerCheckJob"; @Autowired private IHiddenDangerCheckJobService hiddenDangerCheckJobService; @Autowired private ITroubleshootTypeService troubleshootTypeService; @Autowired private IUserService userService; @Autowired private IRiskListService riskListService; @Autowired private IRegionService regionService; @Autowired private IDeptService deptService; @Autowired private RiskService riskService; @GetMapping() public String hiddenDangerCheckJob() { return prefix + "/job"; } @PostMapping("/list") @ResponseBody public TableDataInfo list(HiddenDangerCheckJob hiddenDangerCheckJob) { startPage(); hiddenDangerCheckJob.setCompanyId(getSysUser().getCompanyId()); List list = hiddenDangerCheckJobService.selectJobList(hiddenDangerCheckJob); return getDataTable(list); } @Log(title = "定时任务", businessType = BusinessType.EXPORT) @PostMapping("/export") @ResponseBody public AjaxResult export(HiddenDangerCheckJob hiddenDangerCheckJob) { List list = hiddenDangerCheckJobService.selectJobList(hiddenDangerCheckJob); ExcelUtil util = new ExcelUtil(HiddenDangerCheckJob.class); return util.exportExcel(list, "定时任务"); } @Log(title = "定时任务", businessType = BusinessType.DELETE) @PostMapping("/remove") @ResponseBody public AjaxResult remove(String ids) throws SchedulerException { hiddenDangerCheckJobService.deleteJobByIds(ids); //todo-2022 删除job与措施的关联关系 riskService.deleteJobAndMeasure(ids); return success(); } @GetMapping("/detail/{jobId}") public String detail(@PathVariable("jobId") Long jobId, ModelMap mmap) { mmap.put("name", "hiddenDangerCheckJob"); HiddenDangerCheckJob hiddenDangerCheckJob = hiddenDangerCheckJobService.selectJobById(jobId); mmap.put("hiddenDangerCheckJob", hiddenDangerCheckJob); return prefix + "/detail"; } /** * 任务调度状态修改 */ @Log(title = "定时任务", businessType = BusinessType.UPDATE) @PostMapping("/changeStatus") @ResponseBody public AjaxResult changeStatus(HiddenDangerCheckJob hiddenDangerCheckJob) throws SchedulerException { HiddenDangerCheckJob newJob = hiddenDangerCheckJobService.selectJobById(hiddenDangerCheckJob.getJobId()); newJob.setStatus(hiddenDangerCheckJob.getStatus()); return toAjax(hiddenDangerCheckJobService.changeStatus(newJob)); } /** * 任务调度立即执行一次 */ @Log(title = "定时任务", businessType = BusinessType.UPDATE) @PostMapping("/run") @ResponseBody public AjaxResult run(HiddenDangerCheckJob hiddenDangerCheckJob) throws SchedulerException { hiddenDangerCheckJobService.run(hiddenDangerCheckJob); return success(); } /** * 新增调度 */ @GetMapping("/add") public String add(ModelMap mmap) { //获取排查类型 User sysUser = getSysUser(); TroubleshootType troubleshootType = new TroubleshootType(); // troubleshootType.setCompanyId(sysUser.getCompanyId()); List troubleshootTypeList = troubleshootTypeService.selectTroubleshootTypeList(troubleshootType); mmap.put("troubleshootTypeList", troubleshootTypeList); //获取所在公司人员信息 if (sysUser != null && sysUser.getCompanyId() != null) { User userTemp = new User(); userTemp.setCompanyId(sysUser.getCompanyId()); List userList = userService.selectUserList(userTemp); mmap.put("userList", userList); } //获取隐患排查基础清单 RiskList riskList = new RiskList(); riskList.setRiskType(TrRiskTypeEnum.BASE.getCode()); riskList.setCompanyId(sysUser.getCompanyId()); List basicRiskList = riskListService.selectRiskListList(riskList); mmap.put("basicRiskList", basicRiskList); return prefix + "/add"; } /** * 新增保存调度 * todo-2022 对应work */ @Log(title = "定时任务", businessType = BusinessType.INSERT) @PostMapping("/add") @ResponseBody public AjaxResult addSave(@Validated HiddenDangerCheckJob hiddenDangerCheckJob) throws SchedulerException, TaskException { if (!StringUtils.isEmpty(hiddenDangerCheckJob.getExecuteUserIdString())) { String[] executeUserIdArray = hiddenDangerCheckJob.getExecuteUserIdString().split(","); String[] executeUserNameArray = hiddenDangerCheckJob.getExecuteUserNameString().split(","); for (int m = 0; m < executeUserIdArray.length; m++) { if(!StringUtil.isEmpty(executeUserIdArray[m])) { String executeUserId = executeUserIdArray[m]; String executeUserName = executeUserNameArray[m]; hiddenDangerCheckJob.setExecuteUserId(Long.valueOf(executeUserId)); hiddenDangerCheckJob.setExecuteUserName(executeUserName); for (int i = 0; i < hiddenDangerCheckJob.getStartTimeList().size(); i++) { if (hiddenDangerCheckJob.getStartTimeList().get(i) != null) { hiddenDangerCheckJob.setJobId(null); hiddenDangerCheckJob.setStartTime(hiddenDangerCheckJob.getStartTimeList().get(i)); hiddenDangerCheckJob.setCreateBy(getSysUser().getLoginName()); hiddenDangerCheckJob.setCreateUserId(getSysUser().getUserId());//创建人ID hiddenDangerCheckJob.setCreateUserName(getSysUser().getUserName());//创建人名字 hiddenDangerCheckJob.setCompanyId(getSysUser().getCompanyId());//所属公司 String cronExpression = getCronExpression(hiddenDangerCheckJob);//执行表达式 if (!StringUtils.isEmpty(cronExpression)) { hiddenDangerCheckJob.setInvokeTarget("hiddenDangerCheckExecuteTask.noParams"); hiddenDangerCheckJob.setCronExpression(cronExpression);//设置定时任务执行表达式 hiddenDangerCheckJob.setMisfirePolicy("1"); hiddenDangerCheckJob.setConcurrent("0"); hiddenDangerCheckJob.setStatus("0"); } //设置基础清单 部门/区域 均为 公司级 if ("1".equals(hiddenDangerCheckJob.getCheckType())) { hiddenDangerCheckJob.setRiskType(TrRiskTypeEnum.BASE.getCode().toString()); //获取公司级区域 Region region = new Region(); region.setParentId(0L); region.setCompanyId(getSysUser().getCompanyId()); List regionList = regionService.selectRegionList(region); if (regionList.size() > 0) { Region r = regionList.get(0); hiddenDangerCheckJob.setRiskPlaceId(r.getRegionId().toString()); hiddenDangerCheckJob.setRiskPlaceName(r.getRegionName()); } //获取公司级部门 Dept dept = new Dept(); dept.setParentId(0L); dept.setCompanyId(getSysUser().getCompanyId()); List deptList = deptService.selectDeptList(dept); if (deptList.size() > 0) { Dept d = deptList.get(0); hiddenDangerCheckJob.setRiskDeptId(d.getDeptId().toString()); hiddenDangerCheckJob.setRiskDeptName(d.getDeptName()); } } hiddenDangerCheckJobService.insertJob(hiddenDangerCheckJob); //todo- 2022 保存job与管控措施的对应关系 hiddenDangerCheckJob.getJobId(); int result = riskService.insertJobAndMeasure(hiddenDangerCheckJob.getJobId());{ if (result < 0 ){ throw new RuntimeException("保存job与管控措施的对应关系失败"); } } } } } } } return toAjax(1); } /** * 获取 执行表达式 */ public String getCronExpression(HiddenDangerCheckJob hiddenDangerCheckJob) { //获取日期的年月日星期的值 Calendar calendar = Calendar.getInstance();//日历对象 calendar.setTime(hiddenDangerCheckJob.getStartTime());//设置执行时间 String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份 int month = calendar.get(Calendar.MONTH) + 1;//获取月份 String monthStr = month + ""; String weekStr = getCurrDayOfWeek(hiddenDangerCheckJob.getStartTime());//获取周 int day = calendar.get(Calendar.DATE);//获取日 String dayStr = day + ""; String hourStr = calendar.get(Calendar.HOUR_OF_DAY) + "";//获取小时 String minuteStr = calendar.get(Calendar.MINUTE) + "";//获取分钟 String secondStr = calendar.get(Calendar.SECOND) + "";//获取秒 //隐患排查周期数 Integer typeCycleNum = hiddenDangerCheckJob.getTroubleshootTypeCycleNum().intValue(); String cronExpression = ""; if (hiddenDangerCheckJob.getTroubleshootTypeCycleType() == 1) {//小时 int initHour = Integer.valueOf(hourStr) % typeCycleNum; cronExpression = secondStr + " " + minuteStr + " " + initHour + "/" + typeCycleNum + " * * ? *"; } else if (hiddenDangerCheckJob.getTroubleshootTypeCycleType() == 2) {//日 if ("工作日隐患排查(周一到周五)".equals(hiddenDangerCheckJob.getTroubleshootTypeName())) { cronExpression = secondStr + " " + minuteStr + " " + hourStr + " ? * Mon-Fri"; } else { cronExpression = secondStr + " " + minuteStr + " " + hourStr + " */" + typeCycleNum + " * ? *"; } } else if (hiddenDangerCheckJob.getTroubleshootTypeCycleType() == 3) {//周 cronExpression = secondStr + " " + minuteStr + " " + hourStr + " ? * " + weekStr; } else if (hiddenDangerCheckJob.getTroubleshootTypeCycleType() == 4) {//月 int initMonth = Integer.valueOf(monthStr) % typeCycleNum; initMonth = (initMonth == 0 ? (initMonth + typeCycleNum) : initMonth); cronExpression = secondStr + " " + minuteStr + " " + hourStr + " " + dayStr + " " + initMonth + "/" + typeCycleNum + " ? *"; } else if (hiddenDangerCheckJob.getTroubleshootTypeCycleType() == 5) {//年 cronExpression = secondStr + " " + minuteStr + " " + hourStr + " " + dayStr + " " + monthStr + " ? " + yearStr + "/" + "" + typeCycleNum; } return cronExpression; } //获取今天周几 public static String getCurrDayOfWeek(Date date) { // SimpleDateFormat dateFm = new SimpleDateFormat("yyyy年MM月dd日 EEEE", Locale.ENGLISH); SimpleDateFormat dateFm = new SimpleDateFormat("E", Locale.ENGLISH); return dateFm.format(date); } /** * 修改调度 */ @GetMapping("/edit/{jobId}") public String edit(@PathVariable("jobId") Long jobId, ModelMap mmap) { //获取排查类型 User sysUser = getSysUser(); TroubleshootType troubleshootType = new TroubleshootType(); // troubleshootType.setCompanyId(sysUser.getCompanyId()); List troubleshootTypeList = troubleshootTypeService.selectTroubleshootTypeList(troubleshootType); mmap.put("troubleshootTypeList", troubleshootTypeList); //获取所在公司人员信息 if (sysUser != null && sysUser.getCompanyId() != null) { User userTemp = new User(); userTemp.setCompanyId(sysUser.getCompanyId()); List userList = userService.selectUserList(userTemp); mmap.put("userList", userList); } //获取隐患排查基础清单设置 RiskList riskList = new RiskList(); riskList.setRiskType(TrRiskTypeEnum.BASE.getCode()); riskList.setCompanyId(sysUser.getCompanyId()); List basicRiskList = riskListService.selectRiskListList(riskList); mmap.put("basicRiskList", basicRiskList); mmap.put("hiddenDangerCheckJob", hiddenDangerCheckJobService.selectJobById(jobId)); return prefix + "/edit"; } /** * 修改保存调度 */ @Log(title = "定时任务", businessType = BusinessType.UPDATE) @PostMapping("/edit") @ResponseBody public AjaxResult editSave(@Validated HiddenDangerCheckJob hiddenDangerCheckJob) throws SchedulerException, TaskException { HiddenDangerCheckJob tempJob = hiddenDangerCheckJobService.selectJobById(hiddenDangerCheckJob.getJobId()); hiddenDangerCheckJob.setCreateBy(tempJob.getCreateBy()); hiddenDangerCheckJob.setCompanyId(getSysUser().getCompanyId());//所属公司 hiddenDangerCheckJob.setUpdateBy(getSysUser().getLoginName()); String cronExpression = getCronExpression(hiddenDangerCheckJob); if (!StringUtils.isEmpty(cronExpression)) { hiddenDangerCheckJob.setInvokeTarget("hiddenDangerCheckExecuteTask.noParams"); hiddenDangerCheckJob.setCronExpression(cronExpression); hiddenDangerCheckJob.setMisfirePolicy("1"); hiddenDangerCheckJob.setConcurrent("0"); hiddenDangerCheckJob.setStatus("0"); } //设置基础清单 部门/区域 均为 公司级 if ("1".equals(hiddenDangerCheckJob.getCheckType())) { hiddenDangerCheckJob.setRiskType(TrRiskTypeEnum.BASE.getCode().toString()); //获取公司级区域 Region region = new Region(); region.setParentId(0L); region.setCompanyId(getSysUser().getCompanyId()); List regionList = regionService.selectRegionList(region); if (regionList.size() > 0) { Region r = regionList.get(0); hiddenDangerCheckJob.setRiskPlaceId(r.getRegionId().toString()); hiddenDangerCheckJob.setRiskPlaceName(r.getRegionName()); } //获取公司级部门 Dept dept = new Dept(); dept.setParentId(0L); dept.setCompanyId(getSysUser().getCompanyId()); List deptList = deptService.selectDeptList(dept); if (deptList.size() > 0) { Dept d = deptList.get(0); hiddenDangerCheckJob.setRiskDeptId(d.getDeptId().toString()); hiddenDangerCheckJob.setRiskDeptName(d.getDeptName()); } } //todo- 2022 保存job与管控措施的对应关系 //1、先查询基础清单是否有变更 HiddenDangerCheckJob jobById = hiddenDangerCheckJobService.getJobById(hiddenDangerCheckJob.getJobId()); if (!jobById.getRiskId().equals(hiddenDangerCheckJob.getRiskId())){ // 2、若有变更,再次添加关联关系 riskService.updateJobAndMeasure(hiddenDangerCheckJob); } return toAjax(hiddenDangerCheckJobService.updateJob(hiddenDangerCheckJob)); } /** * 校验cron表达式是否有效 */ @PostMapping("/checkCronExpressionIsValid") @ResponseBody public boolean checkCronExpressionIsValid(HiddenDangerCheckJob hiddenDangerCheckJob) { return hiddenDangerCheckJobService.checkCronExpressionIsValid(hiddenDangerCheckJob.getCronExpression()); } }