whyclxw
6 天以前 d61a41cffb4a70aa3bef833bf12542016d1cce36
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.DevAlarmEnum;
import com.whyc.constant.PowerAlarmEnum;
import com.whyc.dto.AlarmDto;
import com.whyc.dto.Param.AlmAnalyseDto;
import com.whyc.dto.Real.AlmDto;
import com.whyc.dto.Real.RealDateDTO;
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.mapper.PwrdevAlarmMapper;
import com.whyc.pojo.db_alarm.DevalarmData;
import com.whyc.pojo.db_data_history.BattRealdataId;
import com.whyc.pojo.db_data_history.PwrdevHistorydataId;
import com.whyc.pojo.db_param.AlmAnalysisParam;
import com.whyc.pojo.db_param.AppParam;
import com.whyc.pojo.db_pwrdev_alarm.PwrdevAlarm;
import com.whyc.pojo.db_pwrdev_alarm.PwrdevAlarmParam;
import com.whyc.pojo.web_site.AlarmInspection;
import com.whyc.util.ActionUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
import static java.lang.Thread.sleep;
 
@Service
public class PwrdevAlarmService {
    @Autowired(required = false)
    private PwrdevAlarmMapper mapper;
    @Autowired
    private AppParamService appParamService;
    @Autowired
    private AlmAnalysisParamService almAnalysisParamService;
    @Autowired
    private SubTablePageInfoService subTablePageInfoService;
    //获取电源实时告警信息
    public Response getPwrAlmReal(AlmDto almDto) {
        PageHelper.startPage(almDto.getPageNum(),almDto.getPageSize());
        List<PwrdevAlarm> list=mapper.getPwrAlmReal(almDto);
        list.forEach(data->{
            data.setAlmName(PowerAlarmEnum.getValue(data.getAlmId()));
        });
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"获取电源实时告警信息");
    }
    //确认电源告警
    public Response updatePwrConfrim(Integer num) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.set("alm_is_confirmed",1);
        wrapper.set("alm_confirmed_time",new SimpleDateFormat(ActionUtil.time_yyyyMMddHHmmss).format(new Date()));
        // 通过num修改
        wrapper.eq("num",num);
        int flag=mapper.update((PwrdevAlarm) ActionUtil.objeNull,wrapper);
        return  new Response().set(1,flag>0,flag>0?"确认成功":"确认失败");
    }
    //获取一级未处理实时告警数
    public Integer getPwrAlmNum(Integer powerId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("power_id",powerId);
        wrapper.eq("alm_is_confirmed",0);
        wrapper.eq("alm_level",1);
        List<DevalarmData> list=mapper.selectList(wrapper);
        return list!=null?list.size():0;
    }
    //获取电源告警类型(下拉)
    public Response getPwrAlmIdType() {
        Map<String,Map<Integer,String>> map= PowerAlarmEnum.getOpInfo();
        return new Response().setII(1,true,map,"获取电源告警类型(下拉)");
    }
 
    public List<AlarmInspection> getListGreatThan(Long id) {
        List<AlarmInspection> listGreatThan = mapper.getListGreatThan(id);
        Date now = new Date();
        listGreatThan.forEach(data->{
            data.setType(1);
            data.setIsExist(1);
            data.setCreateTime(now);
        });
        return listGreatThan;
    }
 
    public List<Long> getNumListInDB(List<Long> powerAlarmNumList) {
        QueryWrapper<PwrdevAlarm> query = Wrappers.query();
        query.select("num");
        query.in("num",powerAlarmNumList);
        return mapper.selectList(query).stream().map(PwrdevAlarm::getNum).collect(Collectors.toList());
    }
 
    public List<AlarmDto> getListByUserId(Integer userId,Integer alarmLevel) {
        return mapper.getListByUserId(userId,alarmLevel);
    }
 
    //预警分析管理-电源告警
    public Response getPwrtAlmAnalyse(AlmAnalyseDto dto) {
        PageHelper .startPage(dto.getPageNum(),dto.getPageSize());
        List<PwrdevAlarm> list=mapper.getPwrtAlmAnalyse(dto);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"电源告警分析");
    }
    //预警分析管理-电源主属性和分析属性
    public Response getPwrHisRealInAlm(Integer powerId, String startTime, Integer almId,Integer intervalTime) throws ParseException, InterruptedException {
        //获取告警almId对应的分析配置属性值
        AlmAnalysisParam almAnalysisParam= almAnalysisParamService.getAnalysisParam(almId);
        List<PwrdevHistorydataId> dataList = new LinkedList<>();
        Integer cycleTime=0;
        //获取预警分析周期阈值
        if(intervalTime==null){
            AppParam appParam = appParamService.getAlarmAnalysisCycle();
            cycleTime=appParam.getParamValue().intValue();
        }else{
            cycleTime=intervalTime;
        }
        Date cyscleDate=ActionUtil.getDateAdd(ThreadLocalUtil.parse(startTime,1),cycleTime*(-1));
        //获取cyscleDate到现在所有的历史实时数据
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(cyscleDate,new Date());
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            pool.execute(() -> {
                int finalII = finalI;
                PwrdevHistorydataId pwrHis = new PwrdevHistorydataId();
                pwrHis.setRecordDatetime(monthTimeList.get(finalII).get(0));
                pwrHis.setRecordDatetime1(monthTimeList.get(finalII).get(1));
                String table = powerId + "_" + ThreadLocalUtil.format(pwrHis.getRecordDatetime(),2);
                pwrHis.setTableName(table);//表名时间格式部分
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<PwrdevHistorydataId> list = new ArrayList();
                if (tableNum > 0) {
                    list = subTablePageInfoService.getPwrHisRealInAlm(pwrHis);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrdevHistorydataId::getRecordDatetime)).collect(Collectors.toList());
        return new Response().setIII(1,true, dataListSorted,almAnalysisParam,"预警分析管理-电源主属性和分析属性");
    }
}