package com.fzzy.igds.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.fzzy.igds.constant.RedisConst;
|
import com.fzzy.igds.domain.WeatherCity;
|
import com.fzzy.igds.domain.WeatherConf;
|
import com.fzzy.igds.domain.WeatherInfo;
|
import com.fzzy.igds.mapper.WeatherCityMapper;
|
import com.fzzy.igds.mapper.WeatherConfMapper;
|
import com.fzzy.igds.mapper.WeatherInfoMapper;
|
import com.fzzy.igds.utils.ContextUtil;
|
import com.ruoyi.common.core.redis.RedisCache;
|
import com.ruoyi.common.utils.StringUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
import org.springframework.stereotype.Service;
|
import javax.annotation.Resource;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* @Description
|
* @Author CZT
|
* @Date 2026/01/09 11:55
|
*/
|
@Slf4j
|
@Service
|
public class WeatherService {
|
|
@Resource
|
private WeatherInfoMapper weatherInfoMapper;
|
@Resource
|
private WeatherConfMapper weatherConfMapper;
|
@Resource
|
private WeatherCityMapper weatherCityMapper;
|
@Resource
|
private RedisCache redisCache;
|
|
/**
|
* 查询配置信息
|
*
|
* @param companyId
|
* @param deptId
|
* @return
|
*/
|
public List<WeatherConf> getConfData(String companyId, String deptId) {
|
if (StringUtils.isEmpty(companyId)) {
|
companyId = ContextUtil.getCompanyId();
|
}
|
if (StringUtils.isEmpty(deptId)) {
|
deptId = ContextUtil.subDeptId(null);
|
}
|
|
QueryWrapper<WeatherConf> queryWrapper = new QueryWrapper<>();
|
|
queryWrapper.eq("company_id", companyId);
|
|
queryWrapper.eq("dept_id", deptId);
|
|
return weatherConfMapper.selectList(queryWrapper);
|
}
|
|
/**
|
* 查询配置信息
|
*
|
* @return
|
*/
|
public List<WeatherConf> getConfData() {
|
QueryWrapper<WeatherConf> queryWrapper = new QueryWrapper<>();
|
return weatherConfMapper.selectList(queryWrapper);
|
}
|
|
/**
|
* 保存配置信息
|
*
|
* @param data
|
* @return
|
*/
|
public String saveConf(WeatherConf data) {
|
if (StringUtils.isEmpty(data.getId())) {
|
data.setId(ContextUtil.generateId());
|
data.setCompanyId(ContextUtil.getCompanyId());
|
data.setDeptId(ContextUtil.subDeptId(null));
|
}
|
if(StringUtils.isBlank(data.getCreateBy())){
|
data.setCreateBy(ContextUtil.getLoginUserName());
|
data.setCreateTime(new Date());
|
}
|
if(StringUtils.isBlank(data.getUpdateBy())){
|
data.setUpdateBy(ContextUtil.getLoginUserName());
|
data.setUpdateTime(new Date());
|
}
|
weatherConfMapper.insert(data);
|
return null;
|
}
|
|
/**
|
* 删除配置信息
|
*
|
* @param data
|
* @return
|
*/
|
public String delConf(WeatherConf data) {
|
weatherConfMapper.deleteById(data);
|
return null;
|
}
|
|
/**
|
* 保存气息信息
|
*
|
* @param data
|
*/
|
private void addWeatherInfo(WeatherInfo data) {
|
|
if(StringUtils.isBlank(data.getId())){
|
data.setId(ContextUtil.UUID());
|
}
|
if(StringUtils.isBlank(data.getCreateBy())){
|
data.setCreateBy(ContextUtil.getLoginUserName());
|
data.setCreateTime(new Date());
|
}
|
if(StringUtils.isBlank(data.getUpdateBy())){
|
data.setUpdateBy(ContextUtil.getLoginUserName());
|
data.setUpdateTime(new Date());
|
}
|
weatherInfoMapper.insert(data);
|
}
|
|
/**
|
* 分页查询数据
|
* @param page
|
* @param param
|
*/
|
public void getInfoData(Page<WeatherInfo> page, Map<String, Object> param) {
|
|
QueryWrapper<WeatherInfo> queryWrapper = new QueryWrapper<>();
|
|
queryWrapper.eq("company_id", ContextUtil.getCompanyId());
|
queryWrapper.eq("dept_id", ContextUtil.subDeptId(null));
|
|
weatherInfoMapper.selectPage(page, queryWrapper);
|
}
|
|
/**
|
* 分页查询数据
|
* @param page
|
* @param param
|
*/
|
public void pageCity(Page<WeatherCity> page, Map<String, Object> param) {
|
|
QueryWrapper<WeatherCity> queryWrapper = new QueryWrapper<>();
|
|
weatherCityMapper.selectPage(page, queryWrapper);
|
}
|
|
/**
|
* 判断气象信息是否需要保存
|
*
|
* @param weather
|
*/
|
public void updateCacheAndSave(WeatherInfo weather) {
|
if (null == weather.getCompanyId()) {
|
weather.setCompanyId(ContextUtil.getCompanyId());
|
}
|
try {
|
// 更新缓存
|
updateCacheWeather(weather);
|
// 判断是不是需要保存
|
if (this.isSave(weather.getUpdateTime())) {
|
addWeatherInfo(weather);
|
}
|
|
} catch (Exception e) {
|
String msg = e.getMessage();
|
if (msg.indexOf("PRIMARY") > 0) {
|
log.error("持久化气象信息异常,主键冲突,请注意更新时间,出现异常是否合理");
|
} else {
|
log.error("更新保存气象信息异常:", e);
|
}
|
}
|
|
}
|
|
|
/**
|
* 根据组织编码设置缓存
|
*
|
* @param weather
|
*/
|
public void updateCacheWeather(WeatherInfo weather) {
|
if (null == weather.getCompanyId()) {
|
weather.setCompanyId(ContextUtil.getCompanyId());
|
}
|
String key = RedisConst.buildKey(weather.getCompanyId(), RedisConst.KEY_WEATHER_INFO);
|
log.debug("气象信息更新到缓存中,info={}", weather.toString());
|
redisCache.setCacheObject(key, weather);
|
}
|
|
/**
|
* 根据cityId设置缓存
|
*
|
* @param weather
|
* @param cityId
|
*/
|
public void setCacheWeatherByCityId(WeatherInfo weather, String cityId) {
|
String key = RedisConst.buildKey(RedisConst.KEY_WEATHER_INFO, cityId);
|
|
log.debug("气象信息更新到缓存中,info={}", weather.toString());
|
redisCache.setCacheObject(key, weather, 60 * 30, TimeUnit.MICROSECONDS);
|
}
|
|
/**
|
* 根据cityId获取缓存信息
|
*
|
* @param cityId
|
* @return
|
*/
|
public WeatherInfo getCacheWeatherByCityId(String cityId) {
|
String key = RedisConst.buildKey(RedisConst.KEY_WEATHER_INFO, cityId);
|
WeatherInfo info = (WeatherInfo) redisCache.getCacheObject(key);
|
if (null == info) {
|
log.error("缓存中没有获取到气象信息!!");
|
}
|
return info;
|
}
|
|
/**
|
* 根据companyId获取缓存信息
|
*
|
* @param companyId
|
* @return
|
*/
|
public WeatherInfo getCacheWeather(String companyId) {
|
String key = RedisConst
|
.buildKey(companyId, RedisConst.KEY_WEATHER_INFO);
|
WeatherInfo info = (WeatherInfo) redisCache.getCacheObject(key);
|
if (null == info) {
|
log.error("缓存中没有获取到气象信息!!");
|
}
|
return info;
|
}
|
|
/**
|
* 创建连接
|
*
|
* @param address
|
* @param port
|
*/
|
public void onCreate(String address, Integer port) {
|
log.debug("气象站自动连接通知暂不处理……");
|
}
|
|
/**
|
* 销毁链接
|
*
|
* @param address
|
* @param port
|
*/
|
public void onDestroy(String address, Integer port) {
|
log.debug("气象站自动断开通知暂不处理……");
|
}
|
|
/**
|
* 判断当前气象信息是否需要持久化,默认系统只保存在上午8点 和下午3点的气象信。
|
*
|
* @param updateTime
|
* @return
|
*/
|
public static boolean isSave(Date updateTime) {
|
|
String tag = DateFormatUtils.format(updateTime, "HHmm");
|
String tag1 = "0800";
|
String tag2 = "1500";
|
|
if (tag.equals(tag1) || tag.equals(tag2)) {
|
return true;
|
}
|
|
return false;
|
}
|
}
|