VVT789
2025-02-13 9a33fc4f8a0ace2d076418bd0f8d6441cb34b9a3
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
413
414
415
416
417
418
419
package com.zhitan.energyMonitor.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.zhitan.basicdata.domain.MeterImplement;
import com.zhitan.basicdata.mapper.MeterImplementMapper;
import com.zhitan.common.constant.CommonConst;
import com.zhitan.common.constant.TimeTypeConst;
import com.zhitan.common.utils.*;
import com.zhitan.energyMonitor.domain.vo.ElectricThreePhaseDetail;
import com.zhitan.energyMonitor.domain.vo.ElectricThreePhaseItem;
import com.zhitan.energyMonitor.domain.vo.ElectricThreePhaseTempModel;
import com.zhitan.energyMonitor.domain.vo.ElectricThreePhaseVO;
import com.zhitan.energyMonitor.service.IElectricThreePhaseService;
import com.zhitan.model.domain.EnergyIndex;
import com.zhitan.realtimedata.domain.TagValue;
import com.zhitan.realtimedata.service.RealtimeDatabaseService;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Description: sensor_alarm_item
 * @Author: jeecg-boot
 * @Date: 2022-04-19
 * @Version: V1.0
 */
@Service
public class ElectricThreePhaseServiceImpl implements IElectricThreePhaseService {
 
    @Autowired
    private RealtimeDatabaseService realtimeDatabaseService;
    @Resource
    private MeterImplementMapper meterImplementMapper;
 
