package com.ruoyi.predict.api;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.predict.domain.PvPowerStation;
import com.ruoyi.predict.domain.WeatherForecast;
import com.ruoyi.predict.domain.Windplant;
import com.ruoyi.predict.service.IPvPowerStationService;
import com.ruoyi.predict.service.IWeatherForecastService;
import com.ruoyi.predict.service.IWindplantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hlf
 * @date 2022/8/9 9:46
 * 文件说明：天气预报
 */
@Api(value = "天气预报Controller", tags = {"天气预报操作接口"})
@Controller
@RequestMapping("/predict/api/wfc")
public class WeatherForecastController extends BaseController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IWeatherForecastService weatherForecastService;

    @Autowired
    private IWindplantService windplantService;

    @Autowired
    private IPvPowerStationService pvPowerStationService;

    @ApiOperation("获取天气预报数据")
    @GetMapping("/getWeatherForecastData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
    })
    public AjaxResult getWeatherForecastData(@RequestParam(value = "stationNumber") String stationNumber) {
        //获取当天日期
        Date time = DateUtil.beginOfDay(DateUtil.date());
        List<WeatherForecast> weatherForecastList = new ArrayList<>();
        WeatherForecast weatherForecastDq = weatherForecastService.selectWeatherForecastById(Convert.toLong(stationNumber), time);
        if (ObjectUtil.isNotNull(weatherForecastDq)) {
            weatherForecastDq.setType("当日");
            weatherForecastList.add(weatherForecastDq);
        }
        WeatherForecast weatherForecastTq = weatherForecastService.selectWeatherForecastById(Convert.toLong(stationNumber), DateUtil.offset(time, DateField.YEAR, -1));
        if (ObjectUtil.isNotNull(weatherForecastTq)) {
            weatherForecastTq.setType("同期");
            weatherForecastList.add(weatherForecastTq);
        }
        return AjaxResult.success(weatherForecastList);
    }

    @ApiOperation("获取所有场站天气预报数据")
    @GetMapping("/getWeatherForecastAllTerminalData")
    @ResponseBody
    public AjaxResult getWeatherForecastAllTerminalData() {
        //获取当天日期
        Date time = DateUtil.beginOfDay(DateUtil.date());
        List<WeatherForecast> weatherForecastList = new ArrayList<>();
        List<Windplant> windplantList = windplantService.selectWindplantListByGlFlag();
        for (Windplant obj : windplantList) {
            WeatherForecast weatherForecast = weatherForecastService.selectWeatherForecastById(obj.getWindplantNo(), time);
            if (ObjectUtil.isNotNull(weatherForecast)) {
                weatherForecast.setTerminalName(obj.getName());
                weatherForecastList.add(weatherForecast);
            }
        }
        List<PvPowerStation> pvPowerStationList = pvPowerStationService.selectPvPowerStationList(new PvPowerStation());
        for (PvPowerStation obj : pvPowerStationList) {
            WeatherForecast weatherForecast = weatherForecastService.selectWeatherForecastById(obj.getPvStationNo(), time);
            if (ObjectUtil.isNotNull(weatherForecast)) {
                weatherForecast.setTerminalName(obj.getName());
                weatherForecastList.add(weatherForecast);
            }
        }
        return AjaxResult.success(weatherForecastList);
    }

    /*@ApiOperation("获取天气预报数据单参数")
    @GetMapping("/getWeatherForecastDataOne")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityId", value = "城市ID", required = true),
    })
    public AjaxResult getWeatherForecastDataOne(@RequestParam(value = "cityId") String cityId) {
        String url = "http://t.weather.sojson.com/api/weather/city/" + cityId;
        ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
        return AjaxResult.success(entity);
    }

    @ApiOperation("获取天气预报数据双参数")
    @GetMapping("/getWeatherForecastDataTwo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "参数一", required = true),
            @ApiImplicitParam(name = "srcid", value = "参数二", required = true),
    })
    public AjaxResult getWeatherForecastDataTwo(@RequestParam(value = "query") String query,
                                                @RequestParam(value = "srcid") String srcid) {
        String url = "http://weathernew.pae.baidu.com/weathernew/pc?query=" + query + "&srcid=" + srcid;
        ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
        return AjaxResult.success(entity);
    }*/
}
