whycxzp
2023-03-01 cb4a5cec6bad3e6273aa41cf6f4d798e058b523c
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.AlarmConstant;
import com.whyc.dto.AlarmDaoFactory;
import com.whyc.dto.Response;
import com.whyc.mapper.PwrdevAlarmMapper;
import com.whyc.mapper.PwrdevAlarmParamMapper;
import com.whyc.pojo.PwrdevAlarm;
import com.whyc.pojo.PwrdevAlarmParam;
import com.whyc.pojo.UserInf;
import com.whyc.util.ActionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
 
@Service
public class PwrdevAlarmService {
 
    @Resource
    private PwrdevAlarmMapper mapper;
 
    @Resource
    private PwrdevAlarmParamMapper paramMapper;
 
    //电源实时告警查询
    public Response getAllPage(PwrdevAlarm pwrdevAlarm){
        //分页信息
        PageHelper.startPage(pwrdevAlarm.getPage().getPageCurr(), pwrdevAlarm.getPage().getPageSize());
        pwrdevAlarm.setUsrId(ActionUtil.getUser().getUId().intValue());
        List<PwrdevAlarm> list=mapper.getAllPage(pwrdevAlarm);
        for (PwrdevAlarm p:list) {
            p.setAlarmName(AlarmDaoFactory.getAllAlarmName(p.getAlmType()));
            if(p.getAlmIndex()!=0){
                p.setAlarmName(p.getAlarmName().replace("N","第"+p.getAlmIndex()+"路"));
            }
        }
        PageInfo pageInfo=new PageInfo(list);
        return new Response().set(1,pageInfo);
    }
    //电源实时告警查询-webSocket
    public Response getAllPageOfWebSocket(PwrdevAlarm pwrdevAlarm){
        //分页信息
        PageHelper.startPage(pwrdevAlarm.getPage().getPageCurr(), pwrdevAlarm.getPage().getPageSize());
        List<PwrdevAlarm> list=mapper.getAllPage(pwrdevAlarm);
        for (PwrdevAlarm p:list) {
            p.setAlarmName(AlarmDaoFactory.getAllAlarmName(p.getAlmType()));
            if(p.getAlmIndex()!=0){
                p.setAlarmName(p.getAlarmName().replace("N","第"+p.getAlmIndex()+"路"));
            }
        }
        PageInfo pageInfo=new PageInfo(list);
        return new Response().set(1,pageInfo);
    }
 
