package com.gyee.frame.controller.goodness;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.gyee.frame.common.conf.AjaxStatus;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.auto.Brownoutsevent2;
import com.gyee.frame.model.auto.Inputoroutputspeedtotal;
import com.gyee.frame.model.auto.WarningRecords;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.EchartDataVo;
import com.gyee.frame.model.custom.ShutdowneventVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.service.*;
import com.gyee.frame.service.goodness.WindturbinegoodnessService;
import com.gyee.frame.util.DateUtils;
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.util.*;

/**
 * 风机区间图表功能展示
 */
@Controller
@RequestMapping("/goodness")
@Api(value = "单机信息总览风机区间图表",tags =  "单机信息总览风机区间图表")
public class WindturbineDetailLineChartController {
    @Resource
    private WindturbinegoodnessService windturbinegoodnessService;
    @Resource
    private InputoroutputspeedtotalService inputoroutputspeedtotalServicenegoodnessService;
    @Resource
    private WarningRecordsService warningRecordsService;
    @Resource
    private Brownoutsevent2Service brownoutsevent2Service;
    @Resource
    private Shutdownevent2Service shutdownevent2Service;
    @Resource
    private WarningInfoDayService warningInfoDayService;
    @Resource
    private WindturbineanalysisdayService windturbineanalysisdayService;
    @Resource
    private WindturbineinfodayService windturbineinfodayService;
    @Resource
    private WindturbinecurvefittingService windturbinecurvefittingService;
    @Resource
    private WindturbinethewindinfoService windturbinethewindinfoService;
    @Resource
    private WindpowerstationthewindinfoService windpowerstationthewindinfoService;


    /**
     * 获得功率曲线数据查询
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @PostMapping("/findWtRealPowerChar")
    @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 findWtRealPowerChar(String wtId, String recorddate) throws Exception {
        Date date = DateUtils.parseStrtoDate(recorddate);
        List<DataVo> result = windturbinegoodnessService.findWtRealPowerChar(wtId, date);

//
//        System.out.println( JSONArray.toJSON(result));

        if (result != null) {
            return AjaxResult.successData(AjaxStatus.success.code, result);
        } else {
            return AjaxResult.successData(AjaxStatus.success.code, result);
        }
    }
    /**
     * 获得曲线区间区分功能数据查询
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @PostMapping("/getplotBands")
    @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 getplotBands(String wtId, String recorddate) throws Exception {
        Date date = DateUtils.parseStrtoDate(recorddate);
        List<List<EchartDataVo>> result = windturbinegoodnessService.getplotBands(wtId, date);

//
       System.out.println( JSONArray.toJSON(result));

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

    /**
     * 获取切入风速
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @PostMapping("/getInputSmall")
    @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 getInputSmall(String wtId, String recorddate) throws Exception {
        Date date = DateUtils.parseStrtoDate(recorddate);
        double inputsmall=0.0;
        List<Inputoroutputspeedtotal> ls = inputoroutputspeedtotalServicenegoodnessService.getInputoroutputspeedtotal(wtId, date);
        if(!ls.isEmpty())
        {
            Inputoroutputspeedtotal is=ls.get(0);
            if(StringUtils.notEmp(is) && StringUtils.notEmp(is.getMonthinputsmall()) )
            {
                inputsmall=is.getMonthinputsmall();
            }else
            {
                inputsmall=3.0;
            }

        }
        return AjaxResult.successData(AjaxStatus.success.code, inputsmall);
    }

    /**
     * 获取24小时功率曲线
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @PostMapping("/findRealPowerCharBy24")
    @ResponseBody
    @ApiOperation(value = "获取24小时功率曲线", notes = "获取24小时功率曲线")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult findRealPowerCharBy24(String wtId, String recorddate) throws Exception {
        List<DataVo> resultList = new ArrayList<>();
        if (StringUtils.notEmp(recorddate) && StringUtils.notEmp(wtId)) {

            resultList = windturbinegoodnessService.findRealPowerCharBy24(wtId, DateUtils.parseDate(recorddate));
        }

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


    /*
     * 单台风机当日报警记录
     */
    @PostMapping("/bjjllist")
    @ResponseBody
    @ApiOperation(value = "单台风机当日报警记录", notes = "单台风机当日报警记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tablepar", value = "分页排序对象", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult bjjllist(Tablepar tablepar, String wtId, String recorddate) {

        PageInfo<WarningRecords> resultList =new PageInfo<>();
        if(StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);

            cal.setTime(DateUtils.parseStrtoDate(recorddate));
            Date beginDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate = cal.getTime();

            resultList = warningRecordsService.getWarningRecordsList(tablepar, wtId, beginDate, endDate);

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

    /*
     * 单台风机当日停机记录
     */
    @PostMapping("/gzjllist")
    @ResponseBody
    @ApiOperation(value = "单台风机当日停机记录", notes = "单台风机当日停机记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tablepar", value = "分页排序对象", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult gzjllist(Tablepar tablepar, String wtId, String recorddate) {

        PageInfo<ShutdowneventVo> resultList =new PageInfo<>();
        if(StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);

            cal.setTime(DateUtils.parseStrtoDate(recorddate));
            Date beginDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate = cal.getTime();

            resultList = shutdownevent2Service.getShutdowneventList(tablepar, wtId, beginDate, endDate);

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

    /*
     * 单台风机当日限电记录
     */
    @PostMapping("/xdjllist")
    @ResponseBody
    @ApiOperation(value = "单台风机当日限电记录", notes = "单台风机当日限电记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tablepar", value = "分页排序对象", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public AjaxResult xdjllist(Tablepar tablepar, String wtId, String recorddate) {

        PageInfo<Brownoutsevent2> resultList = new PageInfo<>();
        if(StringUtils.notEmp(wtId) && StringUtils.notEmp(recorddate))
        {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);

            cal.setTime(DateUtils.parseStrtoDate(recorddate));
            Date beginDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate = cal.getTime();

             resultList = brownoutsevent2Service.getBrownoutsevent2List(tablepar, wtId, beginDate, endDate);


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

    /**
     * 获取区域弹出列表信息
     *

     */
    @PostMapping("/plotBandAjax")
    @ResponseBody
    @ApiOperation(value = "获取区域弹出列表信息", notes = "获取区域弹出列表信息")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "beginDate", value = "开始时间", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = true, dataType = "string", paramType = "query")})

    public AjaxResult plotBandAjax(String wtId,String beginDate,String endDate) throws Exception {

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


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

                    map=windturbinegoodnessService.getPlotBand(wtId,beginDate,endDate);
                    if (map != null) {
                        return AjaxResult.successData(AjaxStatus.success.code, map);
                    } else {
                        return AjaxResult.successData(AjaxStatus.success.code, map);
                    }

            }

        return AjaxResult.successData(AjaxStatus.success.code, map);
    }


}
