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.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.util.DateUtils;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Li ZaiXun
 * @purpose
 * @date 2020-05-16 15:48
 */
@Service
public class Windspeedpowercurvedetail extends SocketToolService {
    @Autowired
    private InitialRunner initialRunner;
    private static int decimal_digit = 2;
    IRealTimeDataBaseUtil mongodb= RealTimeDataBaseFactory.createRealTimeDataBase();
    public static Map<String,String> windpowerstationdetail_twentyfourhourlinechart_target_pointMap = new HashMap<>();
    private double decimal(int num,double value){
        return new BigDecimal(value).setScale(num, RoundingMode.HALF_EVEN).doubleValue();
    }
    public <T> void windpowerstationdetail_twentyfourhourlinechart_target() throws Exception {
        Map<String, Map<String, WindPowerStationTestingPoint2>> stationPointmap = InitialRunner.stationPointmap;
        String fs = Constant.TPOINT_WP_SSFS;
        String gl = Constant.TPOINT_WP_SJGL;
        String llgl = Constant.TPOINT_WP_ZLLGL;
        String ycgl = Constant.TPOINT_WP_ZZSGL;
        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,List<PointData>>> windpowerstationpointDataMap = new HashMap();


        for (Windpowerstation windpowerstation : wpls) {
            Map<String,List<PointData>> map = new HashMap<>();
            windpowerstationpointDataMap.put(windpowerstation.getId(),map);
            List<PointData> listPointData = new ArrayList<>();

            try {
                //风速功率曲线图，风速
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
                Date nowTime = DateUtils.getNowDate();
                Date endTime = sdf.parse(sdf.format(nowTime));
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                Date beginTime = sdf1.parse(sdf1.format(nowTime));
                Long count = (endTime.getTime() - beginTime.getTime()) / 1000L / 3600L;
                WindPowerStationTestingPoint2 point = stationPointmap.get(windpowerstation.getId()).get(fs);
                listPointData = mongodb.getHistoryDatasSnap(point,
                        beginTime.getTime() / 1000, endTime.getTime() / 1000 + 3600L,
                        (long) count+1, 3600L);
                for (int i = 0; i < listPointData.size(); i++) {
                    listPointData.get(i).setPointTime(listPointData.get(i).getPointTime() - 3600);
                    listPointData.get(i).setPointValueInDouble(this.decimal(2,listPointData.get(i).getPointValueInDouble()));
                }
                if(listPointData.size()>0) {
                    listPointData.remove(0);
                }
                map.put("fs",listPointData);
            } catch (Exception e) {
                //System.out.println(e.getMessage());
            }


            try {
                //风速功率曲线图，功率
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
                Date nowTime = DateUtils.getNowDate();
                Date endTime = sdf.parse(sdf.format(nowTime));
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                Date beginTime = sdf1.parse(sdf1.format(nowTime));
                Long count = (endTime.getTime() - beginTime.getTime()) / 1000L / 3600L;
                WindPowerStationTestingPoint2 point = stationPointmap.get(windpowerstation.getId()).get(gl);
                listPointData = mongodb.getHistoryDatasSnap(point,
                        beginTime.getTime() / 1000, endTime.getTime() / 1000 + 3600L,
                        (long) count+1, 3600L);
                for (int i = 0; i < listPointData.size(); i++) {
                    listPointData.get(i).setPointTime(listPointData.get(i).getPointTime() - 3600);
                    listPointData.get(i).setPointValueInDouble(this.decimal(2,listPointData.get(i).getPointValueInDouble()));
                }
                if(listPointData.size()>0) {
                    listPointData.remove(0);
                }
                map.put("gl",listPointData);
            } catch (Exception e) {
                //System.out.println(e.getMessage());
            }


            try {
                //风速功率曲线图，理论功率
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
                Date nowTime = DateUtils.getNowDate();
                Date endTime = sdf.parse(sdf.format(nowTime));
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                Date beginTime = sdf1.parse(sdf1.format(nowTime));
                Long count = (endTime.getTime() - beginTime.getTime()) / 1000L / 3600L;
                WindPowerStationTestingPoint2 point = stationPointmap.get(windpowerstation.getId()).get(llgl);
                listPointData = mongodb.getHistoryDatasSnap(point,
                        beginTime.getTime() / 1000, endTime.getTime() / 1000 + 3600L,
                        (long) count+1, 3600L);
                for (int i = 0; i < listPointData.size(); i++) {
                    listPointData.get(i).setPointTime(listPointData.get(i).getPointTime() - 3600);
                    listPointData.get(i).setPointValueInDouble(this.decimal(2,listPointData.get(i).getPointValueInDouble()/1000));
                }
                if(listPointData.size()>0) {
                    listPointData.remove(0);
                }
                map.put("llgl",listPointData);
            } catch (Exception e) {
                //System.out.println(e.getMessage());
            }


            try {
                //风速功率曲线图，预测功率
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
                Date nowTime = DateUtils.getNowDate();
                Date endTime = sdf.parse(sdf.format(nowTime));
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                Date beginTime = sdf1.parse(sdf1.format(nowTime));
                Long count = (endTime.getTime() - beginTime.getTime()) / 1000L / 3600L;
                WindPowerStationTestingPoint2 point = stationPointmap.get(windpowerstation.getId()).get(ycgl);
                listPointData = mongodb.getHistoryDatasSnap(point,
                        beginTime.getTime() / 1000, endTime.getTime() / 1000 + 3600L,
                        (long) count+1, 3600L);
                for (int i = 0; i < listPointData.size(); i++) {
                    listPointData.get(i).setPointTime(listPointData.get(i).getPointTime() - 3600);
                    listPointData.get(i).setPointValueInDouble(this.decimal(2,listPointData.get(i).getPointValueInDouble()/1000));
                }
                if(listPointData.size()>0) {
                    listPointData.remove(0);
                }
                map.put("ycgl",listPointData);
            } catch (Exception e) {
                //System.out.println(e.getMessage());
            }
        }
        this.pushToWeb("pageNumber_7","functionNumber_4","all",windpowerstationpointDataMap, Messagetype.MESSAGE);
    }
    public <T> void windpowerstationdetail_sevendaycolumn_target() throws Exception {
        Map<String, Map<String, WindPowerStationTestingPoint2>> stationPointmap = InitialRunner.stationPointmap;
        String rfdl = Constant.TPOINT_WP_RFDL;
        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,List<PointData>>> windpowerstationpointDataMap = new HashMap();

        IRealTimeDataBaseUtil mongodb= RealTimeDataBaseFactory.createRealTimeDataBase();
        for (Windpowerstation windpowerstation : wpls) {
            Map<String,List<PointData>> map = new HashMap<>();
            windpowerstationpointDataMap.put(windpowerstation.getId(),map);
            List<PointData> listPointData = new ArrayList<>();

            try {
                //发电量柱状图，发电量
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
                Date nowTime = DateUtils.getNowDate();
                Date endTime = sdf.parse(sdf.format(nowTime));
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                Date beginTime = DateUtils.addDays(endTime, -7);
                WindPowerStationTestingPoint2 point = stationPointmap.get(windpowerstation.getId()).get(rfdl);
                listPointData = mongodb.getHistoryDatasSnap(point,
                        beginTime.getTime() / 1000, endTime.getTime() / 1000 + 3600L*24,
                        8L, 3600 * 24L);
                for (int i = 0; i < listPointData.size(); i++) {
                    listPointData.get(i).setPointTime(listPointData.get(i).getPointTime() - 24 * 3600);
                    listPointData.get(i).setPointValueInDouble(this.decimal(2,listPointData.get(i).getPointValueInDouble()));
                }

                if(listPointData.size()>0) {
                    listPointData.remove(0);
                }
                map.put("rfdl",listPointData);
            } catch (Exception e) {
                //System.out.println(e.getMessage());
            }
        }
        this.pushToWeb("pageNumber_7","functionNumber_5","all",windpowerstationpointDataMap, Messagetype.MESSAGE);
    }
}