package com.gyee.frame.controller.health;

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.WindTurbineTestingPointAi;
import com.gyee.frame.model.auto.Windturbinecurvefittingmonth;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.custom.QxpcVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.service.CurvefittingsubService;
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("/wtcurve")
@Api(value = "单机区间曲线偏差率" ,tags =  "单机区间曲线偏差率")
public class WindTurbineCurveController {

    @Resource
    private CurvefittingsubService curvefittingsubService;
    @Resource
    private WindturbinecurvefittingmonthService windturbinecurvefittingmonthService;

    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;


    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();
    /**单机区间曲线偏差率**/
    @PostMapping("/windTurbineCurveList")
    @ResponseBody
    @ApiOperation(value = "单机区间曲线偏差率", notes = "单机区间曲线偏差率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tablepar", value = "分页排序对象", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult windTurbineCurveList(Tablepar tablepar, String wpId, String recorddate) throws Exception {

        List<QxpcVo> vos = new ArrayList<QxpcVo>();
        if (StringUtils.notEmp(wpId) && StringUtils.notEmp(recorddate))
        {
            Date date= DateUtils.parseDate(recorddate);
            vos=curvefittingsubService.getCurvefittingsubList(tablepar, wpId, date);
        }

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

    }

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

    public AjaxResult scatterWtByDayAjax(String wtId, String recorddate) throws Exception {

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


            Date date = DateUtils.parseDate(recorddate);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            Date endDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, -7);
            Date beginDate = cal.getTime();


            WindTurbineTestingPointAi gl = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wtId, Constant.TPOINT_WT_FJGL);
            int daynum = DateUtils.daysDiff(beginDate, endDate);
            long pried = 300;
            long count = 12 * 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);

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

            cal = Calendar.getInstance();
            cal.setTime(date);
            List<List<Double>> vos2 = new ArrayList<List<Double>>();

            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;

            List<Windturbinecurvefittingmonth> wcls = windturbinecurvefittingmonthService.getWindturbinecurvefittingmonthList(wtId, String.valueOf(year), String.valueOf(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.getOptimalpower());
                    // vo.setValue1(wc.getSpeed());// 风速
                    // vo.setValue2(wc.getActualpower());// 实际拟合功率
                    // vo.setValue3(wc.getOptimalpower());// 最优拟合功率
                    vos2.add(ds);
                }
            }

            result.put("line", vos2);

            result.put("scatter", vos);


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

    }

}
