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.DNAVal;
import com.gyee.frame.model.custom.RecommenmainVo;
import com.gyee.frame.model.custom.ShutdowneventVo;
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.util.*;

@Service
public class SandtableService {


    @Resource
    private WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;
    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;
    @Resource
    private WeatherService weatherService;
    @Resource
    private WindpowerinfodayService windpowerinfodayService;
    @Resource
    private Windturbineinfodaytop2Service windturbineinfodaytop2Service;
    @Resource
    private RecommenmainService recommenmainService;
    @Resource
    private ShutdowneventService shutdowneventService;
    @Resource
    private Alertrule2ervice alertrule2ervice;
    @Resource
    private WeatherDay5Service weatherDay5Service;

    private final String QS = "0";
    private final String FD = "-1";
    private final String GF = "-2";
    private final String DEFAULT_FDC = "MHS_FDC";
    private final double YCFDLXS = 1.08;
    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();


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

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


//        map.put(QS, findBasicDataInfo(QS));
//        map.put(FD, findBasicDataInfo(FD));
//        map.put(GF, findBasicDataInfo(GF));
//
//        map.put("SBQ_FDC", findBasicDataInfo("SBQ_FDC"));
//        map.put("SBQ02_GC", findBasicDataInfo("SBQ02_GC"));
//        map.put("SBQ05_XL", findBasicDataInfo("SBQ05_XL"));
        return map;

    }

    public Map<String, Object> findSandtableInfo(String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.notEmp(id)) {


/*************************************************天气指标*************************************************************/

            Map<String, Object> tqmap1 = weatherService.getWeatherRealInfo(id);
            map.put("tqmap1", tqmap1);
            Map<String, Object> tqmap5 = weatherDay5Service.getWeatherRealDay5Info(id);
            map.put("tqmap5", tqmap5);
/*************************************************天气指标*************************************************************/
/*************************************************等级评估*************************************************************/

//            Calendar cal = Calendar.getInstance();
//            int year=cal.get(Calendar.YEAR);
//            int month=cal.get(Calendar.MONTH)+1;
//            if(id.equals(QS))
//            {
//                List<Windturbineinfodaytop2> topls=windturbineinfodaytop2Service.getWindturbineinfodaytop2List(null,year,month);
//                map.put("topls", topls);
//            }else
//            {
//                List<Windturbineinfodaytop2> topls=windturbineinfodaytop2Service.getWindturbineinfodaytop2List(id,year,month);
//                map.put("topls", topls);
//            }

            if (id.equals(QS)) {
                List<ShutdowneventVo> sels = shutdowneventService.queryWpStopTop5(null);
                map.put("sels", sels);
            } else {
                List<ShutdowneventVo> sels = shutdowneventService.queryWpStopTop5(id);
                map.put("sels", sels);
            }

/*************************************************等级评估*************************************************************/

/*************************************************健康管理*************************************************************/

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH, 1);
            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.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date endDate = cal.getTime();
            List<RecommenmainVo> list = new ArrayList<>();
            if (id.equals(QS)) {
                list = recommenmainService.getRecommenmainTopList(null, beginDate, endDate);
            } else {
                list = recommenmainService.getRecommenmainTopList(id, beginDate, endDate);
            }


            List<RecommenmainVo> rmls = new ArrayList<>();
            if (!list.isEmpty()) {
                for (RecommenmainVo vo : list) {
                    RecommenmainVo rmvo = new RecommenmainVo();
                    Recommenmain rm = recommenmainService.getRecommenmain(vo.getWtId(), beginDate, endDate);
                    rmvo.setWtId(rm.getWtid());
                    rmvo.setRecodedate(rm.getCreatedate());
                    rmvo.setReason(rm.getReason());
                    rmvo.setSpeed(rm.getSpeed());

                    Random r = new Random(100);


                    List<Windturbineparts> windturbinepartsls=InitialRunner.windturbinepartsls;

                    int ran = r.nextInt(windturbinepartsls.size());

                    Windturbineparts wps=windturbinepartsls.get(ran);
                    rmvo.setTypename(wps.getName());
                    String rule=wps.getId();

                    if (rule.equals("CDL") || rule.equals("FDJ") || rule.equals("BJXT")) {

                        rmvo.setOperation("停机");
                    } else {
                        rmvo.setOperation("可运行");

                    }


                    rmls.add(rmvo);
                }
            }
            map.put("rmls", rmls);
/*************************************************健康管理*************************************************************/


