双重预防项目-国泰新华二开定制版
kongzy
2024-08-14 135a322f44708b1187d37ea4b6859b5d82113e89
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
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<Date> 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<BaseCheckPoint> 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<RiskCheckPoint> 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<String, String> extrasMap = new HashMap<String, String>();
        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;
}