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.*;
import com.gyee.frame.service.*;
import com.gyee.frame.service.weather.WeatherDay5Service;
import com.gyee.frame.service.weather.WeatherService;
import com.gyee.frame.util.*;
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 WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;
    @Resource
    private WeatherService weatherService;
    @Resource
    private WeatherDay5Service weatherDay5Service;
    @Resource
    private PowerToPowerService powerToPowerService;
    @Resource
    private ProjectPlanService projectPlanService;
    private final int digit = 2;
    private final double NUM = 3.6;
    private final double XZNUM = 1.25;
    private final double YCFDLXS = 1.08;

    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();


    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);

                WindTurbineTestingPointAi jkztpoint =windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(),Constant.FJJKZZ);
                jkpoints[x] = jkztpoint.getId();
                WindTurbineTestingPointAi 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++) {


                WindTurbineTestingPointAi 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, Object>> wpmapls = new ArrayList<Map<String, Object>>();
        List<Map<String, String>> stoptypels = new ArrayList<Map<String, String>>();
        // 获得天气预报每小时数据
        for (Windpowerstation wp : InitialRunner.wpls) {


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

            String wpId = wp.getId();

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

                double tqyb=1.0;// 天气预报
                double ycfs=0.0;// 预测风速
                double sjfs=0.0;// 实际风速
                List<SimpleVo> gzIdLs = new ArrayList<SimpleVo>();// 故障风机编号集合

                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);
//                        if(StringUtils.isNumeric(strtemp))
//                        {
//                            tqyb = Double.valueOf(strtemp);
//                        }else
//                        {
//                            temp = temp.substring(0, temp.length() - 2);
//                            strtemp = temp.substring(temp.lastIndexOf("-")+1);
//                            if(StringUtils.isNumeric(strtemp)) {
//                                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();
//                    wpmap.put("ycfs", ycfs);
//                    wpmap.put("tqyb", tqyb);
//
//                } else {
//                    wpmap.put("ycfs", 6.0);
//                    wpmap.put("tqyb", 1.0);
//                }

                Map<String,Object> weathermap=weatherService.getWeatherRealInfo(wp.getId());
                if(null!=weathermap && !weathermap.isEmpty())
                {
                    if(weathermap.containsKey("tqtp"))
                    {
                        wpmap.put("tqyb", Double.valueOf(String.valueOf(weathermap.get("tqtp"))));
                    }
                    if(weathermap.containsKey("fs"))
                    {
                        wpmap.put("ycfs", weathermap.get("fs"));
                    }
                }
                // 获得实时风速测点编号

                WindPowerStationTestingPoint point = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.TPOINT_WP_SSFS);
                String wppintId = point.getCode();

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

                wpmap.put("sjfs", sjfs);
                wpmap.put("name",wp.getName());
                wpmap.put("wpId",wp.getId());
                List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wp.getId());
                wpmap.put("jrts",wtls.size());
                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();


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


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

                    WindTurbineTestingPointAi ai1 = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtidls.get(x), codejkls.get(x));
                    jkpoints[x] = ai1.getId();
                    WindTurbineTestingPointAi 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);



                        if (!svos.isEmpty()) {
                            for (ShutdowneventVo vo : svos) {
                                Map<String, String> stoptypemap = new HashMap<String, String>();
                                String stoptype=vo.getStopTypeId();

                                if(StringUtils.notEmp(stoptype))
                                {
                                    if (stoptype.equals("fdj")) {
                                        stoptypemap.put("stopTypeId", "发电机");
                                    } else if (stoptype.equals("clx")) {
                                        stoptypemap.put("stopTypeId", "齿轮箱");
                                    } else if (stoptype.equals("zz")) {
                                        stoptypemap.put("stopTypeId", "主控");
                                    } else if (stoptype.equals("bj")) {
                                        stoptypemap.put("stopTypeId", "变桨");
                                    } else if (stoptype.equals("ph")) {
                                        stoptypemap.put("stopTypeId", "偏航");
                                    } else if (stoptype.equals("other")) {
                                        stoptypemap.put("stopTypeId", "主系统");
                                    } else if (stoptype.equals("kz")) {
                                        stoptypemap.put("stopTypeId", "控制");
                                    } else {
                                        stoptypemap.put("stopTypeId", "其它");
                                    }
                                }



                                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);
                            }
                        }

                        SimpleVo vo=new SimpleVo();
                        vo.setId(wtId);
                        if(InitialRunner.wtmap.containsKey(wtId))
                        {
                            Windturbine wt=InitialRunner.wtmap.get(wtId);
                            if (StringUtils.notEmp(wt.getStatus())) {
                                String num = wt.getStatus().substring(wt.getStatus().length() - 3);
                                vo.setName(num);

                            }
                        }

                        gzIdLs.add(vo);
                        csl++;
                    } else {
                        if (jk >= 60) {

                            ysl++;

                        } else if (jk < 60 && jk >= 30) {
                            lsl++;
                        } else {

                            SimpleVo vo=new SimpleVo();
                            vo.setId(wtId);
                            if(InitialRunner.wtmap.containsKey(wtId))
                            {
                                Windturbine wt=InitialRunner.wtmap.get(wtId);
                                if (StringUtils.notEmp(wt.getStatus())) {
                                    String num = wt.getStatus().substring(wt.getStatus().length() - 3);
                                    vo.setName(num);

                                }
                            }
                            gzIdLs.add(vo);
                            csl++;

                        }

                    }

                }

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

                wpmap.put("jkd", jkd);
                wpmap.put("gzId", gzIdLs);
                wpmapls.add(wpmap);
            }

        }
        map.put("wpmap",wpmapls);

        Collections.sort(stoptypels, new Comparator<Map<String, String>>() {
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                String stopTime1 = o1.get("stopTime");
                String stopTime2 = o2.get("stopTime");
                return stopTime2.compareTo(stopTime1);
            }
        });
        map.put("gzls", stoptypels);
        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;// 差数量

                int sl1 = 0;
                int sl2= 0;
                int sl3 = 0;
                int sl4 = 0;
                int sl5 = 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;

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

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

                WindPowerStationTestingPoint point2 = windPowerstationTestingPoint2Service.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<Integer> yhls = new ArrayList<Integer>();// 隐患集合
                Map<String,Object> weathermap= weatherDay5Service.getWeatherRealDay5Info(wpId);

                if(!weathermap.isEmpty() && weathermap.containsKey("ls"))
                {
                    java.util.List<Map<String,Object>> weatherls=(java.util.List<Map<String,Object>>)weathermap.get("ls");
                    if(!weatherls.isEmpty() && weatherls.size()>8 )
                    {
                        Map<Double, Integer> speedmap = new HashMap<Double, Integer>();
                    for (int i=0;i<8;i++) {

                        Map<String,Object> wh=weatherls.get(i);

                        double fs=(double)wh.get("fs");
                        ycdl15minute = StringUtils.round(powerToPowerService.getPowerByWp(fs, wpId, 1) * 0.75, 2);
                        ycdl1hour = powerToPowerService.getPowerByWp(fs, wpId, 1)*3;

                        if (speedmap.containsKey(fs)) {
                            int temp = speedmap.get(fs) + 180;
                            speedmap.put(fs, temp);
                        } else {
                            speedmap.put(fs, 180);
                        }

                    }
                    ycdl1day = powerToPowerService.getPowerByWp(speedmap, wpId, 1);
                    }
                }
