parent
dffb2aa293
commit
a5cd1d7fe1
@ -0,0 +1,37 @@
|
|||||||
|
package com.xjs.weather.controller;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import com.ruoyi.common.log.annotation.Log;
|
||||||
|
import com.ruoyi.common.security.annotation.RequiresLogin;
|
||||||
|
import com.xjs.weather.domain.NowWeather;
|
||||||
|
import com.xjs.weather.service.WeatherService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 天气控制器
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-01-16
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("weather")
|
||||||
|
@Api(tags = "业务模块-天气管理")
|
||||||
|
@Log4j2
|
||||||
|
public class WeatherController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WeatherService weatherService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
@ApiOperation("获取天气信息")
|
||||||
|
@Log(title = "获取天气")
|
||||||
|
@RequiresLogin
|
||||||
|
public R<NowWeather> getWeatherApiData() {
|
||||||
|
return R.ok(weatherService.saveNowWeather());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.xjs.weather.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.xjs.weather.domain.NowWeather;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* nowWeather mapper
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-01-16
|
||||||
|
*/
|
||||||
|
public interface NowWeatherMapper extends BaseMapper<NowWeather> {
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.xjs.weather.service;
|
||||||
|
|
||||||
|
import com.xjs.weather.domain.NowWeather;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 天气服务
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-01-16
|
||||||
|
*/
|
||||||
|
public interface WeatherService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存实时天气并放入缓存
|
||||||
|
* @return NowWeather
|
||||||
|
*/
|
||||||
|
NowWeather saveNowWeather();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.xjs.weather.service.impl;
|
||||||
|
|
||||||
|
import com.ruoyi.common.redis.service.RedisService;
|
||||||
|
import com.xjs.exception.BusinessException;
|
||||||
|
import com.xjs.weather.domain.NowWeather;
|
||||||
|
import com.xjs.weather.factory.WeatherFactory;
|
||||||
|
import com.xjs.weather.mapper.NowWeatherMapper;
|
||||||
|
import com.xjs.weather.service.WeatherService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.xjs.consts.RedisConst.NOW_WEATHER;
|
||||||
|
import static com.xjs.consts.RedisConst.NOW_WHEATHER_EXPIRE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 天气服务实现
|
||||||
|
*
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-01-16
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class WeatherServiceImpl implements WeatherService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WeatherFactory<NowWeather> gaodeNowWeatherFactory;
|
||||||
|
@Resource
|
||||||
|
private NowWeatherMapper nowWeatherMapper;
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public NowWeather saveNowWeather() {
|
||||||
|
if (!redisService.hasKey(NOW_WEATHER)) {
|
||||||
|
NowWeather nowWeather = gaodeNowWeatherFactory.weatherApi();
|
||||||
|
if (Objects.nonNull(nowWeather)) {
|
||||||
|
nowWeatherMapper.insert(nowWeather);
|
||||||
|
redisService.setCacheObject(NOW_WEATHER, nowWeather, NOW_WHEATHER_EXPIRE, TimeUnit.MINUTES);
|
||||||
|
return nowWeather;
|
||||||
|
} else {
|
||||||
|
throw new BusinessException("获取天气数据为空");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return (NowWeather) redisService.getCacheObject(NOW_WEATHER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue