package com.gyee.frame.service.leaderboard;

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.util.StringUtils;
import org.springframework.stereotype.Service;

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

@Service
public class LeaderboardService {

    @Resource
    private ActivePowerDataService activePowerDataService;
    @Resource
    private WindturbinecurvefittingmonthService windturbinecurvefittingmonthService;
    @Resource
    private WindturbineinfodayService windturbineinfodayService;
    @Resource
    private Windturbineinfoday3Service windturbineinfoday3Service;
    @Resource
    private WarningInfoDayService warningInfoDayService;
    @Resource
    private WindturbinecurvefittingService windturbinecurvefittingService;
    @Resource
    private CurvefittingmainService curvefittingmainService;
    @Resource
    private CurvefittingmonthmainService curvefittingmonthmainService;

    private final String SJBZ = "sjbz";//实际保证
    private final String SJZY = "sjzy";//实际最优
    private final String ZYBZ = "zybz";//最优保证
    private final String TB = "tb";//同比
    private final String HB = "hb";//环比
    private final String BG = "bg";//标杆

    private String name1;
    private String name2;

    public List<StatisticalAnalysisTopVo> getRnewTopDate(String wpIps, Date beginDate, Date endDate) {

//        List<String> wpls=new ArrayList<>();
//        if (StringUtils.empty(wpIps)) {
//
//            for (Windpowerstation wp : InitialRunner.wpls) {
//                if(wp.getId().endsWith("FDC"))
//                {
//                    wpls.add(wp.getId());
//                }
//
//            }
//
//        } else {
//            wpls.add(wpIps);
//        }
        List<StatisticalAnalysisRnewTopVo> list =activePowerDataService.getStatisticalAnalysisRnewTopVoList(wpIps,beginDate, endDate);

//        List<Windturbinecurvefittingmonth> windturbineList=windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wpIps,endDate);
//
//        Map<String, Map<Double, Windturbinecurvefittingmonth>> res = new HashMap<String, Map<Double,Windturbinecurvefittingmonth>>();
//        Map<Double, Windturbinecurvefittingmonth> map = null;
//        if(windturbineList != null && !windturbineList.isEmpty()){
//            for(Windturbinecurvefittingmonth mp : windturbineList){
//                if(res.containsKey(mp.getWindturbineid())){
//                    map = res.get(mp.getWindturbineid());
//                    map.put(mp.getSpeed(), mp);
//                }else{
//                    map = new HashMap<Double, Windturbinecurvefittingmonth>();
//                    map.put(mp.getSpeed(), mp);
//                    res.put(mp.getWindturbineid(), map);
//                }
//            }
//        }

        Map<String, Map<Double, Windturbinecurvefittingmonth>> res = new HashMap<String, Map<Double,Windturbinecurvefittingmonth>>();

        Map<String, StatisticalAnalysisTopVo> theMap = new HashMap<String, StatisticalAnalysisTopVo>();
        List<StatisticalAnalysisTopVo> theList = new ArrayList<StatisticalAnalysisTopVo>();
        for (StatisticalAnalysisRnewTopVo item : list) {

            // 对非线性方程：
            // （1）计算残差平方和Q=∑(y-y*)^2和∑y^2，其中，y代表的是实测值，y*代表的是预测值；
            // （2）拟合度指标RNew=1-(Q/∑y^2)^(1/2)
            // Rnew是最近才出现的用于判定非线性回归方程的拟合度的统计参数，现在我还没有看到它的中文名称。之所以用角标new就是为了和线性回归方程的判定系数R2、adjusted
            // R2进行区别。在对方程拟合程度的解释上，Rnew和R2、adjusted R2是等价的，其意义也相同。
            // 对线性方程：
            // R^2==∑(y预测-y)^2/==∑(y实际-y)^2，y是平均数。如果R2=0.775，则说明变量y的变异中有77.5％是由变量X引起的。当R2＝1时，表示所有的观测点全部落在回归直线上。当R2=0时，表示自变量与因变量无线性关系。

            if (InitialRunner.mprdMap.containsKey(item.getModelId())) {

                if (InitialRunner.mprdMap.get(item.getModelId())
                        .containsKey(
                                item.getSpeed())) {
                    ModelpowerRd mp = InitialRunner.mprdMap.get(
                            item.getModelId()).get(item.getSpeed());
                    double value1 = item.getAvgpower() - mp.getEnsurepower();
                    value1 *= value1;
                    double value2 = item.getAvgpower();
                    value2 *= value2;

                    if (theMap.containsKey(item.getWindTurbineId())) {
                        StatisticalAnalysisTopVo vo = theMap.get(item
                                .getWindTurbineId());
                        vo.setNum(vo.getNum() + 1);
                        vo.setSpeed(vo.getSpeed() + item.getSpeed());
                        vo.setGeneratingCapacity(vo.getGeneratingCapacity()
                                + value2);
                        vo.setPower(vo.getPower() + value1);

                    } else {
                        StatisticalAnalysisTopVo vo = new StatisticalAnalysisTopVo();
                        vo.setNum(1);
                        vo.setWindturbine(item.getWindTurbineId());
                        vo.setWindfarm(InitialRunner.wpmap.get(
                                item.getWindPowerStationId()).getName());
                        vo.setBeginDate(beginDate);
                        vo.setEndDate(endDate);
                        vo.setGeneratingCapacity(value2);
                        vo.setSpeed(item.getSpeed());
                        vo.setPower(value1);
                        vo.setModel(item.getModelName());
                        theList.add(vo);
                        theMap.put(item.getWindTurbineId(), vo);
                    }
                }
            }
            else
            {
                if (res.containsKey(item.getWindTurbineId())) {

                    if (res.get(item.getWindTurbineId())
                            .containsKey(
                                    item.getSpeed())) {
                        Windturbinecurvefittingmonth mp = res.get(
                                item.getWindTurbineId()).get(item.getSpeed());


                        double value1 = item.getAvgpower() - mp.getOptimalpower();
                        value1 *= value1;
                        double value2 = item.getAvgpower();
                        value2 *= value2;

                        if (theMap.containsKey(item.getWindTurbineId())) {
                            StatisticalAnalysisTopVo vo = theMap.get(item
                                    .getWindTurbineId());
                            vo.setNum(vo.getNum() + 1);
                            vo.setSpeed(vo.getSpeed() + item.getSpeed());
                            vo.setGeneratingCapacity(vo.getGeneratingCapacity()
                                    + value2);
                            vo.setPower(vo.getPower() + value1);

                        } else {
                            StatisticalAnalysisTopVo vo = new StatisticalAnalysisTopVo();
                            vo.setNum(1);
                            vo.setWindturbine(item.getWindTurbineId());
                            vo.setWindfarm(InitialRunner.wpmap.get(
                                    item.getWindPowerStationId()).getName());
                            vo.setBeginDate(beginDate);
                            vo.setEndDate(endDate);
                            vo.setGeneratingCapacity(value2);
                            vo.setSpeed(item.getSpeed());
                            vo.setPower(value1);
                            vo.setModel(item.getModelName());
                            theList.add(vo);
                            theMap.put(item.getWindTurbineId(), vo);
                        }
                    }
                }
            }
        }

        for(StatisticalAnalysisTopVo item :theList)
        {
            double rnew=0;
            if(item.getGeneratingCapacity()!=0)
            {
                BigDecimal b1=new BigDecimal(item.getPower());
                BigDecimal b2=new BigDecimal(item.getGeneratingCapacity());
                rnew=Math.sqrt(b1.divide(b2,2, RoundingMode.HALF_EVEN).doubleValue());
            }
            item.setPower(rnew);
            double speed=0;
            if(item.getNum()!=0)
            {
                BigDecimal b1=new BigDecimal(item.getSpeed());
                BigDecimal b2=new BigDecimal(item.getNum());
                speed=b1.divide(b2,2, RoundingMode.HALF_EVEN).doubleValue();
            }
            item.setSpeed(speed);
        }
        return theList;
    }