//                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.setTime(new Date());
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH)+1;
                List<ProjectPlan> pjls = projectPlanService.getProjectPlanList(wpId,String.valueOf(year),String.valueOf(month));

                double yfdljh=0.0;
                if(!pjls.isEmpty())
                {
                    for (ProjectPlan plan : pjls) {

                        double fdl = null != plan.getGeneratingcapacity() ? Double.valueOf(plan.getGeneratingcapacity()) : 0.0;

                        yfdljh = yfdljh + fdl;
                    };
                }

                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 = powerToPowerService.getPowerByWp(speedmap, wpId, 24);
                    //预测电量临时算法=(月发电量+日预测发电量*(日历天数-当前日数))*预测发电量系数
                    cal = Calendar.getInstance();
                    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                    int daynum = cal.get(Calendar.DAY_OF_MONTH);
                    cal = Calendar.getInstance();
                    ycdl1month = StringUtils.round((yfdl + ycdl1day*(daynum-cal.get(Calendar.DAY_OF_MONTH)))*YCFDLXS, 2);


                    if(yfdljh!=0)
                    {
                        //与月计划发电量结合判断月预测发电量
                        if(ycdl1month<yfdljh)
                        {
                            ycdl1month=yfdljh*1.05;

                        }else if(ycdl1month>(yfdljh*1.2))
                        {
                            ycdl1month=yfdljh*1.2;
                        }
                    }

                }

                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);

                }

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

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

                    WindTurbineTestingPointAi ai1 = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtidls.get(x), codejkls.get(x));
                    jkpoints[x] = ai1.getId();
                    WindTurbineTestingPointAi 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);



                        if (!svos.isEmpty()) {
                            for (ShutdowneventVo vo : svos) {
                                Map<String, String> stoptypemap = new HashMap<String, String>();
                                stoptypemap.put("stopTypeId", vo.getStopTypeId());
                                String stoptype=vo.getStopTypeId();

                                if(StringUtils.notEmp(stoptype))
                                {
                                    if (stoptype.equals("fdj")) {
                                        stoptypemap.put("stopTypeId", "发电机");
                                    } else if (stoptype.equals("clx")) {
                                        stoptypemap.put("stopTypeId", "齿轮箱");
                                    } else if (stoptype.equals("zz")) {
                                        stoptypemap.put("stopTypeId", "主控");
                                    } else if (stoptype.equals("bj")) {
                                        stoptypemap.put("stopTypeId", "变桨");
                                    } else if (stoptype.equals("ph")) {
                                        stoptypemap.put("stopTypeId", "偏航");
                                    } else if (stoptype.equals("other")) {
                                        stoptypemap.put("stopTypeId", "主系统");
                                    } else if (stoptype.equals("kz")) {
                                        stoptypemap.put("stopTypeId", "控制");
                                    } else {
                                        stoptypemap.put("stopTypeId", "其它");
                                    }
                                }
                                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", " ");
                                }
                                gzls.add(stoptypemap);
                            }
                        }


                        String[] str = new String[3];
                        str[0] = wtId;
                        str[1] = "3";
                        if(InitialRunner.wtmap.containsKey(wtId))
                        {
                            Windturbine wt=InitialRunner.wtmap.get(wtId);
                            if (StringUtils.notEmp(wt.getStatus())) {
                                String num = wt.getStatus().substring(wt.getStatus().length() - 3);
                                str[2] = num;

                            }
                        }
                        wtIdls.add(str);
                        csl++;
                    } else {
                        String[] str = new String[3];
                        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++;
                        }
                        if(InitialRunner.wtmap.containsKey(wtId))
                        {
                            Windturbine wt=InitialRunner.wtmap.get(wtId);
                            if (StringUtils.notEmp(wt.getStatus())) {
                                String num = wt.getStatus().substring(wt.getStatus().length() - 3);
                                str[2] = num;

                            }
                        }
                        if (jk >= 80) {

                            sl1++;
                        } else if (jk < 80 && jk >= 60) {

                            sl2++;
                        }else if (jk < 60 && jk >= 40) {

                            sl3++;
                        }else if (jk < 40 && jk >= 30) {

                            sl4++;
                        } else {

                            sl5++;
                        }



                    }

                }

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

                yhls.add(sl1);
                yhls.add(sl2);
                yhls.add(sl3);
                yhls.add(sl4);
                yhls.add(sl5);
                map.put("yhls", yhls);
                map.put("jkd", jkd);
                map.put("csl", csl);
                map.put("lsl", lsl);
                map.put("ysl", ysl);
                map.put("ycdl15minute", ycdl15minute);
                map.put("ycdl1hour", ycdl1hour);
                map.put("ycdl1day", ycdl1day);
                map.put("ycdl1month", ycdl1month);
                map.put("rfdl", rfdl);
                map.put("yfdl", yfdl);
                map.put("yczqlday", yczqlday);
                map.put("yczqlmonth", yczqlmonth);
                map.put("wtIdls", wtIdls);
                Collections.sort(gzls, new Comparator<Map<String, String>>() {
                    public int compare(Map<String, String> o1, Map<String, String> o2) {
                        String stopTime1 = o1.get("stopTime") ;//name1是从你list里面拿出来的一个
                        String stopTime2 = o2.get("stopTime") ; //name1是从你list里面拿出来的第二个name
                        return stopTime2.compareTo(stopTime1);
                    }
                });
                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 LinkedHashMap<>();
        for (Windpowerstation wp : InitialRunner.wpls) {

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


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


            List<String> jczbls = new ArrayList<>();
            //良好状态数量
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.FJLHSL).getCode());
            //正常状态数量
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.FJZCSL).getCode());
            //注意状态数量
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.FJZYSL).getCode());
            //严重状态数量
            jczbls.add(windPowerstationTestingPoint2Service.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;
    }
}
