package com.weather.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.weather.entity.WeatherAlert;
import com.weather.service.IWeatherAlertService;
import com.weather.vo.ApiResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author : fengyuhao
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/weather/notify")
public class WeatherAlertController {

    @Autowired
    IWeatherAlertService alertService;

    @GetMapping("/notify/{areacode}")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "滚动通知", response = ApiResult.class)
    public ApiResult one(@PathVariable("areacode") String areacode)  {

        QueryWrapper<WeatherAlert> wrapper=new QueryWrapper<>();

        // todo 2021年02月02日10:21:21 没有今天的数据 模拟一下时间

        String date = DateUtil.format(new Date(), "yyyyMMdd");
        if (areacode.equalsIgnoreCase("520000")){
            areacode="520100";
        }

        wrapper.lambda()
                .eq(WeatherAlert::getCountyCode,areacode)
                //.likeRight(WeatherAlertEntity::getDatetime,date)
        ;
        List<WeatherAlert> list = alertService.list(wrapper);

        if (list==null||list.size()<=0){
            return ApiResult.error("暂无预警信息");
        }
        // 只有一条数据
        return ApiResult.success(list.get(0));
    }

}
