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
403
404
405
406
407
408
409
410
411
412
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.*;
import com.whyc.dto.paramter.GroupTestCapPar;
import com.whyc.dto.result.GroupTestCapRes;
import com.whyc.dto.result.ReportBattTestDTO;
import com.whyc.mapper.*;
import com.whyc.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class BatttestdataInfService {
 
    @Resource
    private BatttestdataInfMapper mapper;
 
    @Resource
    private BattTestDataStopMapper stopMapper;
 
    @Resource
    private BattParamLowMapper battParamLowMapper;
 
    @Resource
    AlarmParamMapper alarmParamMapper;
 
    @Resource
    BattInfMapper binfMapper;
 
    //在线监测-历史监控-获取充放电记录
    public Response searchBattTestInfDataById(int battGroupId, int fbsDeviceId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("BattGroupId", battGroupId);
        wrapper.eq("data_available", 1);
        wrapper.orderByDesc("test_starttime");
        int judgeNum = mapper.judge(battGroupId);
        if (judgeNum > 0) {
            List<BatttestdataInf> list = mapper.selectList(wrapper);
            list.stream().forEach(tinf -> {
                //放电终止原因
                if (fbsDeviceId / 100000 == 6185) {
                    //61850设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_6185(tinf.getTestStoptype()));
                } else if (fbsDeviceId / 100000 == 4016) {
                    //6度设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_4016(tinf.getTestStoptype()));
                } else if (fbsDeviceId / 100000 == 9110) {
                    //假负载设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_9110(tinf.getTestStoptype()));
                } else if ((fbsDeviceId / 100000 == 9120) || (fbsDeviceId / 100000 == 9140)) {
                    // 逆变设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_9120(tinf.getTestStoptype()));
                } else if (fbsDeviceId / 100000 == 8059) {
                    //一体机设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_8059(tinf.getTestStoptype()));
                } else if (fbsDeviceId / 100000 == 6087) {
                    //电操核容设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_6087(tinf.getTestStoptype()));
                } else if (fbsDeviceId / 100000 == 4830) {
                    //FBO4830设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_4830(tinf.getTestStoptype()));
                } else if (fbsDeviceId / 100000 == 4831) {
                    //FBO4831设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_4831(tinf.getTestStoptype()));
                } else if (fbsDeviceId / 100000 == 4815) {
                    //FBO4815设备放电停止原因
                    tinf.setTestStoptypeReason(BattTestData.getStopType_4815(tinf.getTestStoptype()));
                } else {
                    tinf.setTestStoptypeReason(BattTestData.getStopType(tinf.getTestStarttype(), tinf.getTestStoptype()));
                }
            });
            PageInfo pageInfo=new PageInfo(list);
            return new Response().set(1,pageInfo);
        }else{
            return new Response().set(0);
        }
    }
 
    //最后一次核容放电数据
    public Response searchBattLastHrDataById(int battGroupId, int devId) {
        PageHelper.startPage(1, 1);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("BattGroupId", battGroupId);
        wrapper.eq("data_available", 1);
        wrapper.eq("test_type", 3);
        wrapper.eq("test_starttype", 3);
        wrapper.orderByDesc("test_starttime");
        BatttestdataInf tinf = mapper.selectOne(wrapper);
        List<BattTestDataStop> list = new ArrayList<BattTestDataStop>();
        if (tinf != null) {
            //放电终止原因
            if (devId / 100000 == 6185) {
                //61850设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_6185(tinf.getTestStoptype()));
            } else if (devId / 100000 == 4016) {
                //6度设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_4016(tinf.getTestStoptype()));
            } else if (devId / 100000 == 9110) {
                //假负载设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_9110(tinf.getTestStoptype()));
            } else if ((devId / 100000 == 9120) || (devId / 100000 == 9140)) {
                // 逆变设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_9120(tinf.getTestStoptype()));
            } else if (devId / 100000 == 8059) {
                //一体机设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_8059(tinf.getTestStoptype()));
            } else if(devId / 100000 == 6087) {
                //电操核容设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_6087(tinf.getTestStoptype()));
            }else if (devId / 100000 == 4830) {
                // FBO4830设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_4830(tinf.getTestStoptype()));
            } else if (devId / 100000 == 4831) {
                // FBO4830设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_4831(tinf.getTestStoptype()));
            } else if (devId / 100000 == 4815) {
                //FBO4815设备放电停止原因
                tinf.setTestStoptypeReason(BattTestData.getStopType_4815(tinf.getTestStoptype()));
            } else {
                tinf.setTestStoptypeReason(BattTestData.getStopType(tinf.getTestStarttype(), tinf.getTestStoptype()));
            }
            list = stopMapper.getTestDataStop(tinf.getBattGroupId(), tinf.getTestStarttime());
            tinf.setBatttestdataList(list);
        }
        return new Response().setII(1, tinf != null ? true : false, tinf, "");
    }
 
    //报表分析使用
    public List<BatttestdataInf> searchByTestType(BatttestdataInf tinf){
        List<BatttestdataInf> list = mapper.searchByTestType(tinf);
        float cap=0f;//实际容量
        float restcap=0f;//剩余容量
        for (BatttestdataInf binf:list) {
            int hourRate = BattCapFactory.GetHourRate(tinf.getTestCap(),binf.getTestCurr());
            cap = (float) BattCapFactory.GetMonomerCap(tinf.getTestCap(), hourRate,binf.getTestCap(), binf.getMaxMonvol(), binf.getMinMonvol(), tinf.getGroupVol(), BattCapFactory.CapType_Real);
            restcap=(float) BattCapFactory.GetMonomerCap(tinf.getTestCap(), hourRate,binf.getTestCap(), binf.getMaxMonvol(), binf.getMinMonvol(), tinf.getGroupVol(), BattCapFactory.CapType_Rest);
            binf.setGroupVol(cap);;
            binf.setMonVol(restcap);
        }
        return list;
    }
    //1.4电池组性能评估(根据电池组id查询所有的放电记录求出放电总次数,最高历史容量,最低历史容量,平均容量,最新测试容量)
    public List searchDischargeTest(BatttestdataInf tinf){
        List<BatttestdataInf> list = mapper.searchDischargeTest(tinf);
        int sum=0;//总测试次数
        float cap=0f;//实际容量
        float allCap=0f;//总容量
        float maxCap=0f;//最高容量
        float minCap=10000f;//最低容量
        float avgCap=0f;//平均容量
        float lastCap=0f;//最近测试容量
        for (int i = 0; i <list.size() ; i++) {
            BatttestdataInf binf = list.get(i);
            int hourRate = BattCapFactory.GetHourRate(tinf.getTestCap(),binf.getTestCurr());
            cap = (float) BattCapFactory.GetMonomerCap(tinf.getTestCap(), hourRate,binf.getTestCap(), binf.getMaxMonvol(), binf.getMinMonvol(), tinf.getGroupVol(), BattCapFactory.CapType_Real);
            if(sum==0){
                lastCap=cap;
            }
            if(maxCap<=cap){
                maxCap=cap;//最大
            }
            if(minCap>=cap){
                minCap=cap;//最小
            }
            allCap+=cap;
            sum++;
        }
        if(sum!=0){
            avgCap=allCap/sum;//平均容量
        }else{
            avgCap=0;
            minCap=0;
        }
        List list1 = new ArrayList<>();
        list1.add(tinf.getBattGroupId());
        list1.add(sum);
        list1.add(maxCap);
        list1.add(avgCap);
        list1.add(minCap);
        list1.add(lastCap);
        return list1;
    }
 
    public Response searchBattLifeNow(int pageNum,int pageSize,Battinf binf){
        PageHelper.startPage(pageNum,pageSize);
        List<BattState> list = mapper.searchBattLifeNow(binf);
        PageInfo<BattState> pageInfo = new PageInfo<>(list);
        return new Response().set(1,pageInfo,"查询成功");
    }
 
    public Response searchBattLife(int pageNum,int pageSize,Battinf binf){
        //PageHelper.startPage(pageNum,pageSize);
        List<ReportBattTestDTO> list = mapper.searchBattLife(binf);
        float param = 0;//容量有效参数
        double STDAH = 0;//标存容量
        BattParamLow capLow=new BattParamLow();
        capLow.setLowType(BattCapFactory.CapType_type);;
        capLow.setLowNametype(BattCapFactory.CapType_name);
        capLow.setLowMethod(BattCapFactory.CapType_method);
        List<BattParamLow> listC = battParamLowMapper.serchByLow(capLow);
        if (listC!=null && listC.size()>0){
            param = listC.get(listC.size()-1).getLowValue();
        }
        int flag=0;//该次放电是否有效判断
        List<ReportBattTestDTO> listE=new ArrayList();//存放放电有效的测试
        //当前页记录列表
        LinkedList<ReportBattTestDTO> data = new LinkedList<>();
        //对结果进行分页
        Page page = new Page();
        if (list!=null &&list.size()>0){
            for(int i=0;i<list.size();i++){
                if (list.get(i).getBinf().getMonCapStd() != null) {
                    STDAH = list.get(i).getBinf().getMonCapStd();
                }
                float testCap=list.get(i).getTdata().getTestCap();//此次放电量统计
                //System.out.println(list.get(i).getTdata().getBattGroupId()+"  "+list.get(i).getTdata().getTest_record_count()+"   "+testCap+"  "+param+"  "+STDAH);
                flag=BattCapFactory.Judge(testCap, param, STDAH);
                if(flag==1){
                    if(listE.size()>0){
                        int tdata_battgroupid=list.get(i).getTdata().getBattGroupId();
                        int e_battgroupid=listE.get(listE.size()-1).getTdata().getBattGroupId();
                        if(tdata_battgroupid!=e_battgroupid){
                            listE.add(list.get(i));
                        }else{
                            continue;
                        }
                    }else{
                        listE.add(list.get(i));
                    }
                    flag=0;
                }
            }
 
            page.setPageCurr(pageNum);
            page.setPageSize(pageSize);
            //总页数
            page.setPageAll(listE.size());
            if(listE.size()>pageSize*pageNum) {
                for (int i = (pageNum-1)*pageSize; i < pageSize*pageNum; i++) {
                    data.add(listE.get(i));
                }
            }else {
                int a1 = (pageNum - 1) * pageSize;
                int a2 = (listE.size() - ((pageNum - 1) * pageSize));
                for (int i = (pageNum - 1) * pageSize; i < listE.size(); i++) {
                    data.add(listE.get(i));
                }
            }
        }
        page.setData(data);
        return new Response().set(1, page, "查询成功");
    }
 
    //统计时间段内设备的测试容量
    public Response searchGroupTestCap(GroupTestCapPar groupCapPar) {
        PageHelper.startPage(groupCapPar.getPageCurr(), groupCapPar.getPageSize());
        List<GroupTestCapRes> list = mapper.searchGroupTestCap(groupCapPar);
        PageInfo pageInfo = new PageInfo(list);
        return new Response().setII(1, list.size() > 0 ? true : false, pageInfo, "统计时间段内设备的测试容量");
    }
 
    //管理员首页头部信息加入
    public Map<String, Integer> getYearAnalysis(int userId) {
        //本年
        List<BatttestdataInf> listYear = mapper.selectYear(userId);
        Map<String, Integer> map = analysisTestData(listYear);
        return map;
    }
 
    //测试信息
    @Transactional
    public Response getTestdataInfAnalysis(int userId) {
        try {
            Map<String, Object> map = new HashMap<>();
            //本月数据
            List<BatttestdataInf> listMonth = mapper.selectMonth(userId);
            map.put("month", analysisTestData(listMonth));
            //本季度
            List<BatttestdataInf> listQuarter = mapper.selectQuarter(userId);
            map.put("quarter", analysisTestData(listQuarter));
            //本年
            List<BatttestdataInf> listYear = mapper.selectYear(userId);
            map.put("year", analysisTestData(listYear));
            return new Response().setII(1, true, map, "测试信息");
        } catch (Exception e) {
            return new Response().set(1, false, "发生异常:" + e.getCause());
        }
    }
 
    //管理员首页统计测试信息
    public Map<String, Integer> analysisTestData(List<BatttestdataInf> list) {
        Map<String, Integer> map = new HashMap<>();
        int disNum = 0;//放电
        int chNum = 0;//充电
        int hrdisNum = 0;//核容放电
        int hrchNum = 0;//核容充电
        Map<Integer, List<BatttestdataInf>> listDisMap = list.stream().collect(Collectors.groupingBy(tinf -> tinf.getTestType()));
        for (Integer dis : listDisMap.keySet()) {
            //放电
            if (dis == 3) {
                disNum = listDisMap.get(dis).size();
            }
            //充电
            if (dis == 2) {
                chNum = listDisMap.get(dis).size();
            }
        }
        Map<String, List<BatttestdataInf>> listMap = list.stream().collect(Collectors.groupingBy(tinf -> tinf.getTestType() + "_" + tinf.getTestStarttype()));
        for (String type : listMap.keySet()) {
            //核容放电
            if (type.equals("3_3")) {
                hrdisNum = listMap.get(type).size();
 
            }
            //核容充电
            if (type.equals("2_3")) {
                hrchNum = listMap.get(type).size();
            }
        }
        map.put("hrdisNum", hrdisNum);
        map.put("hrchNum", hrchNum);
        map.put("jcdisNum", disNum - hrdisNum);
        map.put("jcchNum", chNum - hrchNum);
        return map;
    }
 
    //查询今天所有正在放电的机房总数
    public int getPlanStaticToday(int userId) {
        int num = mapper.getPlanStaticToday(userId);
        return num;
    }
 
    //今日放电站点记录
    public Response getChargeStaticToday(int userId) {
        List<BatttestdataInf> list = mapper.getChargeStaticToday(userId);
        return new Response().setII(1, true, "", "今日放电统计");
    }
 
    //本季度核容放电电池组(testdate_inf中test_type=3,test_startType=3)
    public int getHrQuarter(int userId) {
        //本季度
        int hrQuarter = mapper.getHrQuarter(userId);
        return hrQuarter;
    }
 
    //蓄电池组优劣分析(用蓄电池组组后评估的统计)
    public Response getGroupAnalysis(int userId) {
        try {
            //0.查询劣化(告警)和损坏(更换)的阈值
            QueryWrapper<AlarmParam> alarmWrapper = new QueryWrapper();
            alarmWrapper.and(wrapper -> {
                return wrapper.eq("alm_name", "Batt_Alarm_Type_CapAlarm").or().eq("alm_name", "Batt_Alarm_Type_CapChange");
            });
            alarmWrapper.orderByAsc("alm_id");
            List<AlarmParam> paramList = alarmParamMapper.selectList(alarmWrapper);
            float capAlarm = 0f;
            float capChange = 0f;
            if (paramList != null && paramList.size() > 0) {
                capAlarm = paramList.get(0).getAlmLowCoe();//劣化参数0.8
                capChange = paramList.get(1).getAlmLowCoe();//损坏参数0.6
            } else {
                capAlarm = 0.8f;
                capChange = 0.6f;
            }
            //1.查出符合条件的电池组(核容数据)按照时间倒叙排序
            List<BatttestdataInf> list = mapper.getGroupAnalysis(userId);
            //2.分组取出每个电池组的最新一笔数据(取电池组放电数据的第一个)
            //Map<String, String> map = new HashedMap();
            //小于capAlarm
            int alarmNum = 0;
            //小于capChange
            int changeNum = 0;
            String battGroupId = "0";
            if (list != null && list.size() > 0) {
                for (BatttestdataInf inf : list) {
                    if (!battGroupId.equals(inf.getBattGroupId().toString())) {
                        int hourRate = BattCapFactory.GetHourRate(inf.getMonCapStd(), inf.getTestCurr());
                        float cap = (float) BattCapFactory.GetMonomerCap(inf.getMonCapStd(), hourRate, inf.getTestCap(), inf.getMaxMonvol(), inf.getMinMonvol(), inf.getMonVolStd(), BattCapFactory.CapType_Real);
                        if (cap < capAlarm * inf.getMonCapStd()) {
                            alarmNum = alarmNum + 1;
                        }
                        if (cap < capChange * inf.getMonCapStd()) {
                            changeNum = changeNum + 1;
                        }
                        battGroupId = inf.getBattGroupId().toString();
                        //map.put(battGroupId, inf.getMonCapStd() + " " + inf.getMonVolStd() + " " + cap + " " + capAlarm + " " + capChange);
                    }
                }
            }
            Map<String, Integer> numMap = new HashMap<>();
            numMap.put("alarmNum", (alarmNum - changeNum));
            numMap.put("changeNum", changeNum);
            //总电池组(分类)
            int monNum = binfMapper.geGroupCount(userId);
            numMap.put("monNum", monNum);
            //蓄电池优良
            numMap.put("goodSum", (monNum - alarmNum));
            return new Response().setII(1, true, numMap, "蓄电池组优劣分析");
        } catch (Exception e) {
            return new Response<>().set(1, false, "发生异常:" + e.getCause());
        }
    }
 
 
}