whyclxw
9 小时以前 c7795eb09f3059f1a0310bc817e2fa0e9e64d29e
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whyc.constant.PwrCapperformanceEnum;
import com.whyc.dto.AnalysisAlm.ResAnalysis;
import com.whyc.dto.Real.CompareDto;
import com.whyc.dto.Real.PwrHisRealAcInDto;
import com.whyc.dto.Real.PwrHisRealDcoutInDto;
import com.whyc.dto.Real.QuarterDto;
import com.whyc.dto.RealDataStatic.ResRealDataAc;
import com.whyc.dto.RealDataStatic.SticRealDataAc;
import com.whyc.dto.Response;
import com.whyc.dto.Statistic.QuarterPwr7Res;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.mapper.CommonMapper;
import com.whyc.pojo.db_data_history.BattRealdataId;
import com.whyc.pojo.db_data_history.PwrdevHistorydataId;
import com.whyc.pojo.db_param.AppParam;
import com.whyc.pojo.db_param.ApptimeParam;
import com.whyc.pojo.db_param.PowerPropertyParam;
import com.whyc.pojo.db_param.PowerheartParam;
import com.whyc.pojo.db_station.PowerInf;
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 javax.annotation.Resource;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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 PwrdevHistorydataIdService {
    @Autowired
    private SubTablePageInfoService subTablePageInfoService;
    @Resource
    private CommonMapper commonMapper;
 
    @Autowired
    private PowerInfService  powerInfService;
 
    @Autowired
    private AppParamService  appParamService;
 
    @Autowired
    private ApptimeParamService  apptimeParamService;
 
    @Autowired
    private PowerPropertyParamService  propertyParamService;
 
    @Autowired
    private PowerheartParamService heartService;
 
    //系统概览前N笔交流输入统计
    public Response getPwrHisAcinData100(Integer powerId) throws ParseException, InterruptedException {
        //获取前笔数*时间间隔
        PowerheartParam heartParam=heartService.getHeartParamByPowerId(powerId);
        Integer interverCount=100*5;//默认值
        Integer granularity=5;//默认值
        if(heartParam!=null){
            interverCount=heartParam.getAcinInterverCfg()*heartParam.getAcinCountCfg();
            granularity=heartParam.getAcinInterverCfg();
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String dateTime = now.format(formatter);
        Date endTime=ActionUtil.sdf.parse(dateTime);
        // 计算笔数*时间间隔的时间点
        LocalDateTime halfHourAgo = now.minusMinutes(interverCount);
        // 格式化输出
        String halfHourAgoTime=halfHourAgo.format(formatter);
        Date startTime=ActionUtil.sdf.parse(halfHourAgoTime);
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(startTime,endTime);
        List<PwrHisRealAcInDto> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String table = powerId + "_" + ThreadLocalUtil.format(recordDatetime,2);
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<PwrHisRealAcInDto> list = new ArrayList();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    list=subTablePageInfoService.getHalfHourPwrHisAcinData(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrHisRealAcInDto::getRecordDatetime)).collect(Collectors.toList());
        return new Response().setII(1,dataList.size()>0,dataListSorted,"获取半小时内交流输入统计");
    }
    //系统概览获取前N笔直流输出统计
    public Response getHalfHourPwrHisDcoutData(Integer powerId) throws InterruptedException, ParseException {
        //获取前笔数*时间间隔
        PowerheartParam heartParam=heartService.getHeartParamByPowerId(powerId);
        Integer interverCount=100*5;//默认值
        Integer granularity=5;//默认值
        if(heartParam!=null){
            interverCount=heartParam.getAcoutInterverCfg()*heartParam.getAcoutCountCfg();
            granularity=heartParam.getAcoutInterverCfg();//默认时间间隔一分钟一笔,所以时间间隔就是几笔取一笔
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String dateTime = now.format(formatter);
        Date endTime=ActionUtil.sdf.parse(dateTime);
        // 计算笔数*时间间隔的时间点
        LocalDateTime halfHourAgo = now.minusMinutes(interverCount);
        // 格式化输出
        String halfHourAgoTime=halfHourAgo.format(formatter);
        Date startTime=ActionUtil.sdf.parse(halfHourAgoTime);
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(startTime,endTime);
        List<PwrHisRealDcoutInDto> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String table = powerId + "_" + ThreadLocalUtil.format(recordDatetime,2);
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<PwrHisRealDcoutInDto> list = new ArrayList();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    list=subTablePageInfoService.getHalfHourPwrHisDcoutData(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrHisRealDcoutInDto::getRecordDatetime)).collect(Collectors.toList());
        Long modelCfg=0l;
        PowerInf pinf=powerInfService.getPowerInfById(powerId);
        if(pinf!=null){
            modelCfg=pinf.getModelCfg();
        }
        return new Response().setIII(1,dataList.size()>0,dataListSorted,modelCfg,"获取半小时内直流输出统计");
    }
    //1.2.7优良电源统计上一季度的电源某一属性和参数阈值超过的次数
    public Integer getPwrQuarter7(Integer powerId,Integer powerType) throws NoSuchFieldException, IllegalAccessException {
        Map<String,Integer> map=new HashMap<>();
        //获取优良电源性能统计在平台上的时间参数设置表
        ApptimeParam apptimeParam=apptimeParamService.getPwrPerformanceStrartTime(powerId);
        //获取指定时间到当前时间一共多少月的集合
        List<String> datelist=ActionUtil.getDateListBetweenDates(apptimeParam.getParamValue(), new Date());
        //获取电源优良判断的标准参数值
        List<PowerPropertyParam> paramList=propertyParamService.getPwrStandardParam(powerType);
        List<String> propertyNameList = paramList.stream()
                .map(param -> param.getPropertyName())
                .collect(Collectors.toList());
        for (int i=0;i<datelist.size();i++) {
            String date=datelist.get(i);
            String tableName ="db_data_history.tb_pwrdev_historydata_"+powerId+"_"+date;
            String existTableName = commonMapper.existTable("db_data_history", "tb_pwrdev_historydata_"+powerId+"_"+date);
            //判断表是否存在
            if(existTableName == null){
                continue;
            }
            List<PwrdevHistorydataId> datalist=subTablePageInfoService.getPwrQuarter7(tableName,propertyNameList);
            // 遍历 dataList 并比较属性值
            for (PwrdevHistorydataId data : datalist) {
                for (int j=0;j<paramList.size();j++) {
                    PowerPropertyParam param = paramList.get(j);
                    String propertyName = ActionUtil.toCamelCase(param.getPropertyName());
                    Field field = PwrdevHistorydataId.class.getDeclaredField(propertyName);
                    field.setAccessible(true); // 如果字段是私有的,需要设置为可访问
                    Float value = (Float) field.get(data);
                    if ((value > (param.getBasisVal()*param.getAlarmLimithUpper()))||(value <(param.getBasisVal()*param.getAlarmLimitlLower()))) {
                        int limitNum=map.get(param.getPropertyName())==null?0:map.get(param.getPropertyName());
                        map.put(param.getPropertyName(), limitNum+1);
                    }
                }
            }
        }
        //判断优良电源性能
        int pwrPerformence=judgePwrPerformence(map);
        return pwrPerformence;
    }
    //判断优良电源性能
    private Integer judgePwrPerformence(Map<String,Integer> map){
        //获取apps中优良电源判断参数
        List<AppParam> appParamList=appParamService.getPwrPerformenceParam();
        Float excellentVal=0f;
        Float commonlyVal=0f;
        Float warningVal=0f;
        for (AppParam appParam : appParamList) {
            if(appParam.getParamNamePsx().equals("power_excellent_val")){
                excellentVal=appParam.getParamValue();
            }
            if(appParam.getParamNamePsx().equals("power_commonly_val")){
                commonlyVal=appParam.getParamValue();
            }
            if(appParam.getParamNamePsx().equals("power_warning_val")){
                warningVal=appParam.getParamValue();
            }
        }
        Integer performance = PwrCapperformanceEnum.PWRSTATE_1.getStateId(); // 初始化为最优等级
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            Integer level = evaluateValue(entry.getValue(), excellentVal, commonlyVal, warningVal);
            // 根据等级排序确定当前是否是最差等级
            if (PwrCapperformanceEnum.PWRSTATE_4.getStateId()==level) {
                performance = PwrCapperformanceEnum.PWRSTATE_4.getStateId();
                return performance; // 一旦出现差,直接返回
            } else if (PwrCapperformanceEnum.PWRSTATE_3.getStateId()==level && PwrCapperformanceEnum.PWRSTATE_4.getStateId()!=performance) {
                performance = PwrCapperformanceEnum.PWRSTATE_3.getStateId();
            } else if (PwrCapperformanceEnum.PWRSTATE_2.getStateId()==level &&(PwrCapperformanceEnum.PWRSTATE_3.getStateId()!=performance||PwrCapperformanceEnum.PWRSTATE_4.getStateId()!=performance)) {
                performance = PwrCapperformanceEnum.PWRSTATE_2.getStateId();
            }
        }
        return performance;
    }
    // 判断单个值的等级
    public  Integer evaluateValue(Integer value, Float excellentVal, Float commonlyVal, Float warningVal) {
        if (value <= excellentVal) {
            return PwrCapperformanceEnum.PWRSTATE_1.getStateId();
        } else if (value <= commonlyVal&&value > excellentVal) {
            return PwrCapperformanceEnum.PWRSTATE_2.getStateId();
        } else if (value <= warningVal&&value > commonlyVal) {
            return PwrCapperformanceEnum.PWRSTATE_3.getStateId();
        } else {
            return PwrCapperformanceEnum.PWRSTATE_4.getStateId();
        }
    }
 
    //电源历史实时查询
    public Response getPwrRealDataHis(Integer powerId, Integer granularity, String startTime, String endTime) throws InterruptedException, ParseException {
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(ThreadLocalUtil.parse(startTime,1),ThreadLocalUtil.parse(endTime,1));
        List<PwrHisRealDcoutInDto> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String table = powerId + "_" + ThreadLocalUtil.format(recordDatetime,2);
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<PwrHisRealDcoutInDto> list = new ArrayList();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    list=subTablePageInfoService.getPwrRealDataHis(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrHisRealDcoutInDto::getRecordDatetime)).collect(Collectors.toList());
        return new Response().setII(1,dataList.size()>0,dataListSorted,"获取半小时内直流输出统计");
    }
    //实时数据数据曲线统计-交流单元
    public Response getSticRealAcData(SticRealDataAc stic) throws ParseException, InterruptedException {
        //获取前笔数*时间间隔
        PowerheartParam heartParam=heartService.getHeartParamByPowerId(stic.getPowerId());
        Integer interverCount=100*5;//默认值
        Integer granularity=5;//默认值
        if(heartParam!=null){
            interverCount=heartParam.getAcinInterverCfg()*heartParam.getAcinCountCfg();
            granularity=heartParam.getAcinInterverCfg();
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String dateTime = now.format(formatter);
        Date endTime=ActionUtil.sdf.parse(dateTime);
        // 计算笔数*时间间隔的时间点
        LocalDateTime halfHourAgo = now.minusMinutes(interverCount);
        // 格式化输出
        String halfHourAgoTime=halfHourAgo.format(formatter);
        Date startTime=ActionUtil.sdf.parse(halfHourAgoTime);
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(startTime,endTime);
        List<ResRealDataAc> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String table = stic.getPowerId() + "_" + ThreadLocalUtil.format(recordDatetime,2);
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<ResRealDataAc> list = new ArrayList();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    list=subTablePageInfoService.getSticRealAcData(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum,stic.getPropertyInfo());
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(ResRealDataAc::getRecordDatetime)).collect(Collectors.toList());
        return new Response().setII(1,dataList.size()>0,dataListSorted,"获取半小时内交流输入统计");
    }
}