package com.gyee.frame.service.app.windpowerstationdetail;

import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.Weatherfh;
import com.gyee.frame.model.auto.WindPowerStationTestingPoint2;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.enumeration.Messagetype;
import com.gyee.frame.netty.websocket.util.SocketToolService;
import com.gyee.frame.service.WeatherfhService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class Windspeedpowermodule extends SocketToolService {
    @Autowired
    private InitialRunner initialRunner;
    private static int decimal_digit = 0;

    @Resource
    private WeatherfhService weatherfhService;

    public void index_windSpeed_power_target_detail() throws Exception{
        Map<String, Map<String, WindPowerStationTestingPoint2>> stationPointmap = InitialRunner.stationPointmap;
        List<Windpowerstation> wpls = new ArrayList<>();
        for(Windpowerstation wp:InitialRunner.wpls){
            wpls.add(wp);
        }
        Windpowerstation windpowerstationAll = new Windpowerstation();
        windpowerstationAll.setId("0");
        wpls.add(windpowerstationAll);
        Map<String,Map<String,Object>> windpowerstationpointDataMap = new HashMap();
        IRealTimeDataBaseUtil mongodb= RealTimeDataBaseFactory.createRealTimeDataBase();

        double ycfs=0;
        double ycfsMax=0;
        double ycfsMin=0;

        double pjfs=0;
        double pjfsMax=0;
        double pjfsMin=0;

        double sjgl=0;
        double sjglMax=0;
        double sjglMin=0;

        double llgl=0;
        double llglMax=0;
        double llglMin=0;


        for(Windpowerstation windpowerstation:wpls){

            Calendar cal=Calendar.getInstance();

            cal.set(Calendar.HOUR_OF_DAY,0);
            cal.set(Calendar.MINUTE,0);
            cal.set(Calendar.SECOND,0);
            Date beginDate = cal.getTime();
            Date endDate = DateUtils.addDays(beginDate, 1);
            Map<String,Object> pointDataMap = new HashMap();


        //风速****************************************************************
            WindPowerStationTestingPoint2 pjfspoint=stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_PJFS);
            List<PointData> pjfsls = mongodb.getHistoryDatasSnap(pjfspoint, beginDate.getTime() / 1000, endDate.getTime() / 1000, 24L, 3600L);

            if(!pjfsls.isEmpty())
            {
                pointDataMap.put("pjfs_real",pjfsls.get(0));
                // 平均数
                pjfs = pjfsls.stream().mapToDouble(PointData::getPointValueInDouble).average().getAsDouble();
                pjfs= StringUtils.round(pjfs,2);
                //System.out.println("average:" + pjfs);


                // 最大值
                pjfsMax = pjfsls.stream().mapToDouble(PointData::getPointValueInDouble).max().getAsDouble();
                pjfsMax= StringUtils.round(pjfsMax,2);
                //System.out.println("max:" + pjfsMax);

                // 最小值
                pjfsMin = pjfsls.stream().mapToDouble(PointData::getPointValueInDouble).min().getAsDouble();
                pjfsMin= StringUtils.round(pjfsMin,2);
                //System.out.println("min:" + pjfsMin);
            }


            //预测风速****************************************************************
            List<Weatherfh> list=weatherfhService.listDayHour(windpowerstation.getId());

            if(!list.isEmpty())
            {
                // 平均数
                ycfs = list.stream().mapToDouble(Weatherfh::getSpeed).average().getAsDouble();
                ycfs= StringUtils.round(ycfs,2);
                //System.out.println("average:" + ycfs);


                // 最大值
                ycfsMax = list.stream().mapToDouble(Weatherfh::getSpeed).max().getAsDouble();
                ycfsMax= StringUtils.round(ycfsMax,2);
                //System.out.println("max:" + ycfsMax);

                // 最小值
                ycfsMin = list.stream().mapToDouble(Weatherfh::getSpeed).min().getAsDouble();
                ycfsMin= StringUtils.round(ycfsMin,2);
                //System.out.println("min:" + ycfsMin);

            }

            //实际风功****************************************************************

            WindPowerStationTestingPoint2 sjglpoint=stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_SJGL);
            List<PointData> sjglls = mongodb.getHistoryDatasSnap(sjglpoint, beginDate.getTime() / 1000, endDate.getTime() / 1000, 24L, 3600L);

            if(!sjglls.isEmpty())
            {
                pointDataMap.put("sjgl_real",sjglls.get(0));

                // 平均数
                sjgl = sjglls.stream().mapToDouble(PointData::getPointValueInDouble).average().getAsDouble();
                sjgl= StringUtils.round(sjgl,2);
                //System.out.println("average:" + sjgl);


                // 最大值
                sjglMax = sjglls.stream().mapToDouble(PointData::getPointValueInDouble).max().getAsDouble();
                sjglMax= StringUtils.round(sjglMax,2);
                //System.out.println("max:" + sjglMax);

                // 最小值
                sjglMin = sjglls.stream().mapToDouble(PointData::getPointValueInDouble).min().getAsDouble();
                sjglMin= StringUtils.round(sjglMin,2);
                //System.out.println("min:" + sjglMin);
            }



            //理论功率****************************************************************

            WindPowerStationTestingPoint2 llglpoint=stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_ZLLGL);
            List<PointData>  llglls = mongodb.getHistoryDatasSnap(llglpoint, beginDate.getTime() / 1000, endDate.getTime() / 1000, 24L, 3600L);


            if(!llglls.isEmpty())
            {
                pointDataMap.put("llgl_real",llglls.get(0));
                // 平均数
                llgl = sjglls.stream().mapToDouble(PointData::getPointValueInDouble).average().getAsDouble();
                llgl= StringUtils.round(llgl,2);
                //System.out.println("average:" + llgl);


                // 最大值
                llglMax = sjglls.stream().mapToDouble(PointData::getPointValueInDouble).max().getAsDouble();
                llglMax= StringUtils.round(llglMax,2);
                //System.out.println("max:" + llglMax);

                // 最小值
                llglMin = sjglls.stream().mapToDouble(PointData::getPointValueInDouble).min().getAsDouble();
                llglMin= StringUtils.round(llglMin,2);
                //System.out.println("min:" + llglMin);
            }



            pointDataMap.put("pjfs",pjfs);
            pointDataMap.put("pjfsMin",pjfsMin);
            pointDataMap.put("pjfsMax",pjfsMax);
            pointDataMap.put("ycfs",ycfs);
            pointDataMap.put("ycfsMin",ycfsMin);
            pointDataMap.put("ycfsMax",ycfsMax);
            pointDataMap.put("sjgl",sjgl);
            pointDataMap.put("sjglMin",sjglMin);
            pointDataMap.put("sjglMax",sjglMax);
            pointDataMap.put("llgl",llgl);
            pointDataMap.put("llglMin",llglMin);
            pointDataMap.put("llglMax",llglMax);
            windpowerstationpointDataMap.put(windpowerstation.getId(),pointDataMap);
        }
        this.pushToWeb("pageNumber_7","functionNumber_3","all",windpowerstationpointDataMap, Messagetype.MESSAGE);
    }
}
