/*
 * 文 件 名:  WeatherController
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/5/8
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.common.data.controller;

import com.alibaba.fastjson2.JSON;
import com.huashe.common.data.config.ApiAuth;
import com.huashe.common.data.model.CallResponse;
import com.huashe.common.data.service.TaskService;
import com.huashe.common.data.service.WeatherService;
import com.huashe.common.domain.model.WeatherForecast;
import com.huashe.common.domain.model.WeatherRt;
import com.huashe.common.domain.model.WeatherType;
import com.huashe.common.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 天气相关API
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/5/8]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@RestController
@CrossOrigin(allowedHeaders = "*", allowCredentials = "false")
@RequestMapping("/weather")
@Api(value = "WeatherController", description = "天气数据接口")
public class WeatherController {
    private static final Logger log = LoggerFactory.getLogger(WeatherController.class);

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private TaskService taskService;

    /**
     * 实时天气采集
     */
    @ApiAuth
    @RequestMapping(value = "/getWeather", method = RequestMethod.GET)
    @ApiOperation(value = "/getWeather", notes = "获取实时天气")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<WeatherRt> getWeather(@RequestParam(value = "adcode") String adcode) {
        CallResponse<WeatherRt> callResponse = new CallResponse<>();

        try {
            WeatherRt weatherRt = weatherService.getWeatherRt(adcode);
            callResponse.setData(weatherRt);
            callResponse.setCode(0);
            callResponse.setMessage("success");
        }
        catch (Exception e) {
            throw new BusinessException(-1, e.getMessage());
        }

        return callResponse;
    }

    /**
     * 获取历史天气数据
     */
    @ApiAuth
    @RequestMapping(value = "/getWeatherHis", method = RequestMethod.GET)
    @ApiOperation(value = "/getWeatherHis", notes = "获取天气历史")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<List<WeatherRt>> getWeatherHis(@RequestParam(value = "adcode") String adcode,
        @RequestParam(value = "startTime") String startTime, @RequestParam(value = "endTime") String endTime) {
        CallResponse<List<WeatherRt>> callResponse = new CallResponse<>();

        try {
            List<WeatherRt> list = weatherService.getWeatherHis(adcode, startTime, endTime);
            callResponse.setData(list);
            callResponse.setCode(0);
            callResponse.setMessage("success");
        }
        catch (Exception e) {
            throw new BusinessException(-1, e.getMessage());
        }

        return callResponse;
    }

    /**
     * 获取历史天气数据
     */
    @ApiAuth
    @RequestMapping(value = "/getWeatherForecast", method = RequestMethod.GET)
    @ApiOperation(value = "/getWeatherForecast", notes = "获取天气预报")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<List<WeatherForecast>> getWeatherForecast(@RequestParam(value = "adcode") String adcode) {
        CallResponse<List<WeatherForecast>> callResponse = new CallResponse<>();

        try {
            List<WeatherForecast> list = weatherService.getWeatherForecast(adcode);
            callResponse.setData(list);
            callResponse.setCode(0);
            callResponse.setMessage("success");
        }
        catch (Exception e) {
            throw new BusinessException(-1, e.getMessage());
        }

        return callResponse;
    }

    /**
     * 获取天气类型
     */
    @RequestMapping(value = "/getWeatherType", method = RequestMethod.GET)
    @ApiOperation(value = "/getWeatherType", notes = "获取天气类型")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<List<WeatherType>> getWeatherType() {
        CallResponse<List<WeatherType>> callResponse = new CallResponse<>();
        try {
            List<WeatherType> list = weatherService.getWeatherType();
            callResponse.setCode(0);
            callResponse.setMessage("success");
            callResponse.setData(list);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(-1, e.getMessage());
        }

        return callResponse;
    }

    /**
     * 实时天气采集
     */
    @RequestMapping(value = "/triggerCol", method = RequestMethod.GET)
    @ApiOperation(value = "/triggerCol", notes = "触发-天气实况采集")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<Void> triggerCol() {
        CallResponse<Void> callResponse = new CallResponse<>();
        try {
            taskService.collectWeather();
            callResponse.setCode(0);
            callResponse.setMessage("success");
        }
        catch (Exception e) {
            throw new BusinessException(-1, e.getMessage());
        }

        return callResponse;
    }
}