package com.gyee.runeconomy.controller.goodness;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.common.util.DateUtils;
import com.gyee.common.vo.benchmark.DataVo;
import com.gyee.runeconomy.dto.EchartDataVo;
import com.gyee.runeconomy.dto.R;
import com.gyee.runeconomy.dto.ResultMsg;
import com.gyee.runeconomy.model.auto.ProEconBrownoutsEvent2;
import com.gyee.runeconomy.model.auto.ProEconInOrOutSpeedTotal;
import com.gyee.runeconomy.model.auto.ProEconShutdownEvent2;
import com.gyee.runeconomy.service.BrownoutsEvent2Service;
import com.gyee.runeconomy.service.ShutdownEvent2Service;
import com.gyee.runeconomy.service.auto.IProEconInOrOutSpeedTotalService;
import com.gyee.runeconomy.service.goodness.WindturbinegoodnessService;
import com.gyee.runeconomy.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.GetMapping;
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 IProEconInOrOutSpeedTotalService proEconInOrOutSpeedTotalService;

    @Resource
    private ShutdownEvent2Service shutdownEvent2Service;
    @Resource
    private BrownoutsEvent2Service brownoutsEvent2Service;


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

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

        if (null != result) {
            return R.data(ResultMsg.ok(result));
        } else {
            return R.data(ResultMsg.error());
        }
    }

    /**
     * 获得曲线区间区分功能数据查询
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @GetMapping("/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 R getplotBands(String wtId, String recorddate) throws Exception {
        Date date = DateUtils.parseDate(recorddate);
        List<List<EchartDataVo>> result = windturbinegoodnessService.getplotBands(wtId, date);

        if (null != result) {
            return R.data(ResultMsg.ok(result));
        } else {
            return R.data(ResultMsg.error());
        }
    }

    /**
     * 获取切入风速
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @GetMapping("/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 R getInputSmall(String wtId, String recorddate) throws Exception {
        Date date = DateUtils.parseDate(recorddate);
        double inputsmall = 0.0;
        QueryWrapper<ProEconInOrOutSpeedTotal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("windturbine_id", wtId).eq("record_date", date);
        List<ProEconInOrOutSpeedTotal> ls = proEconInOrOutSpeedTotalService.list(queryWrapper);
        //                .stream().filter(i->
        //                i.getWindturbineId().equals(wtId)
        //                && i.getRecordDate().compareTo(DateUtils.truncate(date))==0
        //        ).collect(Collectors.toList());
        if (!ls.isEmpty()) {
            ProEconInOrOutSpeedTotal is = ls.get(0);
            if (StringUtils.notEmp(is) && StringUtils.notEmp(is.getMonthInputSmall())) {
                inputsmall = is.getMonthInputSmall();
            } else {
                inputsmall = 3.0;
            }

        }
        return R.data(ResultMsg.ok(inputsmall));
    }

    /**
     * 获取24小时功率曲线
     *
     * @param wtId
     * @param recorddate
     * @return
     * @throws Exception
     */
    @GetMapping("/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 R 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 (null != resultList) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.data(ResultMsg.error());
        }
    }


    /*
     * 单台风机当日报警记录
     */
    @PostMapping("/bjjllist")
    @ResponseBody
    @ApiOperation(value = "单台风机当日报警记录", notes = "单台风机当日报警记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public R bjjllist(Integer pageNum, Integer pageSize, String wtId, String recorddate) {

        //
        Page<ProEconShutdownEvent2> resultList = new Page<>();
        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.parseDate(recorddate));
            Date beginDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate = cal.getTime();

            resultList = shutdownEvent2Service.getShutdownEventList(pageNum, pageSize, wtId, beginDate, endDate);
        }
        if (null != resultList) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.data(ResultMsg.error());
        }
    }

    /*
     * 单台风机当日停机记录
     */
    @PostMapping("/gzjllist")
    @ResponseBody
    @ApiOperation(value = "单台风机当日停机记录", notes = "单台风机当日停机记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public R gzjllist(Integer pageNum, Integer pageSize, String wtId, String recorddate) {

        Page<ProEconShutdownEvent2> resultList = new Page<>();
        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.parseDate(recorddate));
            Date beginDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate = cal.getTime();

            resultList = shutdownEvent2Service.getShutdownEventList(pageNum, pageSize, wtId, beginDate, endDate);
        }
        if (null != resultList) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.data(ResultMsg.error());
        }
    }

    /*
     * 单台风机当日限电记录
     */
    @PostMapping("/xdjllist")
    @ResponseBody
    @ApiOperation(value = "单台风机当日限电记录", notes = "单台风机当日限电记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "wtId", value = "风机编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "recorddate", value = "日期", required = true, dataType = "string", paramType = "query")})

    public R xdjllist(Integer pageNum, Integer pageSize, String wtId, String recorddate) {

        Page<ProEconBrownoutsEvent2> resultList = new Page<>();
        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.parseDate(recorddate));
            Date beginDate = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH, 1);
            Date endDate = cal.getTime();

            resultList = brownoutsEvent2Service.getBrownoutsEvent2List(pageNum, pageSize, wtId, beginDate, endDate);
        }
        if (null != resultList) {
            return R.data(ResultMsg.ok(resultList));
        } else {
            return R.data(ResultMsg.error());
        }
    }

    /**
     * 获取区域弹出列表信息
     */
    @GetMapping("/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 R 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 (null != map) {
            return R.data(ResultMsg.ok(map));
        } else {
            return R.data(ResultMsg.error());
        }
    }


}
