package com.yeshi.buwan.service.imp; import com.yeshi.buwan.dao.system.DetailSystemConfigDao; import com.yeshi.buwan.domain.system.DetailSystemConfig; import com.yeshi.buwan.domain.system.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 javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class DetailSystemConfigService { @Resource private DetailSystemConfigDao configDao; // 获取配置文件 public List getConfig(Long systemId, int version) { return configDao.listBySystemIdAndMaxVersion(systemId, version); } public DetailSystemConfig getConfig(int id) { return configDao.find(DetailSystemConfig.class, id); } public void saveConfig(DetailSystemConfig config) { configDao.save(config); } public List listConfig(Long systemId, String key) { return configDao.list(systemId, key); } public List listConfigByValue(Long systemId, String value) { return configDao.listByValue(systemId, value); } @Cacheable(value = "configCache", key = "'getConfigAsMap-'+#system.id+'-'+#version") public Map getConfigAsMap(DetailSystem system, int version) { Map map = new HashMap(); List list = getConfig(Long.parseLong(system.getId()), version); for (DetailSystemConfig cg : list) { map.put(cg.getKey(), cg.getValue()); } return map; } public DetailSystemConfig getConfigByKey(String key, DetailSystem system, int version) { return configDao.selectByKey(key, Long.parseLong(system.getId()), version); } @Cacheable(value = "configCache", key = "'getConfigValueByKey-'+#key+'-'+#detailSystemId+'-'+#version") public String getConfigValueByKey(String key, String detailSystemId, int version) { DetailSystemConfig detailSystemConfig = configDao.selectByKey(key, Long.parseLong(detailSystemId), version); if (detailSystemConfig == null) return null; return detailSystemConfig.getValue(); } // 更新某个配置文件 public void updateConfig(final DetailSystemConfig 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 cgs) { return (Boolean) configDao.excute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { try { session.getTransaction().begin(); for (DetailSystemConfig cg : cgs) { session.update(cg); } session.flush(); session.clear(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); return false; } return true; } }); } }