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