swb
2024-09-10 9367b86590a9774cd1e4f5d02d9f394605fe6a7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityConfigWeatherForecast;
import com.moral.api.entity.CityWeatherForecast;
import com.moral.api.mapper.CityWeatherForecastMapper;
import com.moral.api.service.CityConfigWeatherForecastService;
import com.moral.api.service.CityWeatherForecastService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.DateUtils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 城市预测气象数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-12-30
 */
@Service
public class CityWeatherForecastServiceImpl extends ServiceImpl<CityWeatherForecastMapper, CityWeatherForecast> implements CityWeatherForecastService {
 
    @Autowired
    private CityConfigWeatherForecastService cityConfigWeatherForecastService;
 
    @Autowired
    private CityWeatherForecastMapper cityWeatherForecastMapper;
 
 
    //城市预测气象数据来源于,和风天气,逐小时天气预报(未来72小时)商业版
    @Override
    public void insertCityWeatherForecast() {
        RestTemplate restTemplate = new RestTemplate(
                new HttpComponentsClientHttpRequestFactory()); // 使用HttpClient,支持GZIP
        restTemplate.getMessageConverters().set(1,
                new StringHttpMessageConverter(StandardCharsets.UTF_8));
        Date nextDay = DateUtils.addDays(new Date(), 1);
        String nextTime = DateUtils.dateToDateString(nextDay, DateUtils.yyyy_MM_dd_EN);
        //获取城市配置
        QueryWrapper<CityConfigWeatherForecast> wrapper = new QueryWrapper<>();
        wrapper.select("city_code", "location_id").eq("is_delete", Constants.NOT_DELETE);
        List<CityConfigWeatherForecast> list = cityConfigWeatherForecastService.list(wrapper);
 
        List<CityWeatherForecast> cityWeatherForecasts = new ArrayList<>();
 
        for (CityConfigWeatherForecast cityConfigWeatherForecast : list) {
            Integer cityCode = cityConfigWeatherForecast.getCityCode();
            Integer locationId = cityConfigWeatherForecast.getLocationId();
            //Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, locationId);
            Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/72h?key=5c27e3442bee4a0891eae34afca125f3&gzip=n&location={1}", Map.class, locationId);
            //Map<String, Object> data = restTemplate.getForObject(url,Map.class);
            List<Map<String, Object>> hourly = (List<Map<String, Object>>) data.get("hourly");
            for (Map<String, Object> hourlyMap : hourly) {
                String fxTime = hourlyMap.get("fxTime").toString();
                String dayTime = fxTime.split("T")[0];
                if (dayTime.equals(nextTime)) {
                    String hourTime = fxTime.substring(0, 17).replaceAll("T", " ");
                    CityWeatherForecast cityWeatherForecast = new CityWeatherForecast();
                    cityWeatherForecast.setCityCode(cityCode);
                    cityWeatherForecast.setTime(DateUtils.getDate(hourTime, DateUtils.yyyy_MM_dd_HH_EN));
                    cityWeatherForecast.setValue(JSONObject.toJSONString(hourlyMap));
                    cityWeatherForecasts.add(cityWeatherForecast);
                }
            }
        }
        cityWeatherForecastMapper.insertCityWeatherForecast(cityWeatherForecasts);
    }
 
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate(
                new HttpComponentsClientHttpRequestFactory()); // 使用HttpClient,支持GZIP
        restTemplate.getMessageConverters().set(1,
                new StringHttpMessageConverter(StandardCharsets.UTF_8));
       /* String url = "https://devapi.qweather.com/v7/weather/24h?location=101010100&key=2430ab9e636c4950a686fbd84e3ccb3a"; //此处换为自己的地址
 
        Map<String, Object> data = restTemplate.getForObject(url,Map.class);*/
        /*RestTemplate restTemplate = new RestTemplate();*/
        Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/72h?key=5c27e3442bee4a0891eae34afca125f3&gzip=n&location={1}", Map.class, 101010100);
 
        List<Map<String, Object>> hourly = (List<Map<String, Object>>) data.get("hourly");
        int i = 0;
 
 
 
 
        //String url = "https://devapi.qweather.com/v7/weather/24h?location=101010100&key=2430ab9e636c4950a686fbd84e3ccb3a"; //此处换为自己的地址
      //  String response = restTemplate.getForObject(url,String.class);
 
 
 
    }
 
}