package com.gyee.frame.controller.scatter;

import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.dao.IRealTimeDataBaseUtil;
import com.gyee.frame.common.dao.golden.EdosUtil;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.model.auto.WindPowerStationTestingPoint2;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi2;
import com.gyee.frame.model.auto.Windturbinecurvefittingmonth;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.service.WindPowerstationTestingPointService;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
import com.gyee.frame.service.WindturbinecurvefittingmonthService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.*;

@Controller
@RequestMapping("/scatter")
public class ScatterController {

    @Autowired
    private WindturbinecurvefittingmonthService windturbinecurvefittingmonthService;

    @Autowired
    private WindPowerstationTestingPointService windPowerstationTestingPointService;

    @Autowired
    private WindTurbineTestingPointAiService  windTurbineTestingPointAiService;
    IRealTimeDataBaseUtil realApiUtil = new EdosUtil();
    /**
     *
     * @Title: scatterAjax
     * @Description: 获得选择场站、项目、线路、年份、月份相关散点图
     * @throws
     */
    @PostMapping("/scatterAjax")
    @ResponseBody
    public AjaxResult scatterAjax(String wpId, String pjId,String lnId, String year, String month) throws Exception {

        List<List<Double>> vos = new ArrayList<List<Double>>();
        if (StringUtils.notEmp(year) && StringUtils.notEmp(month)) {

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

            cal.set(Calendar.YEAR, Integer.valueOf(year));
            cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date endDate = cal.getTime();


            WindPowerStationTestingPoint2 gl=null;
            WindPowerStationTestingPoint2 fs=null;

            if (StringUtils.notEmp(wpId)) {
                gl=windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId,Constant.TPOINT_WP_SJGL);
                fs=windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId,Constant.TPOINT_WP_SSFS);
            } else if (StringUtils.notEmp(pjId)) {
                gl=windPowerstationTestingPointService.getWindPowerStationTestingPoint2(pjId,Constant.TPOINT_WP_SJGL);
                fs=windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId,Constant.TPOINT_WP_SSFS);

            } else if (StringUtils.notEmp(lnId)) {
                gl=windPowerstationTestingPointService.getWindPowerStationTestingPoint2(lnId,Constant.TPOINT_WP_SJGL);
                fs=windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId,Constant.TPOINT_WP_SSFS);
            }


            int daynum= DateUtils.daysDiff(beginDate, endDate);
            long pried=60*10;
            long count=6*24*daynum;

            DecimalFormat dcmFmt = new DecimalFormat("0.00");
            List<PointData> gls = realApiUtil.getHistoryDatasSnap(gl, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);

            List<PointData> fss = realApiUtil.getHistoryDatasSnap(fs, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);


            if (!gls.isEmpty() && !fss.isEmpty()) {
                for (int i = 0; i < fss.size(); i++) {

                    List<Double> ds = new ArrayList<Double>();

                    double f = fss.get(i).getPointValueInDouble();
                    ds.add(Double.valueOf(dcmFmt.format(f)));
                    f = gls.get(i).getPointValueInDouble();
                    ds.add(Double.valueOf(dcmFmt.format(f)));
                    vos.add(ds);
                }
            }

        }

        if (null!=vos) {
            return AjaxResult.successData(AjaxStatus.success.code, vos);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, vos);
        }

    }

    /**
     *
     * @Title: scatterAjax
     * @Description: 获得选择风机、年份、月份相关散点图
     * @throws
     */
    @PostMapping("/scatterWtAjax")
    @ResponseBody
    public AjaxResult scatterWtAjax(String wtId, String year, String month) throws Exception {

        Map<String, List<List<Double>>> result = new HashMap<String, List<List<Double>>>();
        if (StringUtils.notEmp(year) && StringUtils.notEmp(month)) {

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

            cal.set(Calendar.YEAR, Integer.valueOf(year));
            cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date endDate = cal.getTime();

            Map<String, WindTurbineTestingPointAi2> map = null;

            WindTurbineTestingPointAi2 gl =windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtId,Constant.TPOINT_WT_FJGL);
            int daynum=DateUtils.daysDiff(beginDate, endDate);
            long pried=60*10;
            long count=6*24*daynum;
            DecimalFormat dcmFmt = new DecimalFormat("0.00");
            List<PointData> gls = realApiUtil.getHistoryDatasSnap(gl, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);
            WindTurbineTestingPointAi2 fs = map.get(Constant.TPOINT_WT_FJSSFS);
            List<PointData> fss = realApiUtil.getHistoryDatasSnap(fs, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);


            List<List<Double>> vos = new ArrayList<List<Double>>();
            if (!gls.isEmpty() && !fss.isEmpty()) {
                for (int i = 0; i < fss.size(); i++) {

                    List<Double> ds = new ArrayList<Double>();

                    double f = fss.get(i).getPointValueInDouble();
                    ds.add(Double.valueOf(dcmFmt.format(f)));
                    f = gls.get(i).getPointValueInDouble();
                    ds.add(Double.valueOf(dcmFmt.format(f)));
                    vos.add(ds);
                }
            }

            List<List<Double>> vos2 = new ArrayList<List<Double>>();

            List<Windturbinecurvefittingmonth> wcls = windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId,year,month);

            for (Windturbinecurvefittingmonth wc : wcls) {

                List<Double> ds = new ArrayList<Double>();
                int c = (int) wc.getSpeed().intValue();

                if (c == wc.getSpeed()) {
                    ds.add(wc.getSpeed());
                    ds.add(wc.getActualpower());
                    // vo.setValue1(wc.getSpeed());// 风速
                    // vo.setValue2(wc.getActualpower());// 实际拟合功率
                    // vo.setValue3(wc.getOptimalpower());// 最优拟合功率
                    vos2.add(ds);
                }
            }
            result.put("scatter", vos);
            result.put("line", vos2);

        }

        if (null!=result) {
            return AjaxResult.successData(AjaxStatus.success.code, result);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, result);
        }

    }



}