    public Map<String, List> fittingtopchart(String wtIds, Date beginDate, Date endDate) throws Exception {
        Map<String, List> map=new HashMap<>();
        if (StringUtils.notEmp(wtIds) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {
            String[] wtid = wtIds.split(",");
            List<String> wtids=new ArrayList<>();
           for(String w:wtid)
           {
               wtids.add(w);
           }
            map = activePowerDataService.getActivePowerDataChart(wtids, beginDate, endDate);

        }

        return map;
    }

    public List<StatisticalAnalysisTopVo> totalPowerCapacityTopList(String wpIds, Date beginDate, Date endDate)  {
        List<StatisticalAnalysisTopVo> ls=new ArrayList<>();

        if ( StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {
            List<String> wpids=new ArrayList<>();
            if(StringUtils.isNotEmpty(wpIds))
            {
                String[] wpid = wpIds.split(",");

                for(String w:wpid)
                {
                    wpids.add(w);
                }
            }else
            {
                for(Windpowerstation wp:InitialRunner.wpls)
                {
                    if(wp.getId().endsWith("FDC"))
                    {
                        wpids.add(wp.getId());
                    }
                }
            }

            ls = windturbineinfodayService.getWindturbineinfo(wpids, beginDate, endDate);

        }

        return ls;
    }

    public List<Windturbineinfoday3> utilizationList(Tablepar tablepar, String wpIds, Date beginDate, Date endDate, String type)  {
        List<Windturbineinfoday3> ls=new ArrayList<>();

        if ( StringUtils.notEmp(type) &&  StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {


            ls = windturbineinfoday3Service.utilizationList(tablepar, wpIds, beginDate, endDate, type);

        }

        return ls;
    }

    public List<Windturbineinfoday3> lossList(Tablepar tablepar, String wpIds, Date beginDate, Date endDate, String type)  {
        List<Windturbineinfoday3> ls=new ArrayList<>();

        if ( StringUtils.notEmp(type) &&  StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {


            ls = windturbineinfoday3Service.lossList(tablepar, wpIds, beginDate, endDate, type);

        }

        return ls;
    }

    public List<StatisticalAnalysisTopVo> loadfactortoplist(String type,String wpIds, Date beginDate, Date endDate)  {
        List<StatisticalAnalysisTopVo> vos=new ArrayList<>();

        if (StringUtils.notEmp(type) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {
            List<String> wpids=new ArrayList<>();

            if(StringUtils.notEmp(wpids))
            {
                wpids.add(wpIds);
            }else
            {
                if(type.equals("-1"))
                {
                    for(Windpowerstation wp:InitialRunner.wpallls)
                    {
                        if(wp.getId().endsWith("FDC"))
                        {
                            wpids.add(wp.getId());
                        }
                    }
                }else if(type.equals("-2"))
                {
                    for(Windpowerstation wp:InitialRunner.wpallls)
                    {
                        if(wp.getId().endsWith("GDC"))
                        {
                            wpids.add(wp.getId());
                        }
                    }
                }
            }



            vos = windturbineinfodayService.getWindturbineinfo(wpids, beginDate, endDate);

            if (!vos.isEmpty()) {
                Map<String, Equipmentmodel> mlmap = InitialRunner.mlmap;
                Map<String, Windturbine> wtmap = InitialRunner.wtmap;
                for (StatisticalAnalysisTopVo vo : vos) {
                    if (StringUtils.notEmp(vo.getWindturbine()) && wtmap.containsKey(vo.getWindturbine())) {
                        Windturbine wt = wtmap.get(vo.getWindturbine());
                        if (StringUtils.notEmp(wt.getModelid()) && mlmap.containsKey(wt.getModelid())) {
                            Equipmentmodel ml = mlmap.get(wt.getModelid());
                            if (ml.getPowerproduction() != 0) {
                                BigDecimal b1 = new BigDecimal(vo.getGeneratingCapacity());
                                BigDecimal b2 = new BigDecimal(ml.getPowerproduction());

                                vo.setGeneratingCapacity(b1.divide(b2.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue());

                            }

                        }
                    }
                }


            }

        }

        return vos;
    }


    public List<SawVo> getWarningTopDate(Tablepar tablepar, String type, Date beginDate, Date endDate)  {
        List<SawVo> vos=new ArrayList<>();

        if(StringUtils.isNotEmpty(type))
        {
            switch (type) {
                case "1":
                    vos=warningInfoDayService.getWarningInfoDayByWarningid(tablepar,beginDate, endDate);
                    break;
                case "2":
                    vos=warningInfoDayService.getWarningInfoDayByWarningClassify(tablepar,beginDate, endDate);
                    break;
                case "3":
                    vos=warningInfoDayService.getWarningInfoDayByManufacturer(tablepar,beginDate, endDate);
                    break;
                default:

                    break;
            }
        }

        return vos;
    }
    public List<SawVo> getWarningTopDatePercentage(String type, Date beginDate, Date endDate)  {
        List<SawVo> vos=new ArrayList<>();

        if(StringUtils.isNotEmpty(type))
        {
            switch (type) {
                case "1":
                    vos=warningInfoDayService.getWarningInfoDayByWarningidPercentage(beginDate, endDate);
                    break;
                case "2":
                    vos=warningInfoDayService.getWarningInfoDayByWarningClassifyPercentage(beginDate, endDate);
                    break;
                case "3":
                    vos=warningInfoDayService.getWarningInfoDayByManufacturerPercentage(beginDate, endDate);
                    break;
                default:

                    break;
            }
        }
        return vos;
    }
    /*******************************************************************************************************************************************************/

    public List<Curvefittingmain> curvefittingmainList(Tablepar tablepar, String wpId, Date recorddate)  {

        List<Curvefittingmain> list =new ArrayList<>();
        List<String> wtIds=new ArrayList<>();
        if(StringUtils.notEmp(wpId) && StringUtils.notEmp(recorddate))
        {
            List<Windturbine> wtls=InitialRunner.wp_wtmap.get(wpId);
            for(Windturbine wt:wtls)
            {
                wtIds.add(wt.getId());
            }


            list =curvefittingmainService.getCurvefittingmainList(tablepar,wtIds,recorddate);
        }

        return list;

    }

    public  Map<String, Object>  getCurvechatAjax(String type,String wtId,  Date recorddate)  {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(type) && StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            List<DataVo> datavos = new ArrayList<DataVo>();
            List<Windturbinecurvefitting> wcls = windturbinecurvefittingService.comparechatAjax(wtId, recorddate);
            Map<String, Windturbine> wtmap = InitialRunner.wtmap;
            Map<String, Map<String, ModelPower>> modelPowermap = InitialRunner.modelPowermap;
            Map<String, ModelPower> modelmap = null;
            if (wtmap.containsKey(wtId)) {
                Windturbine wt = wtmap.get(wtId);
                if (modelPowermap.containsKey(wt.getModelid())) {
                    modelmap = modelPowermap.get(wt.getModelid());
                }
            }

            if (!wcls.isEmpty()) {
                Collections.sort(wcls, new Comparator<Windturbinecurvefitting>() {
                    public int compare(Windturbinecurvefitting arg0, Windturbinecurvefitting arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());

                    }
                });
                for (Windturbinecurvefitting wc : wcls) {

                    DataVo vo = new DataVo();
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        vo.setValue1(wc.getSpeed());
                        if (StringUtils.notEmp(type) && type.equals(SJBZ) && null != modelmap) {
                            if (modelmap.containsKey(String.valueOf(wc.getSpeed()))) {
                                ModelPower mp = modelmap.get(String.valueOf(wc.getSpeed()));
                                vo.setValue3(mp.getEnsurepower());
                            } else {
                                vo.setValue3(0.0);
                            }
                            vo.setValue2(wc.getActualpower());

                        } else if (StringUtils.notEmp(type) && type.equals(SJZY)) {
                            vo.setValue2(wc.getActualpower());
                            vo.setValue3(wc.getOptimalpower());

                        } else if (StringUtils.notEmp(type) && type.equals(ZYBZ) && null != modelmap) {
                            if (modelmap.containsKey(String.valueOf(wc.getSpeed()))) {
                                ModelPower mp = modelmap.get(String.valueOf(wc.getSpeed()));
                                vo.setValue3(mp.getEnsurepower());
                            } else {
                                vo.setValue3(0.0);
                            }
                            vo.setValue2(wc.getOptimalpower());

                        }

                        datavos.add(vo);

                    }
                }
            }
            curvechat(type);

            map.put("datas", datavos);
            map.put("name1", name1);
            map.put("name2", name2);
        }
        return map;
    }


    public  Map<String, Object>  curvechatAjaxtb(String type,String wtId,  Date recorddate)  {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(type) && StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            List<DataVo> datavos = new ArrayList<DataVo>();
            Map<Double, Double> speedmap = new HashMap<Double, Double>();
            List<Windturbinecurvefitting> thisdayls = windturbinecurvefittingService.comparechatAjax(wtId,recorddate);

            // 去年统计指标
            Calendar cal = Calendar.getInstance();
            cal.setTime(recorddate);
            cal.add(Calendar.YEAR, -1);

            List<Windturbinecurvefitting> lastyearls = windturbinecurvefittingService.comparechatAjax(wtId,cal.getTime());

            if (!lastyearls.isEmpty()) {

                for (Windturbinecurvefitting wc : lastyearls) {
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        speedmap.put(wc.getSpeed(), wc.getActualpower());
                    }
                }
            }
            if (!thisdayls.isEmpty()) {
                Collections.sort(thisdayls, new Comparator<Windturbinecurvefitting>() {
                    public int compare(Windturbinecurvefitting arg0, Windturbinecurvefitting arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                    }
                });

                for (Windturbinecurvefitting wc : thisdayls) {

                    DataVo vo = new DataVo();
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        vo.setValue1(wc.getSpeed());
                        vo.setValue2(wc.getActualpower());

                        if (speedmap.containsKey(wc.getSpeed())) {
                            vo.setValue3(speedmap.get(wc.getSpeed()));
                        } else {
                            vo.setValue3(0.0);
                        }
                        datavos.add(vo);

                    }
                }
            }

            curvechat(type);

            map.put("datas", datavos);
            map.put("name1", name1);
            map.put("name2", name2);
        }
        return map;
    }

