package com.bizmatics.service.impl; import com.bizmatics.common.core.util.Arith; import com.bizmatics.common.core.util.DateUtils; import com.bizmatics.common.mvc.base.AbstractCrudService; import com.bizmatics.common.spring.util.JsonUtils; import com.bizmatics.model.HadSiteStatic; import com.bizmatics.model.HtAnalogData; import com.bizmatics.persistence.mapper.HtAnalogDataMapper; import com.bizmatics.service.HadSiteStaticService; import com.bizmatics.service.HtAnalogDataService; import com.bizmatics.service.util.SecurityUtils; import com.bizmatics.service.vo.CommonIcoVO; import com.bizmatics.service.vo.HadCountVO; import com.bizmatics.service.vo.TimeShareVO; import com.fasterxml.jackson.core.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Optional; /** * @author yq * @date 2021/7/20 16:49 */ @Service public class HtAnalogDataServiceImpl extends AbstractCrudService implements HtAnalogDataService { @Autowired private HadSiteStaticService hadSiteStaticService; @Override public HadCountVO selectCount() { HadCountVO hadCountVO = new HadCountVO(); Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue(); Date date = new Date(); hadCountVO.setDayCount(hadSiteStaticService.getCount(userId, DateUtils.getDayStartTime(date), date, null)); hadCountVO.setMonthCount(hadSiteStaticService.getCount(userId, DateUtils.getFirstDayOfMonth(date), date, null)); hadCountVO.setYearCount(hadSiteStaticService.getCount(userId, DateUtils.getBeginDayOfYear(date), date, null)); return hadCountVO; } @Override public List selectTrendByDate(Integer siteId) { Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue(); Date date = new Date(); Date sTime = DateUtils.getDayStartTime(date); Date eTime = DateUtils.getDayEndTime(date); Date ysTime = DateUtils.addDays(sTime, -1); Date yeTime = DateUtils.addDays(eTime, -1); List list = new ArrayList<>(); if (null != siteId) { list.add(getTreedBySite("昨日", siteId, ysTime, yeTime)); list.add(getTreedBySite("今日", siteId, sTime, eTime)); } else { list.add(getTrendOne("昨日", userId, ysTime, yeTime)); list.add(getTrendOne("今日", userId, sTime, eTime)); } return list; } public CommonIcoVO getTrendOne(String name, Integer userId, Date startTime, Date endTime) { List list = hadSiteStaticService.list(null, startTime, endTime, userId); 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.setHours(startTime, i); startTime = hours; dates.add(DateUtils.getTime(hours)); Double hourCount = 0.00; for (HadSiteStatic hadSiteStatic : list) { CommonIcoVO commonIcoVO = JsonUtils.fromJson(hadSiteStatic.getLoadIco(), CommonIcoVO.class); List dataList = (List) commonIcoVO.getList(); hourCount += dataList.get(i / 2); } objects.add(Arith.div(hourCount, 1, 2)); } return CommonIcoVO.builder().name(name).list(objects).listDate(dates).build(); } CommonIcoVO getTreedBySite(String name, Integer siteId, Date startTime, Date endTime) { Date sTime = DateUtils.getDayStartTime(startTime); Date eTime = DateUtils.getDayEndTime(endTime); HadSiteStatic hadSiteStatic = hadSiteStaticService.oneBySite(siteId, sTime, eTime); CommonIcoVO commonIcoVO = null; if (null != hadSiteStatic) { commonIcoVO = JsonUtils.fromJson(hadSiteStatic.getLoadIco(), CommonIcoVO.class); commonIcoVO.setName(name); } else { 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.setHours(startTime, i); objects.add(0.0); startTime = hours; dates.add(DateUtils.getTime(hours)); } commonIcoVO = CommonIcoVO.builder().name(name).list(objects).listDate(dates).build(); } return commonIcoVO; } @Override public List selectTrendByMonth() { Date date = new Date(); Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue(); List list = new ArrayList<>(); list.add(selectTreedMonthOne(DateUtils.getLastDayOfMonth(DateUtils.addMonths(date, -1)), userId, "上月趋势图")); list.add(selectTreedMonthOne(DateUtils.getLastDayOfMonth(date), userId, "当月趋势图")); return list; } public CommonIcoVO selectTreedMonthOne(Date date, Integer userId, String name) { List objects = new ArrayList<>(); List dateList = new ArrayList<>(); 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(hadSiteStaticService.getCount(userId, dayStartTime, dayEndTime, null)); dateList.add(DateUtils.getDay(setDays)); } return CommonIcoVO.builder().name(name).list(objects).listDate(dateList).build(); } @Override public HadCountVO getCountBySite(Integer siteId) { Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue(); Date date = new Date(); //当日开始时间 Date firstDayOfDate = DateUtils.getDayStartTime(date); //当月开始时间 Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(date); //当年开始时间 Date firstDayOfYear = DateUtils.getBeginDayOfYear(date); HadCountVO hadCountVO = new HadCountVO(); hadCountVO.setDayCount(hadSiteStaticService.getCount(userId, DateUtils.getDayStartTime(date), date, siteId)); hadCountVO.setMonthCount(hadSiteStaticService.getCount(userId, DateUtils.getFirstDayOfMonth(date), date, siteId)); hadCountVO.setYearCount(hadSiteStaticService.getCount(userId, DateUtils.getBeginDayOfYear(date), date, siteId)); hadCountVO.setLastDayCount(hadSiteStaticService.getCount(userId, DateUtils.addDays(firstDayOfDate, -1), DateUtils.addDays(date, -1), siteId)); hadCountVO.setLastMonthCount(hadSiteStaticService.getCount(userId, DateUtils.addMonths(firstDayOfMonth, -1), DateUtils.addMonths(date, -1), siteId)); hadCountVO.setLastYearCount(hadSiteStaticService.getCount(userId, DateUtils.addYears(firstDayOfYear, -1), DateUtils.addYears(date, -1), siteId)); 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, Integer type) { TimeShareVO timeShareVO = new TimeShareVO(); Date date = new Date(); if (0 == type) { HadSiteStatic hadSiteStatic = hadSiteStaticService.oneBySite(siteId, date, date); Optional.ofNullable(hadSiteStatic).ifPresent(hads -> JsonUtils.fromJson(hads.getTimeShare(), timeShareVO.getClass())); } else { List list = hadSiteStaticService.list(siteId, DateUtils.getFirstDayOfMonth(date), date); for (HadSiteStatic hadSiteStatic : list) { Optional.ofNullable(hadSiteStatic.getTimeShare()) .ifPresent(has -> { TimeShareVO ts = JsonUtils.fromJson(has, TimeShareVO.class); timeShareVO.setFlat(Arith.add(timeShareVO.getFlat(), ts.getFlat())); timeShareVO.setPeak(Arith.add(timeShareVO.getPeak(), ts.getPeak())); timeShareVO.setNeed(Arith.add(timeShareVO.getNeed(), ts.getNeed())); timeShareVO.setGrain(Arith.add(timeShareVO.getGrain(), ts.getGrain())); }); } } return timeShareVO; } @Override public List getDemandIco(Integer siteId) { Date date = new Date(); List maxList = new ArrayList<>(); List minList = new ArrayList<>(); List avgList = new ArrayList<>(); List dateList = new ArrayList<>(); List hadSiteStatics = hadSiteStaticService.list(siteId, 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); maxList.add(hadSiteStatics.stream() .filter(hadSiteStatic -> DateUtils.isEffectiveDate(hadSiteStatic.getHadTime(), dayStartTime, dayEndTime) ).findFirst().map(HadSiteStatic::getMaxDemand).orElse(0.00)); minList.add(hadSiteStatics.stream() .filter(hadSiteStatic -> DateUtils.isEffectiveDate(hadSiteStatic.getHadTime(), dayStartTime, dayEndTime) ).findFirst().map(HadSiteStatic::getMinDemand).orElse(0.00)); avgList.add(hadSiteStatics.stream() .filter(hadSiteStatic -> DateUtils.isEffectiveDate(hadSiteStatic.getHadTime(), dayStartTime, dayEndTime) ).findFirst().map(HadSiteStatic::getAvgDemand).orElse(0.00)); dateList.add(DateUtils.getDay(setDays)); } List list = new ArrayList<>(); list.add(CommonIcoVO.builder().name("最大值").list(maxList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("最小值").list(minList).listDate(dateList).build()); list.add(CommonIcoVO.builder().name("平均值").list(avgList).listDate(dateList).build()); return list; } @Override public List getElectricIco(Integer siteId, Date date) { 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"); HadSiteStatic hadSiteStatic = hadSiteStaticService.oneBySite(siteId, date, date); List htList = Optional.ofNullable(hadSiteStatic) .map(HadSiteStatic::getHisIco) .map(his -> JsonUtils.fromJson(hadSiteStatic.getHisIco(), new TypeReference>() { })) .orElse(null); for (int i = 2; i < 24; i += 2) { Date hours = DateUtils.setHours(date, i); date = hours; Date finalDate = date; Optional htAnalogData = Optional.ofNullable(htList).flatMap(hts -> hts.stream() .filter(hads -> DateUtils.isEffectiveDate(hads.getDataTime(), finalDate, hours)) .findFirst()); iaList.add(htAnalogData.map(HtAnalogData::getIa).orElse(0.00)); ibList.add(htAnalogData.map(HtAnalogData::getIb).orElse(0.00)); icList.add(htAnalogData.map(HtAnalogData::getIc).orElse(0.00)); uaList.add(htAnalogData.map(HtAnalogData::getUa).orElse(0.00)); ubList.add(htAnalogData.map(HtAnalogData::getUb).orElse(0.00)); ucList.add(htAnalogData.map(HtAnalogData::getUc).orElse(0.00)); dateList.add(htAnalogData.map(HtAnalogData::getDataTime).map(s -> DateUtils.getTime(hours)).orElse(DateUtils.getTime(hours))); } 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; } }