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.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 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
*/
@Service
public class HtAnalogDataServiceImpl extends AbstractCrudService implements HtAnalogDataService {
@Autowired
private HtAnalogDataMapper htAnalogDataMapper;
@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 todayList = hadDataLogService.list(DateUtils.getDayStartTime(date),date);
for (HadCrVO hadCr:getEppData(idList, todayList)) {
hadCountVO.setDayCount(Arith.add(hadCountVO.getDayCount(),Arith.sub(hadCr.getHadMax(),hadCr.getHadMax())));
}
//月
Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(date);
hadCountVO.setMonthCount(getMonthAndYearDate(firstDayOfMonth, date, idList));
//年
Date beginDayOfYear = DateUtils.getBeginDayOfYear(date);
hadCountVO.setYearCount(getMonthAndYearDate(beginDayOfYear, date, idList));
return hadCountVO;
}
public Double getMonthAndYearDate(Date startTime,Date endTime,List idList){
AtomicReference count = new AtomicReference<>(0.00);
List startList = hadDataLogService.list(startTime,DateUtils.getDayEndTime(startTime));
List endList = hadDataLogService.list(DateUtils.getDayStartTime(endTime),endTime);
List startEppData = getEppData(idList, startList);
List endEppData = getEppData(idList, endList);
for (HadCrVO hadCrVO:startEppData) {
endEppData.stream()
.filter(hadCr -> hadCr.getSiteId().equals(hadCrVO.getSiteId()))
.findAny()
.ifPresent(had ->{
count.updateAndGet(v -> v + had.getHadMax() - hadCrVO.getHadMin());
});
}
return count.get();
}
@Override
public List selectTrendByDate(Date date,Integer siteId) {
Integer userId = SessionLocal.getUserId();
List list = new ArrayList<>();
list.add(getByDate("today", userId, siteId, date));
list.add(getByDate("yesterday", userId, siteId, DateUtils.addDays(date,-1)));
return list;
}
@Override
public HadCountVO getCountBySite(Integer siteId) {
Date date = new Date();
Integer userId = SessionLocal.getUserId();
//当日开始时间
Date firstDayOfDate = DateUtils.getDayStartTime(date);
//当月开始时间
Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(date);
//当年开始时间
Date firstDayOfYear = DateUtils.getBeginDayOfYear(date);
HadCountVO hadCountVO = new HadCountVO();
hadCountVO.setDayCount(getElectricity(userId,siteId,firstDayOfDate,date));
hadCountVO.setMonthCount(getElectricity(userId,siteId,firstDayOfMonth,date));
hadCountVO.setYearCount(getElectricity(userId,siteId,firstDayOfYear,date));
hadCountVO.setLastDayCount(getElectricity(userId,siteId,DateUtils.addDays(firstDayOfDate, -1),DateUtils.addDays(date, -1)));
hadCountVO.setLastMonthCount(getElectricity(userId,siteId,DateUtils.addMonths(firstDayOfMonth, -1),DateUtils.addMonths(date, -1)));
hadCountVO.setLastYearCount(getElectricity(userId,siteId,DateUtils.addYears(firstDayOfYear, -1),DateUtils.addYears(date, -1)));
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) {
Integer userId = SessionLocal.getUserId();
TimeShareVO timeShareVO = new TimeShareVO();
timeShareVO.setNeed(getElectricity(userId, siteId, DateUtils.setHours(date, 9), DateUtils.setHours(date, 12)));
//高峰
Double peakStart = getElectricity(userId, siteId, DateUtils.setHours(date, 8), DateUtils.setHours(date, 12));
Double peakEnd = getElectricity(userId, siteId, DateUtils.setHours(date, 16), DateUtils.setHours(date, 20));
timeShareVO.setPeak(peakStart+peakEnd);
//平
Double flatStart = getElectricity(userId, siteId, DateUtils.setHours(date, 6), DateUtils.setHours(date, 8));
Double flatCentre = getElectricity(userId, siteId, DateUtils.setHours(date, 12), DateUtils.setHours(date, 16));
Double flatEnd = getElectricity(userId, siteId, DateUtils.setHours(date, 20), DateUtils.setHours(date, 22));
timeShareVO.setFlat(flatStart+flatCentre+flatEnd);
timeShareVO.setGrain(getElectricity(userId, siteId, DateUtils.setHours(date, 22), DateUtils.setHours(DateUtils.addDays(date, 1), 6)));
return timeShareVO;
}
@Override
public List getDemandIco(Integer siteId) {
Date date = DateUtils.getLastDayOfMonth(new Date());
List