package com.gyee.frame.controller.scatter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gyee.frame.common.cache.IGlobalCache;
import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.model.auto.WindPowerStationTestingPoint;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi;
import com.gyee.frame.model.auto.Windturbinecurvefittingmonth;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.service.WindPowerstationTestingPoint2Service;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
import com.gyee.frame.service.WindturbinecurvefittingmonthService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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 javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

@Controller
@RequestMapping("/scatter")
@Api(value = "风场、项目、线路功率风速散点图", tags = "风场、项目、线路功率风速散点图")
public class ScatterController {

    @Resource
    private WindturbinecurvefittingmonthService windturbinecurvefittingmonthService;

    @Resource
    private WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;

    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;
    @Resource
    IGlobalCache globalCache;
    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();

    /**
     * @throws
     * @Title: scatterAjax
     * @Description: 获得选择场站、项目、线路、年份、月份相关散点图
     */
    @PostMapping("/scatterAjax")
    @ResponseBody
    @ApiOperation(value = "获得选择场站、项目、线路、年份、月份相关散点图", notes = "获得选择场站、项目、线路、年份、月份相关散点图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pjId", value = "项目编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "lnId", value = "线路编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "string", paramType = "query")})

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

            int years = cal.get((Calendar.YEAR));
            int months = cal.get((Calendar.MONTH));

            if (years == Integer.valueOf(year) && months <= (Integer.valueOf(month) - 1)) {
                StringBuilder sb = new StringBuilder();
                if (StringUtils.notEmp(wpId)) {
                    sb.append("scatter_").append(wpId).append("_").append(month);
                } else if (StringUtils.notEmp(pjId)) {
                    sb.append("scatter_").append(pjId).append("_").append(month);
                } else if (StringUtils.notEmp(lnId)) {
                    sb.append("scatter_").append(lnId).append("_").append(month);
                }


                String scatter = (String) (globalCache.get(String.valueOf(sb)));
                vos = JSONObject.parseObject(scatter, new TypeReference<List<List<Double>>>() {
                });

            } else {
                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();


                WindPowerStationTestingPoint gl = null;
                WindPowerStationTestingPoint fs = null;

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

                } else if (StringUtils.notEmp(lnId)) {
                    gl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(lnId, Constant.TPOINT_WP_SJGL);
                    fs = windPowerstationTestingPoint2Service.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.success.code, vos);
        }

    }

    /**
     * @throws
     * @Title: scatterAjax
     * @Description: 获得选择风机、年份、月份相关散点图
     */
    @PostMapping("/scatterWtAjax")
    @ResponseBody
    @ApiOperation(value = "获得选择风机、年份、月份相关散点图", notes = "获得选择风机、年份、月份相关散点图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", required = true, dataType = "string", paramType = "query")})

    public AjaxResult scatterWtAjax(String wtId, String year, String month) throws Exception {

        Map<String, List<List<Double>>> result = new HashMap<String, List<List<Double>>>();

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


            int years = cal.get((Calendar.YEAR));
            int months = cal.get((Calendar.MONTH));


            if (years == Integer.valueOf(year) && months <= (Integer.valueOf(month) - 1)) {
                StringBuilder sb = new StringBuilder();
                if (StringUtils.notEmp(wtId)) {
                    sb.append("scatter_").append(wtId).append("_").append(Integer.valueOf(month));
                }



                String scatter = (String) (globalCache.get(String.valueOf(sb)));
                vos = JSONObject.parseObject(scatter, new TypeReference<List<List<Double>>>() {
                });

            } else {

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

                WindTurbineTestingPointAi 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);
                WindTurbineTestingPointAi fs = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtId, Constant.TPOINT_WT_FJSSFS);
                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);
                    }
                }
            }
                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.success.code, result);
        }

    }


}
