package com.gyee.frame.service.websocket;


import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.service.WindPowerstationTestingPoint2Service;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
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.stereotype.Service;

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

@Service
public class RealPowerPushService {


    @Resource
    private WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;
    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;

    private final String QS = "0";
    private final String FD = "-1";
    private final String GF = "-2";


    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();


    public Map<String, Object> genreSetMap() throws Exception {

        Map<String, Object> map = new HashMap<>();


        map.put(QS, findRealPower(QS));
        map.put(FD, findRealPower(FD));
        map.put(GF, findRealPower(GF));
        map.put("QS_FDC",findRealPower("QS_FDC"));
        map.put("QSQ02_GC",findRealPower("QSQ02_GC"));
        map.put("QSQ05_XL",findRealPower("QSQ05_XL"));

        return map;

    }


    public Map<String, Object> genreSetWpMap(String wpId) throws Exception {

        Map<String, Object> map = new HashMap<>();

        map.put(wpId, findRealPower(wpId));

//        map.put("QS_FDC",findRealPower("QS_FDC"));
//        map.put("QSQ02_GC",findRealPower("QSQ02_GC"));
//        map.put("QSQ05_XL",findRealPower("QSQ05_XL"));

        return map;

    }

    public List<DataVo> findRealPower(String id) throws Exception {
        List<DataVo> glvos = new ArrayList<>();
        if (StringUtils.notEmp(id)) {


/****************************************************曲线历史**********************************************************/
            glvos = findRealPowerChar(id, new Date());
/*************************************************曲线历史*************************************************************/
        }
        return glvos;
    }

