jinpengyong
2022-01-17 3ddfa12fbc43e80e99e4959fbac8881eaa8e3ca3
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityWeather;
import com.moral.api.mapper.CityWeatherMapper;
import com.moral.api.service.CityWeatherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.Map;
 
/**
 * <p>
 * 城市实测小时气象数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-10-28
 */
@Service
public class CityWeatherServiceImpl extends ServiceImpl<CityWeatherMapper, CityWeather> implements CityWeatherService {
 
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    CityWeatherMapper cityWeatherMapper;
    @Override
    public Map<String, Object> queryWeatherByRegionCode(Integer regionCode) {
        Map<String,Object> value = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.CITY_WEATHER,String.valueOf(regionCode));
        if(value==null)
            value = queryCityWeatherByRegionCodeFromDB(regionCode);
        return value;
    }
 
    /**
    * @Description: 从数据库查询天气数据
            * @Param: [regionCode]
            * @return: java.util.Map<java.lang.String,java.lang.Object>
            * @Author: 陈凯裕
            * @Date: 2021/10/29
            */
    private Map<String, Object> queryCityWeatherByRegionCodeFromDB(Integer regionCode) {
        QueryWrapper<CityWeather> wrapper = new QueryWrapper();
        wrapper.eq("city_code",regionCode);
        wrapper.orderByDesc("time");
        wrapper.last(true,"limit 1");
        CityWeather weather = cityWeatherMapper.selectOne(wrapper);
        if(weather==null)
            return null;
        String value = weather.getValue();
        redisTemplate.opsForHash().put(RedisConstants.CITY_WEATHER,regionCode,value);
        return  JSON.parseObject(value,Map.class);
    }
}