whycxzp
2021-08-27 9f1dab940da5cef62e90333ec978d2fe48635273
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
package com.whyc.service;
 
import com.whyc.dto.AnalysisMonCapDTO;
import com.whyc.dto.BatteryInfo;
import com.whyc.dto.Response;
import com.whyc.mapper.*;
import com.whyc.dto.AnalysisMonFieldDTO;
import com.whyc.pojo.BatteryEndurance;
import com.whyc.pojo.BatteryRTState;
import com.whyc.pojo.Tables;
import com.whyc.util.BattCapFactory;
import com.whyc.util.MathUtil;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class BatteryDataService {
 
    @Resource
    private BatteryDataMapper mapper;
 
    @Resource
    private BatteryResDataMapper resMapper;
 
    @Resource
    private BatteryTestDataMapper testDataMapper;
 
    @Resource
    private AnalysisMonFieldMapper monFieldMapper;
 
    @Resource
    private DBMapper dbMapper;
 
    public Response getEndurance(Integer userId) {
        List<BatteryEndurance> enduranceList = mapper.endurance(userId);
        List<Integer> times = Arrays.asList(1, 2, 3);
        Map resultMap = enduranceAnalysis(enduranceList,times);
        return new Response<Map>().set(1,resultMap);
    }
 
    /**按时间阶段统计续航*/
    private static Map enduranceAnalysis(List<BatteryEndurance> enduranceList, List<Integer> times) {
        Map<String,Integer> map = new HashMap<>();
 
        for (int i = 0; i < enduranceList.size(); i++) {
            //每一个具体的续航进行分组
            if(enduranceList.get(i).getEnduranceActualTime()<times.get(0)){
                map.put("续航"+times.get(0)+"小时内",Optional.ofNullable(map.get("续航"+times.get(0)+"小时内")).orElse(0)+1);
            }
            else if(enduranceList.get(i).getEnduranceActualTime()>times.get(times.size()-1)){
                map.put("续航"+times.get(times.size()-1)+"小时以上",Optional.ofNullable(map.get("续航"+times.get(times.size()-1)+"小时以上")).orElse(0)+1);
            }else {
                for (int j = 0; j < times.size(); j++) {
                    if (enduranceList.get(i).getEnduranceActualTime() <= times.get(j)) {
                        map.put("续航" + times.get(j - 1) + "小时到" + times.get(j) + "小时", Optional.ofNullable(map.get("续航" + times.get(j - 1) + "小时到" + times.get(j) + "小时")).orElse(0) + 1);
                        break;
                    }
                }
            }
        }
        //排序
        TreeMap<String, Integer> maps = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
 
        maps.putAll(map);
        map.clear();
 
        return maps;
    }
 
    public Response getBatteryCap(Integer userId) {
        List<BatteryRTState> batteryRTStates = mapper.getBatteryCap(userId);
        List<Float> percents = Arrays.asList(0.8f, 0.85f,0.9f,0.95f);
        Map resultMap = batteryCapAnalysis(batteryRTStates,percents);
        return new Response<Map>().set(1,resultMap);
    }
 
    private Map batteryCapAnalysis(List<BatteryRTState> batteryRTStates, List<Float> percents) {
        Map<String,Integer> map = new HashMap<>();
 
        for (int i = 0; i < batteryRTStates.size(); i++) {
            //每一个具体的续航进行分组
            if((Float)MathUtil.divide(batteryRTStates.get(i).getBatteryRealCap(),batteryRTStates.get(i).getMonCapStd(),1)<percents.get(0)){
                map.put(percents.get(0)+"以下",Optional.ofNullable(map.get(percents.get(0)+"以下")).orElse(0)+1);
            }
            else if((Float)MathUtil.divide(batteryRTStates.get(i).getBatteryRealCap(),batteryRTStates.get(i).getMonCapStd(),1)>percents.get(percents.size()-1)){
                map.put(percents.get(percents.size()-1)+"以上",Optional.ofNullable(map.get(percents.get(percents.size()-1)+"以上")).orElse(0)+1);
            }else {
                for (int j = 0; j < percents.size(); j++) {
                    if ((Float)MathUtil.divide(batteryRTStates.get(i).getBatteryRealCap(),batteryRTStates.get(i).getMonCapStd(),1) <= percents.get(j)) {
                        map.put(percents.get(j - 1) + "到" + percents.get(j), Optional.ofNullable(map.get(percents.get(j - 1) + "到" + percents.get(j))).orElse(0) + 1);
                        break;
                    }
                }
            }
        }
        //排序
        TreeMap<String, Integer> maps = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
 
        maps.putAll(map);
        map.clear();
 
        return maps;
    }
 
    /**单体信息统计*/
    public List<AnalysisMonFieldDTO> monFieldStatistics(String filedName, String fieldName2) {
            //查询所有的电池组
            List<Tables> tables = dbMapper.getTableNames("db_batt_testdata", "tb_batttestdata\\_[1-9]+");
            List<String> tableNames = new LinkedList<>();
            tables.forEach(tableName -> {
                tableNames.add(tableName.getTableName());
            });
            //查询每个电池组的单体字段数据
            List<AnalysisMonFieldDTO> list = testDataMapper.getStatisticsByTableNames(tableNames,filedName,fieldName2);
 
        return list;
    }
 
    /**单体内阻统计*/
    public List<AnalysisMonFieldDTO> monResStatistics() {
        //查询所有的电池组
        List<Tables> tables = dbMapper.getTableNames("db_batt_testdata", "tb_battresdata\\_[1-9]+");
        List<String> tableNames = new LinkedList<>();
        tables.forEach(tableName -> {
            tableNames.add(tableName.getTableName());
        });
        //查询每个电池组的单体字段数据
        List<AnalysisMonFieldDTO> list = resMapper.getResStatisticsByTableNames(tableNames);
 
        return list;
    }
 
    /**单体容量统计*/
    public List<AnalysisMonFieldDTO> monCapStatistics() {
        List<AnalysisMonFieldDTO> analysisMonFieldList = new LinkedList<>();
        //查询所有的stop表
        List<Tables> tables = dbMapper.getTableNames("db_batt_testdata","tb_batttestdatastop*");
        List<String> groupIds = new LinkedList<>();
        tables.forEach(tableName -> {
            groupIds.add(tableName.getTableName().split("_")[2]);
        });
        //查询每个电池组的单体字段数据,同时要查询出来标准字段,以此来计算每个单体的容量
        List<AnalysisMonCapDTO> analysisMonCapDTOs = testDataMapper.getCapStatisticsByGroupIds(groupIds);
        Map<Integer, List<AnalysisMonCapDTO>> analysisCapMap = analysisMonCapDTOs.stream().collect(Collectors.groupingBy(AnalysisMonCapDTO::getBattGroupId));
        //遍历每个电池组,获取统计数据
        for(Map.Entry<Integer,List<AnalysisMonCapDTO>> entry : analysisCapMap.entrySet()){
            AnalysisMonFieldDTO analysisMonField = new AnalysisMonFieldDTO();
            double minCap = 0d;
            double maxCap = 0d;
            double avgCap = 0d;
            double realCap = 0d;
            double capSum = 0d;
            int size =0;
 
            Integer battGroupId = entry.getKey();
            List<AnalysisMonCapDTO> capDTOList = entry.getValue();
            //执行容量统计
            for (AnalysisMonCapDTO monCapDTO : capDTOList){
                if(monCapDTO.getMonVol()>monCapDTO.getMonVolStd()*0.9f){
                    int hourRate=BattCapFactory.GetHourRate(monCapDTO.getMonCapStd(),monCapDTO.getTestCurr());
                    realCap = BattCapFactory.GetMonomerCap(monCapDTO.getMonCapStd(),hourRate,monCapDTO.getTestCap(),monCapDTO.getMaxMonVol(),
                            monCapDTO.getMonVol(),monCapDTO.getMonVolStd(),BattCapFactory.CapType_Real);
                }else{
                    //查询电池测试表中对应的测试批次的单体剩余容量
                    realCap = testDataMapper.getRestCapByCondition(monCapDTO);
                }
                if(realCap<minCap){
                    minCap = realCap;
                }
                if(realCap>maxCap){
                    maxCap=realCap;
                }
                capSum+=realCap;
                size++;
            }
 
            avgCap = capSum/size;
 
            analysisMonField.setBattGroupId(battGroupId);
            analysisMonField.setBattGroupName(capDTOList.get(0).getBattGroupName());
            analysisMonField.setStationName(capDTOList.get(0).getStationName());
            analysisMonField.setMinMonCap(minCap);
            analysisMonField.setMaxMonCap(maxCap);
            analysisMonField.setAvgMonCap(avgCap);
            //将统计数据放到数组中
            analysisMonFieldList.add(analysisMonField);
        }
 
        return analysisMonFieldList;
    }
 
    public Response getMonVol(Integer userId) {
        /*List<BatteryInfo> batteryInfos =  testDataMapper.getStationAndBatteryGroupIds(userId);
        for (BatteryInfo temp:batteryInfos) {
            //根据BattGroupIds查询到对应的数值
            String[] batteryGroupIds = temp.getBatteryGroupIds().split(",");
            Integer[] ids = new Integer[batteryGroupIds.length];
            for (int i = 0; i < batteryGroupIds.length; i++) {
                ids[i] = Integer.parseInt(batteryGroupIds[i]);
            }
            DataAnalysisFloatDTO dataStatistics = testDataMapper.getStatisticsByBattGroupIds(ids,"mon_vol");
            resMap.put(temp.getStationName(),dataStatistics);
        }*/
 
        //查询单体电压数据
        List<AnalysisMonFieldDTO> monFields = monFieldMapper.getMonField("tb_mon_vol",userId);
 
        return new Response<>().set(1,monFields);
    }
 
    public Response getMonTemp(Integer userId) {
        HashMap<String, Object> resMap = new HashMap<>();
        /*List<BatteryInfo> batteryInfos =  testDataMapper.getStationAndBatteryGroupIds(userId);
        for (BatteryInfo temp:batteryInfos) {
            //根据BattGroupIds查询到对应的数值
            String[] batteryGroupIds = temp.getBatteryGroupIds().split(",");
            Integer[] ids = new Integer[batteryGroupIds.length];
            for (int i = 0; i < batteryGroupIds.length; i++) {
                ids[i] = Integer.parseInt(batteryGroupIds[i]);
            }
            DataAnalysisFloatDTO dataStatistics = testDataMapper.getStatisticsByBattGroupIds(ids,"mon_tmp");
            resMap.put(temp.getStationName(),dataStatistics);
        }*/
 
        //查询单体温度数据
        List<AnalysisMonFieldDTO> monFields = monFieldMapper.getMonField("tb_mon_tmp",userId);
 
 
        return new Response<>().set(1,monFields);
    }
 
    public Response getMonRes(Integer userId) {
        HashMap<String, Object> resMap = new HashMap<>();
        /*List<BatteryInfo> batteryInfos =  resMapper.getResStationAndBatteryGroupIds(userId);
        for (BatteryInfo temp:batteryInfos) {
            //根据BattGroupIds查询到对应的数值
            String[] batteryGroupIds = temp.getBatteryGroupIds().split(",");
            Integer[] ids = new Integer[batteryGroupIds.length];
            for (int i = 0; i < batteryGroupIds.length; i++) {
                ids[i] = Integer.parseInt(batteryGroupIds[i]);
            }
            DataAnalysisFloatDTO dataStatistics = resMapper.getResStatisticsByBattGroupIds(ids);
            resMap.put(temp.getStationName(),dataStatistics);
        }*/
 
        //查询单体内阻数据
        List<AnalysisMonFieldDTO> monFields = monFieldMapper.getMonField("tb_mon_res",userId);
 
        return new Response<>().set(1,monFields);
    }
 
    public Response getMonCap(Integer userId) {
        /*HashMap<String, Object> resMap = new HashMap<>();
        List<BatteryInfo> batteryInfos =  testDataMapper.getStationAndBatteryGroupIds(userId);
        for (BatteryInfo temp:batteryInfos) {
            String[] batteryGroupIds = temp.getBatteryGroupIds().split(",");
            Integer[] ids = new Integer[batteryGroupIds.length];
            for (int i = 0; i < batteryGroupIds.length; i++) {
                ids[i] = Integer.parseInt(batteryGroupIds[i]);
            }
            //查询标准
            List<BatteryInfo> batteryStdList = testDataMapper.getVolAndCapStd(ids);
            //查询最值,电池组内每个单体的最值集合
            List<BatteryInfo> batteryStatisticList = testDataMapper.getCapStatisticsByBattGroupIds(ids);
 
            //统计值
            double minMonCap = 0.00;
            double maxMonCap = 0.00;
            double avgMonCap = 0.00;
            double avgMonCapSum=0.00;
            int count = 0;
            DecimalFormat decimalFormat = new DecimalFormat(".00");
 
            List<Double> minMonCapList =new LinkedList<>();
            List<Double> maxMonCapList =new LinkedList<>();
            List<Double> avgMonCapList =new LinkedList<>();
 
            for(BatteryInfo monData:batteryStatisticList){
                //求每个单体容量的最值
                //获取最小单体实际容量
                for (int i = 0; i < batteryStdList.size(); i++) {
                    if(batteryStdList.get(i).getBatteryGroupId().intValue()==monData.getBatteryGroupId()){
 
                        int hourRateMin = BattCapFactory.GetHourRate(batteryStdList.get(i).getMonCapStd(), monData.getMinTestCurr());
                        double monRealCapMin = BattCapFactory.GetMonomerCap(batteryStdList.get(i).getMonCapStd(), hourRateMin, monData.getMinTestCap(), monData.getMaxMonVol(), monData.getMinMonVol(), batteryStdList.get(i).getMonVolStd(), BattCapFactory.CapType_Real);
                        monRealCapMin = Double.parseDouble(decimalFormat.format(monRealCapMin));
                        minMonCapList.add(monRealCapMin);
 
                        //获取最大单体实际容量
                        int hourRateMax = BattCapFactory.GetHourRate(batteryStdList.get(i).getMonCapStd(), monData.getMaxTestCurr());
                        double monRealCapMax = BattCapFactory.GetMonomerCap(batteryStdList.get(i).getMonCapStd(), hourRateMax, monData.getMaxTestCap(), monData.getMaxMonVol(), monData.getMinMonVol(), batteryStdList.get(i).getMonVolStd(), BattCapFactory.CapType_Real);
                        monRealCapMax = Double.parseDouble(decimalFormat.format(monRealCapMax));
                        maxMonCapList.add(monRealCapMax);
 
                        //获取平均单体实际容量
                        int hourRateAvg = BattCapFactory.GetHourRate(batteryStdList.get(i).getMonCapStd(), monData.getAvgTestCurr());
                        double monRealCapAvg = BattCapFactory.GetMonomerCap(batteryStdList.get(i).getMonCapStd(), hourRateAvg, monData.getAvgTestCap(), monData.getMaxMonVol(), monData.getMinMonVol(), batteryStdList.get(i).getMonVolStd(), BattCapFactory.CapType_Real);
                        avgMonCapSum+=monRealCapAvg;
                        count++;
                    }
                }
            }
            minMonCap = min(minMonCapList);
            maxMonCap = max(maxMonCapList);
            avgMonCap = avgMonCapSum / count;
            avgMonCap=Double.parseDouble(decimalFormat.format(avgMonCap));
 
            //单体容量
            minMonCapList.add(minMonCap);
            maxMonCapList.add(maxMonCap);
            avgMonCapList.add(avgMonCap);
 
            List<List> capList = new LinkedList<>();
            capList.add(minMonCapList);
            capList.add(maxMonCapList);
            capList.add(avgMonCapList);
            
            resMap.put(temp.getStationName(),capList);
        }*/
        //return new Response<>().set(1,resMap);
 
        //查询单体电压数据
        List<AnalysisMonFieldDTO> monFields = monFieldMapper.getMonField("tb_mon_cap",userId);
 
        return new Response<>().set(1,monFields);
 
    }
 
    public double min(List<Double> list){
        double min=0.00;
        for (int i = 0; i < list.size(); i++) {
            if(i==0){
                min=list.get(i);
            }else{
                if (list.get(i)<min){
                    min =list.get(i);
                }
            }
        }
        return min;
 
    }
 
    public double max(List<Double> list){
        double max=0.00;
        for (int i = 0; i < list.size(); i++) {
            if(i==0){
                max=list.get(i);
            }else{
                if (list.get(i)>max){
                    max =list.get(i);
                }
            }
        }
        return max;
    }
 
 
    public static void main(String[] args) {
        BatteryEndurance endurance = new BatteryEndurance();
        endurance.setEnduranceActualTime(1.5);
 
        BatteryEndurance endurance2 = new BatteryEndurance();
        endurance2.setEnduranceActualTime(1.5);
 
        BatteryEndurance endurance3 = new BatteryEndurance();
        endurance3.setEnduranceActualTime(2.5);
 
        BatteryEndurance endurance4 = new BatteryEndurance();
        endurance4.setEnduranceActualTime(3.5);
 
        List<BatteryEndurance> batteryEnduranceList = Arrays.asList(endurance, endurance2, endurance3, endurance4);
 
        List<Integer> timeList = Arrays.asList(1, 2, 3);
 
        Map map = enduranceAnalysis(batteryEnduranceList, timeList);
 
    }
}