    public List<DataVo> findRealPowerChar(String id, Date currentDate) throws Exception {

        List<DataVo> vos = new ArrayList<DataVo>();


        if (StringUtils.notEmp(id)) {

            Calendar cal = Calendar.getInstance();

            cal.setTime(currentDate);


            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

            Date beginDate = cal.getTime();

            cal.setTime(currentDate);

            if (cal.get(Calendar.MINUTE) > 30) {
                cal.set(Calendar.MINUTE, 0);
                cal.add(Calendar.HOUR_OF_DAY, 1);
            } else {
                cal.set(Calendar.MINUTE, 0);
            }

            Date endDate = cal.getTime();

            long pried = 3600l;
            long num = DateUtils.hoursDiff(beginDate, endDate) ;
            int cdqyc = 4; //未来4小时超短期预测
            String[] ycPoints1 = Constant.TPOINT_WP_FGLCDQ1.split(",");
            String[] ycPoints2 = Constant.TPOINT_WP_FGLCDQ2.split(",");
            int dqyc = 24; //未来24小时超短期预测
            String[] ycDqPoints1 = Constant.TPOINT_WP_FGLDQ1.split(",");
            String[] ycDqPoints2 = Constant.TPOINT_WP_FGLDQ2.split(",");
            //String[] ycPoints3 = Constant.TPOINT_QS3_YGCL.split(",");

            int length = 72;
            Calendar c = Calendar.getInstance();
            cal.setTime(beginDate);
            for (int i = 0; i < length; i++) {

                DataVo vo = new DataVo();

               vo.setTime(c.getTime().getTime());
//                vo.setValue1(0.0);// 应发功率
//                vo.setValue2(0.0);// 实发功率
//                vo.setValue3(0.0);// 理论功率
//                vo.setValue4(0.0);// 4预测功率
//                vo.setValue5(0.0);// 保证功率
//                vo.setValue6(0.0);// 平均风速
//                vo.setValue7(0.0);// 24小时预测功率
//                vo.setValue8(0.0);// 72小时预测功率
//                vo.setValue9(0.0);// 最优功率律

                vos.add(vo);
                c.add(Calendar.HOUR_OF_DAY,1);
            }

            if (id.equals(QS) || id.equals(FD) || id.equals(GF)) {


                List<Windpowerstation> stations = new ArrayList<Windpowerstation>();
                if (id.equals("") || id.equals(QS)) {
                    for (Windpowerstation station : InitialRunner.wpallls) {
                        stations.add(station);
                    }
                } else if (id.equals(FD)) {
                    for (Windpowerstation station : InitialRunner.wpallls) {
                        if (station.getId().endsWith("FDC")) {
                            stations.add(station);
                        }
                    }
                } else {
                    for (Windpowerstation station : InitialRunner.wpallls) {
                        if (station.getId().endsWith("GDC")) {
                            stations.add(station);
                        }
                    }
                }

                for (Windpowerstation station : stations) {


                    if (num > 0) {

/***************************************************************超短期功率预测***************************************************************************/
                        WindPowerStationTestingPoint ycgl1 = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(station.getId(), ycPoints1[0]);

                        if (null != ycgl1) {
                            List<PointData> datas = realApiUtil.getHistoryDatasSnap(ycgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, pried);
                            if (!datas.isEmpty()) {

                                if (InitialRunner.appendYcglMap.containsKey(station.getId())) {
                                    Map<String, Object> ycmap = InitialRunner.appendYcglMap.get(station.getId());
                                    if (ycmap.containsKey("cdq1")) {
                                        String qs3_ycgl1 = (String) ycmap.get("cdq1");

                                        List<PointData> qs3_datas = realApiUtil.getHistoryDatasSnap(qs3_ycgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, pried);

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

                                            vos.get(j).setTime(datas.get(j).getPointTime() * 1000);
                                            vos.get(j).setValue4(StringUtils.round(datas.get(j).getPointValueInDouble() + qs3_datas.get(j).getPointValueInDouble(), 2));
                                            if (j == datas.size() - 1) {
                                                cal.setTime(new Date(datas.get(j).getPointTime() * 1000));
                                            }
                                        }
                                    }


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

                                        vos.get(j).setTime(datas.get(j).getPointTime() * 1000);

                                        double ycgl = (vos.get(j).getValue4() == null ? 0.0 : vos.get(j).getValue4()) + (datas.get(j) == null ? 0.0 : datas.get(j).getPointValueInDouble());

                                        vos.get(j).setValue4(StringUtils.round(ycgl, 2));

                                        if (j == datas.size() - 1) {
                                            cal.setTime(new Date(datas.get(j).getPointTime() * 1000));
                                        }
                                    }
                                }

                                int subresult = 0;
                                int total = 0;

                                if (length > datas.size()) {
                                    subresult = length - datas.size();
                                    if (subresult >= cdqyc) {
                                        subresult = cdqyc;
                                    }

                                    if (InitialRunner.appendYcglMap.containsKey(station.getId())) {
                                        Map<String, Object> ycmap = InitialRunner.appendYcglMap.get(station.getId());
                                        if (ycmap.containsKey("cdq2")) {
                                            String[] ycPoints3 = (String[]) ycmap.get("cdq2");
                                            for (int i = 0; i < subresult; i++) {
                                                cal.add(Calendar.HOUR_OF_DAY, 1);
                                                vos.get(datas.size() + i).setTime(cal.getTime().getTime());
                                                String qs3_ycgl = ycPoints3[i];
                                                WindPowerStationTestingPoint ycglpoint = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(station.getId(), ycPoints2[i]);

                                                vos.get(datas.size() + i).setValue4(StringUtils.round(realApiUtil.getRealData(ycglpoint).getPointValueInDouble() + realApiUtil.getRealData(qs3_ycgl).getPointValueInDouble(), 2));
                                            }
                                        }


                                    } else {
                                        for (int i = 0; i < subresult; i++) {
                                            cal.add(Calendar.HOUR_OF_DAY, 1);
                                            vos.get(datas.size() + i).setTime(cal.getTime().getTime());

                                            WindPowerStationTestingPoint ycglpoint = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(station.getId(), ycPoints2[i]);

                                            double ycgl = realApiUtil.getRealData(ycglpoint).getPointValueInDouble();

                                            ycgl = (vos.get(datas.size() + i).getValue4() == null ? 0.0 : vos.get(datas.size() + i).getValue4()) + ycgl;

                                            vos.get(datas.size() + i).setValue4(StringUtils.round(ycgl, 2));
                                        }
                                    }


//                                    total = datas.size() + subresult;
//                                    subresult = length - (total);
//
//                                    if (subresult > 0)
//                                        for (int i = 0; i < subresult; i++) {
//                                            cal.add(Calendar.HOUR_OF_DAY, 1);
//                                            vos.get(total + i).setTime(cal.getTime().getTime());
//                                            vos.get(total + i).setValue4(null);
//                                        }
                                }

                            }

                        }
/***************************************************************超短期功率预测***************************************************************************/


/***************************************************************短期功率预测***************************************************************************/
                        WindPowerStationTestingPoint dqgl1 = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(station.getId(), ycDqPoints1[0]);

                        if (null != dqgl1) {
                            List<PointData> datas = realApiUtil.getHistoryDatasSnap(dqgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, pried);
                            if (!datas.isEmpty()) {

                                if (InitialRunner.appendYcglMap.containsKey(station.getId())) {
                                    Map<String, Object> ycmap = InitialRunner.appendYcglMap.get(station.getId());
                                    if (ycmap.containsKey("dq1")) {
                                        String qs3_ycgl1 = (String) ycmap.get("dq1");

                                        List<PointData> qs3_datas = realApiUtil.getHistoryDatasSnap(qs3_ycgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, pried);

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

                                            vos.get(j).setTime(datas.get(j).getPointTime() * 1000);
                                            vos.get(j).setValue7(StringUtils.round(datas.get(j).getPointValueInDouble() + qs3_datas.get(j).getPointValueInDouble(), 2));
                                            if (j == datas.size() - 1) {
                                                cal.setTime(new Date(datas.get(j).getPointTime() * 1000));
                                            }
                                        }
                                    }


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

                                        vos.get(j).setTime(datas.get(j).getPointTime() * 1000);

                                        double ycgl = (vos.get(j).getValue7() == null ? 0.0 : vos.get(j).getValue7()) + (datas.get(j) == null ? 0.0 : datas.get(j).getPointValueInDouble());

                                        vos.get(j).setValue7(StringUtils.round(ycgl, 2));

                                        if (j == datas.size() - 1) {
                                            cal.setTime(new Date(datas.get(j).getPointTime() * 1000));
                                        }
                                    }
                                }

                                int subresult = 0;
                                int total = 0;

                                if (length > datas.size()) {
                                    subresult = length - datas.size();
                                    if (subresult >= dqyc) {
                                        subresult = dqyc;
                                    }

                                    if (InitialRunner.appendYcglMap.containsKey(station.getId())) {
                                        Map<String, Object> ycmap = InitialRunner.appendYcglMap.get(station.getId());
                                        if (ycmap.containsKey("dq2")) {
                                            String[] ycPoints3 = (String[]) ycmap.get("dq2");
                                            for (int i = 0; i < subresult; i++) {
                                                cal.add(Calendar.HOUR_OF_DAY, 1);
                                                vos.get(datas.size() + i).setTime(cal.getTime().getTime());
                                                String qs3_ycgl = ycPoints3[i];
                                                WindPowerStationTestingPoint ycglpoint = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(station.getId(), ycDqPoints2[i]);

                                                vos.get(datas.size() + i).setValue7(StringUtils.round(realApiUtil.getRealData(ycglpoint).getPointValueInDouble() + realApiUtil.getRealData(qs3_ycgl).getPointValueInDouble(), 2));
                                            }
                                        }


                                    } else {
                                        for (int i = 0; i < subresult; i++) {
                                            cal.add(Calendar.HOUR_OF_DAY, 1);
                                            vos.get(datas.size() + i).setTime(cal.getTime().getTime());

                                            WindPowerStationTestingPoint ycglpoint = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(station.getId(), ycDqPoints2[i]);

                                            double ycgl = realApiUtil.getRealData(ycglpoint).getPointValueInDouble();

                                            ycgl = (vos.get(datas.size() + i).getValue7() == null ? 0.0 : vos.get(datas.size() + i).getValue7()) + ycgl;

                                            vos.get(datas.size() + i).setValue7(StringUtils.round(ycgl, 2));
                                        }
                                    }


//                                    total = datas.size() + subresult;
//                                    subresult = length - (total);
//
//                                    if (subresult > 0)
//                                        for (int i = 0; i < subresult; i++) {
//                                            cal.add(Calendar.HOUR_OF_DAY, 1);
//                                            vos.get(total + i).setTime(cal.getTime().getTime());
//                                            vos.get(total + i).setValue7(null);
//                                        }
                                }

                            }

                        }
/***************************************************************短期功率预测***************************************************************************/


                    }

                }


                // 实际发功率
                WindPowerStationTestingPoint sjgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_SJGL);
                // 总应发功率
                WindPowerStationTestingPoint yfgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_ZZSGL);
                // 总理论功率
                WindPowerStationTestingPoint llgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_ZLLGL);
                // 总保证功率
                WindPowerStationTestingPoint bzgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.ZBZGL);
                //实时平均风速
                WindPowerStationTestingPoint pjfs = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_SSFS);
                // 最优功率
                WindPowerStationTestingPoint zygl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_ZZYGL);


                // 光伏实际发功率
                WindPowerStationTestingPoint gdsjgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(GF, Constant.TPOINT_WP_SJGL);
                List<PointData> gdsjglls = realApiUtil.getHistoryDatasSnap(gdsjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

                try {


                    if (null != yfgl) {


                        List<PointData> yfglls = realApiUtil.getHistoryDatasSnap(yfgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

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

                            if (i < yfglls.size() && StringUtils.notEmp(yfglls.get(i).getPointTime())) {

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp1 = yfglls.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();


                                    if (id.equals(QS)) {
                                        vos.get(i).setValue1(StringUtils.round(temp1 + temp2, 2));// 应发功率
                                    } else {
                                        vos.get(i).setValue1(StringUtils.round(temp1, 2));// 应发功率
                                    }
                                } else {
                                    vos.get(i).setValue1(0d);// 应发功率
                                }

                            }
                        }


                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }

                try {

                    if (null != llgl) {
                        List<PointData> llglls = realApiUtil.getHistoryDatasSnap(llgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

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

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

                                if (vos.get(i).getTime() <= DateUtils.now().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();

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

                                        if (id.equals(QS)) {
                                            vos.get(i).setValue3(StringUtils.round(temp1 + temp2, 2));// 理论功率
                                        } else {
                                            vos.get(i).setValue3(StringUtils.round(temp1, 2));// 理论功率
                                        }

                                    }

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

                            }
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }

                try {
                    if (null != bzgl) {
                        List<PointData> bzglls = realApiUtil.getHistoryDatasSnap(bzgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

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

                            if (i < bzglls.size() && StringUtils.notEmp(bzglls.get(i).getPointTime())) {

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp1 = bzglls.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();

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

                                        if (id.equals(QS)) {
                                            vos.get(i).setValue5(StringUtils.round(temp1 + temp2, 2));// 保证功率
                                        } else {
                                            vos.get(i).setValue5(StringUtils.round(temp1, 2));// 保证功率
                                        }
                                    }

                                    count++;
                                } else {
                                    vos.get(i).setValue5(0d);// 保证功率
                                }

                            }
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }

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

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

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

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

                        }
                    }

                }
                try {
                    if (null != pjfs) {
                        List<PointData> pjfsls = realApiUtil.getHistoryDatasSnap(pjfs, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

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

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

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp = pjfsls.get(i).getPointValueInDouble();

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

                            }
                        }
                    }
                } catch (Exception e) {

                }
                try {
                    if (null != zygl) {
                        List<PointData> zyglls = realApiUtil.getHistoryDatasSnap(zygl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

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

                            if (i < zyglls.size() && StringUtils.notEmp(zyglls.get(i).getPointTime())) {

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp = zyglls.get(i).getPointValueInDouble();
                                    temp = new BigDecimal(temp).divide(new BigDecimal(100000), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                    if (Math.abs(temp) < 10000) {
                                        vos.get(i).setValue9(StringUtils.round(temp, 2));// 最优功率
                                    }
                                } else {
                                    vos.get(i).setValue9(0d);// 最优功率
                                }

                            }
                        }
                    }
                } catch (Exception e) {

                }

            } else if (InitialRunner.wpmap.containsKey(id) || InitialRunner.pjmap.containsKey(id) || InitialRunner.lnmap.containsKey(id)) {

                if (InitialRunner.wpmap.containsKey(id)) {
                    if (num > 0) {
 /***************************************************************超短期功率预测***************************************************************************/
                        WindPowerStationTestingPoint ycgl1 = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, ycPoints1[0]);

                        if (null != ycgl1) {
                            List<PointData> datas = realApiUtil.getHistoryDatasSnap(ycgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, pried);
                            if (!datas.isEmpty()) {

                                if (InitialRunner.appendYcglMap.containsKey(id)) {
                                    Map<String, Object> ycmap = InitialRunner.appendYcglMap.get(id);
                                    if (ycmap.containsKey("cdq1")) {
                                        String qs3_ycgl1 = (String) ycmap.get("cdq1");

                                        List<PointData> qs3_datas = realApiUtil.getHistoryDatasSnap(qs3_ycgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, pried);

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

//                                            vos.get(j).setTime(datas.get(j).getPointTime() * 1000);
                                            vos.get(j).setValue4(StringUtils.round(datas.get(j).getPointValueInDouble() + qs3_datas.get(j).getPointValueInDouble(), 2));
                                            if (j == datas.size() - 1) {
                                                cal.setTime(new Date(datas.get(j).getPointTime() * 1000));
                                            }
                                        }
                                    }

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

//                                        vos.get(j).setTime(datas.get(j).getPointTime() * 1000);

                                        double ycgl = (vos.get(j).getValue4() == null ? 0.0 : vos.get(j).getValue4()) + (datas.get(j) == null ? 0.0 : datas.get(j).getPointValueInDouble());

                                        vos.get(j).setValue4(StringUtils.round(ycgl, 2));

                                        if (j == datas.size() - 1) {
                                            cal.setTime(new Date(datas.get(j).getPointTime() * 1000));
                                        }
                                    }
                                }

                                int subresult = 0;
                                int total = 0;

                                if (length > datas.size()) {
                                    subresult = length - datas.size();
                                    if (subresult >= cdqyc) {
                                        subresult = cdqyc;
                                    }

                                    if (InitialRunner.appendYcglMap.containsKey(id)) {
                                        Map<String, Object> ycmap = InitialRunner.appendYcglMap.get(id);
                                        if (ycmap.containsKey("cdq2")) {
                                            String[] ycPoints3 = (String[]) ycmap.get("cdq2");
                                            for (int i = 0; i < subresult; i++) {
                                                cal.add(Calendar.HOUR_OF_DAY, 1);
                                                vos.get(datas.size() + i).setTime(cal.getTime().getTime());
                                                String qs3_ycgl = ycPoints3[i];
                                                WindPowerStationTestingPoint ycglpoint = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, ycPoints2[i]);

                                                vos.get(datas.size() + i).setValue4(StringUtils.round(realApiUtil.getRealData(ycglpoint).getPointValueInDouble() + realApiUtil.getRealData(qs3_ycgl).getPointValueInDouble(), 2));
                                            }
                                        }

                                    } else {
                                        for (int i = 0; i < subresult; i++) {
                                            cal.add(Calendar.HOUR_OF_DAY, 1);

                                            vos.get(datas.size() + i).setTime(cal.getTime().getTime());

                                            WindPowerStationTestingPoint ycglpoint = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, ycPoints2[i]);

                                            double ycgl = realApiUtil.getRealData(ycglpoint).getPointValueInDouble();

                                            ycgl = (vos.get(datas.size() + i).getValue4() == null ? 0.0 : vos.get(datas.size() + i).getValue4()) + ycgl;

                                            vos.get(datas.size() + i).setValue4(StringUtils.round(ycgl, 2));
                                        }
                                    }


                                    total = datas.size() + subresult;
                                    subresult = length - (total);

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

                            }

                        }
