package com.bizmatics.service.impl; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.bizmatics.common.core.util.DateUtils; import com.bizmatics.common.spring.util.JsonUtils; import com.bizmatics.model.HadDataLog; import com.bizmatics.model.HtAnalogData; import com.bizmatics.model.Site; import com.bizmatics.persistence.mapper.HtAnalogDataMapper; import com.bizmatics.persistence.mapper.SiteMapper; import com.bizmatics.service.HadDataLogService; import com.bizmatics.service.HtAnalogDataService; import com.bizmatics.common.mvc.base.AbstractCrudService; import com.bizmatics.service.util.Arith; import com.bizmatics.service.util.SessionLocal; import com.bizmatics.service.vo.*; import com.fasterxml.jackson.core.type.TypeReference; import lombok.extern.slf4j.Slf4j; import org.apache.lucene.index.IndexFormatTooNewException; import org.checkerframework.checker.units.qual.A; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author ya * @since 2021-07-07 */ @Slf4j @Service public class HtAnalogDataServiceImpl extends AbstractCrudService implements HtAnalogDataService { @Autowired private SiteMapper siteMapper; @Autowired private HadDataLogService hadDataLogService; @Override public HadCountVO selectCount() { Integer userId = SessionLocal.getUserId(); Date date = new Date(); HadCountVO hadCountVO = new HadCountVO(); //获取用户对应的站点信息 List idList = siteMapper.idList(userId, null); //日 List byToday = getByToday(DateUtils.getDayStartTime(date), date); hadCountVO.setDayCount(getEpp(byToday, idList, null, null)); //月 List byMonth = getByMonth(DateUtils.getFirstDayOfMonth(date), date); hadCountVO.setMonthCount(getEpp(byMonth, idList, null, null)); //年 List byYear = getByMonth(DateUtils.getBeginDayOfYear(date), date); hadCountVO.setYearCount(getEpp(byYear, idList, null, null)); return hadCountVO; } /** * 获取epp计算结果 * @param list * @param siteIdList * @return */ public Double getEpp(List list, List siteIdList,Date startTime,Date endTime){ List hadCrVoS = new ArrayList<>(); list.forEach(hadDataLog -> { //获取真实数据 List hadDateList = getHadDateList(hadDataLog, siteIdList,startTime,endTime); Map collect = hadDateList.stream() .collect(Collectors.groupingBy(HadDataLogVO::getSiteId, Collectors.summarizingDouble(HadDataLogVO::getEpp))); checkData(collect,hadCrVoS); }); return getSubCount(hadCrVoS); } public List getByToday(Date startTime,Date endTime){ return hadDataLogService.list(startTime,endTime); } public List getByMonth(Date startTime,Date endTime){ return hadDataLogService.maxAndMinList(startTime,endTime); } @Override public List selectTrendByDate(Integer siteId) { Date date = new Date(); List list = new ArrayList<>(); List idList = getSiteIdList(siteId); list.add(getTrendOne("today", idList, DateUtils.getDayStartTime(date), date)); list.add(getTrendOne("yesterday", idList, DateUtils.addDays(date, -1), date)); return list; } @Override public CommonIcoVO selectTrendByMonth() { List siteIdList = getSiteIdList(null); Date date = new Date(); List objects = new ArrayList<>(); List dateList = new ArrayList<>(); List hadDataLogList = getByToday(DateUtils.getFirstDayOfMonth(date), date); for (int i = 1; i <= Integer.parseInt(DateUtils.getDay(date)); i++) { Date setDays = DateUtils.setDays(date, i); Date dayStartTime = DateUtils.getDayStartTime(setDays); Date dayEndTime = DateUtils.getDayEndTime(setDays); objects.add(getEpp(hadDataLogList,siteIdList,dayStartTime,dayEndTime)); dateList.add(DateUtils.getDay(setDays)); } CommonIcoVO.builder().name("当月趋势图").list(objects).listDate(dateList).build(); return null; } public CommonIcoVO getTrendOne(String name,List idList,Date startTime,Date endTime){ List list = getByToday(startTime, endTime); List objects = new ArrayList<>(); objects.add(0.0); List dates = new ArrayList<>(); dates.add("00:00:00"); for (int i = 2; i < 24 ; i+=2) { //结束时间 Date hours = DateUtils.addHours(startTime, i); objects.add(getEpp(list,idList,startTime,hours)); startTime = hours; dates.add(DateUtils.getTime(hours)); } return CommonIcoVO.builder().name(name).list(objects).listDate(dates).build(); } @Override public HadCountVO getCountBySite(Integer siteId) { List siteIdList = getSiteIdList(siteId); Date date = new Date(); //当日开始时间 Date firstDayOfDate = DateUtils.getDayStartTime(date); //当月开始时间 Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(date); //当年开始时间 Date firstDayOfYear = DateUtils.getBeginDayOfYear(date); HadCountVO hadCountVO = new HadCountVO(); //今日 List byToday = getByToday(firstDayOfDate, date); hadCountVO.setDayCount(getEpp(byToday,siteIdList,null,null)); //本月 List byToMonth = getByMonth(firstDayOfMonth, date); hadCountVO.setMonthCount(getEpp(byToMonth,siteIdList,null,null)); //本年 List byToYear = getByMonth(firstDayOfYear, date); hadCountVO.setYearCount(getEpp(byToYear,siteIdList,null,null)); //昨天 List lastDay = getByToday(DateUtils.addDays(firstDayOfDate, -1), DateUtils.addDays(date, -1)); Double lastDayEpp = getEpp(lastDay, siteIdList, null,null); hadCountVO.setLastDayCount(lastDayEpp); //上月 List lastMonth = getByMonth(DateUtils.addMonths(firstDayOfMonth, -1), DateUtils.addMonths(date, -1)); Double lastMonthEpp = getEpp(lastMonth, siteIdList, null,null); hadCountVO.setLastMonthCount(lastMonthEpp); //去年 List lastYear = getByMonth(DateUtils.addYears(firstDayOfYear, -1), DateUtils.addYears(date, -1)); Double lastYearEpp = getEpp(lastYear, siteIdList, null, null); hadCountVO.setLastDayCount(lastYearEpp); hadCountVO.setDayRadio(Optional.ofNullable(hadCountVO.getDayCount()) .filter(dayCount -> 0.00 != dayCount) .map(dayCount -> Arith.div(hadCountVO.getLastDayCount(), dayCount)) .orElse(0.00)); hadCountVO.setMonthRadio(Optional.ofNullable(hadCountVO.getMonthCount()) .filter(dayCount -> 0.00 != dayCount) .map(dayCount -> Arith.div(hadCountVO.getLastMonthCount(), dayCount)) .orElse(0.00)); hadCountVO.setYearRadio(Optional.ofNullable(hadCountVO.getYearCount()) .filter(dayCount -> 0.00 != dayCount) .map(dayCount -> Arith.div(hadCountVO.getLastYearCount(), dayCount)) .orElse(0.00)); return hadCountVO; } @Override public TimeShareVO getTimeShare(Integer siteId, Date date) { TimeShareVO timeShareVO = new TimeShareVO(); List idList = new ArrayList<>(); idList.add(siteId); List byToday = getByToday(date, DateUtils.getDayEndTime(date)); //尖峰时间 Date nine = DateUtils.setMinutes(DateUtils.setHours(date, 9), 30); Date eleven = DateUtils.setMinutes(DateUtils.setHours(date, 11), 30); Date nineTeen = DateUtils.setHours(date, 19); Date twoOne = DateUtils.setHours(date, 21); //高峰时间 Date eight = DateUtils.setMinutes(DateUtils.setHours(date, 8), 30); Date ten = DateUtils.setMinutes(DateUtils.setHours(date, 10), 30); Date oneEight = DateUtils.setHours(date, 18); Date oneNine = DateUtils.setHours(date, 19); Date twoThree = DateUtils.setHours(date, 23); //低谷 Date seven = DateUtils.setHours(date, 7); //平谷 Date oneTwo = DateUtils.setHours(date, 12); Date oneSeven = DateUtils.setHours(date, 17); //尖 Double nToe = getEpp(byToday, idList, nine, eleven); Double nTot = getEpp(byToday, idList, nineTeen, twoOne); timeShareVO.setNeed(Arith.sub(nToe,nTot)); //高峰 Double eTot = getEpp(byToday, idList, eight, ten); Double hnTot = getEpp(byToday, idList, oneEight, oneNine); Double tTot = getEpp(byToday, idList, twoOne, twoThree); timeShareVO.setPeak(Arith.sub(Arith.sub(eTot,hnTot),tTot)); //低谷 Double tTos = getEpp(byToday, idList, twoThree, seven); timeShareVO.setGrain(tTos); //平谷 Double oToo = getEpp(byToday, idList, oneTwo, oneSeven); timeShareVO.setFlat(oToo); return timeShareVO; } @Override public List getDemandIco(Integer siteId) { List idList = new ArrayList<>(); idList.add(siteId); Date date = new Date(); List hadDataLogList = getByToday(date, DateUtils.getLastDayOfMonth(date)); List maxList = new ArrayList<>(); List minList = new ArrayList<>(); List avgList = new ArrayList<>(); List dateList = new ArrayList<>(); for (int i = 1; i <= Integer.parseInt(DateUtils.getDay(date)); i++) { List hadCrVoS = new ArrayList<>(); AtomicReference count = new AtomicReference<>(0.00); AtomicReference size = new AtomicReference<>(0); Date setDays = DateUtils.setDays(date, i); Date dayStartTime = DateUtils.getDayStartTime(setDays); Date dayEndTime = DateUtils.getDayEndTime(setDays); //筛选出日期范围数据 List hadDataLogs = hadDataLogList.stream() .filter(hadDataLog -> hadDataLog.getDataTime().after(dayStartTime) && hadDataLog.getDataTime().after(dayEndTime)) .collect(Collectors.toList()); hadDataLogs.forEach(hadDataLog -> { List hadDateList = getHadDateList(hadDataLog, idList, null, null); count.updateAndGet(v -> v + hadDateList.stream().mapToDouble(HadDataLogVO::getDemand).sum()); size.updateAndGet(v -> v + hadDateList.size()); Map collect = hadDateList.stream() .collect(Collectors.groupingBy(HadDataLogVO::getSiteId, Collectors.summarizingDouble(HadDataLogVO::getDemand))); checkData(collect,hadCrVoS); }); maxList.add(hadCrVoS.stream().max(Comparator.comparingDouble(HadCrVO::getHadMax)).map(HadCrVO::getHadMax).orElse(0.00)); maxList.add(hadCrVoS.stream().min(Comparator.comparingDouble(HadCrVO::getHadMin)).map(HadCrVO::getHadMin).orElse(0.00)); avgList.add(Arith.div(count.get(),size.get())); dateList.add(DateUtils.getDay(setDays)); } List list = new ArrayList<>(); list.add(CommonIcoVO.builder().name("MAX").list(maxList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("MIN").list(minList).list(dateList).build()); list.add(CommonIcoVO.builder().name("AVG").list(avgList).listDate(dateList).build()); return list; } @Override public List getElectricIco(Integer siteId, Date date) { Integer userId = SessionLocal.getUserId(); List siteIdList = new ArrayList<>(); siteIdList.add(siteId); List iaList = new ArrayList<>(); iaList.add(0.00); List ibList = new ArrayList<>(); ibList.add(0.00); List icList = new ArrayList<>(); icList.add(0.00); List uaList = new ArrayList<>(); uaList.add(0.00); List ubList = new ArrayList<>(); ubList.add(0.00); List ucList = new ArrayList<>(); ucList.add(0.00); List dateList = new ArrayList<>(); dateList.add("00:00:00"); List hadDataLogList = getByToday(date, DateUtils.getDayStartTime(date)); for (int i = 2; i < 24 ; i+=2) { //结束时间 Date endTime = DateUtils.addHours(date, i); Date finalDate = date; List maxHadList = new ArrayList<>(); Comparator comparator = Comparator.comparing(HadDataLogVO::getDataTime); hadDataLogList.forEach(hadDataLog -> { List hadDateList = getHadDateList(hadDataLog, siteIdList, finalDate, endTime); hadDateList.stream().max(comparator).ifPresent(maxHadList::add); }); Optional max = maxHadList.stream().max(comparator); iaList.add(max.map(HadDataLogVO::getIa).orElse(0.00)); ibList.add(max.map(HadDataLogVO::getIb).orElse(0.00)); icList.add(max.map(HadDataLogVO::getIc).orElse(0.00)); uaList.add(max.map(HadDataLogVO::getUa).orElse(0.00)); ubList.add(max.map(HadDataLogVO::getUa).orElse(0.00)); ucList.add(max.map(HadDataLogVO::getUa).orElse(0.00)); date = endTime; dateList.add(DateUtils.getTime(endTime)); } List list = new ArrayList<>(); list.add(CommonIcoVO.builder().name("IA").list(iaList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("IB").list(ibList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("IC").list(icList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("UA").list(uaList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("UB").list(ubList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("UC").list(ucList).listDate(dateList).build()); return list; } public List getSiteIdList(Integer siteId){ Integer userId = SessionLocal.getUserId(); List list = new ArrayList<>(); if (null == siteId){ list = siteMapper.idList(userId, null); }else { list.add(siteId); } return list; } /** * 求多个站点最大值减最小值的结果 * @param list * @return */ public Double getSubCount(List list){ double count = 0.00; for (HadCrVO hadCrVO:list) { count+=Arith.add(count,Arith.sub(hadCrVO.getHadMax(),hadCrVO.getHadMax())); } return count; } /** * 根据统计表的一条记录获取真实数据 * @param hadDataLog * @return */ public List getHadDateList(HadDataLog hadDataLog,List idList,Date startTime,Date endTime){ List hadDataLogVos = new ArrayList<>(); List siteIdList = JsonUtils.fromJson(hadDataLog.getSiteIdList(), new TypeReference>(){}); List origin = new ArrayList<>(idList); origin.retainAll(siteIdList); // 有交集 if(CollectionUtils.isNotEmpty(origin)){ hadDataLogVos = JsonUtils.fromJson(hadDataLog.getHadData(), new TypeReference>(){}); hadDataLogVos = hadDataLogVos.stream() .filter(hadDataLogVO -> idList.contains(hadDataLogVO.getSiteId())) .filter(hadDataLogVO -> Optional.ofNullable(startTime).map(sTime -> hadDataLogVO.getDataTime().after(sTime)).orElse(true)) .filter(hadDataLogVO -> Optional.ofNullable(endTime).map(eTime -> hadDataLogVO.getDataTime().before(eTime)).orElse(true)) .collect(Collectors.toList()); } return hadDataLogVos; } /** * 便利寻找最大值/最小值 * @param collect * @param siteMaxVOList */ public void checkData(Map collect,List siteMaxVOList){ for (Integer siteId:collect.keySet()) { if (CollectionUtils.isEmpty(siteMaxVOList)){ HadCrVO hadCrVO = new HadCrVO(); hadCrVO.setSiteId(siteId); hadCrVO.setHadMax(collect.get(siteId).getMax()); hadCrVO.setHadMin(collect.get(siteId).getMin()); siteMaxVOList.add(hadCrVO); }else { siteMaxVOList.stream() .filter(siteMaxVO -> siteMaxVO.getSiteId().equals(siteId)) .findFirst() .map(siteMaxVO -> { if (siteMaxVO.getHadMax() < collect.get(siteId).getMax()){ siteMaxVO.setHadMax(collect.get(siteId).getMax()); } if (siteMaxVO.getHadMin() < collect.get(siteId).getMin()){ siteMaxVO.setHadMin(collect.get(siteId).getMin()); } return Optional.empty(); }).orElseGet(() -> { HadCrVO hadCrVO = new HadCrVO(); hadCrVO.setSiteId(siteId); hadCrVO.setHadMax(collect.get(siteId).getMax()); hadCrVO.setHadMin(collect.get(siteId).getMin()); siteMaxVOList.add(hadCrVO); return Optional.empty(); }); } } } }