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

import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.WindPowerstationTestingPoint;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.custom.DataVo;
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.WindpowerinfodayService;
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.*;

/**
 *
 */
@Service
public class IndexDetailService extends SocketToolService {
	@Autowired
    private InitialRunner initialRunner;
    private static int decimal_digit = 2;
    IRealTimeDataBaseUtil mongodb= RealTimeDataBaseFactory.createRealTimeDataBase();
    @Resource
    private WindpowerinfodayService windpowerinfodayService;
    @Resource
    private FocastpowerService focastpowerService;

    public void index_curve_columnar_windSpeed_power() throws Exception {
        System.out.println("pppppp");
        Map<String, List<DataVo>> pointDataMap = new HashMap();

        List<Windpowerstation> wpls = new ArrayList<>();
        for (Windpowerstation wp : InitialRunner.wpls) {
            wpls.add(wp);
        }
        Windpowerstation windpowerstationAll = new Windpowerstation();
        windpowerstationAll.setId("0");
        wpls.add(windpowerstationAll);

        String[] ycPoints = Constant.TPOINT_WP_FGLYC.split(",");
        String[] ycPoints2 = Constant.TPOINT_WP_FGLYC2.split(",");



        Calendar c = Calendar.getInstance();
        Date endDate = c.getTime();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date beginDate = c.getTime();
        long num = DateUtils.hoursDiff(beginDate, endDate);
        Map<String, Map<String, WindPowerstationTestingPoint>> stationPointmap = InitialRunner.stationPointmap;

        for (Windpowerstation windpowerstation : wpls) {

            List<DataVo> vos = new ArrayList<DataVo>();
            int length = 24;
            for (int i = 0; i < length; i++) {
                DataVo vo = new DataVo();

                vo.setValue1(0.0);// 预测功率
                vo.setValue2(0.0);// 理论功率
                vo.setValue3(0.0);// 实发功率
                vo.setValue4(0.0);// 平均风速
                vos.add(vo);
            }

            Map<String, WindPowerstationTestingPoint> map = stationPointmap.get(windpowerstation.getId());


            WindPowerstationTestingPoint sjgl = map.get(Constant.TPOINT_WP_SJGL);
            WindPowerstationTestingPoint llgl = map.get(Constant.TPOINT_WP_ZZSGL);
            WindPowerstationTestingPoint pjfs = map.get(Constant.TPOINT_WP_SSFS);

            Calendar cal = Calendar.getInstance();
            //理论功率
            if (!windpowerstation.getId().equals("0") && !windpowerstation.getId().equals("-1") && !windpowerstation.getId().equals("-2")) {

                // /////////////////////////////////////////////
                WindPowerstationTestingPoint ycgl1 = map.get(ycPoints[0]);
                Date tempdate = null;
                if (num > 0) {
                    List<PointData> datas = mongodb.getHistoryDatasSnap(ycgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, 3600L);

                    for (int j = 0; j < datas.size(); j++) {

                        vos.get(j).setTime(datas.get(j).getPointTime() * 1000);
                        vos.get(j).setValue1(StringUtils.round(datas.get(j).getPointValueInDouble(), 2));
                        if (j == datas.size() - 1) {
                            tempdate = new Date(datas.get(j).getPointTime() * 1000);
                            cal.setTime(tempdate);
                        }
                    }
                    int subresult = 0;
                    int total = 0;
                    if (length > datas.size()) {
                        subresult = length - datas.size();
                        if (subresult >= 8) {
                            subresult = 8;
                        }
                        for (int i = 0; i < subresult; i++) {
                            cal.add(Calendar.MINUTE, 30);
                            vos.get(datas.size() + i).setTime(cal.getTime().getTime());
                            WindPowerstationTestingPoint ycgl = map.get(ycPoints2[i]);
                            vos.get(datas.size() + i).setValue1(StringUtils.round(mongodb.getRealData(ycgl).getPointValueInDouble(), 2));
                        }
                        total = datas.size() + subresult;
                        subresult = length - (total);

                        if (subresult > 0)
                            for (int i = 0; i < subresult; i++) {
                                cal.add(Calendar.MINUTE, 30);
                                vos.get(total + i).setTime(cal.getTime().getTime());
                                vos.get(total + i).setValue1(null);
                            }
                    }
                    for (int k = 0; k < vos.size(); k++) {
                        vos.get(k).setValue7(48.0);
                    }
                }
            } else {
                List<Windpowerstation> stations = new ArrayList<Windpowerstation>();
                if (windpowerstation.getId().equals("") || windpowerstation.getId().equals("0")) {
                    for (Windpowerstation station : InitialRunner.wpls) {
                        stations.add(station);
                    }
                }
                for (Windpowerstation station : stations) {

                    Map<String, WindPowerstationTestingPoint> wpmap = stationPointmap.get(station.getId());
                    Date tempdate = null;
                    if (num > 0) {
                        WindPowerstationTestingPoint ycgl1 = wpmap.get(ycPoints[0]);

                        if(null !=ycgl1)
                        {
                            List<PointData> datas = mongodb.getHistoryDatasSnap(ycgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, 3600L);
                            if(datas.isEmpty())
                            {
                                continue;
                            }
                            for (int j = 0; j < datas.size(); j++) {

                                vos.get(j).setTime(datas.get(j).getPointTime() * 1000);
                                vos.get(j).setValue1(StringUtils.round(vos.get(j).getValue1() + datas.get(j).getPointValueInDouble(), 2));
                                if (j == datas.size() - 1) {
                                    tempdate = new Date(datas.get(j).getPointTime() * 1000);
                                    cal.setTime(tempdate);
                                }
                            }
                            int subresult = 0;
                            int total = 0;
                            if (length > datas.size()) {
                                subresult = length - datas.size();
                                if (subresult >= 8) {
                                    subresult = 8;
                                }
                                for (int i = 0; i < subresult; i++) {
                                    cal.add(Calendar.MINUTE, 30);
                                    vos.get(datas.size() + i).setTime(cal.getTime().getTime());
                                    WindPowerstationTestingPoint ycgl = wpmap.get(ycPoints2[i]);
                                    vos.get(datas.size() + i).setValue1(StringUtils.round(vos.get(datas.size() + i).getValue1() + mongodb.getRealData(ycgl).getPointValueInDouble(), 2));
                                }
                                total = datas.size() + subresult;
                                subresult = length - (total);

                                if (subresult > 0)
                                    for (int i = 0; i < subresult; i++) {
                                        cal.add(Calendar.MINUTE, 30);
                                        vos.get(total + i).setTime(cal.getTime().getTime());
                                        vos.get(total + i).setValue1(null);
                                    }
                            }

                        }

                    }
                }
            }

            WindPowerstationTestingPoint gdsjgl = InitialRunner.stationPointmap.get("0").get(Constant.TPOINT_WP_SJGL);
            List<PointData> gdsjglls = mongodb.getHistoryDatasSnap(gdsjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, 3600L);

            //实际功率
            try {
                if (null != llgl) {
                           List<PointData> llglls = mongodb.getHistoryDatasSnap(llgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, 3600L);

                    int count = 0;
                    double temp1 = 0;
                    double temp2 = 0;
                    for (int i = 0; i < vos.size(); i++) {

                        if (i < llglls.size() && StringUtils.isNotNull(llglls.get(i).getPointTime())) {

                            if (vos.get(i).getTime() <= new Date().getTime()) {
                                temp1 = llglls.get(i).getPointValueInDouble();
                                temp2 = gdsjglls.get(i).getPointValueInDouble();
                                temp1 = new BigDecimal(temp1).divide(new BigDecimal(100000), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                // temp2 = new BigDecimal(temp2).divide(
                                // new BigDecimal(100000), 2,
                                // RoundingMode.HALF_EVEN).multiply(
                                // new BigDecimal(100)).doubleValue();

                                if (Math.abs(temp1) < 10000) {

                                    if (windpowerstation.getId().equals("0")) {
                                        vos.get(i).setValue2(StringUtils.round(temp1 + temp2, 2));// 理论功率
                                    } else {
                                        vos.get(i).setValue2(StringUtils.round(temp1, 2));// 理论功率
                                    }

                                }

                                count++;
                            } else {
                                vos.get(i).setValue2(0d);// 理论功率
                            }

                        }
                    }
                    // for(int i=0;i<vos.size();i++){
                    // vos.get(i).setValue8((double)count);
                    // }
                }
            } catch (Exception e) {
                // TODO: handle exception
            }


            if (null != sjgl) {
                List<PointData> sjglls = mongodb.getHistoryDatasSnap(sjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, 3600L);
                int count = 0;
                double temp = 0;
                for (int i = 0; i < vos.size(); i++) {

                    if (i < sjglls.size() && StringUtils.isNotNull(sjglls.get(i).getPointTime())) {

                        if (vos.get(i).getTime() <= new Date().getTime()) {
                            temp = sjglls.get(i).getPointValueInDouble();
                            if (Math.abs(temp) < 10000) {
                                vos.get(i).setValue3(StringUtils.round(temp, 2));// 实际功率
                            }

                            count++;
                        } else {
                            vos.get(i).setValue3(0d);// 实际功率
                        }

                    }
                }
                for (int i = 0; i < vos.size(); i++) {
                    vos.get(i).setValue3((double) count);
                }
            }
            try {
                if (null != pjfs) {
                    List<PointData> pjfsls = mongodb.getHistoryDatasSnap(pjfs, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, 3600L);

                    double temp = 0;
                    for (int i = 0; i < vos.size(); i++) {

                        if (i < pjfsls.size() && StringUtils.isNotNull(pjfsls.get(i).getPointTime())) {

                            if (vos.get(i).getTime() <= new Date().getTime()) {
                                temp = pjfsls.get(i).getPointValueInDouble();

                                if (Math.abs(temp) < 10000) {
                                    vos.get(i).setValue4(StringUtils.round(temp, 2));// 平均风速
                                }
                            } else {
                                vos.get(i).setValue4(0d);// 平均风速
                            }

                        }
                    }
                }
            } catch (Exception e) {
                // TODO: handle exception
            }

            pointDataMap.put(windpowerstation.getId(),vos);

        }

        this.pushToWeb("pageNumber_6", "functionNumber_6", pointDataMap, Messagetype.MESSAGE);

    }


    public void index_curve_columnar_five_loss() throws Exception {

        Map<String, List<DataVo>> pointDataMap = new HashMap();

        List<Windpowerstation> wpls = new ArrayList<>();
        for (Windpowerstation wp : InitialRunner.wpls) {
            wpls.add(wp);
        }
        Windpowerstation windpowerstationAll = new Windpowerstation();
        windpowerstationAll.setId("0");
        wpls.add(windpowerstationAll);


        Calendar c = Calendar.getInstance();
        Date endDate = c.getTime();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.add(Calendar.DAY_OF_MONTH,-7);
        Date beginDate = c.getTime();

        Map<String, Map<String, WindPowerstationTestingPoint>> stationPointmap = InitialRunner.stationPointmap;

        for (Windpowerstation windpowerstation : wpls) {

            List<DataVo> vos = new ArrayList<DataVo>();
            long length = 7;
            for (int i = 0; i < length; i++) {
                DataVo vo = new DataVo();

                vo.setValue1(0.0);// 发电量
                vo.setValue2(0.0);// 故障损失
                vo.setValue3(0.0);// 检修损失
                vo.setValue4(0.0);// 限电损失
                vo.setValue5(0.0);// 性能损失
                vo.setValue6(0.0);// 受累损失
                vo.setValue7(0.0);// 应发电量
                vos.add(vo);
            }
            Map<String, WindPowerstationTestingPoint> map = stationPointmap.get(windpowerstation.getId());

//            public static final String RGZZSDL = "RGZZSDL";// 故障
//            public static final String RSZZSDL = "RSZZSDL";// 故障
//            public static final String RJXZSDL = "RJXZSDL";// 维护
//            public static final String RLZZSDL = "RLZZSDL";// 维护
//            public static final String RQFZSDL = "RQFZSDL";// 性能
//            public static final String RXDZSDL = "RXDZSDL";// 限电
//            public static final String RWZZSDL = "RWZZSDL";// 受累
//            public static final String RTZZSDL = "RTZZSDL";// 受累

            WindPowerstationTestingPoint fdl = map.get(Constant.TPOINT_WP_RFDL);

            WindPowerstationTestingPoint gzss = map.get(Constant.RGZZSDL);
            WindPowerstationTestingPoint gzsl = map.get(Constant.RSZZSDL);

            WindPowerstationTestingPoint jxss = map.get(Constant.RJXZSDL);
            WindPowerstationTestingPoint jxsl = map.get(Constant.RLZZSDL);

            WindPowerstationTestingPoint xnss = map.get(Constant.RQFZSDL);
            WindPowerstationTestingPoint xdss = map.get(Constant.RXDZSDL);

            WindPowerstationTestingPoint slss = map.get(Constant.RWZZSDL);
            WindPowerstationTestingPoint slsl = map.get(Constant.RTZZSDL);

            Calendar cal = Calendar.getInstance();
            //发电量
            List<PointData> fdlls = mongodb.getHistoryDatasSnap(fdl, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);

            for (int j = 0; j < fdlls.size()-1; j++) {

                vos.get(j).setTime(fdlls.get(j).getPointTime() * 1000);
                vos.get(j).setValue1(StringUtils.round(fdlls.get(j).getPointValueInDouble(),2));
            }

            try {

                //故障损失
                List<PointData> gzssls = mongodb.getHistoryDatasSnap(gzss, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);
                //故障受累失
                List<PointData> gzslls = mongodb.getHistoryDatasSnap(gzsl, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);

                double temp1 = 0;
                double temp2 = 0;
                for (int i = 0; i < vos.size(); i++) {

                   // temp1 =gzssls.get(i).getPointValueInDouble();
                    temp1 =new BigDecimal(gzssls.get(i).getPointValueInDouble()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                    //temp2 =gzslls.get(i).getPointValueInDouble();
                    temp2 =new BigDecimal(gzslls.get(i).getPointValueInDouble()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                    vos.get(i).setValue2(StringUtils.round(temp1+temp2, 2));// 故障损失

                }

            } catch (Exception e) {
                // TODO: handle exception
            }

            try {
                //检修损失
                List<PointData> jxssls = mongodb.getHistoryDatasSnap(jxss, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);
                //检修受累
                List<PointData> jxslls = mongodb.getHistoryDatasSnap(jxsl, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);

                double temp1 = 0;
                double temp2 = 0;
                for (int i = 0; i < vos.size(); i++) {

                   // temp1 = jxssls.get(i).getPointValueInDouble();
                    temp1 =new BigDecimal(jxssls.get(i).getPointValueInDouble()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                  //  temp2 = jxslls.get(i).getPointValueInDouble();
                    temp2 =new BigDecimal(jxslls.get(i).getPointValueInDouble()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                    vos.get(i).setValue3(StringUtils.round(temp1+temp2, 2));// 检修损失

                }

            } catch (Exception e) {
                // TODO: handle exception
            }

            try {
                //限电损失
                List<PointData> xdssls = mongodb.getHistoryDatasSnap(xdss, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);

                double temp = 0;
                for (int i = 0; i < vos.size(); i++) {
                    //temp = xdssls.get(i).getPointValueInDouble();
                    temp =new BigDecimal(xdssls.get(i).getPointValueInDouble()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                    vos.get(i).setValue4(StringUtils.round(temp, 2));// 限电损失

                }

            } catch (Exception e) {
                // TODO: handle exception
            }
            try {
                //性能损失
                List<PointData> xnssls = mongodb.getHistoryDatasSnap(xnss, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);

                double temp = 0;
                for (int i = 0; i < vos.size(); i++) {
                   // temp = xnssls.get(i).getPointValueInDouble();
                    temp =new BigDecimal(xnssls.get(i).getPointValueInDouble()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                    vos.get(i).setValue5(StringUtils.round(temp, 2));// 性能损失

                }

            } catch (Exception e) {
                // TODO: handle exception
            }

            try {
                //天气受累
                List<PointData> slssls = mongodb.getHistoryDatasSnap(slss, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);
                //电网受累
                List<PointData> slslls = mongodb.getHistoryDatasSnap(slsl, beginDate.getTime() / 1000, endDate.getTime() / 1000, length, 3600*24L);

                double temp1 = 0;
                double temp2 = 0;
                for (int i = 0; i < vos.size(); i++) {

                   // temp1 = slssls.get(i).getPointValueInDouble();
                    temp1 =new BigDecimal(slssls.get(i).getPointValueInDouble()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                   // temp2 = slslls.get(i).getPointValueInDouble();
                    temp2 =new BigDecimal(slslls.get(i).getPointValueInDouble()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                    vos.get(i).setValue6(StringUtils.round(temp1+temp2, 2));// 受累损失

                }

            } catch (Exception e) {
                // TODO: handle exception
            }

            for(DataVo vo:vos)
            {
                vo.setValue7(StringUtils.round(vo.getValue1()+vo.getValue2()+vo.getValue3()+vo.getValue4()+vo.getValue5()+vo.getValue6(), 2));
            }
            pointDataMap.put(windpowerstation.getId(),vos);

        }

        this.pushToWeb("pageNumber_6", "functionNumber_7", pointDataMap, Messagetype.MESSAGE);

    }


    public void index_windStation_card() throws Exception {
        System.out.println("000");
        Map<String, Map<String, WindPowerstationTestingPoint>> 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();

        for(Windpowerstation windpowerstation:wpls){

            Map<String,Object> pointDataMap = new HashMap();
            double rfdl = 0;
            double ycdl = 0;
            double ssgl = 0;
            double ssfs = 0;


            try {
                //日发电量
                PointData rfdl_real = mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_RFDL).getCode());
                rfdl = new BigDecimal(rfdl_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
                pointDataMap.put("rfdl_real",rfdl_real);
                pointDataMap.put("rfdl",rfdl);
            }catch (Exception e){
                System.out.println(e.getMessage());
            }

            try {
                //实时功率

                PointData ssgl_real = mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_SJGL).getCode());
                ssgl = new BigDecimal(ssgl_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
                pointDataMap.put("ssgl",ssgl);
                pointDataMap.put("ssgl_real",ssgl_real);
            }catch (Exception e){
                System.out.println(e.getMessage());
            }


            try{
                //实时风速
                PointData ssfs_real=mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_SSFS).getCode());
                ssfs = new BigDecimal(ssfs_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
                pointDataMap.put("ssfs",ssfs);
                pointDataMap.put("ssfs_real",ssfs_real);
            }catch (Exception e){
                System.out.println(e.getMessage());
            }

            //预测发电量
            ycdl=focastpowerService.focastpower(windpowerstation.getId());
            ycdl =Double.valueOf( String.format("%.2f", ycdl));

            pointDataMap.put("ycdl",ycdl);

            windpowerstationpointDataMap.put(windpowerstation.getId(),pointDataMap);
        }


        this.pushToWeb("pageNumber_6","functionNumber_8",windpowerstationpointDataMap, Messagetype.MESSAGE);
    }


}