/***************************************************************超短期功率预测***************************************************************************/


/***************************************************************短期功率预测***************************************************************************/
                        WindPowerStationTestingPoint dqgl1 = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, ycDqPoints1[0]);

                        if (null != dqgl1) {
                            List<PointData> datas = realApiUtil.getHistoryDatasSnap(dqgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, pried);
                            if (!datas.isEmpty()) {

                                if (InitialRunner.appendYcglMap.containsKey(id)) {
                                    Map<String, Object> ycmap = InitialRunner.appendYcglMap.get(id);
                                    if (ycmap.containsKey("dq1")) {
                                        String qs3_ycgl1 = (String) ycmap.get("dq1");

                                        List<PointData> qs3_datas = realApiUtil.getHistoryDatasSnap(qs3_ycgl1, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, pried);

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

                                            vos.get(j).setTime(datas.get(j).getPointTime() * 1000);
                                            vos.get(j).setValue7(StringUtils.round(datas.get(j).getPointValueInDouble() + qs3_datas.get(j).getPointValueInDouble(), 2));
                                            if (j == datas.size() - 1) {
                                                cal.setTime(new Date(datas.get(j).getPointTime() * 1000));
                                            }
                                        }
                                    }

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

                                        vos.get(j).setTime(datas.get(j).getPointTime() * 1000);

                                        double ycgl = (vos.get(j).getValue7() == null ? 0.0 : vos.get(j).getValue7()) + (datas.get(j) == null ? 0.0 : datas.get(j).getPointValueInDouble());

                                        vos.get(j).setValue7(StringUtils.round(ycgl, 2));

                                        if (j == datas.size() - 1) {
                                            cal.setTime(new Date(datas.get(j).getPointTime() * 1000));
                                        }
                                    }
                                }

                                int subresult = 0;
                                int total = 0;

                                if (length > datas.size()) {
                                    subresult = length - datas.size();
                                    if (subresult >= dqyc) {
                                        subresult = dqyc;
                                    }

                                    if (InitialRunner.appendYcglMap.containsKey(id)) {
                                        Map<String, Object> ycmap = InitialRunner.appendYcglMap.get(id);
                                        if (ycmap.containsKey("dq2")) {
                                            String[] ycPoints3 = (String[]) ycmap.get("dq2");
                                            for (int i = 0; i < subresult; i++) {
                                                cal.add(Calendar.HOUR_OF_DAY, 1);
                                                vos.get(datas.size() + i).setTime(cal.getTime().getTime());
                                                String qs3_ycgl = ycPoints3[i];
                                                WindPowerStationTestingPoint ycglpoint = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, ycDqPoints2[i]);

                                                vos.get(datas.size() + i).setValue7(StringUtils.round(realApiUtil.getRealData(ycglpoint).getPointValueInDouble() + realApiUtil.getRealData(qs3_ycgl).getPointValueInDouble(), 2));
                                            }
                                        }

                                    } else {
                                        for (int i = 0; i < subresult; i++) {
                                            cal.add(Calendar.HOUR_OF_DAY, 1);

                                            vos.get(datas.size() + i).setTime(cal.getTime().getTime());

                                            WindPowerStationTestingPoint ycglpoint = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, ycDqPoints2[i]);

                                            double ycgl = realApiUtil.getRealData(ycglpoint).getPointValueInDouble();

                                            ycgl = (vos.get(datas.size() + i).getValue7() == null ? 0.0 : vos.get(datas.size() + i).getValue7()) + ycgl;

                                            vos.get(datas.size() + i).setValue7(StringUtils.round(ycgl, 2));
                                        }
                                    }


