package com.gyee.frame.service.health;

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.DNAVal;
import com.gyee.frame.model.custom.MatrixVo;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.custom.ShutdowneventVo;
import com.gyee.frame.service.*;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.MathUtil;
import com.gyee.frame.util.StringUtils;
import com.gyee.frame.util.golden.EdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class HealthMainService {

    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;
    @Resource
    private WeatherfhService weatherfhService;
    @Resource
    private WeatherfdService weatherfdService;
    @Resource
    private ShutdowneventService shutdowneventService;
    @Resource
    private WindPowerstationTestingPointService windPowerstationTestingPointService;


    private final int digit = 2;
    private final double NUM = 3.6;
    private final double XZNUM = 1.25;

    IRealTimeDataBaseUtil realApiUtil = new EdosUtil();


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


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

        for (Windpowerstation wp:InitialRunner.wpls) {



            int ysl = 0;// 优数量
            int lsl = 0;// 良数量
            int csl = 0;// 差数量

            List<String[]> wtIdls = new ArrayList<String[]>();// 风机编号集合

            List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wp.getId());


            String[] jkpoints = new String[wtls.size()];
            String[] ztpoints = new String[wtls.size()];

            // 获得训练参数对象
            for (int x = 0; x < wtls.size(); x++) {

                Windturbine wt=wtls.get(x);

                WindTurbineTestingPointAi2 jkztpoint =windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(),Constant.FJJKZZ);
                jkpoints[x] = jkztpoint.getId();
                WindTurbineTestingPointAi2 fjztpoint =windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(),Constant.FJZT);
                ztpoints[x] = fjztpoint.getId();

            }

            // 获得风机健康状态结果数组
            List<PointData> jkvalues = realApiUtil.getRealData(jkpoints);
            // 获得风机实时状态结果数组
            List<PointData> ztvalues = realApiUtil.getRealData(ztpoints);


            Calendar cal2 = Calendar.getInstance();
            Date end = cal2.getTime();
            cal2.add(Calendar.DAY_OF_MONTH, -1);
            Date begin = cal2.getTime();
            for (int x = 0; x < wtls.size(); x++) {
                String wtId = wtls.get(x).getId();
                double jk = jkvalues.get(x).getPointValueInDouble();
                double zt = ztvalues.get(x).getPointValueInDouble();


                if (zt == 2.0) {

                    String[] str = new String[2];
                    str[0] = wtId;
                    str[1] = "3";
                    wtIdls.add(str);
                    csl++;
                } else {
                    String[] str = new String[2];
                    if (jk >= 60) {
                        str[0] = wtId;
                        str[1] = "1";
                        wtIdls.add(str);
                        ysl++;
                    } else if (jk < 60  && jk >= 30) {
                        str[0] = wtId;
                        str[1] = "2";
                        wtIdls.add(str);
                        lsl++;
                    } else {
                        str[0] = wtId;
                        str[1] = "3";
                        wtIdls.add(str);
                        csl++;
                    }

                }

            }

            map.put(wp.getId() + "_csl", csl);
            map.put(wp.getId() + "_lsl", lsl);
            map.put(wp.getId() + "_ysl", ysl);
            map.put(wp.getId() + "_wtIdls", wtIdls);

        }

        return map;
    }
    /**
     * 获得健康状态日情况图表
     *
     * @param wpId
     * @param num  多少个点
     * @param type 1、当日2、 7日3、当月
     * @return
     * @throws Exception
     */
    public Map<String, Object[]> findAllChart(String wpId, int num, int type) throws Exception {

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

        if (StringUtils.notEmp(wpId) && StringUtils.notEmp(wpId) && StringUtils.notEmp(wpId)) {
            String format1 = "yyyy-MM-dd HH点";
            String format2 = "yyyy-MM-dd";
            String format3 = "MM-dd";

            SimpleDateFormat df1 = new SimpleDateFormat(format1);
            SimpleDateFormat df2 = new SimpleDateFormat(format2);
            SimpleDateFormat df3 = new SimpleDateFormat(format3);

            Calendar cal = Calendar.getInstance();

            String uniformcode = Constant.YCFJJKZT1DAY;
            switch (type) {
                case 1:
                    cal.add(Calendar.HOUR_OF_DAY, 2);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);

                    uniformcode = Constant.YCFJJKZT1DAY;
                    break;
                case 2:
                    cal.add(Calendar.DAY_OF_MONTH, 1);
                    cal.set(Calendar.HOUR_OF_DAY, 0);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);

                    uniformcode = Constant.YCFJJKZT1MONTH;
                    //   uniformcode = Constant.YCFJJKZT7DAY;
                    break;
                case 3:
                    cal.add(Calendar.DAY_OF_MONTH, 1);
                    cal.set(Calendar.HOUR_OF_DAY, 0);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);
                    uniformcode = Constant.YCFJJKZT1MONTH;
                    break;
                default:
                    break;
            }

            int ysl = 0;// 优数量
            int lsl = 0;// 良数量
            int csl = 0;// 差数量
            double jkd = 0.0;

            Double[] yslchart = new Double[num];
            Double[] lslchart = new Double[num];
            Double[] cslchart = new Double[num];
            Double[] lvchart = new Double[num];

            String[] datechart = new String[num];
            List<Windturbine> wtls = new ArrayList<Windturbine>();

            if (InitialRunner.wpmap.containsKey(wpId)) {
                wtls = InitialRunner.wp_wtmap.get(wpId);
            } else {
                for (Windturbine wt : InitialRunner.wtls) {
                    wtls.add(wt);
                }
            }

            List<String> wtidls = new ArrayList<String>();
            List<String> codejkls = new ArrayList<String>();


            for (Windturbine wt : wtls) {
                wtidls.add(wt.getId());
                codejkls.add(uniformcode);

            }

            String[] jkpoints = new String[wtidls.size()];

            // 获得训练参数对象
            for (int x = 0; x < wtidls.size(); x++) {


                WindTurbineTestingPointAi2 ai = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtidls.get(x), codejkls.get(x));
                jkpoints[x] = ai.getId();
            }

            for (int i = 0; i < num; i++) {
                ysl = 0;// 优数量
                lsl = 0;// 良数量
                csl = 0;// 差数量
                jkd = 0.0;

                int recodeDate = new BigDecimal(cal.getTime().getTime()).divide(new BigDecimal(1000), 0, RoundingMode.HALF_EVEN).intValue();

                // 获得风机健康状态结果数组
                DNAVal[] jkvalues = realApiUtil.getHistMatrix(jkpoints, recodeDate);

                for (int x = 0; x < wtidls.size(); x++) {

                    double jk = jkvalues[x].DValue;

                    if (jk == 4.0) {

                        csl++;
                    } else if (jk == 3.0) {

                        lsl++;
                    } else if (jk == 2.0 || jk == 1.0) {

                        ysl++;
                    } else {

                        ysl++;
                    }
                }
                jkd = new BigDecimal(ysl + lsl).divide(new BigDecimal(wtidls.size()), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                yslchart[i] = Double.valueOf(ysl);
                lslchart[i] = Double.valueOf(lsl);
                cslchart[i] = Double.valueOf(csl);
                lvchart[i] = Double.valueOf(jkd);

                switch (type) {
                    case 1:
                        datechart[i] = df1.format(cal.getTime());
                        cal.add(Calendar.HOUR_OF_DAY, 2);

                        break;
                    case 2:
                        datechart[i] = df2.format(cal.getTime());
                        cal.add(Calendar.DAY_OF_MONTH, 1);
                        break;
                    case 3:
                        datechart[i] = df3.format(cal.getTime());
                        cal.add(Calendar.DAY_OF_MONTH, 1);
                        break;
                    default:
                        break;
                }

            }

            map.put("yslchart", yslchart);
            map.put("lslchart", lslchart);
            map.put("cslchart", cslchart);
            map.put("lvchart", lvchart);
            map.put("datechart", datechart);

        }

        return map;

    }


    /**
     * 获得全省健康状态基础数据
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> findAllMap() throws Exception {

        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);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        Date endDate = cal.getTime();

        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, String>> gzls = new ArrayList<Map<String, String>>();
        // 获得天气预报每小时数据
        for (Windpowerstation wp : InitialRunner.wpls) {

            String wpId = wp.getId();

            if (wpId.endsWith("FDC")) {

                double tqyb;// 天气预报
                double ycfs;// 预测风速
                double sjfs;// 实际风速
                List<String> gzWtId = new ArrayList<String>();// 故障风机编号集合
                List<String> lhWtId = new ArrayList<String>();// 良好风机编号集合
                List<String> bjWtId = new ArrayList<String>();// 故障部件编号集合

                int ysl = 0;// 优数量
                int lsl = 0;// 良数量
                int csl = 0;// 差数量
                double jkd = 0.0;
                List<Weatherfh> wfhls = weatherfhService.findWeatherfh(beginDate, endDate, wp.getId());
                if (!wfhls.isEmpty()) {

                    Weatherfh wth = wfhls.get(wfhls.size() - 1);

                    String temp = wth.getWeather();
                    if (StringUtils.notEmp(temp)) {
                        String strtemp = temp.substring(temp.length() - 6, temp.length() - 4);
                        tqyb = Double.valueOf(strtemp);
                    } else {
                        tqyb = 1.0;
                    }

                    ycfs = wth.getSpeed();

                    ycfs = new BigDecimal(ycfs).multiply(new BigDecimal(XZNUM)).divide(new BigDecimal(NUM), 2, RoundingMode.HALF_EVEN).doubleValue();
                    map.put(wpId + "_ycfs", ycfs);
                    map.put(wpId + "_tqyb", tqyb);

                } else {
                    map.put(wpId + "_ycfs", 6.0);
                    map.put(wpId + "_tqyb", 1.0);
                }
                // 获得实时风速测点编号

                WindPowerStationTestingPoint2 point = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wp.getId(), Constant.TPOINT_WP_SSFS);
                String wppintId = point.getCode();

                PointData ssfspoint = realApiUtil.getRealData(wppintId);
                sjfs = StringUtils.round(ssfspoint.getPointValueInDouble(), digit);

                map.put(wpId + "_sjfs", sjfs);
                List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wp.getId());
                List<String> wtidls = new ArrayList<String>();
                List<String> codejkls = new ArrayList<String>();

                List<String> codeztls = new ArrayList<String>();

                for (Windturbine wt : wtls) {
                    wtidls.add(wt.getId());
                    codejkls.add(Constant.FJJKZZ);
                    codeztls.add(Constant.FJZT);
                }

                cal = Calendar.getInstance();
                // cal.add(Calendar.MINUTE, -1);
                // int recodeDate = new BigDecimal(cal.getTime().getTime())
                // .divide(new BigDecimal(1000), 0,
                // RoundingMode.HALF_EVEN).intValue();

                String[] jkpoints = new String[wtidls.size()];
                String[] ztpoints = new String[wtidls.size()];


                // 获得训练参数对象
                for (int x = 0; x < wtidls.size(); x++) {

                    WindTurbineTestingPointAi2 ai1 = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtidls.get(x), codejkls.get(x));
                    jkpoints[x] = ai1.getId();
                    WindTurbineTestingPointAi2 ai2 = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtidls.get(x), codeztls.get(x));
                    ztpoints[x] = ai2.getId();

                }
                // 获得风机健康状态结果数组
                DNAVal[] jkvalues = realApiUtil.getRealtimeTagValues(jkpoints);

                // 获得风机实时状态结果数组
                DNAVal[] ztvalues = realApiUtil.getRealtimeTagValues(ztpoints);

                Calendar cal2 = Calendar.getInstance();
                Date end = cal2.getTime();
                cal2.add(Calendar.DAY_OF_MONTH, -1);
                Date begin = cal2.getTime();
                for (int x = 0; x < wtidls.size(); x++) {
                    String wtId = wtidls.get(x);
                    double jk = jkvalues[x].DValue;

                    double zt = ztvalues[x].DValue;
                    if (zt == 2.0) {

                        List<ShutdowneventVo> svos = shutdowneventService.queryEvents(null, wtId, begin, end, null);

                        List<Map<String, String>> stoptypels = new ArrayList<Map<String, String>>();

                        if (!svos.isEmpty()) {
                            for (ShutdowneventVo vo : svos) {
                                Map<String, String> stoptypemap = new HashMap<String, String>();
                                stoptypemap.put("stopTypeId", vo.getStopTypeId());
                                stoptypemap.put("windTurbineId", vo.getWindTurbineId());
                                stoptypemap.put("stopTime", DateUtils.formatDateTime(vo.getStopTime()));

                                if (StringUtils.notEmp(vo.getWarningId()) && InitialRunner.warningmap.containsKey(vo.getWarningId())) {
                                    stoptypemap.put("warningId", InitialRunner.warningmap.get(vo.getWarningId()).getChinesetext());
                                } else {
                                    stoptypemap.put("warningId", " ");
                                }
                                stoptypels.add(stoptypemap);
                            }
                        }

                        if (StringUtils.notEmp(stoptypels) && !stoptypels.isEmpty()) {
                            if (StringUtils.notEmp(stoptypels.get(0))) {
                                Map<String, String> strmap = stoptypels.get(0);

                                String stoptype = strmap.get("stopTypeId");
                                if (stoptype.equals("fdj")) {
                                    bjWtId.add("1");
                                    strmap.put("stopTypeId", "发电机");
                                } else if (stoptype.equals("clx")) {
                                    bjWtId.add("2");
                                    strmap.put("stopTypeId", "齿轮箱");
                                } else if (stoptype.equals("zz")) {
                                    bjWtId.add("3");
                                    strmap.put("stopTypeId", "主控");
                                } else if (stoptype.equals("bj")) {
                                    bjWtId.add("4");
                                    strmap.put("stopTypeId", "变桨");
                                } else if (stoptype.equals("ph")) {
                                    bjWtId.add("5");
                                    strmap.put("stopTypeId", "偏航");
                                } else if (stoptype.equals("other")) {
                                    bjWtId.add("6");
                                    strmap.put("stopTypeId", "主系统");
                                } else if (stoptype.equals("kz")) {
                                    bjWtId.add("7");
                                    strmap.put("stopTypeId", "控制");
                                } else {
                                    bjWtId.add("8");
                                    strmap.put("stopTypeId", "其它");
                                }

                                gzls.add(strmap);
                            }

                        }
                        gzWtId.add(wtId);
                        csl++;
                    } else {
                        if (jk >= 60) {

                            ysl++;

                        } else if (jk < 60 && jk >= 30) {
                            lhWtId.add(wtId);
                            lsl++;
                        } else {
                            gzWtId.add(wtId);
                            csl++;

                        }

                    }

                }

                map.put(wpId + "_csl", csl);
                map.put(wpId + "_lsl", lsl);
                map.put(wpId + "_ysl", ysl);
                jkd = new BigDecimal(ysl + lsl).divide(new BigDecimal(wtidls.size()), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                map.put(wpId + "_jkd", jkd);
                map.put(wpId + "_gzWtId", gzWtId);
                map.put(wpId + "_lhWtId", lhWtId);
                map.put(wpId + "_bjWtId", bjWtId);

            }
        }
        map.put("_gzls", gzls);
        return map;
    }


    /**
     * 获得风场健康状态基础数据
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> findWpMap(String wpId) throws Exception {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        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();


        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, String>> gzls = new ArrayList<Map<String, String>>();
        if (StringUtils.notEmp(wpId) && InitialRunner.wpmap.containsKey(wpId)) {

            if (wpId.endsWith("FDC")) {
                int ysl = 0;// 优数量
                int lsl = 0;// 良数量
                int csl = 0;// 差数量
                double jkd = 0.0;
                double ycdl15minute = 0.0;
                double ycdl1hour = 0.0;
                double ycdl1day = 0.0;
                double ycdl1month = 0.0;
                double yczqlday = 0.0;
                double yczqlmonth = 0.0;

                // 获得实时风速测点编号

                WindPowerStationTestingPoint2 point = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_RFDL);
                String wppintId1 = point.getCode();
                PointData ssfspoint1 = realApiUtil.getRealData(wppintId1);
                double rfdl = StringUtils.round(ssfspoint1.getPointValueInDouble(), digit);

                WindPowerStationTestingPoint2 point2 = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_YFDL);
                String wppintId2 = point2.getCode();
                PointData ssfspoint2 = realApiUtil.getRealData(wppintId2);
                double yfdl = StringUtils.round(ssfspoint2.getPointValueInDouble(), digit);

                List<String[]> wtIdls = new ArrayList<String[]>();// 风机编号集合
                List<String> bjWtId = new ArrayList<String>();// 故障部件编号集合

                List<Weatherfh> wfhls = weatherfhService.findWeatherfh(beginDate, endDate, wpId);
                if (!wfhls.isEmpty()) {

                    Map<Double, Integer> speedmap = new HashMap<Double, Integer>();
                    for (Weatherfh wh : wfhls) {
                        cal.setTime(wh.getRecodedata());
                        int hourtemp = cal.get(Calendar.HOUR_OF_DAY);
                        if (hourtemp == hour) {
                            ycdl15minute = StringUtils.round(getPowerByWp(wh.getSpeed(), wpId, 1) * 0.25, 2);
                        }
                        if (hourtemp == hour + 1) {
                            ycdl1hour = getPowerByWp(wh.getSpeed(), wpId, 1);
                        }

                        if (speedmap.containsKey(wh.getSpeed())) {
                            int temp = speedmap.get(wh.getSpeed()) + 1;
                            speedmap.put(wh.getSpeed(), temp);
                        } else {
                            speedmap.put(wh.getSpeed(), 1);
                        }

                    }
                    ycdl1day = getPowerByWp(speedmap, wpId, 1);

                }
                cal = Calendar.getInstance();
                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);

                beginDate = cal.getTime();
                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                cal.add(Calendar.DAY_OF_MONTH, 1);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                endDate = cal.getTime();

                List<Weatherfd> wfdls = weatherfdService.findWeatherfd(beginDate, endDate, wpId);

                if (!wfdls.isEmpty()) {
                    Map<Double, Integer> speedmap = new HashMap<Double, Integer>();

                    for (Weatherfd wd : wfdls) {
                        if (speedmap.containsKey(wd.getSpeed1())) {
                            int temp = speedmap.get(wd.getSpeed1()) + 1;
                            speedmap.put(wd.getSpeed1(), temp);
                        } else {
                            speedmap.put(wd.getSpeed1(), 1);
                        }
                        if (speedmap.containsKey(wd.getSpeed2())) {
                            int temp = speedmap.get(wd.getSpeed2()) + 1;
                            speedmap.put(wd.getSpeed2(), temp);
                        } else {
                            speedmap.put(wd.getSpeed2(), 1);
                        }

                    }

                    ycdl1month = getPowerByWp(speedmap, wpId, 24);

                }

                if (ycdl1day != 0.0) {
                    yczqlday = new BigDecimal(rfdl).divide(new BigDecimal(ycdl1day), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                }
                if (ycdl1month != 0.0) {
                    yczqlmonth = new BigDecimal(yfdl).divide(new BigDecimal(ycdl1month), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                }

                List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wpId);
                List<String> wtidls = new ArrayList<String>();
                List<String> codejkls = new ArrayList<String>();
                List<String> codeztls = new ArrayList<String>();


                for (Windturbine wt : wtls) {

                    wtidls.add(wt.getId());
                    codejkls.add(Constant.FJJKZZ);
                    codeztls.add(Constant.FJZT);

                }

                // cal = Calendar.getInstance();
                // // cal.add(Calendar.MINUTE, -1);
                // int recodeDate = new
                // BigDecimal(cal.getTime().getTime()).divide(new
                // BigDecimal(1000), 0, RoundingMode.HALF_EVEN).intValue();

                String[] jkpoints = new String[wtidls.size()];
                String[] ztpoints = new String[wtidls.size()];

                // 获得训练参数对象
                for (int x = 0; x < wtidls.size(); x++) {

                    WindTurbineTestingPointAi2 ai1 = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtidls.get(x), codejkls.get(x));
                    jkpoints[x] = ai1.getId();
                    WindTurbineTestingPointAi2 ai2 = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtidls.get(x), codeztls.get(x));
                    ztpoints[x] = ai2.getId();

                }
                // // 获得风机健康状态结果数组
                // DNAVal[] jkvalues = realApiUtil.getHistMatrix(jkpoints,
                // recodeDate);
                // // 获得风机实时状态结果数组
                // DNAVal[] ztvalues = realApiUtil.getHistMatrix(ztpoints,
                // recodeDate);

                // 获得风机健康状态结果数组
                DNAVal[] jkvalues = realApiUtil.getRealtimeTagValues(jkpoints);
                // 获得风机实时状态结果数组
                DNAVal[] ztvalues = realApiUtil.getRealtimeTagValues(ztpoints);


                Calendar cal2 = Calendar.getInstance();
                Date end = cal2.getTime();
                cal2.add(Calendar.DAY_OF_MONTH, -1);
                Date begin = cal2.getTime();
                for (int x = 0; x < wtidls.size(); x++) {
                    String wtId = wtidls.get(x);
                    double jk = jkvalues[x].DValue;
                    double zt = ztvalues[x].DValue;


                    if (zt == 2.0) {

                        List<ShutdowneventVo> svos = shutdowneventService.queryEvents(null, wtId, begin, end, null);

                        List<Map<String, String>> stoptypels = new ArrayList<Map<String, String>>();

                        if (!svos.isEmpty()) {
                            for (ShutdowneventVo vo : svos) {
                                Map<String, String> stoptypemap = new HashMap<String, String>();
                                stoptypemap.put("stopTypeId", vo.getStopTypeId());
                                stoptypemap.put("windTurbineId", vo.getWindTurbineId());
                                stoptypemap.put("stopTime", DateUtils.formatDateTime(vo.getStopTime()));

                                if (StringUtils.notEmp(vo.getWarningId()) && InitialRunner.warningmap.containsKey(vo.getWarningId())) {
                                    stoptypemap.put("warningId", InitialRunner.warningmap.get(vo.getWarningId()).getChinesetext());
                                } else {
                                    stoptypemap.put("warningId", " ");
                                }
                                stoptypels.add(stoptypemap);
                            }
                        }
                        if (StringUtils.notEmp(stoptypels) && !stoptypels.isEmpty()) {
                            if (StringUtils.notEmp(stoptypels.get(0))) {
                                Map<String, String> strmap = stoptypels.get(0);

                                String stoptype = strmap.get("stopTypeId");
                                if (stoptype == null) {
                                    continue;

                                }
                                if (stoptype.equals("fdj")) {
                                    bjWtId.add("1");
                                    strmap.put("stopTypeId", "发电机");
                                } else if (stoptype.equals("clx")) {
                                    bjWtId.add("2");
                                    strmap.put("stopTypeId", "齿轮箱");
                                } else if (stoptype.equals("zz")) {
                                    bjWtId.add("3");
                                    strmap.put("stopTypeId", "主控");
                                } else if (stoptype.equals("bj")) {
                                    bjWtId.add("4");
                                    strmap.put("stopTypeId", "变桨");
                                } else if (stoptype.equals("ph")) {
                                    bjWtId.add("5");
                                    strmap.put("stopTypeId", "偏航");
                                } else if (stoptype.equals("other")) {
                                    bjWtId.add("6");
                                    strmap.put("stopTypeId", "主系统");
                                } else if (stoptype.equals("kz")) {
                                    bjWtId.add("7");
                                    strmap.put("stopTypeId", "控制");
                                } else {
                                    bjWtId.add("8");
                                    strmap.put("stopTypeId", "其它");
                                }

                                gzls.add(strmap);
                            }

                        }

                        String[] str = new String[2];
                        str[0] = wtId;
                        str[1] = "3";
                        wtIdls.add(str);
                        csl++;
                    } else {
                        String[] str = new String[2];
                        if (jk >= 60) {
                            str[0] = wtId;
                            str[1] = "1";
                            wtIdls.add(str);
                            ysl++;
                        } else if (jk < 60 && jk >= 30) {
                            str[0] = wtId;
                            str[1] = "2";
                            wtIdls.add(str);
                            lsl++;
                        } else {
                            str[0] = wtId;
                            str[1] = "3";
                            wtIdls.add(str);
                            csl++;
                        }

                    }

                }
                jkd = new BigDecimal(ysl + lsl).divide(new BigDecimal(wtidls.size()), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                map.put(wpId + "_jkd", jkd);
                map.put(wpId + "_csl", csl);
                map.put(wpId + "_lsl", lsl);
                map.put(wpId + "_ysl", ysl);
                map.put(wpId + "_ycdl15minute", ycdl15minute);
                map.put(wpId + "_ycdl1hour", ycdl1hour);
                map.put(wpId + "_ycdl1day", ycdl1day);
                map.put(wpId + "_ycdl1month", ycdl1month);
                map.put(wpId + "_rfdl", rfdl);
                map.put(wpId + "_yfdl", yfdl);
                map.put(wpId + "_yczqlday", yczqlday);
                map.put(wpId + "_yczqlmonth", yczqlmonth);
                map.put(wpId + "_wtIdls", wtIdls);

            }
        }
        map.put("_gzls", gzls);
        return map;
    }


    private double getPowerByWp(double speed, String wpId, double step) {
        double power = 0.0;

        if (InitialRunner.wp_wtmap.containsKey(wpId)) {
            List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wpId);
            for (Windturbine wt : wtls) {
                Map<Double, Windturbinepowercurvefitting> map = InitialRunner.wpcmap.get(wt.getId());
                if (map.containsKey(speed)) {
                    Windturbinepowercurvefitting wc = map.get(speed);

                    double actualpower = wc.getActualpower();
                    // / <summary>
                    // /电量分析主要针对省公司和各风场的每月实际发电量与计划的对比分析，可以查看省公司及各风场每月计划的完成情况和总体的计划完成情况，可以同比和环比进行比较；
                    // /对省公司和各风场的实际发电量与理论发电量的对比分析，利用标准功率曲线中的风速，理论功率，和固定时间段的风频进行统计，设计统计分析公式为：
                    // /理论发电量=（T*f1/f）*P1+（T*f2/f）*P2+…………（T*fn/f）*Pn
                    // /T是选取的时间段；
                    // /f是所有风频的和；
                    // /f1、f2、fn是某风速对应的风频；
                    // /P1、Pn是某风速对应理论曲线上的功率。
                    // /必要性：如果理论电量能够实现计算的准确，调峰电量就可以用理论电量来减去实际电量得出。
                    // /b、可选一段时间内的实际功率和理论功率能够形成曲线形式展示对比,理论功率根据实际风速利用理论功率曲线得出。
                    // /c、风频数据取值时间段需要统一。
                    // / </summary>
                    // / <param name="powerDataList">功率集合</param>
                    // / <param name="hours">时长小时数</param>
                    // / <returns>理论发电量</returns>
                    power = power + new BigDecimal(step).multiply(new BigDecimal(actualpower)).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                }
            }
        }
        return StringUtils.round(power, 2);
    }

    private double getPowerByWp(Map<Double, Integer> speedmap, String wpId, double step) {
        double power = 0.0;

        if (InitialRunner.wp_wtmap.containsKey(wpId)) {
            List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wpId);
            for (Windturbine wt : wtls) {
                Map<Double, Windturbinepowercurvefitting> map = InitialRunner.wpcmap.get(wt.getId());

                Set<Map.Entry<Double, Integer>> set = speedmap.entrySet();
                for (Iterator<Map.Entry<Double, Integer>> it = set.iterator(); it.hasNext(); ) {
                    Map.Entry<Double, Integer> entry = (Map.Entry<Double, Integer>) it.next();
                    // System.out.println(entry.getKey() + "--->" +
                    // entry.getValue());

                    double speed = entry.getKey() > 24.99 ? entry.getKey() : 24.99;
                    if (null != map && map.containsKey(speed)) {
                        Windturbinepowercurvefitting wc = map.get(speed);

                        double actualpower = wc.getActualpower();
                        // / <summary>
                        // /电量分析主要针对省公司和各风场的每月实际发电量与计划的对比分析，可以查看省公司及各风场每月计划的完成情况和总体的计划完成情况，可以同比和环比进行比较；
                        // /对省公司和各风场的实际发电量与理论发电量的对比分析，利用标准功率曲线中的风速，理论功率，和固定时间段的风频进行统计，设计统计分析公式为：
                        // /理论发电量=（T*f1/f）*P1+（T*f2/f）*P2+…………（T*fn/f）*Pn
                        // /T是选取的时间段；
                        // /f是所有风频的和；
                        // /f1、f2、fn是某风速对应的风频；
                        // /P1、Pn是某风速对应理论曲线上的功率。
                        // /必要性：如果理论电量能够实现计算的准确，调峰电量就可以用理论电量来减去实际电量得出。
                        // /b、可选一段时间内的实际功率和理论功率能够形成曲线形式展示对比,理论功率根据实际风速利用理论功率曲线得出。
                        // /c、风频数据取值时间段需要统一。
                        // / </summary>
                        // / <param name="powerDataList">功率集合</param>
                        // / <param name="hours">时长小时数</param>
                        // / <returns>理论发电量</returns>

                        power = power + new BigDecimal(step).multiply(new BigDecimal(entry.getValue())).divide(new BigDecimal(speedmap.size()), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(actualpower)).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                    }
                }

            }
        }
        return StringUtils.round(power, 2);
    }


    public Map<String, Object> findHealthMatrixMap() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();


        //        Map<String, List<MatrixVo>> fjmap = new HashMap<>();
        List<List<MatrixVo>> fjvos = new ArrayList<>();
        Map<String, Map<String, Object>> fczbmap = new HashMap<>();
        for (Windpowerstation wp : InitialRunner.wpls) {

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


            jczbmap.put("name", wp.getName());
            /*************************************************风场指标统计*************************************************************/


            List<String> jczbls = new ArrayList<>();
            //良好状态数量
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wp.getId(), Constant.FJLHSL).getCode());
            //正常状态数量
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wp.getId(), Constant.FJZCSL).getCode());
            //注意状态数量
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wp.getId(), Constant.FJZYSL).getCode());
            //严重状态数量
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wp.getId(), Constant.FJYZSL).getCode());



            List<PointData> jczblist = realApiUtil.getRealData(jczbls);

            if (!jczblist.isEmpty() && jczblist.size() == jczbls.size()) {

                //良好状态数量
                jczbmap.put("lhsl", MathUtil.twoBit(jczblist.get(0).getPointValueInDouble()));
                //正常状态数量
                jczbmap.put("zcsl", MathUtil.twoBit(jczblist.get(1).getPointValueInDouble()));
                //注意状态数量
                jczbmap.put("zysl", MathUtil.twoBit(jczblist.get(2).getPointValueInDouble()));
                //严重状态数量
                jczbmap.put("yzsl", MathUtil.twoBit(jczblist.get(3).getPointValueInDouble()));

            } else {
                //良好状态数量
                jczbmap.put("lhsl", 0.0);
                //正常状态数量
                jczbmap.put("zcsl", 0.0);
                //注意状态数量
                jczbmap.put("zysl", 0.0);
                //严重状态数量
                jczbmap.put("yzsl", 0.0);
            }

            fczbmap.put(wp.getId(), jczbmap);
