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.AgriculturalWeatherForecast;
import com.weather.service.IAgriculturalWeatherForecastService;
import com.weather.vo.ApiResult;
import com.weather.vo.res.AgriculturalWeatherForecastRes;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
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
 */
@Slf4j
@RestController
@RequestMapping("/agricultural/weather")
public class AgriculturalWeatherForecastController {

    @Autowired
    IAgriculturalWeatherForecastService weatherForecastService;

    @GetMapping("/{typename}/{stationcode}")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "七天农用天气预报", response = ApiResult.class)
    public ApiResult one(@PathVariable("typename") String typename, @PathVariable("stationcode") String stationcode)  {

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

        // todo 2021年02月02日10:21:21 没有今天的数据 模拟一下时间
        //String date = DateUtil.format(DateUtil.beginOfMonth(new Date()), "MM月dd日");
        String date = DateUtil.format(new Date(), "MM月dd日");
        //if (stationcode.equalsIgnoreCase("57816")){
        //    stationcode="57816";
        //}

        wrapper.lambda()
                .eq(AgriculturalWeatherForecast::getStationCode,stationcode)
                .eq(AgriculturalWeatherForecast::getCropName,typename)
                .orderByDesc(AgriculturalWeatherForecast::getT1Forecast)
        ;
        List<AgriculturalWeatherForecast> list = weatherForecastService.list(wrapper);

        if (list==null||list.size()<=0){
            return ApiResult.error("暂无天气信息");
        }
        // 只有一条数据
        AgriculturalWeatherForecastRes res=new AgriculturalWeatherForecastRes();
        AgriculturalWeatherForecast entity = list.get(0);
        BeanUtils.copyProperties(entity,res);
        String[] arr1 = res.getT1Forecast().split("日气温");
        res.setT1Date(arr1[0]+"日");
        res.setT1Weather("气温"+arr1[1]);

        String[] arr2 = res.getT2Forecast().split("日气温");
        res.setT2Date(arr2[0]+"日");
        res.setT2Weather("气温"+arr2[1]);

        String[] arr3 = res.getT3Forecast().split("日气温");
        res.setT3Date(arr3[0]+"日");
        res.setT3Weather("气温"+arr3[1]);

        String[] arr4 = res.getT4Forecast().split("日气温");
        res.setT4Date(arr4[0]+"日");
        res.setT4Weather("气温"+arr4[1]);

        String[] arr5 = res.getT5Forecast().split("日气温");
        res.setT5Date(arr5[0]+"日");
        res.setT5Weather("气温"+arr5[1]);

        String[] arr6 = res.getT6Forecast().split("日气温");
        res.setT6Date(arr6[0]+"日");
        res.setT6Weather("气温"+arr6[1]);

        String[] arr7 = res.getT7Forecast().split("日气温");
        res.setT7Date(arr7[0]+"日");
        res.setT7Weather("气温"+arr7[1]);

        return ApiResult.success(res);
    }

    @Test
    public void test01(){
        String str="02月04日气温08－15℃，白天小雨，夜间小雨";
        String[] strings = str.split("日气温");

        log.info(strings[0]);
        log.info(strings[1]);
    }

}