    //电源实时告警查询-webSocket
    @Transactional
    public Response getSendPwrAlarmOfWebSocket(Integer uId){
        List<PwrdevAlarm> list=mapper.getSendPwrdevAlarm(uId);
        Date now = new Date();
        ListIterator<PwrdevAlarm> it = list.listIterator();
        while (it.hasNext()){
            PwrdevAlarm adata = it.next();
            if(adata.getAlmLevel()==1 && adata.getRecordTime()!=null && now.getTime()-adata.getRecordTime().getTime()<24*3600000){
                it.remove();
            }
            if(adata.getAlmLevel()==2 && adata.getRecordTime()!=null && now.getTime()-adata.getRecordTime().getTime()<7*24*3600000){
                it.remove();
            }
            if(adata.getAlmLevel()==3 && adata.getRecordTime()!=null && now.getTime()-adata.getRecordTime().getTime()<30*7*24*3600000){
                it.remove();
            }
            adata.setAlarmName(AlarmDaoFactory.getAllAlarmName(adata.getAlmType()));
            if(adata.getAlmIndex()!=0){
                adata.setAlarmName(adata.getAlarmName().replace("N","第"+adata.getAlmIndex()+"路"));
            }
        }
        return new Response().set(1,list);
 
    }
    //电源实时确认告警
    public Response confirm(int num) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.set("alm_is_confirmed",1);
        wrapper.set("alm_confirmed_time",new SimpleDateFormat(ActionUtil.time_yyyyMMddHHmmss).format(new Date()));
        wrapper.eq("num",num);
        int flag=mapper.update(null,wrapper);
        return new Response().set(flag);
    }
    //电源实时取消告警
    public Response cancle(int num) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.set("alm_is_confirmed",2);
        wrapper.set("alm_confirmed_time",new SimpleDateFormat(ActionUtil.time_yyyyMMddHHmmss).format(new Date()));
        wrapper.eq("num",num);
        int flag=mapper.update(null,wrapper);
        return new Response().set(flag);
    }
 
    //电源实时删除告警
    public Response delete(int num) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("num",num);
        int flag=mapper.delete(wrapper);
        return new Response().set(flag);
    }
    //电源实时删除告警批量
    @Transactional
    public Response deletepro(List<Integer> list) {
        int flag=0;
        for (Integer num:list) {
            UpdateWrapper wrapper = new UpdateWrapper<>();
            // 通过num删除
            wrapper.eq("num",num);
            flag=mapper.delete(wrapper);
        }
        return  new Response().set(flag);
    }
    //电源告警个数
    public Response getAlarmNum() {
        UserInf uinf= ActionUtil.getUser();
        int powerAlarmNum=mapper.getAlarmNum(uinf.getUId().intValue());
        return  new Response().set(1,powerAlarmNum);
    }
    //通信电源告警--认证送检-配电柜专用
    public Response getAllPage2(PwrdevAlarm pwrdevAlarm) {
        //分页信息
        PageHelper.startPage(pwrdevAlarm.getPage().getPageCurr(), pwrdevAlarm.getPage().getPageSize());
        pwrdevAlarm.setUsrId(ActionUtil.getUser().getUId().intValue());
        List<PwrdevAlarm> list=mapper.getAllPage2(pwrdevAlarm);
        List<String> tableList = paramMapper.getParamList();
        //取第一条记录,即param表
        String alarmParamTableName = tableList.get(0);
        List<PwrdevAlarmParam>  paramList=paramMapper.searchAll(alarmParamTableName);
        for (PwrdevAlarm p:list) {
            int almType = p.getAlmType();
            AtomicBoolean matchFlag = new AtomicBoolean(false);
            paramList.stream().forEach(param->{
                if(param.getAlarmId() == almType){
                    p.setAlarmName(param.getAlarmName());
                    matchFlag.set(true);
                    return;
                }
            });
            if(!matchFlag.get()){
                p.setAlarmName("未知告警类型");
            }
            int alarmIndex = p.getAlmIndex();
            if(alarmIndex!=0){
                p.setAlmIndex(alarmIndex);
                p.setAlarmName(p.getAlarmName().replace("N","第"+alarmIndex+"路"));
            }
        }
        PageInfo pageInfo=new PageInfo(list);
        return new Response().set(1,pageInfo);
    }
    //通信电源告警--认证送检-配电柜专用-webSocket
    public Response getAllPage2OfWebSocket(PwrdevAlarm pwrdevAlarm) {
        //分页信息
        PageHelper.startPage(pwrdevAlarm.getPage().getPageCurr(), pwrdevAlarm.getPage().getPageSize());
        List<PwrdevAlarm> list=mapper.getAllPage2(pwrdevAlarm);
        List<String> tableList = paramMapper.getParamList();
        //取第一条记录,即param表
        String alarmParamTableName = tableList.get(0);
        List<PwrdevAlarmParam>  paramList=paramMapper.searchAll(alarmParamTableName);
        for (PwrdevAlarm p:list) {
            int almType = p.getAlmType();
            AtomicBoolean matchFlag = new AtomicBoolean(false);
            paramList.stream().forEach(param->{
                if(param.getAlarmId() == almType){
                    p.setAlarmName(param.getAlarmName());
                    matchFlag.set(true);
                    return;
                }
            });
            if (!matchFlag.get()) {
                p.setAlarmName("未知告警类型");
            }
            int alarmIndex = p.getAlmIndex();
            if (alarmIndex != 0) {
                p.setAlmIndex(alarmIndex);
                p.setAlarmName(p.getAlarmName().replace("N", "第" + alarmIndex + "路"));
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        return new Response().set(1, pageInfo);
    }
 
    /**
     *
     * @param userId
     * @param type 1,查询24小时内;0,查询所有
     * @return
     */
    public List<PwrdevAlarm> getList(Long userId, int type) {
        return mapper.getList(userId,type);
    }
 
    //查询分级告警的个数
    public Response serchByLevel(Integer uId, List<Integer> almTypes) {
        List list = new ArrayList();
        //分四级查询个数
        for (int i = 1; i <= 4; i++) {
            int countLevel = mapper.serchByLevel(i, uId, almTypes);
            list.add(countLevel);
        }
        return new Response().setII(1, list.size() > 0, list, "分级告警数");
    }
 
    /**
     * +告警机房数和占比
     * @return
     * @param userId
     */
    public Response getAcABCAnalysis(int userId){
 
        //交流A/B/C-过压,
        //交流A/B/C-欠压,
        //交流A/B/C-缺相,
        //交流A/B/C-过流
        Map<String,Integer> resultMap = new HashMap<>();
        resultMap.put("过压",0);
        resultMap.put("欠压",0);
        resultMap.put("缺相",0);
        resultMap.put("过流",0);
        try {
            List<Integer> types = Arrays.asList(
                    AlarmConstant.acIn1_over_volA,
                    AlarmConstant.acIn1_over_volB,
                    AlarmConstant.acIn1_over_volC,
                    AlarmConstant.acIn1_under_volA,
                    AlarmConstant.acIn1_under_volB,
                    AlarmConstant.acIn1_under_volC,
                    AlarmConstant.acIn1_less_A,
                    AlarmConstant.acIn1_less_B,
                    AlarmConstant.acIn1_less_C,
                    AlarmConstant.acb1_over_currA,
                    AlarmConstant.acb1_over_currB,
                    AlarmConstant.acb1_over_currC);
 
            List<PwrdevAlarm> data = mapper.getAnalysis(userId, types);
 
            for (int i = 0; i < data.size(); i++) {
                switch (data.get(i).getAlmType()) {
                    case AlarmConstant.acIn1_over_volA:
                    case AlarmConstant.acIn1_over_volB:
                    case AlarmConstant.acIn1_over_volC:
                        resultMap.put("过压", resultMap.get("过压") + data.get(i).getNum().intValue());
                        break;
                    case AlarmConstant.acIn1_under_volA:
                    case AlarmConstant.acIn1_under_volB:
                    case AlarmConstant.acIn1_under_volC:
                        resultMap.put("欠压", resultMap.get("欠压") + data.get(i).getNum().intValue());
                        break;
                    case AlarmConstant.acIn1_less_A:
                    case AlarmConstant.acIn1_less_B:
                    case AlarmConstant.acIn1_less_C:
                        resultMap.put("缺相", resultMap.get("缺相") + data.get(i).getNum().intValue());
                        break;
                    case AlarmConstant.acb1_over_currA:
                    case AlarmConstant.acb1_over_currB:
                    case AlarmConstant.acb1_over_currC:
                        resultMap.put("过流", resultMap.get("过流") + data.get(i).getNum().intValue());
                        break;
                    default:
                }
 
            }
 
            //告警机房总数和比例
            HashMap<String, Integer> map2 = new HashMap<>();
 
            //int alarmStationCount = mapper.getAlarmStationCountSpec(userId,types);
            //int stationCount = infoMapper.getStationCount(userId);
 
            //map2.put("告警机房总数",alarmStationCount);
            //map2.put("告警机房数比例",(int)MathUtil.divide(alarmStationCount,stationCount,2));
 
            List<Map<String, Integer>> mapList = Arrays.asList(resultMap, map2);
            return new Response<>().setII(1, true,mapList,null);
        }catch (Exception e){
            return new Response<>().set(0,false,"发生异常");
        }
    }
}