package com.elephant.framework.galaxy.aroad.module.roadcondition.controller;

import com.elephant.framework.galaxy.aroad.module.roadcondition.dto.WeatherDto;
import com.elephant.framework.galaxy.aroad.module.roadcondition.dto.WeatherLivesDto;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import com.elephant.framework.galaxy.aroad.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.*;

/**
 * <p>
 * 高德实时天气
 * </p>
 * @author xuzhitu
 * @since 2019-04-23
 */
@RestController
@RequestMapping("/qxfw")
public class GlgzcxSstqController extends BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/cxqx")
    public R getWeatherLivesOfShandong(){
        //获取山东17地市的实时天气数据并写入缓存
        List<WeatherLivesDto> weatherLives = new ArrayList<>();

        weatherLives = (List)redisTemplate.opsForValue().get("weatherLives");
        if(weatherLives == null || weatherLives.size() <= 0) {
            System.out.println("111");
            weatherLives = getWeatherLives();
            redisTemplate.opsForValue().set("weatherLives", weatherLives);
            // 实时天气Redis缓存时间设为1小时
            redisTemplate.expire("weatherLives",3600l,java.util.concurrent.TimeUnit.SECONDS);
        }
        return new R<>(weatherLives);
    }

    private static Iterable<Object[]> getCityCodes() {
        return Arrays.asList(new Object[][]{
                //山东17地市
                {370100, "济南市", "base"},
                {370200, "青岛市", "base"},
                {370300, "淄博市", "base"},
                {370400, "枣庄市", "base"},
                {370500, "东营市", "base"},
                {370600, "烟台市", "base"},
                {370700, "潍坊市", "base"},
                {370800, "济宁市", "base"},
                {370900, "泰安市", "base"},
                {371000, "威海市", "base"},
                {371100, "日照市", "base"},
                {371300, "临沂市", "base"},
                {371400, "德州市", "base"},
                {371500, "聊城市", "base"},
                {371600, "滨州市", "base"},
                {371700, "菏泽市", "base"},
        });
    }

    /**
     * 获取实时天气
     */
    private List<WeatherLivesDto> getWeatherLives() {

        List<WeatherLivesDto> weatherLives = new ArrayList<>();
//        RestTemplate restTemplate = new RestTemplate();

        Map<String,Object> map = new HashMap();
        map.put("key","88036e8e6a7f0d69cf8d38e84da278f5");

        Iterable<Object[]> params = getCityCodes();
        params.forEach(p->{

            map.put("city", p[0]);
            map.put("extensions", p[2]);
            URI uri = UriComponentsBuilder
                    .fromUriString("https://restapi.amap.com/v3/weather/weatherInfo?" +
                            "city={city}&key={key}&extensions={extensions}")
                    .build(map);
            WeatherDto weatherResponse = restTemplate.getForObject(uri, WeatherDto.class);

            if(weatherResponse.getStatus() != 1) {
                return;
            }
            //组装批量操作数据
            weatherResponse.getLives().forEach(m ->{
                weatherLives.add(
                    WeatherLivesDto.builder()
                        .province(m.getProvince())
                        .city(m.getCity())
                        .adcode(m.getAdcode())
                        .weather(m.getWeather())
                        .temperature(m.getTemperature())
                        .winddirection(m.getWinddirection())
                        .windpower(m.getWindpower())
                        .humidity(m.getHumidity())
                        .reporttime(m.getReporttime())
                        .build()
                );
            });
        });
        return weatherLives;
    }
}