    public  Map<String, Object>  curvechatAjaxhb(String type,String wtId,  Date recorddate)  {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(type) && StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            List<DataVo> datavos = new ArrayList<DataVo>();
            Map<Double, Double> speedmap = new HashMap<Double, Double>();
            List<Windturbinecurvefitting> thisdayls = windturbinecurvefittingService.comparechatAjax(wtId,recorddate);

            // 上个月统计指标
            Calendar cal = Calendar.getInstance();
            cal.setTime(recorddate);
            cal.add(Calendar.DAY_OF_MONTH, -1);

            List<Windturbinecurvefitting> lastyearls = windturbinecurvefittingService.comparechatAjax(wtId,cal.getTime());

            if (!lastyearls.isEmpty()) {

                for (Windturbinecurvefitting wc : lastyearls) {
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        speedmap.put(wc.getSpeed(), wc.getActualpower());
                    }
                }
            }
            if (!thisdayls.isEmpty()) {
                Collections.sort(thisdayls, new Comparator<Windturbinecurvefitting>() {
                    public int compare(Windturbinecurvefitting arg0, Windturbinecurvefitting arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                    }
                });

                for (Windturbinecurvefitting wc : thisdayls) {

                    DataVo vo = new DataVo();
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        vo.setValue1(wc.getSpeed());
                        vo.setValue2(wc.getActualpower());

                        if (speedmap.containsKey(wc.getSpeed())) {
                            vo.setValue3(speedmap.get(wc.getSpeed()));
                        } else {
                            vo.setValue3(0.0);
                        }
                        datavos.add(vo);

                    }
                }
            }

            curvechat(type);

            map.put("datas", datavos);
            map.put("name1", name1);
            map.put("name2", name2);
        }
        return map;
    }

    public  Map<String, Object>  curvechatAjaxbg(String type,String wtId,  Date recorddate)  {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(type) && StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            List<DataVo> datavos = new ArrayList<DataVo>();
            Map<Double, Double> speedmap = new HashMap<Double, Double>();
            List<Windturbinecurvefitting> thisdayls = windturbinecurvefittingService.comparechatAjax(wtId,recorddate);

            // 标杆风机指标
            Windturbine wt = InitialRunner.wtmap.get(wtId);
            String stid = wt.getStandardid();

            if(StringUtils.notEmp(stid))
            {
                List<Windturbinecurvefitting> stmonthls = windturbinecurvefittingService.comparechatAjax(wtId,recorddate);

                if (null != stmonthls && !stmonthls.isEmpty()) {

                    if (!stmonthls.isEmpty()) {

                        for (Windturbinecurvefitting wc : stmonthls) {
                            int c = (int) wc.getSpeed().intValue();
                            if (c == wc.getSpeed()) {
                                speedmap.put(wc.getSpeed(), wc.getActualpower());
                            }
                        }
                    }
                }

                if (!thisdayls.isEmpty()) {
                    Collections.sort(thisdayls, new Comparator<Windturbinecurvefitting>() {
                        public int compare(Windturbinecurvefitting arg0, Windturbinecurvefitting arg1) {
                            return arg0.getSpeed().compareTo(arg1.getSpeed());
                            /*
                             * int result = 0; if (arg0.getSpeed() >
                             * arg1.getSpeed()) result = 1; else result = 0; return
                             * result;
                             */
                        }
                    });
                    for (Windturbinecurvefitting wc : thisdayls) {

                        DataVo vo = new DataVo();
                        int c = (int) wc.getSpeed().intValue();
                        if (c == wc.getSpeed()) {
                            vo.setValue1(wc.getSpeed());
                            vo.setValue2(wc.getActualpower());

                            if (speedmap.containsKey(wc.getSpeed())) {
                                vo.setValue3(speedmap.get(wc.getSpeed()));
                            } else {
                                vo.setValue3(0.0);
                            }
                            datavos.add(vo);

                        }
                    }
                }

                curvechat(type);

                map.put("datas", datavos);
                map.put("name1", name1);
                map.put("name2", name2);
            }

        }
        return map;
    }

    private void curvechat(String type) {
        if (StringUtils.notEmp(type) && type.equals(SJBZ)) {

            name1 = "实际功率";
            name2 = "保证功率";

        } else if (StringUtils.notEmp(type) && type.equals(SJZY)) {

            name1 = "实际功率";
            name2 = "最优功率";
        } else if (StringUtils.notEmp(type) && type.equals(ZYBZ)) {

            name1 = "最优功率";
            name2 = "保证功率";
        } else if (StringUtils.notEmp(type) && type.equals(TB)) {

            name1 = "本日实际功率";
            name2 = "去年同期实际功率";
        } else if (StringUtils.notEmp(type) && type.equals(HB)) {

            name1 = "本日实际功率";
            name2 = "上日实际功率";
        } else if (StringUtils.notEmp(type) && type.equals(BG)) {

            name1 = "本日实际功率";
            name2 = "标杆实际功率";
        }

    }


    /*******************************************************************************************************************************************************/

    public List<Curvefittingmonthmain> curvefittingmonthmainList(Tablepar tablepar, String wpId, String year,String month)  {

        List<Curvefittingmonthmain> list =new ArrayList<>();
        List<String> wtIds=new ArrayList<>();
        if(StringUtils.notEmp(wpId) && StringUtils.notEmp(year) && StringUtils.notEmp(month))
        {
            List<Windturbine> wtls=InitialRunner.wp_wtmap.get(wpId);
            for(Windturbine wt:wtls)
            {
                wtIds.add(wt.getId());
            }


            list =curvefittingmonthmainService.getCurvefittingmonthmainList(tablepar,wtIds,year,month);
        }

        return list;

    }
    public  Map<String, Object>  curveMonthchatAjax(String type,String wtId, String year,String month)  {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(type) && StringUtils.notEmp(wtId) && StringUtils.notEmp(year) && StringUtils.notEmp(month))
        {
            List<DataVo> datavos = new ArrayList<DataVo>();
            List<Windturbinecurvefittingmonth> wcls = windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId, year,month);
            Map<String, Windturbine> wtmap = InitialRunner.wtmap;
            Map<String, Map<String, ModelPower>> modelPowermap = InitialRunner.modelPowermap;

            Map<String, ModelPower> modelmap = null;
            if (wtmap.containsKey(wtId)) {
                Windturbine wt = wtmap.get(wtId);
                if (modelPowermap.containsKey(wt.getModelid())) {
                    modelmap = modelPowermap.get(wt.getModelid());
                }
            }

            if (!wcls.isEmpty()) {
                Collections.sort(wcls, new Comparator<Windturbinecurvefittingmonth>() {
                    public int compare(Windturbinecurvefittingmonth arg0, Windturbinecurvefittingmonth arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                        /*
                         * int result = 0; if (arg0.getSpeed() >
                         * arg1.getSpeed()) result = 1; else result = 0; return
                         * result;
                         */
                    }
                });
                for (Windturbinecurvefittingmonth wc : wcls) {
                    // private final String SJBZ ="sjbz" ;
                    // private final String SJZY ="sjzy" ;
                    // private final String ZYBZ ="zybz" ;
                    DataVo vo = new DataVo();
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        vo.setValue1(wc.getSpeed());
                        if (StringUtils.notEmp(type) && type.equals(SJBZ) && null != modelmap) {
                            if (modelmap.containsKey(String.valueOf(wc.getSpeed()))) {
                                ModelPower mp = modelmap.get(String.valueOf(wc.getSpeed()));
                                vo.setValue3(mp.getEnsurepower());
                            } else {
                                vo.setValue3(0.0);
                            }
                            vo.setValue2(wc.getActualpower());
                            name1 = "实际功率";
                            name2 = "保证功率";

                        } else if (StringUtils.notEmp(type) && type.equals(SJZY)) {
                            vo.setValue2(wc.getActualpower());
                            vo.setValue3(wc.getOptimalpower());
                            name1 = "实际功率";
                            name2 = "最优功率";
                        } else if (StringUtils.notEmp(type) && type.equals(ZYBZ) && null != modelmap) {
                            if (modelmap.containsKey(String.valueOf(wc.getSpeed()))) {
                                ModelPower mp = modelmap.get(String.valueOf(wc.getSpeed()));
                                vo.setValue3(mp.getEnsurepower());
                            } else {
                                vo.setValue3(0.0);
                            }
                            vo.setValue2(wc.getOptimalpower());

                            name1 = "最优功率";
                            name2 = "保证功率";
                        }

                        datavos.add(vo);

                    }
                }
            }
            curvemonthchat(type);

            map.put("datas", datavos);
            map.put("name1", name1);
            map.put("name2", name2);
        }
        return map;
    }


    public  Map<String, Object>  curveMonthchatAjaxtb(String type,String wtId,  String year,String month)  {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(type) && StringUtils.notEmp(wtId) && StringUtils.notEmp(year) && StringUtils.notEmp(month))
        {
            List<DataVo> datavos = new ArrayList<DataVo>();
            Map<Double, Double> speedmap = new HashMap<Double, Double>();
            List<Windturbinecurvefittingmonth> thismonthls = windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId, year,month);

            // 去年统计指标
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR,Integer.valueOf(year));
            cal.set(Calendar.MONTH,Integer.valueOf(month)-1);
            cal.add(Calendar.YEAR, -1);

            String lastyear=String.valueOf(cal.get(Calendar.YEAR));

            List<Windturbinecurvefittingmonth> lastyearls =windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId, lastyear,month);

            if (!lastyearls.isEmpty()) {

                for (Windturbinecurvefittingmonth wc : lastyearls) {
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        speedmap.put(wc.getSpeed(), wc.getActualpower());
                    }
                }
            }
            if (!thismonthls.isEmpty()) {
                Collections.sort(thismonthls, new Comparator<Windturbinecurvefittingmonth>() {
                    public int compare(Windturbinecurvefittingmonth arg0, Windturbinecurvefittingmonth arg1) {
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                    }
                });

                for (Windturbinecurvefittingmonth wc : thismonthls) {

                    DataVo vo = new DataVo();
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        vo.setValue1(wc.getSpeed());
                        vo.setValue2(wc.getActualpower());
                        name1 = "本月实际功率";
                        name2 = "去年同期实际功率";
                        if (speedmap.containsKey(wc.getSpeed())) {
                            vo.setValue3(speedmap.get(wc.getSpeed()));
                        } else {
                            vo.setValue3(0.0);
                        }
                        datavos.add(vo);

                    }
                }
            }

            curvemonthchat(type);

            map.put("datas", datavos);
            map.put("name1", name1);
            map.put("name2", name2);
        }
        return map;
    }

    public  Map<String, Object>  curveMonthchatAjaxhb(String type,String wtId,  String year,String month)  {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(type) && StringUtils.notEmp(wtId) && StringUtils.notEmp(year) && StringUtils.notEmp(month))
        {
            List<DataVo> datavos = new ArrayList<DataVo>();
            Map<Double, Double> speedmap = new HashMap<Double, Double>();
            List<Windturbinecurvefittingmonth> thismonthls = windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId, year,month);

            // 上个月统计指标
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR,Integer.valueOf(year));
            cal.set(Calendar.MONTH,Integer.valueOf(month)-1);
            cal.add(Calendar.MONTH, -1);

            String lastyearmonth=String.valueOf(cal.get(Calendar.MONTH));

            List<Windturbinecurvefittingmonth> lastmonthls =windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId, year,lastyearmonth);


            if (!lastmonthls.isEmpty()) {

                for (Windturbinecurvefittingmonth wc : lastmonthls) {
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        speedmap.put(wc.getSpeed(), wc.getActualpower());
                    }
                }
            }

            if (!thismonthls.isEmpty()) {
                Collections.sort(thismonthls, new Comparator<Windturbinecurvefittingmonth>() {
                    public int compare(Windturbinecurvefittingmonth arg0, Windturbinecurvefittingmonth arg1) {
                        // 修改横轴排序
                        return arg0.getSpeed().compareTo(arg1.getSpeed());
                        /*
                         * int result = 0; if (arg0.getSpeed() >
                         * arg1.getSpeed()) result = 1; else result = 0; return
                         * result;
                         */
                    }
                });
                for (Windturbinecurvefittingmonth wc : thismonthls) {

                    DataVo vo = new DataVo();
                    int c = (int) wc.getSpeed().intValue();
                    if (c == wc.getSpeed()) {
                        vo.setValue1(wc.getSpeed());
                        vo.setValue2(wc.getActualpower());
                        name1 = "本月实际功率";
                        name2 = "上月实际功率";
                        if (speedmap.containsKey(wc.getSpeed())) {
                            vo.setValue3(speedmap.get(wc.getSpeed()));
                        } else {
                            vo.setValue3(0.0);
                        }
                        datavos.add(vo);

                    }
                }
            }
            curvemonthchat(type);
            map.put("datas", datavos);
            map.put("name1", name1);
            map.put("name2", name2);
        }
        return map;
    }

    public  Map<String, Object>  curveMonthchatAjaxbg(String type,String wtId,  String year,String month)  {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.notEmp(type) && StringUtils.notEmp(wtId) && StringUtils.notEmp(year) && StringUtils.notEmp(month))
        {
            List<DataVo> datavos = new ArrayList<DataVo>();
            Map<Double, Double> speedmap = new HashMap<Double, Double>();
            List<Windturbinecurvefittingmonth> thismonthls = windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId, year,month);

            // 标杆风机指标
            Windturbine wt = InitialRunner.wtmap.get(wtId);
            String stid = wt.getStandardid();

            if(StringUtils.notEmp(stid))
            {
                List<Windturbinecurvefittingmonth> stmonthls = windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(stid, year,month);

                if (null != stmonthls && !stmonthls.isEmpty()) {

                    if (!stmonthls.isEmpty()) {

                        for (Windturbinecurvefittingmonth wc : stmonthls) {
                            int c = (int) wc.getSpeed().intValue();
                            if (c == wc.getSpeed()) {
                                speedmap.put(wc.getSpeed(), wc.getActualpower());
                            }
                        }
                    }
                }

                if (!thismonthls.isEmpty()) {
                    Collections.sort(thismonthls, new Comparator<Windturbinecurvefittingmonth>() {
                        public int compare(Windturbinecurvefittingmonth arg0, Windturbinecurvefittingmonth arg1) {
                            return arg0.getSpeed().compareTo(arg1.getSpeed());
                            /*
                             * int result = 0; if (arg0.getSpeed() >
                             * arg1.getSpeed()) result = 1; else result = 0; return
                             * result;
                             */
                        }
                    });
                    for (Windturbinecurvefittingmonth wc : thismonthls) {

                        DataVo vo = new DataVo();
                        int c = (int) wc.getSpeed().intValue();
                        if (c == wc.getSpeed()) {
                            vo.setValue1(wc.getSpeed());
                            vo.setValue2(wc.getActualpower());
                            name1 = "本月实际功率";
                            name2 = "标杆实际功率";
                            if (speedmap.containsKey(wc.getSpeed())) {
                                vo.setValue3(speedmap.get(wc.getSpeed()));
                            } else {
                                vo.setValue3(0.0);
                            }
                            datavos.add(vo);

                        }
                    }
                }

                curvemonthchat(type);

                map.put("datas", datavos);
                map.put("name1", name1);
                map.put("name2", name2);
            }


        }
        return map;
    }

    private void curvemonthchat(String type) {

        if (StringUtils.notEmp(type) && type.equals(SJBZ)) {

            name1 = "实际功率";
            name2 = "保证功率";

        } else if (StringUtils.notEmp(type) && type.equals(SJZY)) {

            name1 = "实际功率";
            name2 = "最优功率";
        } else if (StringUtils.notEmp(type) && type.equals(ZYBZ)) {

            name1 = "最优功率";
            name2 = "保证功率";
        } else if (StringUtils.notEmp(type) && type.equals(TB)) {

            name1 = "本月实际功率";
            name2 = "去年同期实际功率";
        } else if (StringUtils.notEmp(type) && type.equals(HB)) {

            name1 = "本月实际功率";
            name2 = "上月实际功率";
        } else if (StringUtils.notEmp(type) && type.equals(BG)) {

            name1 = "本月实际功率";
            name2 = "标杆实际功率";
        }

    }
}
