¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.energyMonitor.service.impl; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | 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.enums.CollectionModes; |
| | | import com.zhitan.common.enums.GroupTimeType; |
| | | import com.zhitan.common.utils.ChartUtils; |
| | | import com.zhitan.common.utils.DateTimeUtil; |
| | | import com.zhitan.common.utils.DoubleUtil; |
| | | import com.zhitan.common.utils.StringUtil; |
| | | import com.zhitan.energyMonitor.domain.vo.ListElectricLoadDetail; |
| | | import com.zhitan.energyMonitor.domain.vo.ListElectricLoadItem; |
| | | import com.zhitan.energyMonitor.domain.vo.ListElectricLoadVO; |
| | | import com.zhitan.energyMonitor.service.IElectricLoadService; |
| | | import com.zhitan.model.domain.EnergyIndex; |
| | | import com.zhitan.realtimedata.domain.TagValue; |
| | | import com.zhitan.realtimedata.service.RealtimeDatabaseService; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.joda.time.DateTime; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @Description: sensor_alarm_item |
| | | * @Author: jeecg-boot |
| | | * @Date: 2022-04-19 |
| | | * @Version: V1.0 |
| | | */ |
| | | @Service |
| | | public class ElectricLoadServiceImpl implements IElectricLoadService { |
| | | @Autowired |
| | | private RealtimeDatabaseService realtimeDatabaseService; |
| | | @Resource |
| | | private MeterImplementMapper meterImplementMapper; |
| | | |
| | | @Override |
| | | public ListElectricLoadVO list(String timeType, String timeCode, EnergyIndex energyIndex) { |
| | | ListElectricLoadVO vo = new ListElectricLoadVO(); |
| | | List<ListElectricLoadItem> itemList = new ArrayList<>(); |
| | | vo.setItemList(itemList); |
| | | ListElectricLoadDetail detail = new ListElectricLoadDetail(); |
| | | detail.setMaxTime(CommonConst.DOUBLE_MINUS_SIGN); |
| | | detail.setMax(CommonConst.DOUBLE_MINUS_SIGN); |
| | | detail.setMin(CommonConst.DOUBLE_MINUS_SIGN); |
| | | detail.setMinTime(CommonConst.DOUBLE_MINUS_SIGN); |
| | | detail.setAvg(CommonConst.DOUBLE_MINUS_SIGN); |
| | | detail.setRate(CommonConst.DOUBLE_MINUS_SIGN); |
| | | vo.setDetail(detail); |
| | | |
| | | MeterImplement meterImplement = meterImplementMapper.selectById(energyIndex.getMeterId()); |
| | | |
| | | if (ObjectUtil.isEmpty(meterImplement)) { |
| | | return vo; |
| | | } |
| | | List<Date> dateList = new ArrayList<>(); |
| | | ChartUtils.generateDateList(timeType, timeCode, dateList); |
| | | // å 为influxdb没ææç
§æåç»åæ°æ®ï¼åªè½æç
§æ¥æå¾ªç¯åæ°æ® |
| | | if (TimeTypeConst.TIME_TYPE_YEAR.equals(timeType)) { |
| | | getYearData(timeType, dateList, energyIndex, meterImplement.getMeterName(), itemList); |
| | | } else { |
| | | getDayAndMonthData(timeType, timeCode, energyIndex, meterImplement.getMeterName(), itemList); |
| | | } |
| | | if (!StringUtil.isEmptyOrNull(energyIndex.getCode())) { |
| | | Date start = ChartUtils.getDateTime(timeType, timeCode); |
| | | Date end = ChartUtils.getEndTime(timeType, start); |
| | | |
| | | String code = energyIndex.getCode(); |
| | | TagValue maxTagValueModel = realtimeDatabaseService.statistics(code, start, end, CollectionModes.max); |
| | | TagValue minTagValueModel = realtimeDatabaseService.statistics(code, start, end, CollectionModes.min); |
| | | TagValue avgTagValueModel = realtimeDatabaseService.statistics(code, start, end, CollectionModes.mean); |
| | | |
| | | double avg = CommonConst.DIGIT_DOUBLE_0; |
| | | if (!ObjectUtil.isEmpty(avgTagValueModel) && !ObjectUtil.isEmpty(avgTagValueModel.getValue())) { |
| | | detail.setAvg(DoubleUtil.formatDoubleToStr(avgTagValueModel.getValue()) + CommonConst.ELECTRIC_LOAD_UNIT); |
| | | avg = avgTagValueModel.getValue(); |
| | | } |
| | | |
| | | if (!ObjectUtil.isEmpty(maxTagValueModel) && !ObjectUtil.isEmpty(maxTagValueModel.getValue())) { |
| | | detail.setMax(DoubleUtil.formatDoubleToStr(maxTagValueModel.getValue())); |
| | | detail.setMaxTime(DateTimeUtil.getDateTime(maxTagValueModel.getDataTime(), DateTimeUtil.COMMON_PATTERN)); |
| | | |
| | | if (maxTagValueModel.getValue() != CommonConst.DIGIT_DOUBLE_0) { |
| | | double rate = avg * CommonConst.DIGIT_DOUBLE_100 / maxTagValueModel.getValue(); |
| | | detail.setRate(DoubleUtil.formatDoubleToStr(rate) + CommonConst.SYMBOL_PERCENT); |
| | | } |
| | | } |
| | | |
| | | if (!ObjectUtil.isEmpty(minTagValueModel) && !ObjectUtil.isEmpty(minTagValueModel.getValue())) { |
| | | detail.setMin(DoubleUtil.formatDoubleToStr(minTagValueModel.getValue())); |
| | | detail.setMinTime(DateTimeUtil.getDateTime(minTagValueModel.getDataTime(), DateTimeUtil.COMMON_PATTERN)); |
| | | } |
| | | |
| | | if (!CommonConst.DOUBLE_MINUS_SIGN.equals(detail.getMax())) { |
| | | String m = detail.getMax(); |
| | | detail.setMax(m + CommonConst.ELECTRIC_LOAD_UNIT); |
| | | } |
| | | if (!CommonConst.DOUBLE_MINUS_SIGN.equals(detail.getMin())) { |
| | | String min = detail.getMin(); |
| | | detail.setMin(min + CommonConst.ELECTRIC_LOAD_UNIT); |
| | | } |
| | | if (CommonConst.DOUBLE_MINUS_SIGN.equals(detail.getMax())) { |
| | | detail.setAvg(CommonConst.DOUBLE_MINUS_SIGN); |
| | | } |
| | | } |
| | | |
| | | return vo; |
| | | } |
| | | |
| | | /** |
| | | * è·åæåå¤©æ°æ®,å 为influxdbå¯ä»¥æç
§åãæ¶ã天åç»åæ°ï¼ä¸å¯ä»¥æç
§æåç»åæ°ï¼æä»¥åæä¸¤ä¸ªæ¹æ³æ¥å |
| | | */ |
| | | private void getDayAndMonthData(String timeType, String timeCode, EnergyIndex energyIndex, String meterName, List<ListElectricLoadItem> itemList) { |
| | | String tagCodes = energyIndex.getCode(); |
| | | List<TagValue> maxList = new ArrayList<>(); |
| | | List<TagValue> minList = new ArrayList<>(); |
| | | List<TagValue> avgList = new ArrayList<>(); |
| | | if (TimeTypeConst.TIME_TYPE_DAY.equals(timeType)) { |
| | | } else { |
| | | String tempTimeCode = StringUtil.ifEmptyOrNullReturnValue(timeCode).replace(CommonConst.SINGLE_MINUS_SIGN, CommonConst.EMPTY); |
| | | Date start = DateTimeUtil.toDateTime(tempTimeCode, DateTimeUtil.COMMON_PATTERN_MONTH); |
| | | Date end = DateTimeUtil.addMonths(start, CommonConst.DIGIT_1); |
| | | if (!StringUtil.isEmptyOrNull(tagCodes)) { |
| | | maxList = realtimeDatabaseService.statistics(tagCodes, start, end, CollectionModes.max, GroupTimeType.d); |
| | | minList = realtimeDatabaseService.statistics(tagCodes, start, end, CollectionModes.min, GroupTimeType.d); |
| | | avgList = realtimeDatabaseService.statistics(tagCodes, start, end, CollectionModes.mean, GroupTimeType.d); |
| | | } |
| | | } |
| | | if (CollectionUtils.isEmpty(maxList)) { |
| | | maxList = new ArrayList<>(); |
| | | } |
| | | if (CollectionUtils.isEmpty(minList)) { |
| | | minList = new ArrayList<>(); |
| | | } |
| | | if (CollectionUtils.isEmpty(avgList)) { |
| | | avgList = new ArrayList<>(); |
| | | } |
| | | List<Date> dateList = new ArrayList<>(); |
| | | ChartUtils.generateDateList(timeType, timeCode, dateList); |
| | | Date now = new Date(); |
| | | Date temNow = now; |
| | | switch (timeType) { |
| | | case TimeTypeConst.TIME_TYPE_DAY: |
| | | //å½åæ¶é´è½¬ææ´æ¶æ´ç¹ |
| | | temNow = DateTimeUtil.getHourTime(TimeTypeConst.TIME_TYPE_HOUR, now); |
| | | break; |
| | | case TimeTypeConst.TIME_TYPE_MONTH: |
| | | //å½åæ¶é´è½¬ææ´æ¶æ´ç¹ |
| | | temNow = DateTimeUtil.getHourTime(TimeTypeConst.TIME_TYPE_DAY, now); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | for (Date date : dateList) { |
| | | ListElectricLoadItem temp = new ListElectricLoadItem(); |
| | | temp.setTimeCode(ChartUtils.getTimeCode(timeType, date)); |
| | | temp.setName(ObjectUtils.isNotEmpty(meterName) ? meterName : ""); |
| | | temp.setMax(CommonConst.DOUBLE_MINUS_SIGN); |
| | | temp.setMin(CommonConst.DOUBLE_MINUS_SIGN); |
| | | temp.setAvg(CommonConst.DOUBLE_MINUS_SIGN); |
| | | temp.setTimeCodeChart(ChartUtils.getTimeCodeChart(timeType, date)); |
| | | temp.setRate(CommonConst.DOUBLE_MINUS_SIGN); |
| | | temp.setValue(CommonConst.DOUBLE_MINUS_SIGN); |
| | | itemList.add(temp); |
| | | // 妿大äºå½åæ¶é´é»è®¤-- |
| | | if (DateTimeUtil.compareDateDiff(date, temNow) > CommonConst.DIGIT_0) { |
| | | continue; |
| | | } |
| | | //天ç夿 |
| | | if (TimeTypeConst.TIME_TYPE_DAY.equals(timeType)) { |
| | | // ç±äºå®æ¶åºè¿åçæ¶é´å¯¹åºå¼ä»£è¡¨çæ¯åä¸ä¸ªå¨æçå¼ |
| | | Date nextHour = DateTimeUtil.addHours(date, CommonConst.DIGIT_1); |
| | | if (!StringUtil.isEmptyOrNull(energyIndex.getCode())) { |
| | | TagValue tagValue = realtimeDatabaseService.retrieve(energyIndex.getCode(), nextHour); |
| | | if (!ObjectUtil.isEmpty(tagValue)) { |
| | | if (ObjectUtils.isEmpty(tagValue) || ObjectUtils.isEmpty(tagValue.getValue())) { |
| | | temp.setValue(CommonConst.DOUBLE_MINUS_SIGN); |
| | | } else { |
| | | temp.setValue(String.valueOf(DoubleUtil.formatDouble(tagValue.getValue()))); |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | //æç夿ï¼ç±äºå®æ¶åºè¿åçæ¶é´å¯¹åºå¼ä»£è¡¨çæ¯åä¸ä¸ªå¨æçå¼ |
| | | Date nextDay = DateTimeUtil.addDays(date, CommonConst.DIGIT_1); |
| | | TagValue tagValueMonth = maxList.stream().filter(x -> (new DateTime(x.getDataTime())).withTimeAtStartOfDay().toDate().equals(nextDay)).findAny().orElse(null); |
| | | if (!ObjectUtil.isEmpty(tagValueMonth)) { |
| | | //æçæ¾å¤© |
| | | TagValue minModel = minList.stream().filter(x -> x.getDataTime().equals(tagValueMonth.getDataTime())).findAny().orElse(null); |
| | | if (!ObjectUtil.isEmpty(minModel)) { |
| | | temp.setMin(String.valueOf(DoubleUtil.formatDouble(minModel.getValue()))); |
| | | } |
| | | TagValue avgModel = avgList.stream().filter(x -> x.getDataTime().equals(tagValueMonth.getDataTime())).findAny().orElse(null); |
| | | if (!ObjectUtil.isEmpty(avgModel)) { |
| | | temp.setAvg(String.valueOf(DoubleUtil.formatDouble(avgModel.getValue()))); |
| | | } |
| | | temp.setMax(String.valueOf(DoubleUtil.formatDouble(tagValueMonth.getValue()))); |
| | | |
| | | temp.setRate(CommonConst.DOUBLE_MINUS_SIGN); |
| | | if (Double.parseDouble(temp.getMax()) != CommonConst.DIGIT_DOUBLE_0) { |
| | | double rate = Double.parseDouble(temp.getAvg()) * 100 / Double.parseDouble(temp.getMax()); |
| | | temp.setRate(DoubleUtil.formatDoubleToStr(rate) + CommonConst.SYMBOL_PERCENT); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åå¹´æ°æ® |
| | | */ |
| | | private void getYearData(String timeType, List<Date> dateList, EnergyIndex energyIndex, String meterName, List<ListElectricLoadItem> itemList) { |
| | | String tagCode = StringUtil.ifEmptyOrNullReturnValue(energyIndex.getCode()); |
| | | for (Date date : dateList) { |
| | | ListElectricLoadItem temp = new ListElectricLoadItem(); |
| | | Date endTime = DateTimeUtil.addMonths(date, CommonConst.DIGIT_1); |
| | | temp.setAvg(CommonConst.DOUBLE_MINUS_SIGN); |
| | | temp.setMax(CommonConst.DOUBLE_MINUS_SIGN); |
| | | temp.setMin(CommonConst.DOUBLE_MINUS_SIGN); |
| | | if (!StringUtil.isEmptyOrNull(tagCode)) { |
| | | String code = energyIndex.getCode(); |
| | | TagValue rt3 = realtimeDatabaseService.statistics(code, date, endTime, CollectionModes.max); |
| | | TagValue rt4 = realtimeDatabaseService.statistics(code, date, endTime, CollectionModes.min); |
| | | TagValue rt2 = realtimeDatabaseService.statistics(code, date, endTime, CollectionModes.mean); |
| | | if (ObjectUtils.isNotEmpty(rt2.getValue())) { |
| | | temp.setAvg(String.valueOf(DoubleUtil.formatDouble(rt2.getValue()))); |
| | | } |
| | | if (ObjectUtils.isNotEmpty(rt3.getValue())) { |
| | | temp.setMax(String.valueOf(DoubleUtil.formatDouble(rt3.getValue()))); |
| | | } |
| | | if (ObjectUtils.isNotEmpty(rt4.getValue())) { |
| | | temp.setMin(String.valueOf(DoubleUtil.formatDouble(rt4.getValue()))); |
| | | } |
| | | } |
| | | |
| | | temp.setTimeCode(ChartUtils.getTimeCode(timeType, date)); |
| | | temp.setTimeCodeChart(ChartUtils.getTimeCodeChart(timeType, date)); |
| | | temp.setName(StringUtil.ifEmptyOrNullReturnValue(meterName)); |
| | | temp.setRate(CommonConst.DOUBLE_MINUS_SIGN); |
| | | temp.setValue(CommonConst.DOUBLE_MINUS_SIGN); |
| | | if (!temp.getMax().equals(CommonConst.DOUBLE_MINUS_SIGN) && Double.parseDouble(temp.getMax()) != CommonConst.DIGIT_DOUBLE_0) { |
| | | double rate = Double.parseDouble(temp.getAvg()) * 100 / Double.parseDouble(temp.getMax()); |
| | | temp.setRate(DoubleUtil.formatDoubleToStr(rate) + CommonConst.SYMBOL_PERCENT); |
| | | } |
| | | itemList.add(temp); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |