cjl
2023-08-28 65c692a8174659c446384bcfeb967ce62600d58c
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityConfigWeather;
import com.moral.api.entity.CityWeather;
import com.moral.api.mapper.CityWeatherMapper;
import com.moral.api.service.CityConfigWeatherService;
import com.moral.api.service.CityWeatherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 城市实测小时气象数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-10-11
 */
@Service
public class CityWeatherServiceImpl extends ServiceImpl<CityWeatherMapper, CityWeather> implements CityWeatherService {
 
    @Autowired
    private CityWeatherMapper cityWeatherMapper;
 
    @Autowired
    private CityConfigWeatherService cityConfigWeatherService;
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    //城市气象数据来源于,和风天气,实时天气商业版
    @Override
    public void insertCityWeather() {
        //获取城市配置
        QueryWrapper<CityConfigWeather> wrapper = new QueryWrapper<>();
        wrapper.select("city_code", "location_id").eq("is_delete", Constants.NOT_DELETE);
        List<CityConfigWeather> list = cityConfigWeatherService.list(wrapper);
 
        List<CityWeather> cityWeathers = new ArrayList<>();
 
        for (CityConfigWeather cityConfigWeather : list) {
            CityWeather cityWeather = new CityWeather();
            Integer cityCode = cityConfigWeather.getCityCode();
            Integer locationId = cityConfigWeather.getLocationId();
            Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/now?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, locationId);
            Map<String, Object> now = (Map<String, Object>) data.get("now");
            cityWeather.setCityCode(cityCode);
            //风速km/h->m/s
            double windSpeed = Double.parseDouble(now.get("windSpeed").toString());
            windSpeed = windSpeed * 1000 / 3600;
            String format = String.format("%.1f", windSpeed);
            now.put("windSpeed", format);
            cityWeather.setValue(JSONObject.toJSONString(now));
            Date time = DateUtils.dataToTimeStampTime(new Date(), DateUtils.yyyy_MM_dd_HH_EN);
            cityWeather.setTime(time);
            cityWeathers.add(cityWeather);
            //存入redis
            redisTemplate.opsForHash().put(RedisConstants.CITY_WEATHER, String.valueOf(cityCode), now);
        }
        cityWeatherMapper.insertCityWeather(cityWeathers);
 
    }
 
    @Override
    public CityWeather getDataByCityCodeAndTime(String cityCode, String time) {
        QueryWrapper wrapper_cityWeather = new QueryWrapper();
        wrapper_cityWeather.eq("city_code", cityCode);
        wrapper_cityWeather.eq("time", time);
        CityWeather cityWeather = new CityWeather();
        if (cityWeatherMapper.selectCount(wrapper_cityWeather) == 1) {
            cityWeather = cityWeatherMapper.selectOne(wrapper_cityWeather);
        }
        return cityWeather;
    }
 
    @Override
    public List<CityWeather> getCityWeather() {
        String time = DateUtils.getDateStringOfHour(-1, DateUtils.yyyy_MM_dd_HH_EN) + ":00:00";
        QueryWrapper<CityWeather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("time", time);
        return cityWeatherMapper.selectList(queryWrapper);
    }
}