whycxzp
2021-11-02 7bae5d81e8cf185beebfa33d1b6816a46a9c2e93
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
package com.whyc.schedule;
 
import com.whyc.constant.AlarmConstant;
import com.whyc.constant.SuperConstant;
import com.whyc.constant.WorkflowEnum;
import com.whyc.pojo.WorkflowAlarm;
import com.whyc.pojo.WorkflowMain;
import com.whyc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.awt.image.ImageWatched;
 
import javax.jws.Oneway;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
 
/**
 * 定时任务统一这里开启
 */
 
@Component
@EnableScheduling //开启定时任务
@EnableAsync //开启多线程
public class CustomTask {
 
    @Autowired
    private AlarmDataService service;
 
    @Autowired
    private BattalarmDataService battAlarmService;
 
    @Autowired
    private DeviceAlarmDataService deviceAlarmService;
 
    @Autowired
    private WorkflowMainService mainService;
 
    @Autowired
    private WorkflowAlarmService workflowAlarmService;
 
    /**根据电池告警和设备告警,生成告警工单和工作流主表记录*/
    @Scheduled(cron = SuperConstant.SCHEDULE_PER_MINUTE)
    @Transactional
    public void generateWorkflowAlarmAndMain(){
        System.out.println("CustomTask:当前任务执行时间为:"+new SimpleDateFormat("HHmmss").format(new Date()));
        //从电池/设备告警表获取记录
        List<WorkflowAlarm> battAlarmList = battAlarmService.getAlarmList();
        List<WorkflowAlarm> deviceAlarmList = deviceAlarmService.getAlarmList();
        battAlarmList.addAll(deviceAlarmList);
        List<WorkflowAlarm> alarmList = battAlarmList;
        alarmList.stream().forEach(alarm->alarm.setAlarmName(AlarmConstant.getAllAlarmName(Integer.parseInt(alarm.getAlarmName()))));
        //将记录生成到工作流主表和告警工单表中
        if(alarmList.size()!=0) {
            workflowAlarmService.add(alarmList);
            //example:WF-1-20211101-00001
            String ymd = new SimpleDateFormat("yyyyMMdd").format(new Date());
            final String[] nextSequence = {mainService.getNextSequence()};
            List<WorkflowMain> workflowMainList = new LinkedList<>();
            alarmList.stream().forEach(workflowAlarm -> {
                WorkflowMain temp = new WorkflowMain();
                temp.setOrderId("WF-1-" + ymd + "-" + nextSequence[0]);
                nextSequence[0] = String.format("%05d", Integer.parseInt(nextSequence[0]) + 1);
                temp.setCreateTime(new Date());
                temp.setCreateUserId(workflowAlarm.getUserId());
                if(workflowAlarm.getDeviceId()==null){
                    temp.setTaskDesc("["+workflowAlarm.getStationName()+"]的"+workflowAlarm.getBattGroupName()+"在"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(workflowAlarm.getAlarmTime())+"发生"+workflowAlarm.getAlarmName());
                }else{
                    temp.setTaskDesc("["+workflowAlarm.getStationName()+"]在"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(workflowAlarm.getAlarmTime())+"发生"+workflowAlarm.getAlarmName());
                }
                temp.setAlarmOrderId(workflowAlarm.getId());
                temp.setStatus(WorkflowEnum.MAIN_STATUS_MANUAL.getValue());
                temp.setAuto(WorkflowEnum.MAIN_STATUS_MANUAL.getValue());
                workflowMainList.add(temp);
            });
            mainService.add(workflowMainList);
 
            //将原始数据状态设置为已生成工单,即work_flag=1
            if (battAlarmList.size() != 0) {
                battAlarmService.updateWorkFlag(battAlarmList);
            }
            if (deviceAlarmList.size() != 0) {
                deviceAlarmService.updateWorkFlag(deviceAlarmList);
            }
        }
    }
 
}