    /**
     * 获取三相不平衡数据
     *
     * @param timeType        时间类型
     * @param timeCode        时间编码
     * @param energyIndexList 点位集合
     * @param requestType     类型
     * @return ElectricThreePhaseVo
     * @Date 14:27 2022/5/30
     **/
    @Override
    public ElectricThreePhaseVO list(String timeType, String timeCode, List<EnergyIndex> energyIndexList, String requestType, String meterId) {
        ElectricThreePhaseVO vo = new ElectricThreePhaseVO();
 
        // 获取电压不平衡数据
        if (CommonConst.STR_NUMBER_0.equals(requestType)) {
            energyIndexList = energyIndexList.stream()
                    .filter(x -> StringUtil.ifEmptyOrNullReturnValue(x.getCode()).trim().endsWith(CommonConst.TAG_CODE_VOLTAGE_A)
                            || StringUtil.ifEmptyOrNullReturnValue(x.getCode()).trim().endsWith(CommonConst.TAG_CODE_VOLTAGE_B)
                            || StringUtil.ifEmptyOrNullReturnValue(x.getCode()).trim().endsWith(CommonConst.TAG_CODE_VOLTAGE_C)).collect(Collectors.toList());
        } else {
            energyIndexList = energyIndexList.stream()
                    .filter(x -> StringUtil.ifEmptyOrNullReturnValue(x.getCode()).trim().endsWith(CommonConst.TAG_CODE_CURRENT_A)
                            || StringUtil.ifEmptyOrNullReturnValue(x.getCode()).trim().endsWith(CommonConst.TAG_CODE_CURRENT_B)
                            || StringUtil.ifEmptyOrNullReturnValue(x.getCode()).trim().endsWith(CommonConst.TAG_CODE_CURRENT_C))
                    .collect(Collectors.toList());
        }
        if (ObjectUtil.isEmpty(energyIndexList)) {
            return vo;
        }
        List<String> tagCodeList = energyIndexList.stream().map(EnergyIndex::getCode).collect(Collectors.toList());
        if(ObjectUtil.isEmpty(tagCodeList)){
            tagCodeList.add(CommonConst.STR_NUMBER_MINUS_ONE);
        }
 
        Date start = ChartUtils.getDateTime(timeType, timeCode);
        Date end = getEndTime(timeType, start);
 
        DateTime begin = new DateTime(start);
        DateTime finish = new DateTime(end);
        long millis = new Duration(begin, finish).getMillis();
        int pointCount = IntegerUtil.toInt(millis / CommonConst.DIGIT_3600 / CommonConst.DIGIT_1000);
 
        List<TagValue> tagValueList = realtimeDatabaseService.retrieve(tagCodeList, start, end, pointCount);
        List<ElectricThreePhaseItem> itemList = new ArrayList<>();
        List<Date> dateList = new ArrayList<>();
        ChartUtils.generateDateList(timeType, timeCode, dateList);
        ElectricThreePhaseTempModel tempModel = new ElectricThreePhaseTempModel();
 
        Date now = new Date();
        for (Date date : dateList) {
            Date tempDate = date;
            Date temNow = now;
            switch (timeType) {
                case TimeTypeConst.TIME_TYPE_DAY:
                    tempDate = DateTimeUtil.addHours(date, CommonConst.DIGIT_1);
                    //当前时间转成整时整点
                    temNow = DateTimeUtil.getHourTime(TimeTypeConst.TIME_TYPE_HOUR, now);
                    break;
                case TimeTypeConst.TIME_TYPE_MONTH:
                    tempDate = date;
                    //当前时间转成整时整点
                    temNow = DateTimeUtil.getHourTime(TimeTypeConst.TIME_TYPE_DAY, now);
                    break;
                case TimeTypeConst.TIME_TYPE_YEAR:
                    tempDate = date;
                    temNow = DateTimeUtil.getHourTime(TimeTypeConst.TIME_TYPE_MONTH, now);
                    break;
                default:
                    break;
            }
            MeterImplement meterImplement = meterImplementMapper.selectById(meterId);
 
            ElectricThreePhaseItem temp = new ElectricThreePhaseItem();
            if (ObjectUtil.isNotEmpty(meterImplement)) {
                temp.setName(meterImplement.getMeterName());
            }
            temp.setTimeCode(ChartUtils.getTimeCode(timeType, date));
            temp.setTimeCodeChart(ChartUtils.getTimeCodeChart(timeType, date));
            temp.setValueA(CommonConst.DOUBLE_MINUS_SIGN);
            temp.setValueB(CommonConst.DOUBLE_MINUS_SIGN);
            temp.setValueC(CommonConst.DOUBLE_MINUS_SIGN);
            itemList.add(temp);
            // 如果大于当前时间默认--
            if (DateTimeUtil.compareDateDiff(date, temNow) > CommonConst.DIGIT_0) {
                continue;
            }
            switch (timeType) {
                case TimeTypeConst.TIME_TYPE_DAY:
                    // 构造当天每个小时的数据
                    listDayData(tempDate, tagValueList, temp, tempModel);
                    break;
                case TimeTypeConst.TIME_TYPE_MONTH:
                    // 构造当月每天的数据
                    listMonthData(tempDate, tagValueList, temp, tempModel);
                    break;
                case TimeTypeConst.TIME_TYPE_YEAR:
                    // 构造当年每月的数据
                    listYearData(tempDate, tagValueList, temp, tempModel);
                    break;
                default:
                    break;
            }
        }
 
        if (ObjectUtil.isEmpty(tempModel.getMin()) || CommonConst.MIN_INIT_VALUE == tempModel.getMin()) {
            tempModel.setMin(null);
        }
        //设置值
        ElectricThreePhaseDetail detail = new ElectricThreePhaseDetail();
        BeanUtils.copyProperties(tempModel, detail);
        detail.setMax(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setMaxTime(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setValueMaxA(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setValueMaxB(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setValueMaxC(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setMin(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setMinTime(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setValueMinA(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setValueMinB(CommonConst.DOUBLE_MINUS_SIGN);
        detail.setValueMinC(CommonConst.DOUBLE_MINUS_SIGN);
        if (ObjectUtil.isNotNull(tempModel.getMax())) {
            detail.setMax(DoubleUtil.formatDoubleToStr(tempModel.getMax()) + CommonConst.SYMBOL_PERCENT);
            detail.setMaxTime(DateTimeUtil.getDateTime(tempModel.getMaxTime()));
            detail.setValueMaxA(DoubleUtil.formatDoubleToStr(tempModel.getValueMaxA()));
            detail.setValueMaxB(DoubleUtil.formatDoubleToStr(tempModel.getValueMaxB()));
            detail.setValueMaxC(DoubleUtil.formatDoubleToStr(tempModel.getValueMaxC()));
        }
        if (ObjectUtil.isNotNull(tempModel.getMin())) {
            detail.setMin(DoubleUtil.formatDoubleToStr(tempModel.getMin()) + CommonConst.SYMBOL_PERCENT);
            detail.setMinTime(DateTimeUtil.getDateTime(tempModel.getMinTime()));
            detail.setValueMinA(DoubleUtil.formatDoubleToStr(tempModel.getValueMinA()));
            detail.setValueMinB(DoubleUtil.formatDoubleToStr(tempModel.getValueMinB()));
            detail.setValueMinC(DoubleUtil.formatDoubleToStr(tempModel.getValueMinC()));
        }
        vo.setDetail(detail);
        vo.setItemList(itemList);
        return vo;
    }
 
    /**
     * 构造当月的数据列表
     *
     * @param date
     * @param tagValueList
     * @param temp
     * @param tempModel
     */
    private void listYearData(Date date, List<TagValue> tagValueList, ElectricThreePhaseItem temp, ElectricThreePhaseTempModel tempModel) {
        List<Date> dateList = new ArrayList<>();
        ChartUtils.generateDateList(TimeTypeConst.TIME_TYPE_MONTH, DateTimeUtil.getDateTime(date, DateTimeUtil.COMMON_PATTERN_TO_MONTH), dateList);
        ElectricThreePhaseTempModel tempModelMonth = new ElectricThreePhaseTempModel();
        for (Date date1 : dateList) {
            listMonthData(date1, tagValueList, temp, tempModelMonth);
            temp.setMax(CommonConst.DOUBLE_MINUS_SIGN);
            temp.setMin(CommonConst.DOUBLE_MINUS_SIGN);
            if (ObjectUtil.isNotNull(tempModelMonth.getMax())) {
                temp.setMax(DoubleUtil.formatDoubleToStr(tempModelMonth.getMax()));
            }
            if (ObjectUtil.isNotNull(tempModelMonth.getMin())) {
                temp.setMin(DoubleUtil.formatDoubleToStr(tempModelMonth.getMin()));
            }
            if (ObjectUtil.isEmpty(tempModel.getMin()) || CommonConst.MIN_INIT_VALUE == tempModel.getMin()) {
                tempModel.setMin(null);
            }
            if (ObjectUtil.isNotNull(tempModelMonth.getMax())) {
                if (ObjectUtil.isNull(tempModel.getMax()) || (ObjectUtil.isNotNull(tempModel.getMax()) && tempModelMonth.getMax() > tempModel.getMax())) {
                    tempModel.setMax(DoubleUtil.formatDouble(tempModelMonth.getMax()));
                    tempModel.setMaxTime(tempModelMonth.getMaxTime());
                    tempModel.setValueMaxA(DoubleUtil.formatDouble(tempModelMonth.getValueMaxA()));
                    tempModel.setValueMaxB(DoubleUtil.formatDouble(tempModelMonth.getValueMaxB()));
                    tempModel.setValueMaxC(DoubleUtil.formatDouble(tempModelMonth.getValueMaxC()));
                }
            }
            if (ObjectUtil.isNotNull(tempModelMonth.getMin())) {
                if (ObjectUtil.isNull(tempModel.getMin()) || (ObjectUtil.isNotNull(tempModel.getMin()) && tempModelMonth.getMin() <= tempModel.getMin())) {
                    tempModel.setMin(DoubleUtil.formatDouble(tempModelMonth.getMin()));
                    tempModel.setMinTime(tempModelMonth.getMinTime());
                    tempModel.setValueMinA(DoubleUtil.formatDouble(tempModelMonth.getValueMinA()));
                    tempModel.setValueMinB(DoubleUtil.formatDouble(tempModelMonth.getValueMinB()));
                    tempModel.setValueMinC(DoubleUtil.formatDouble(tempModelMonth.getValueMinC()));
                }
            }
        }
    }
 
    /**
     * 构造当月的数据列表
     *
     * @param date
     * @param tagValueList
     * @param temp
     * @param tempModel
     */
    private void listMonthData(Date date, List<TagValue> tagValueList, ElectricThreePhaseItem temp, ElectricThreePhaseTempModel tempModel) {
        List<Date> dateList = new ArrayList<>();
        ChartUtils.generateDateList(TimeTypeConst.TIME_TYPE_DAY, DateTimeUtil.getDateTime(date, DateTimeUtil.COMMON_PATTERN_TO_DAY), dateList);
        ElectricThreePhaseTempModel tempModelDay = new ElectricThreePhaseTempModel();
        for (Date date1 : dateList) {
            Date tempDate = DateTimeUtil.addHours(date1, CommonConst.DIGIT_1);
            listDayData(tempDate, tagValueList, temp, tempModelDay);
            temp.setMax(CommonConst.DOUBLE_MINUS_SIGN);
            temp.setMin(CommonConst.DOUBLE_MINUS_SIGN);
            if (ObjectUtil.isNotNull(tempModelDay.getMax())) {
                temp.setMax(DoubleUtil.formatDoubleToStr(tempModelDay.getMax()));
            }
            if (ObjectUtil.isNotNull(tempModelDay.getMin())) {
                temp.setMin(DoubleUtil.formatDoubleToStr(tempModelDay.getMin()));
            }
            if (ObjectUtil.isEmpty(tempModel.getMin()) || CommonConst.MIN_INIT_VALUE == tempModel.getMin()) {
                tempModel.setMin(null);
            }
            if (ObjectUtil.isNotNull(tempModelDay.getMax())) {
                if (ObjectUtil.isNull(tempModel.getMax()) || (ObjectUtil.isNotNull(tempModel.getMax()) && tempModelDay.getMax() > tempModel.getMax())) {
                    tempModel.setMax(DoubleUtil.formatDouble(tempModelDay.getMax()));
                    tempModel.setMaxTime(tempModelDay.getMaxTime());
                    tempModel.setValueMaxA(DoubleUtil.formatDouble(tempModelDay.getValueMaxA()));
                    tempModel.setValueMaxB(DoubleUtil.formatDouble(tempModelDay.getValueMaxB()));
                    tempModel.setValueMaxC(DoubleUtil.formatDouble(tempModelDay.getValueMaxC()));
                }
            }
            if (ObjectUtil.isNotNull(tempModelDay.getMin())) {
                if (ObjectUtil.isNull(tempModel.getMin()) || (ObjectUtil.isNotNull(tempModel.getMin()) && tempModelDay.getMin() <= tempModel.getMin())) {
                    tempModel.setMin(DoubleUtil.formatDouble(tempModelDay.getMin()));
                    tempModel.setMinTime(tempModelDay.getMinTime());
                    tempModel.setValueMinA(DoubleUtil.formatDouble(tempModelDay.getValueMinA()));
                    tempModel.setValueMinB(DoubleUtil.formatDouble(tempModelDay.getValueMinB()));
                    tempModel.setValueMinC(DoubleUtil.formatDouble(tempModelDay.getValueMinC()));
                }
            }
        }
    }
 
    /**
     * 获取当天的数据列表
     *
     * @param date
     * @param tagValueList
     * @param tempModel
     */
    private void listDayData(Date date, List<TagValue> tagValueList, ElectricThreePhaseItem temp, ElectricThreePhaseTempModel tempModel) {
        Date endTime = DateTimeUtil.addHours(date, CommonConst.DIGIT_1);
        List<TagValue> currentTagValueList = tagValueList.stream()
                .filter(x -> DateTimeUtil.compareDateDiff(date, x.getDataTime()) <= 0 && DateTimeUtil.compareDateDiff(endTime, x.getDataTime()) > 0).collect(Collectors.toList());
        List<TagValue> currentATagValueList = currentTagValueList.stream()
                .filter(x -> StringUtil.ifEmptyOrNullReturnValue(x.getTagCode()).trim().endsWith(CommonConst.A_PHASE)).collect(Collectors.toList());
        List<TagValue> currentBTagValueList = currentTagValueList.stream()
                .filter(x -> StringUtil.ifEmptyOrNullReturnValue(x.getTagCode()).trim().endsWith(CommonConst.B_PHASE)).collect(Collectors.toList());
        List<TagValue> currentCTagValueList = currentTagValueList.stream()
                .filter(x -> StringUtil.ifEmptyOrNullReturnValue(x.getTagCode()).trim().endsWith(CommonConst.C_PHASE)).collect(Collectors.toList());
        TagValue tagValueA = currentATagValueList.stream().filter(x -> DateTimeUtil.compareDateDiff(date, x.getDataTime()) == 0).findAny().orElse(null);
        TagValue tagValueB = currentBTagValueList.stream().filter(x -> DateTimeUtil.compareDateDiff(date, x.getDataTime()) == 0).findAny().orElse(null);
        TagValue tagValueC = currentCTagValueList.stream().filter(x -> DateTimeUtil.compareDateDiff(date, x.getDataTime()) == 0).findAny().orElse(null);
        Double valueA = null, valueB = null, valueC = null;
        if (ObjectUtil.isNotEmpty(tagValueA)) {
            valueA = tagValueA.getValue();
            temp.setValueA(String.valueOf(DoubleUtil.formatDouble(tagValueA.getValue())));
        } else {
            temp.setValueA(CommonConst.DOUBLE_MINUS_SIGN);
        }
        if (!ObjectUtil.isEmpty(tagValueB)) {
            valueB = tagValueB.getValue();
            temp.setValueB(String.valueOf(DoubleUtil.formatDouble(tagValueB.getValue())));
        } else {
            temp.setValueB(CommonConst.DOUBLE_MINUS_SIGN);
        }
        if (!ObjectUtil.isEmpty(tagValueC)) {
            valueC = tagValueC.getValue();
            temp.setValueC(String.valueOf(DoubleUtil.formatDouble(tagValueC.getValue())));
        } else {
            temp.setValueC(CommonConst.DOUBLE_MINUS_SIGN);
        }
        Double value = calcUnbalanceValue(valueA, valueB, valueC);
        if (ObjectUtil.isEmpty(value)) {
            return;
        }
        if (ObjectUtil.isEmpty(tempModel.getMin()) || CommonConst.MIN_INIT_VALUE == tempModel.getMin()) {
            tempModel.setMin(value);
        }
        if (ObjectUtil.isEmpty(tempModel.getMax()) || value > tempModel.getMax()) {
            tempModel.setMax(DoubleUtil.formatDouble(value));
            if (!ObjectUtil.isEmpty(tagValueA)) {
                tempModel.setMaxTime(tagValueA.getDataTime());
            }
            tempModel.setValueMaxA(DoubleUtil.formatDouble(valueA));
            tempModel.setValueMaxB(DoubleUtil.formatDouble(valueB));
            tempModel.setValueMaxC(DoubleUtil.formatDouble(valueC));
        }
        if (ObjectUtil.isEmpty(tempModel.getMin()) || value <= tempModel.getMin()) {
            tempModel.setMin(DoubleUtil.formatDouble(value));
            tempModel.setValueMinA(DoubleUtil.formatDouble(valueA));
            tempModel.setValueMinB(DoubleUtil.formatDouble(valueB));
            tempModel.setValueMinC(DoubleUtil.formatDouble(valueC));
            if (!ObjectUtil.isEmpty(tagValueA)) {
                tempModel.setMinTime(tagValueA.getDataTime());
            }
        }
    }
 
    /**
     * 计算三相不平衡极值
     *
     * @param valueA
     * @param valueB
     * @param valueC
     * @return
     */
    private Double calcUnbalanceValue(Double valueA, Double valueB, Double valueC) {
        /**
         * 1、计算三相平均电流,A/B/C三相电流相加除以3
         * 2、 MAX(相电流-三相平均电流)/三相平均电流;
         * 比如三相电流分别为IA=9A IB=8A IC=4A,则三相平均电流为7A,相电流-三相平均电流分别为2A 1A 3A,取差值最大那个,故MAX(相电流-三相平均电流)=3A,所以三相电流不平衡度=3/7。
         */
        Double result = null;
        Double sum = null;
        if (ObjectUtil.isNotNull(valueA)) {
            sum = valueA;
        }
        if (ObjectUtil.isNotNull(valueB)) {
            sum += valueB;
        }
        if (ObjectUtil.isNotNull(valueC)) {
            sum += valueC;
        }
        if (ObjectUtil.isNotNull(sum)) {
            double avg = sum / CommonConst.DIGIT_3;
            double diff1 = 0, diff2 = 0, diff3 = 0;
            if (ObjectUtil.isNotNull(valueA)) {
                diff1 = Math.abs(valueA - avg);
            }
            if (ObjectUtil.isNotNull(valueB)) {
                diff2 = Math.abs(valueB - avg);
            }
            if (ObjectUtil.isNotNull(valueC)) {
                diff3 = Math.abs(valueC - avg);
            }
            double max = diff1;
            if (diff2 > max) {
                max = diff2;
            }
            if (diff3 > max) {
                max = diff3;
            }
            if (avg != CommonConst.DIGIT_DOUBLE_0) {
                result = max * CommonConst.DIGIT_DOUBLE_100 / avg;
            }
        }
 
        return result;
    }
 
    /**
     * 获取对应的结束时间
     *
     * @param timeType
     * @param date
     * @return
     */
    public static Date getEndTime(String timeType, Date date) {
        Date d1 = null;
        switch (timeType) {
            case TimeTypeConst.TIME_TYPE_DAY:
                d1 = DateTimeUtil.addDays(date, CommonConst.DIGIT_1);
                break;
            case TimeTypeConst.TIME_TYPE_MONTH:
                d1 = DateTimeUtil.addMonths(date, CommonConst.DIGIT_1);
                break;
            case TimeTypeConst.TIME_TYPE_YEAR:
                d1 = DateTimeUtil.addYears(date, CommonConst.DIGIT_1);
                break;
            default:
                break;
        }
        return d1;
    }
}