package cn.cecep.talroad.service.impl.effective;

import cn.cecep.talroad.service.effective.ReportManageService;
import org.springframework.stereotype.Service;

/**
 * @author zhangshiji
 * @date 2022/8/26
 */
@Service
public class ReportManageServiceImpl implements ReportManageService {
/*

    private static final Logger log = LoggerFactory.getLogger(ReportManageService.class);
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private PCodeRegionMapper pCodeRegionMapper;

    public static void main(String[] args) throws IOException {
        HttpHost httpHost = new HttpHost("10.0.200.93", 9200, "http");
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(httpHost));
        GetIndexRequest getIndexRequest=new GetIndexRequest("q_envgas_area_hour");
        GetIndexResponse getIndexResponse = esClient.indices().
                get(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(getIndexResponse.getAliases());
        System.out.println(getIndexResponse.getMappings());
        System.out.println(getIndexResponse.getSettings());

        //关闭ES客户端
        esClient.close();
    }

    */
/**
     * 区域累计小时数据
     *
     * @param regionCode 区域code
     * @return
     *//*

    public AirQualityData cumulativeAirData(String regionCode) {
        AirParmaVo paramVo = new AirParmaVo();
        paramVo.setRegionCode(regionCode);
        //今天的01小时 到明天的 00小时
        getParamData(paramVo,new Date());
        List<AirQualityData> cumulativeDataList =
                AirDataConverseUtil.converSupplementData
                        (
                                areaData(paramVo),
                                pCodeRegionMapper.selectList(new QueryWrapper<PCodeRegion>().eq("region_code",regionCode)),
                                paramVo
                        );

        List<QEnvgasAreaHour> list = areaData(paramVo);
        if (cumulativeDataList != null && cumulativeDataList.size() > 0) {
            AirQualityData qualityData = AirDataConverseUtil.calcAvgData(cumulativeDataList);
            if(Objects.nonNull(qualityData)){
                BigDecimal data= BasTargetUtil.calcO38HData(list);
                qualityData.setO3(data);
                qualityData.setO38h(data);
            }
            return qualityData;
        }
        return new AirQualityData();
    }

    */
/**
     * 区域  昨天累计小时数据
     * @param regionCode 区域code
     * @return
     *//*

    public AirQualityData cumulativeAirLastData(String regionCode) {
        AirParmaVo paramVo = new AirParmaVo();
        paramVo.setRegionCode(regionCode);
        //昨天天的01小时 到今天的 00小时
        Date date = DateUtils.addDateYears(new Date(), -1); //去年数据
        getParamData(paramVo,date);
        List<AirQualityData> cumulativeDataList =
                AirDataConverseUtil.converSupplementData
                        (
                               areaData(paramVo),
                                pCodeRegionMapper.selectList(new QueryWrapper<PCodeRegion>().eq("region_code",regionCode)),
                                paramVo
                        );
        List<QEnvgasAreaHour> list = areaData(paramVo);
        if (cumulativeDataList != null && cumulativeDataList.size() > 0) {
            AirQualityData qualityData = AirDataConverseUtil.calcAvgData(cumulativeDataList);
            if(Objects.nonNull(qualityData)){
                BigDecimal data= BasTargetUtil.calcO38HData(list);
                qualityData.setO3(data);
                qualityData.setO38h(data);
            }
            return qualityData;
        }
        return new AirQualityData();
    }

    */
/**
     * 判断时间范围   今天的01小时 到明天的 00小时 求
     * @param paramVo
     * @param nowDate
     *//*

    private void getParamData(AirParmaVo paramVo ,Date nowDate){
        //判断时间为00小时
        if (nowDate.getHours()==0){
            Date date = DateUtils.addDateYears(new Date(), -1);   //去年数据
            paramVo.setStartTime(DateUtil.format(date, "yyyy-MM-dd 01:00:00"));
            paramVo.setEndTime(DateUtil.format(nowDate, "yyyy-MM-dd HH:00:00"));
        }else {
            paramVo.setStartTime(DateUtil.format(nowDate, "yyyy-MM-dd 01:00:00"));
            paramVo.setEndTime(DateUtil.format(nowDate, "yyyy-MM-dd HH:00:00"));
        }
    }

    List<QEnvgasAreaHour> areaData(AirParmaVo airParmaVo){
        List<QEnvgasAreaHour> airHourData = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(airParmaVo)) {
            NativeSearchQuery build = concatSearchParams(airParmaVo, 2);
            SearchHits<QEnvgasAreaHour> search = elasticsearchRestTemplate.search(build, QEnvgasAreaHour.class);
            List<SearchHit<QEnvgasAreaHour>> searchHits = search.getSearchHits();
            if (CollectionUtils.isNotEmpty(searchHits)) {
                airHourData = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
            }
        }
        return airHourData;
    }

    NativeSearchQuery concatSearchParams(AirParmaVo airParmaVo, Integer type) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //时间点   参数
        if (StringUtils.isNotBlank(airParmaVo.getDatetime())) {
            QueryBuilder timePointBuilder = QueryBuilders.termQuery("data_time", airParmaVo.getDatetime());
            boolQueryBuilder.must(timePointBuilder);
        }
        //时间区间  参数
        if (StringUtils.isNotBlank(airParmaVo.getStartTime())) {
            QueryBuilder startTimeBuilder = QueryBuilders.rangeQuery("data_time").gte(airParmaVo.getStartTime());
            boolQueryBuilder.must(startTimeBuilder);
        }
        if (StringUtils.isNotBlank(airParmaVo.getEndTime())) {
            QueryBuilder endTimeBuilder = QueryBuilders.rangeQuery("data_time").lte(airParmaVo.getEndTime());
            boolQueryBuilder.must(endTimeBuilder);
        }
        //区域code
        if (type == 2) {
            //单个站点id    参数
            if (StringUtils.isNotBlank(airParmaVo.getRegionCode())) {
                QueryBuilder stationIdBuilder = QueryBuilders.termQuery("region_code", airParmaVo.getRegionCode());
                boolQueryBuilder.must(stationIdBuilder);
            }
            //站点id集合    参数
            if (CollectionUtils.isNotEmpty(airParmaVo.getRegionCodeList())) {
                QueryBuilder idListBuilder = QueryBuilders.termsQuery("region_code", airParmaVo.getRegionCodeList());
                boolQueryBuilder.must(idListBuilder);
            }
        } else {
            //站点id
            //单个站点id
            if (StringUtils.isNotBlank(airParmaVo.getStationId())) {
                QueryBuilder stationIdBuilder = QueryBuilders.termQuery("station_code", airParmaVo.getStationId());
                boolQueryBuilder.must(stationIdBuilder);
            }
            //站点id集合
            if (CollectionUtils.isNotEmpty(airParmaVo.getStationIdList())) {
                QueryBuilder idListBuilder = QueryBuilders.termsQuery("station_code", airParmaVo.getStationIdList());
                boolQueryBuilder.must(idListBuilder);
            }
        }
        //排序  正序 倒序
        NativeSearchQuery build = null;
        if (airParmaVo.getDescSort() != null && airParmaVo.getDescSort() == 1) {
            build = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSorts(SortBuilders.fieldSort("data_time").order(SortOrder.DESC)).build();
        }
        if (airParmaVo.getAscSort() != null && airParmaVo.getAscSort() == 2) {
            build = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSorts(SortBuilders.fieldSort("data_time").order(SortOrder.ASC)).build();
        }
        //es查询超过1毫秒就返回超时，但测试发现与想象的不一样。即使耗时100+毫秒也不会超时。
        build.setTimeout(Duration.ofSeconds(Long.valueOf("15")));
        return build;
    }
*/

}
