双重预防项目-国泰新华二开定制版
16639036659
2022-10-11 7e8d51b6a2d2892c8eca2c8cefd91f7d1b6f8f40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
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<HiddenDangerCheckJob> list = hiddenDangerCheckJobService.selectJobList(hiddenDangerCheckJob);
        return getDataTable(list);
    }
 
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HiddenDangerCheckJob hiddenDangerCheckJob) {
        List<HiddenDangerCheckJob> list = hiddenDangerCheckJobService.selectJobList(hiddenDangerCheckJob);
        ExcelUtil<HiddenDangerCheckJob> util = new ExcelUtil<HiddenDangerCheckJob>(HiddenDangerCheckJob.class);
        return util.exportExcel(list, "定时任务");
    }
 
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    @Transactional
    public AjaxResult remove(String ids) throws SchedulerException {
        hiddenDangerCheckJobService.deleteJobByIds(ids);
 
        //todo-2022 删除job与措施的关联关系
        int result = riskService.deleteJobAndMeasure(ids);
        if (result < 1){
            throw new RuntimeException("删除调度与管控措施的关联失败");
        }
 
        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<TroubleshootType> troubleshootTypeList = troubleshootTypeService.selectTroubleshootTypeList(troubleshootType);
        mmap.put("troubleshootTypeList", troubleshootTypeList);
 
        //获取所在公司人员信息
        if (sysUser != null && sysUser.getCompanyId() != null) {
            User userTemp = new User();
            userTemp.setCompanyId(sysUser.getCompanyId());
            List<User> userList = userService.selectUserList(userTemp);
            mmap.put("userList", userList);
        }
 
        //获取隐患排查基础清单
        RiskList riskList = new RiskList();
        riskList.setRiskType(TrRiskTypeEnum.BASE.getCode());
        riskList.setCompanyId(sysUser.getCompanyId());
        List<RiskList> basicRiskList = riskListService.selectRiskListList(riskList);
        mmap.put("basicRiskList", basicRiskList);
 
 
        return prefix + "/add";
    }
 
    /**
     * 新增保存调度
     * todo-2022 对应work
     */
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    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<Region> 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<Dept> 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(),hiddenDangerCheckJob);{
                                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<TroubleshootType> troubleshootTypeList = troubleshootTypeService.selectTroubleshootTypeList(troubleshootType);
        mmap.put("troubleshootTypeList", troubleshootTypeList);
 
        //获取所在公司人员信息
        if (sysUser != null && sysUser.getCompanyId() != null) {
            User userTemp = new User();
            userTemp.setCompanyId(sysUser.getCompanyId());
            List<User> userList = userService.selectUserList(userTemp);
            mmap.put("userList", userList);
        }
 
        //获取隐患排查基础清单设置
        RiskList riskList = new RiskList();
        riskList.setRiskType(TrRiskTypeEnum.BASE.getCode());
        riskList.setCompanyId(sysUser.getCompanyId());
        List<RiskList> 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
    @Transactional
    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<Region> 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<Dept> 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、先查询基础清单是否有变更
        Long jobIds = hiddenDangerCheckJob.getJobId();
        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());
    }
}