screen-api/src/main/java/com/moral/api/controller/WeatherController.java | ●●●●● patch | view | raw | blame | history | |
screen-api/src/main/java/com/moral/api/service/CityWeatherService.java | ●●●●● patch | view | raw | blame | history | |
screen-api/src/main/java/com/moral/api/service/impl/CityWeatherServiceImpl.java | ●●●●● patch | view | raw | blame | history |
screen-api/src/main/java/com/moral/api/controller/WeatherController.java
@@ -1,12 +1,17 @@ package com.moral.api.controller; import com.moral.api.entity.CityWeather; import com.moral.api.service.CityWeatherService; import com.moral.constant.ResultMessage; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Map; /** * @ClassName WeatherController @@ -16,13 +21,25 @@ * @Version TODO **/ @Slf4j @Api(tags = {"用户管理"}) @Api(tags = {"天气"}) @RestController @RequestMapping("/weather") @CrossOrigin(origins = "*", maxAge = 3600) public class WeatherController { @Autowired CityWeatherService cityWeatherService; /** * @Description: 根据地区码查询天气 * @Param: [regionCode] * @return: com.moral.constant.ResultMessage * @Author: 陈凯裕 * @Date: 2021/10/29 */ @GetMapping("queryByRegionCode") public ResultMessage queryByRegionCode(String regionCode){ return null; public ResultMessage queryByRegionCode(Integer regionCode){ Map<String, Object> value = cityWeatherService.queryWeatherByRegionCode(regionCode); return ResultMessage.ok(value); } } screen-api/src/main/java/com/moral/api/service/CityWeatherService.java
New file @@ -0,0 +1,25 @@ package com.moral.api.service; import com.moral.api.entity.CityWeather; import com.baomidou.mybatisplus.extension.service.IService; import java.util.Map; /** * <p> * 城市实测小时气象数据表 服务类 * </p> * * @author moral * @since 2021-10-28 */ public interface CityWeatherService extends IService<CityWeather> { /** * @Description: 根据地区码查询天气 * @Param: [regionCode] * @return: java.util.Map<java.lang.String,java.lang.Object> * @Author: 陈凯裕 * @Date: 2021/10/29 */ Map<String,Object> queryWeatherByRegionCode(Integer regionCode); } screen-api/src/main/java/com/moral/api/service/impl/CityWeatherServiceImpl.java
New file @@ -0,0 +1,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); } }