/*************************************************风场指标统计*************************************************************/


/*************************************************风机指标统计*************************************************************/
            List<String> fjzbls = new ArrayList<>();

            List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wp.getId());

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

            for (Windturbine wt : wtls) {
                //风机状态
                fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.FJJKZT).getId());

            }

            List<PointData> fjzblist = realApiUtil.getRealData(fjzbls);

            if (!fjzblist.isEmpty() && fjzblist.size() == fjzbls.size()) {

                for (int i = 0; i < wtls.size(); i++) {
                    Windturbine wt = wtls.get(i);

                    MatrixVo vo = new MatrixVo();
                    vo.setWtId(wt.getId());
                    if (StringUtils.notEmp(wt.getStatus())) {
                        String num = wt.getStatus().substring(wt.getStatus().length() - 3);
                        vo.setWtnum(num);

                    }

                    double fjzt = MathUtil.twoBit(fjzblist.get(i).getPointValueInDouble());

                    vo.setFjzt(fjzt);
                    vo.setWtCode(wt.getCode());
                    vo.setWpId(wt.getWindpowerstationid());
                    vos.add(vo);

                }

            }
            fjvos.add(vos);

/*************************************************风机指标统计*************************************************************/

        }
        //风场待机台数
        map.put("fczbmap", fczbmap);
        //风机指标对象
        map.put("fjmap", fjvos);




        return map;
    }
}
