admin
2024-10-17 b30fb8afd3cd6228bda9b182dc412bb3c8daf69c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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<DetailSystemConfig> 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<DetailSystemConfig> listConfig(Long systemId, String key) {
        return configDao.list(systemId, key);
    }
 
    public List<DetailSystemConfig> listConfigByValue(Long systemId, String value) {
        return configDao.listByValue(systemId, value);
    }
 
    @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<DetailSystemConfig> 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<DetailSystemConfig> 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;
            }
        });
    }
}