package com.gyee.frame.controller.weather;

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.Weatherfd;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.service.weather.WeatherService;
import com.gyee.frame.service.websocket.GenreSetPushService;
import com.gyee.frame.service.websocket.RealPowerPushService;
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.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/weather")
@Api(value = "天气预测", tags = "天气预测")
public class WeatherAnalysisController {

    private static final String format = "yyyy-MM-dd HH:mm";
    @Resource
    private WeatherService weatherService;
    @Resource
    private RealPowerPushService realPowerPushService;
    @Resource
    private GenreSetPushService genreSetPushService;


    @PostMapping("/weatherInfo")
    @ResponseBody
    @ApiOperation(value = "天气分析信息", notes = "天气分析信息")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query")})

    public AjaxResult weatherInfo(String wpId) throws Exception {

        Map<String, Object> map = new HashMap<>();
        if (StringUtils.notEmp(wpId)) {

            map = weatherService.getWeatherRealInfo(wpId);

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

    }

    @PostMapping("/weatherChart")
    @ResponseBody
    @ApiOperation(value = "天气图表", notes = "天气图表")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query")})

    public AjaxResult weatherChart(String wpId) throws Exception {

        List<DataVo> glvos = new ArrayList<>();

        if (StringUtils.notEmp(wpId)) {


            glvos = realPowerPushService.findRealPower(wpId);
        }

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

    }

    @PostMapping("/powerChart")
    @ResponseBody
    @ApiOperation(value = "风速、电量、功率图表", notes = "风速、电量、功率图表")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "wpId", value = "风场编号", required = true, dataType = "string", paramType = "query")})

    public AjaxResult powerChart(String wpId) throws Exception {

        List<DataVo> glvos = new ArrayList<>();

        if (StringUtils.notEmp(wpId)) {


            glvos = genreSetPushService.findPowerWpChart(wpId);
        }

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

    }


    @GetMapping("/history/list")
    @ResponseBody
    @ApiOperation(value = "气象历史数据查询", notes = "气象历史数据查询")
    public AjaxResult getWeatherHistory(Tablepar tablepar,
                                        @RequestParam("wpId") String wpId,
                                        @RequestParam("startTs") String startTs,
                                        @RequestParam("endTs") String endTs){
        Date begin = DateUtils.parseDate(startTs);
        Date end = DateUtils.parseDate(endTs);
        PageInfo<Weatherfd> list = weatherService.getWeatherHistory(tablepar, wpId, begin, end);
        return AjaxResult.successData(AjaxStatus.success.code, list);
    }

}