/***************************************************************利用小时 设备可利用率*************************************************************/


            cal = Calendar.getInstance();
            Map<String, Double> gxkmap = new HashMap<>();

            double dxkyss = 0.0;
            double sbklyl = 0.0;

            double tb_dxkyss = 0.0;
            double tb_sbklyl = 0.0;

            double hb_dxkyss = 0.0;
            double hb_sbklyl = 0.0;

            double bg_dxkyss = 0.0;
            double bg_sbklyl = 0.0;

            List<Windpowerinfoday> wpdls = windpowerinfodayService.getWindpowerinfodayList(id, DateUtils.truncate(cal.getTime()));
            if (!wpdls.isEmpty()) {
                Windpowerinfoday wd = wpdls.get(0);

                dxkyss = wd.getMonthdxkyxs();
                sbklyl = wd.getMonthsbklyl();

                gxkmap.put("ydxkyss", dxkyss);
                gxkmap.put("ysbklyl", sbklyl);


            } else {
                gxkmap.put("ydxkyss", 0.0);
                gxkmap.put("ysbklyl", 0.0);

            }
            //上月数据
            cal.add(Calendar.MONTH, -1);
            wpdls = windpowerinfodayService.getWindpowerinfodayList(id, DateUtils.truncate(cal.getTime()));
            if (!wpdls.isEmpty()) {
                Windpowerinfoday wd = wpdls.get(0);


                if (null != wd.getMonthdxkyxs()) {
                    hb_dxkyss = new BigDecimal(dxkyss - wd.getMonthdxkyxs()).divide(new BigDecimal(wd.getMonthdxkyxs()), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                }

                if (null != wd.getMonthsbklyl()) {
                    hb_sbklyl = MathUtil.twoBit(sbklyl - wd.getMonthsbklyl());
                }
                gxkmap.put("hb_dxkyss", hb_dxkyss);
                gxkmap.put("hb_sbklyl", hb_sbklyl);


            } else {
                gxkmap.put("hb_dxkyss", 0.0);
                gxkmap.put("hb_sbklyl", 0.0);

            }
            //去年数据
            cal.setTime(new Date());
            cal.add(Calendar.YEAR, -1);
            wpdls = windpowerinfodayService.getWindpowerinfodayList(id, DateUtils.truncate(cal.getTime()));
            if (!wpdls.isEmpty()) {
                Windpowerinfoday wd = wpdls.get(0);


                if (null != wd.getMonthdxkyxs()) {
                    tb_dxkyss = new BigDecimal(dxkyss - wd.getMonthdxkyxs()).divide(new BigDecimal(wd.getMonthdxkyxs()), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                }

                if (null != wd.getMonthsbklyl()) {
                    tb_sbklyl = MathUtil.twoBit(sbklyl - wd.getMonthsbklyl());
                }

                gxkmap.put("tb_dxkyss", tb_dxkyss);
                gxkmap.put("tb_sbklyl", tb_sbklyl);


            } else {
                gxkmap.put("tb_dxkyss", 0.0);
                gxkmap.put("tb_sbklyl", 0.0);

            }

            //标杆年份
            cal.setTime(new Date());
            cal.set(Calendar.YEAR, 2016);
            wpdls = windpowerinfodayService.getWindpowerinfodayList(id, DateUtils.truncate(cal.getTime()));
            if (!wpdls.isEmpty()) {
                Windpowerinfoday wd = wpdls.get(0);


                if (null != wd.getMonthdxkyxs()) {
                    bg_dxkyss = new BigDecimal(dxkyss - wd.getMonthdxkyxs()).divide(new BigDecimal(wd.getMonthdxkyxs()), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                }

                if (null != wd.getMonthsbklyl()) {
                    bg_sbklyl = MathUtil.twoBit(sbklyl - wd.getMonthsbklyl());
                }

                gxkmap.put("bg_dxkyss", bg_dxkyss);
                gxkmap.put("bg_sbklyl", bg_sbklyl);


            } else {
                gxkmap.put("bg_dxkyss", 0.0);
                gxkmap.put("bg_sbklyl", 0.0);

            }
/***************************************************************利用小时 设备可利用率*************************************************************/
/***************************************************************MTTR、MTBF、MTTF***************************************************************/
            List<String> wtlist = new ArrayList<>();

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

                List<Windpowerstation> wplist = new ArrayList<>();

                for (Windpowerstation wp : InitialRunner.wpallls) {
                    if (wp.getId().endsWith("FDC")) {
                        if (InitialRunner.wp_wtmap.containsKey(wp.getId())) {
                            List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wp.getId());
                            for (Windturbine wt : wtls) {
                                wtlist.add(wt.getId());
                            }
                        }
                    }
                }
            } else {
                if (InitialRunner.wp_wtmap.containsKey(id)) {
                    List<Windturbine> wtls = InitialRunner.wp_wtmap.get(id);
                    for (Windturbine wt : wtls) {
                        wtlist.add(wt.getId());
                    }
                }
            }

            if (!wtlist.isEmpty()) {
                cal.setTime(new Date());
                Map<String, Object> mttrmaps = shutdowneventService.queryMttrAndMtbf(wtlist, DateUtils.truncate(cal.getTime()));
                double mttr = 0.0;
                double mttf = 0.0;
                double mtbf = 0.0;
                if (mttrmaps.containsKey("mttr")) {
                    mttr = (double) mttrmaps.get("mttr");
                }
                if (mttrmaps.containsKey("mttf")) {
                    mttf = (double) mttrmaps.get("mttf");
                }
                if (mttrmaps.containsKey("mtbf")) {
                    mtbf = (double) mttrmaps.get("mtbf");
                }

                gxkmap.put("mttr", mttr);
                gxkmap.put("mttf", mttf);
                gxkmap.put("mtbf", mtbf);
                //上月数据
                cal.add(Calendar.MONTH, -1);
                mttrmaps = shutdowneventService.queryMttrAndMtbf(wtlist, DateUtils.truncate(cal.getTime()));
                double hb_mttr = 0.0;
                double hb_mttf = 0.0;
                double hb_mtbf = 0.0;
                if (mttrmaps.containsKey("mttr")) {
                    double  temp = (double) mttrmaps.get("mttr");
                    if(temp!=0)
                    {
                        hb_mttr = new BigDecimal(mttr - temp).divide(new BigDecimal(temp), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                    }else
                    {
                        hb_mttr = 0.0;

                    }

                }
                if (mttrmaps.containsKey("mttf")) {

                    double  temp = (double) mttrmaps.get("mttf");
                    if(temp!=0)
                    {
                        hb_mttf = new BigDecimal(mttf - temp).divide(new BigDecimal(temp), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                    }else
                    {
                        hb_mttf = 0.0;

                    }

                }
                if (mttrmaps.containsKey("mtbf")) {

                    double  temp = (double) mttrmaps.get("mtbf");
                    if(temp!=0)
                    {
                        hb_mtbf = new BigDecimal(mtbf - temp).divide(new BigDecimal(temp), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();


                    }else
                    {
                        hb_mtbf = 0.0;

                    }

                }
                gxkmap.put("hb_mttr", hb_mttr);
                gxkmap.put("hb_mttf", hb_mttf);
                gxkmap.put("hb_mtbf", hb_mtbf);
                double tb_mttr = 0.0;
                double tb_mttf = 0.0;
                double tb_mtbf = 0.0;
                cal.setTime(new Date());
                cal.add(Calendar.YEAR, -1);
                if (mttrmaps.containsKey("mttr")) {
                    tb_mttr = (double) mttrmaps.get("mttr");
                    tb_mttr = new BigDecimal(mttr - tb_mttr).divide(new BigDecimal(tb_mttr), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                }
                if (mttrmaps.containsKey("mttf")) {
                    tb_mttf = (double) mttrmaps.get("mttf");
                    tb_mttf = new BigDecimal(mttf - tb_mttf).divide(new BigDecimal(tb_mttf), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                }
                if (mttrmaps.containsKey("mtbf")) {
                    tb_mtbf = (double) mttrmaps.get("mtbf");
                    tb_mtbf = new BigDecimal(mtbf - tb_mtbf).divide(new BigDecimal(tb_mtbf), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                }
                gxkmap.put("tb_mttr", tb_mttr);
                gxkmap.put("tb_mttf", tb_mttf);
                gxkmap.put("tb_mtbf", tb_mtbf);
                //标杆年份
                cal.setTime(new Date());
                cal.set(Calendar.YEAR, 2016);
                double bg_mttr = 0.0;
                double bg_mttf = 0.0;
                double bg_mtbf = 0.0;
                if (mttrmaps.containsKey("mttr")) {
                    bg_mttr = (double) mttrmaps.get("mttr");
                    bg_mttr = new BigDecimal(mttr - bg_mttr).divide(new BigDecimal(bg_mttr), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                    if(bg_mttr<-100)
                    {
                        bg_mttr=-100;
                    }

                    if(bg_mttr>100)
                    {
                        bg_mttr=100;
                    }
                }
                if (mttrmaps.containsKey("mttf")) {
                    bg_mttf = (double) mttrmaps.get("mttf");
                    bg_mttf = new BigDecimal(mttf - bg_mttf).divide(new BigDecimal(bg_mttf), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                    if(bg_mttf<-100)
                    {
                        bg_mttf=-100;
                    }

                    if(bg_mttf>100)
                    {
                        bg_mttf=100;
                    }
                }
                if (mttrmaps.containsKey("mtbf")) {
                    bg_mtbf = (double) mttrmaps.get("mtbf");
                    bg_mtbf = new BigDecimal(mtbf - bg_mtbf).divide(new BigDecimal(bg_mtbf), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                    if(bg_mtbf<-100)
                    {
                        bg_mtbf=-100;
                    }

                    if(bg_mtbf>100)
                    {
                        bg_mtbf=100;
                    }
                }
                gxkmap.put("bg_mttr", bg_mttr);
                gxkmap.put("bg_mttf", bg_mttf);
                gxkmap.put("bg_mtbf", bg_mtbf);
            }
/***************************************************************MTTR、MTBF、MTTF***************************************************************/
            map.put("gxkmap", gxkmap);
        }
/*************************************************关系库指标**************************************************************/

        return map;
    }



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

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

        int qs_ysl = 0;
        int qs_lsl = 0;
        int qs_csl = 0;
        int qs_gzsl = 0;
        int fjsl = 0;

        for (Windpowerstation wp : InitialRunner.wpallls) {

            Map<String, Object> wpmap = new HashMap<>();
            List<String> wtidls = new ArrayList<>();
            List<String> codejkls = new ArrayList<>();
            List<String> codeztls = new ArrayList<>();
            if (wp.getId().endsWith("FDC")) {
                if (InitialRunner.wp_wtmap.containsKey(wp.getId())) {


                    List<Windturbine> wtls = InitialRunner.wp_wtmap.get(wp.getId());
                    for (Windturbine wt : wtls) {
                        wtidls.add(wt.getId());
                        codejkls.add(Constant.FJJKZZ);
                        codeztls.add(Constant.FJZT);

                        fjsl++;
                    }

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

                    int ysl = 0;
                    int lsl = 0;
                    int csl = 0;
                    int gzsl = 0;
                    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) {
                            gzsl++;
                        } else {
                            if (jk >= 80) {
                                ysl++;
                            } else if (jk < 80 && jk >= 50) {
                                lsl++;
                            } else {
                                csl++;

                            }
                        }
                    }

                    qs_ysl=qs_ysl+ysl;
                    qs_lsl =qs_lsl+lsl;
                    qs_csl =qs_csl+csl;
                     qs_gzsl =qs_gzsl+gzsl;

                    wpmap.put("csl", csl);
                    wpmap.put("lsl", lsl);
                    wpmap.put("ysl", ysl);
                    wpmap.put("gzsl", gzsl);
                    double gzslz = new BigDecimal(2 * gzsl).divide(new BigDecimal(wtidls.size()), 2, RoundingMode.HALF_EVEN).doubleValue();
                    double cslz = new BigDecimal(csl).divide(new BigDecimal(wtidls.size()), 2, RoundingMode.HALF_EVEN).doubleValue();
                    double lslz = new BigDecimal(0.5 * lsl).divide(new BigDecimal(wtidls.size()), 2, RoundingMode.HALF_EVEN).doubleValue();

                    wpmap.put("jkd", MathUtil.twoBit(100 - gzslz - cslz - lslz));
                    map.put(wp.getId(), wpmap);
                }
            } else {
                wpmap.put("csl", 0);
                wpmap.put("lsl", 0);
                wpmap.put("ysl", wtidls.size());
                wpmap.put("gzsl", 0);

                wpmap.put("jkd", 100.0);
                map.put(wp.getId(), wpmap);
            }



        }

        if(fjsl!=0)
        {
            double gzslz = new BigDecimal(2 * qs_gzsl).divide(new BigDecimal(fjsl), 2, RoundingMode.HALF_EVEN).doubleValue();
            double cslz = new BigDecimal(qs_csl).divide(new BigDecimal(fjsl), 2, RoundingMode.HALF_EVEN).doubleValue();
            double lslz = new BigDecimal(0.5 * qs_lsl).divide(new BigDecimal(fjsl), 2, RoundingMode.HALF_EVEN).doubleValue();
            Map<String, Object> allmap = new HashMap<>();
            allmap.put("csl", qs_csl);
            allmap.put("lsl", qs_lsl);
            allmap.put("ysl", qs_ysl);
            allmap.put("gzsl", qs_gzsl);
            allmap.put("jkd", MathUtil.twoBit(100 - gzslz - cslz - lslz));
            map.put("all", allmap);
        }
        return map;
    }

}


