package com.ld.igds.weather;
|
|
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
|
import com.bstek.dorado.data.provider.Page;
|
import com.ld.igds.constant.RedisConst;
|
import com.ld.igds.models.WeatherCity;
|
import com.ld.igds.models.WeatherConf;
|
import com.ld.igds.models.WeatherInfo;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.igds.util.RedisUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.hibernate.Session;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author: andy.jia
|
* @description: 气象信息业务处理
|
* @version:
|
* @data:2019年12月7日
|
*/
|
@Slf4j
|
@Component(CoreWeatherService.BEAN_ID)
|
public class CoreWeatherServiceImpl extends HibernateDao implements CoreWeatherService {
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
public void updateCacheWeather(WeatherInfo weather) {
|
if (null == weather.getCompanyId()) {
|
weather.setCompanyId(ContextUtil.getDefaultCompanyId());
|
}
|
String key = RedisConst.buildKey(weather.getCompanyId(), RedisConst.KEY_WEATHER_INFO);
|
|
log.debug("气象信息更新到缓存中,info={}", weather.toString());
|
redisUtil.set(key, weather);
|
}
|
|
@Override
|
public WeatherInfo getCacheWeather(String companyId) {
|
String key = RedisConst
|
.buildKey(companyId, RedisConst.KEY_WEATHER_INFO);
|
WeatherInfo info = (WeatherInfo) redisUtil.get(key);
|
if (null == info) {
|
log.error("缓存中没有获取到气象信息!!");
|
}
|
return info;
|
}
|
|
@Override
|
public void onCreate(String address, Integer port) {
|
log.debug("气象站自动连接通知暂不处理……");
|
}
|
|
@Override
|
public void onDestroy(String address, Integer port) {
|
log.debug("气象站自动断开通知暂不处理……");
|
}
|
|
@Override
|
public void updateCacheAndSave(WeatherInfo weather) {
|
if (null == weather.getCompanyId()) {
|
weather.setCompanyId(ContextUtil.getDefaultCompanyId());
|
}
|
try {
|
// 更新缓存
|
updateCacheWeather(weather);
|
// 判断是不是需要保存
|
if (WeatherUtil.isSave(weather.getUpdateTime())) {
|
addWeatherInfo(weather);
|
}
|
|
} catch (Exception e) {
|
String msg = e.getMessage();
|
if (msg.indexOf("PRIMARY") > 0) {
|
log.error("持久化气象信息异常,主键冲突,请注意更新时间,出现异常是否合理");
|
} else {
|
log.error("更新保存气象信息异常:", e);
|
}
|
}
|
|
}
|
|
private void addWeatherInfo(WeatherInfo data) {
|
Session session = this.getSessionFactory().openSession();
|
try {
|
session.save(data);
|
} finally {
|
session.flush();
|
session.close();
|
}
|
}
|
|
@Override
|
public List<WeatherConf> getConfData(String companyId, String deptId) {
|
String hql = " from " + WeatherConf.class.getName() + " where 1=1 ";
|
Map<String, Object> args = new HashMap<>();
|
if (StringUtils.isNotEmpty(companyId)) {
|
hql += " and companyId=:companyId ";
|
args.put("companyId", companyId);
|
}
|
if (StringUtils.isNotEmpty(deptId)) {
|
hql += " and deptId=:deptId ";
|
args.put("deptId", deptId);
|
}
|
return this.query(hql, args);
|
}
|
|
@Override
|
public String saveConf(WeatherConf data) {
|
Session session = this.getSessionFactory().openSession();
|
try {
|
if (StringUtils.isEmpty(data.getId())) {
|
data.setId(ContextUtil.getCurTimeMillis());
|
data.setCompanyId(ContextUtil.getCompanyId());
|
data.setDeptId(ContextUtil.subDeptId(null));
|
session.save(data);
|
} else {
|
session.update(data);
|
}
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
@Override
|
public String delConf(WeatherConf data) {
|
Session session = this.getSessionFactory().openSession();
|
try {
|
session.delete(data);
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
@Override
|
public void getInfoData(Page<WeatherInfo> page) throws Exception {
|
String hql = " from " + WeatherInfo.class.getName()
|
+ " where companyId=:companyId ";
|
|
Map<String, Object> args = new HashMap<String, Object>();
|
args.put("companyId", ContextUtil.getCompanyId());
|
|
String coutSql = "select count(1) " + hql;
|
|
hql += " order by updateTime desc";
|
|
this.pagingQuery(page, hql, coutSql, args);
|
}
|
|
@Override
|
public void pageCity(Page<WeatherCity> page, String key) throws Exception {
|
String hql = " from " + WeatherCity.class.getName()
|
+ " where 1=1 ";
|
Map<String, Object> args = new HashMap<>();
|
if (StringUtils.isNotEmpty(key)) {
|
hql += " and (cityEn like:cityEn or cityZh like:cityZh)";
|
args.put("cityEn", "%" + key + "%");
|
args.put("cityZh", "%" + key + "%");
|
}
|
String countHql = "select count(1) " + hql;
|
|
if (args.isEmpty()) {
|
pagingQuery(page, hql, countHql);
|
} else {
|
pagingQuery(page, hql, countHql, args);
|
}
|
}
|
|
}
|