//                                    total = datas.size() + subresult;
//                                    subresult = length - (total);
//
//                                    if (subresult > 0)
//                                        for (int i = 0; i < subresult; i++) {
//                                            cal.add(Calendar.HOUR_OF_DAY, 1);
//                                            vos.get(total + i).setTime(cal.getTime().getTime());
//                                            vos.get(total + i).setValue7(null);
//                                        }
                                }

                            }

                        }
/***************************************************************短期功率预测***************************************************************************/
                    }
                }


                // 实际发功率
                WindPowerStationTestingPoint sjgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_SJGL);
                // 总应发功率
                WindPowerStationTestingPoint yfgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_ZZSGL);
                // 总理论功率
                WindPowerStationTestingPoint llgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_ZLLGL);
                // 总保证功率
                WindPowerStationTestingPoint bzgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.ZBZGL);
                //实时平均风速
                WindPowerStationTestingPoint pjfs = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_SSFS);
                // 最优功率
                WindPowerStationTestingPoint zygl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_ZZYGL);


                try {


                    if (null != yfgl) {
                        List<PointData> yfglls = realApiUtil.getHistoryDatasSnap(yfgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

                        double temp1 = 0;

                        if (!InitialRunner.wpmap.containsKey(id)) {
                            cal.setTime(beginDate);
                        }
                        for (int i = 0; i < vos.size(); i++) {

                            if (!InitialRunner.wpmap.containsKey(id)) {
                                vos.get(i).setTime(cal.getTime().getTime());
                            }

                            if (i < yfglls.size() && StringUtils.notEmp(yfglls.get(i).getPointTime())) {

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp1 = yfglls.get(i).getPointValueInDouble();

                                    temp1 = new BigDecimal(temp1).divide(new BigDecimal(100000), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                    if (Math.abs(temp1) < 10000) {
                                        vos.get(i).setValue1(StringUtils.round(temp1, 2));// 应发功率
                                    }

                                } else {
                                    vos.get(i).setValue1(0d);// 应发功率
                                }

                            }

                            if (!InitialRunner.wpmap.containsKey(id)) {
                                cal.add(Calendar.HOUR_OF_DAY, 1);
                            }
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }

                try {
                    if (null != llgl) {
                        List<PointData> llglls = realApiUtil.getHistoryDatasSnap(llgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

                        int count = 0;
                        double temp1 = 0;

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

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

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp1 = llglls.get(i).getPointValueInDouble();

                                    temp1 = new BigDecimal(temp1).divide(new BigDecimal(100000), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

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

                                        vos.get(i).setValue3(StringUtils.round(temp1, 2));// 理论功率
                                    }

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

                            }
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }

                try {
                    if (null != bzgl) {
                        List<PointData> bzglls = realApiUtil.getHistoryDatasSnap(bzgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

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

                            if (i < bzglls.size() && StringUtils.notEmp(bzglls.get(i).getPointTime())) {

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp1 = bzglls.get(i).getPointValueInDouble();

                                    temp1 = new BigDecimal(temp1).divide(new BigDecimal(100000), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

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

                                        vos.get(i).setValue5(StringUtils.round(temp1, 2));// 保证功率
                                    }

                                    count++;
                                } else {
                                    vos.get(i).setValue5(0d);// 保证功率
                                }

                            }
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }

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

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

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

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

                        }
                    }

                }
                try {
                    if (null != pjfs) {
                        List<PointData> pjfsls = realApiUtil.getHistoryDatasSnap(pjfs, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

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

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

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp = pjfsls.get(i).getPointValueInDouble();

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

                            }
                        }
                    }
                } catch (Exception e) {

                }
                try {
                    if (null != zygl) {
                        List<PointData> zyglls = realApiUtil.getHistoryDatasSnap(zygl, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, pried);

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

                            if (i < zyglls.size() && StringUtils.notEmp(zyglls.get(i).getPointTime())) {

                                if (vos.get(i).getTime() <= DateUtils.now().getTime()) {
                                    temp = zyglls.get(i).getPointValueInDouble();
                                    temp = new BigDecimal(temp).divide(new BigDecimal(100000), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                    if (Math.abs(temp) < 10000) {
                                        vos.get(i).setValue9(StringUtils.round(temp, 2));// 最优功率
                                    }
                                } else {
                                    vos.get(i).setValue9(0d);// 最优功率
                                }

                            }
                        }
                    }
                } catch (Exception e) {

                }

            }

        }

        return vos;
    }


    public List<Object> findGLDetail(String id, String gltype) throws Exception {

        List<DataVo> vols = new ArrayList<DataVo>();


        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();
        cal.add(Calendar.DAY_OF_MONTH, 1);

        Date endDate = cal.getTime();

        int length = 48;

        int type = 0;

        // Map<String, List> resultMap = new HashMap<String, List>();

        List<Object> resultls = new ArrayList<Object>();
        if (StringUtils.notEmp(id)) {
            if (id.equals(QS) || id.equals(FD) || id.equals(GF)) {

                if (id.equals("") || id.equals(QS)) {
                    for (Windpowerstation station : InitialRunner.wpallls) {
                        DataVo vo = new DataVo();
                        vo.setId(station.getId());
                        vo.setName(station.getName());
                        vols.add(vo);
                    }
                } else if (id.equals(FD)) {
                    for (Windpowerstation station : InitialRunner.wpallls) {
                        if (station.getId().endsWith("FDC")) {
                            DataVo vo = new DataVo();
                            vo.setId(station.getId());
                            vo.setName(station.getName());
                            vols.add(vo);
                        }
                    }
                } else {
                    for (Windpowerstation station : InitialRunner.wpallls) {
                        if (station.getId().endsWith("GDC")) {
                            DataVo vo = new DataVo();
                            vo.setId(station.getId());
                            vo.setName(station.getName());
                            vols.add(vo);
                        }
                    }
                }
            } else if (InitialRunner.wpmap.containsKey(id)) {
                for (Project pj : InitialRunner.pjsmap.get(id)) {
                    DataVo vo = new DataVo();
                    vo.setId(pj.getId());
                    vo.setName(pj.getName());
                    vols.add(vo);

                }
            } else if (InitialRunner.pjmap.containsKey(id)) {
                for (Line ln : InitialRunner.lnsmap.get(id)) {
                    DataVo vo = new DataVo();
                    vo.setId(ln.getId());
                    vo.setName(ln.getName());
                    vols.add(vo);

                }
            } else if (InitialRunner.lnmap.containsKey(id)) {
                for (Windturbine wt : InitialRunner.ln_wtmap.get(id)) {
                    DataVo vo = new DataVo();
                    vo.setId(wt.getId());
                    vo.setName(wt.getName());
                    vols.add(vo);

                    type = 1;
                }
            }
        }

        if (null != id) {

            for (DataVo vo : vols) {
                List<DataVo> vos = new ArrayList<DataVo>();
                for (int i = 0; i < length; i++) {
                    DataVo v = new DataVo();
                    v.setName(vo.getName());
                    v.setId(vo.getId());
                    v.setValue1(0.0);
                    vos.add(v);
                }

                String pointid = null;
                if (type == 0) {
                    switch (gltype) {
                        case "sjgl":

                            // 实际发功率
                            WindPowerStationTestingPoint sjgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(vo.getId(), Constant.TPOINT_WP_SJGL);

                            if(null!=sjgl && null!=sjgl.getCode())
                            {
                                pointid=sjgl.getCode();
                            }
                            break;
                        case "bzgl":
                            // 总保证功率
                            WindPowerStationTestingPoint bzgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(vo.getId(), Constant.ZBZGL);
                            if(null!=bzgl && null!=bzgl.getCode())
                            {
                                pointid=bzgl.getCode();
                            }
                            break;
                        case "yfgl":
                            // 总应发功率
                            WindPowerStationTestingPoint yfgl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(vo.getId(), Constant.TPOINT_WP_ZZSGL);
                            if(null!=yfgl && null!=yfgl.getCode())
                            {
                                pointid=yfgl.getCode();
                            }
                            break;
                        case "ssfs":
                            //实时平均风速
                            WindPowerStationTestingPoint ssfs = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(vo.getId(), Constant.TPOINT_WP_SSFS);
                            if(null!=ssfs && null!=ssfs.getCode())
                            {
                                pointid=ssfs.getCode();
                            }
                            break;
                        default:
                            break;
                    }
                } else {
                    switch (gltype) {
                        case "sjgl":

                            // 实际发功率
                            WindTurbineTestingPointAi sjgl = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(vo.getId(), Constant.TPOINT_WT_FJGL);

                            if(null!=sjgl && null!=sjgl.getCode())
                            {
                                pointid=sjgl.getId();
                            }
                            break;
                        case "bzgl":
                            // 总保证功率
                            WindTurbineTestingPointAi bzgl = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(vo.getId(), Constant.TPOINT_WT_LLGL);
                            if(null!=bzgl && null!=bzgl.getCode())
                            {
                                pointid=bzgl.getId();
                            }
                            break;
                        case "yfgl":
                            // 总应发功率
                            WindTurbineTestingPointAi yfgl = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(vo.getId(), Constant.TPOINT_WT_YFGL);
                            if(null!=yfgl && null!=yfgl.getCode())
                            {
                                pointid=yfgl.getId();
                            }
                            break;
                        case "ssfs":
                            //实时平均风速
                            WindTurbineTestingPointAi ssfs = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(vo.getId(), Constant.TPOINT_WT_FJSSFS);
                            if(null!=ssfs && null!=ssfs.getCode())
                            {
                                pointid=ssfs.getId();
                            }
                            break;
                        default:
                            break;
                    }
                }

                if(StringUtils.notEmp(pointid))
                {
                    List<PointData> glls = realApiUtil.getHistoryDatasSnap(pointid, beginDate.getTime() / 1000, endDate.getTime() / 1000, (long) length, 1800L);

                    for (int i = 0; i < length; i++) {

                        if (i < glls.size() && StringUtils.notEmp(glls.get(i).getPointTime())) {

                            if (glls.get(i).getPointTime() <= DateUtils.now().getTime()) {

                                vos.get(i).setTime(glls.get(i).getPointTime() * 1000);
                                double temp1 = glls.get(i).getPointValueInDouble();

                                if(type==0 && (gltype.equals("bzgl") || gltype.equals("yfgl")))
                                {
                                    temp1 = new BigDecimal(temp1).divide(new BigDecimal(100000), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                                }
                                vos.get(i).setValue1(StringUtils.round(temp1, 2));
                            } else {
                                vos.get(i).setValue1(0d);
                                vos.get(i).setTime(glls.get(i).getPointTime() * 1000);
                            }

                        }
                    }

                    // resultMap.put(vo.getId(), vos);
                    resultls.add(vos);
                }

            }
        }

        // ////////////////////////////////////////////////

        return resultls;
    }

}


