package com.rehome.jpahefengweather.controller;


import com.google.gson.Gson;
import com.rehome.jpahefengweather.dto.TyphoonInfoDto;
import com.rehome.jpahefengweather.entity.TyphoonInfo;
import com.rehome.jpahefengweather.entity.ZjsltStorm;
import com.rehome.jpahefengweather.service.ZjsltStormService;
import jakarta.annotation.Resource;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author huangwenfei
 * @version v1.0.0.0
 * Created DateTime 2025-06-17 13:48
 * @description: 台风接口控制器
 */

@RestController
@RequestMapping("/storm/service")
public class ZjsltStormController {
    //台风服务
    @Resource
    private ZjsltStormService zjsltStormService;

    /**
     * 功能描述 从本地数据库查台风列表，然后返回给前端
     * @author huangwenfei
     * Created DateTime 2021-05-08 14:03
     */
    @CrossOrigin
    @RequestMapping(value = "/getStormListByYear",method = RequestMethod.GET)
    public List<ZjsltStorm> getStormListByYear(@RequestParam(value = "year", required = false) String year){
        String currentYear = new SimpleDateFormat("yyyy").format(new Date());
        String paramYear = year==null?currentYear:year;
        return this.zjsltStormService.findByYear(paramYear);
    }

    /**
     * 功能描述 从本地数据库获取活跃台风列表
     * @author huangwenfei
     * Created DateTime 2025-06-17 14:48
     */
    @CrossOrigin
    @RequestMapping(value = "/getTyhoonActivityList",method = RequestMethod.GET)
    public List<ZjsltStorm> getTyhoonActivityList(){
        return this.zjsltStormService.getTyhoonActivity();
    }

    /**
     * 功能描述 从本地数据库查台风实况和路径，然后返回给前端
     * @author huangwenfei
     * Created DateTime 2021-05-10 14:17
     */
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getTyhoonInfo",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public TyphoonInfoDto getTyhoonInfo(@RequestParam("tfid") String tfid) {
        Gson gson = new Gson();
        TyphoonInfo typhoonInfo = this.zjsltStormService.findTyphoonInfoByTfid(tfid);
        if(!ObjectUtils.isEmpty(typhoonInfo)){
            TyphoonInfoDto typhoonInfoDto = gson.fromJson(typhoonInfo.getStormData(),TyphoonInfoDto.class);
            return typhoonInfoDto;
        }
        return null;
    }
}