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 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")
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
    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.emptyresultset.code, result);
        }
    }
    /**
     * 获得曲线区间区分功能数据查询
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @PostMapping("/getplotBands")
    @ResponseBody
    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.emptyresultset.code, result);
        }
    }

    /**
     * 获取切入风速
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @PostMapping("/getInputSmall")
    @ResponseBody
    public AjaxResult getInputSmall(String wtId, String recorddate) throws Exception {
        Date date = DateUtils.parseStrtoDate(recorddate);
        List<Inputoroutputspeedtotal> result = inputoroutputspeedtotalServicenegoodnessService.getInputoroutputspeedtotal(wtId, date);
        if (result != null) {
            return AjaxResult.successData(AjaxStatus.success.code, result);
        } else {
            return AjaxResult.successData(AjaxStatus.emptyresultset.code, result);
        }
    }

    /**
     * 获取24小时功率曲线
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @PostMapping("/findRealPowerCharBy24")
    @ResponseBody
    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.emptyresultset.code, resultList);
        }
    }


    /*
     * 单台风机当日报警记录
     */
    @PostMapping("/bjjllist")
    @ResponseBody
    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.emptyresultset.code, resultList);
        }
    }

    /*
     * 单台风机当日停机记录
     */
    @PostMapping("/gzjllist")
    @ResponseBody
    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.emptyresultset.code, resultList);
        }
    }

    /*
     * 单台风机当日限电记录
     */
    @PostMapping("/xdjllist")
    @ResponseBody
    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.emptyresultset.code, resultList);
        }
    }

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

     */
    @PostMapping("/plotBandAjax")
    @ResponseBody
    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.emptyresultset.code, map);
                    }

            }

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


}
