package com.yeshi.buwan.service.imp;
|
|
import java.io.Serializable;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
|
import com.yeshi.buwan.domain.DetailSystem;
|
import org.hibernate.HibernateException;
|
import org.hibernate.Session;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.orm.hibernate4.HibernateCallback;
|
import org.springframework.stereotype.Service;
|
|
import com.yeshi.buwan.dao.ConfigDao;
|
import com.yeshi.buwan.domain.Config;
|
|
@Service
|
public class ConfigService {
|
@Resource
|
private ConfigDao configDao;
|
|
public ConfigDao getConfigDao() {
|
return configDao;
|
}
|
|
public void setConfigDao(ConfigDao configDao) {
|
this.configDao = configDao;
|
}
|
|
// 获取配置文件
|
public List<Config> getConfig(Long systemId, int version) {
|
return configDao.listBySystemIdAndMaxVersion(systemId, version);
|
}
|
|
@Cacheable(value = "configCache", key = "'getConfigAsMap-'+#system.id+'-'+#version")
|
public Map<String, String> getConfigAsMap(DetailSystem system, int version) {
|
Map<String, String> map = new HashMap<String, String>();
|
List<Config> list = getConfig(Long.parseLong(system.getId()), version);
|
for (Config cg : list) {
|
map.put(cg.getKey(), cg.getValue());
|
}
|
return map;
|
}
|
|
public Config getConfigByKey(String key, DetailSystem system, int version) {
|
return configDao.selectByKey(key, Long.parseLong(system.getId()), version);
|
}
|
|
// 更新某个配置文件
|
public void updateConfig(final Config cg) {
|
configDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
session.createSQLQuery("update wk_video_config set `value`=? where id=?")
|
.setParameter(0, cg.getValue()).setParameter(1, cg.getId()).executeUpdate();
|
session.flush();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
});
|
}
|
|
public boolean updateConfigList(final List<Config> cgs) {
|
return (Boolean) configDao.excute(new HibernateCallback() {
|
public Object doInHibernate(Session session) throws HibernateException {
|
try {
|
session.getTransaction().begin();
|
for (Config cg : cgs) {
|
session.update(cg);
|
}
|
session.flush();
|
session.clear();
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
session.getTransaction().rollback();
|
return false;
|
}
|
return true;
|
}
|
});
|
}
|
}
|