admin
2019-02-22 e358583d644fa39fc1e93b14b3cafff3644980e4
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.yeshi.fanli.system.service.impl.common;
 
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.fanli.facade.system.entity.common.SystemConfig;
import org.fanli.facade.system.entity.common.SystemManage;
import org.fanli.facade.system.service.common.SystemConfigService;
import org.fanli.facade.system.service.common.SystemManageService;
import org.springframework.cache.annotation.Cacheable;
 
import com.alibaba.dubbo.config.annotation.Service;
import com.yeshi.fanli.base.Constant;
import com.yeshi.fanli.base.exception.NotExistObjectException;
import com.yeshi.fanli.system.dao.common.SystemConfigDao;
import com.yeshi.fanli.system.utils.SystemCommonUtils;
 
@Service(version = "1.0.0")
public class SystemConfigServiceImpl implements SystemConfigService{
 
    @Resource
    private SystemConfigDao systemConfigDao;
    
    @Resource
    private SystemManageService systemManageService;
    
    public int getCount(SystemManage system,String key) {
        Long lcount;
        if(system!=null){
            lcount = systemConfigDao.getCount("select count(sc.id) from SystemConfig sc left join sc.systems s  where s.id=? and sc.name like ?",new Serializable[]{system.getId(),"%"+key+"%"});
        }else{
            lcount = systemConfigDao.getCount("select count(sc.id) from SystemConfig sc where sc.name like ?",new Serializable[]{"%"+key+"%"});
        }
        return lcount.intValue();
    }
 
    public List<SystemConfig> getSystemConfigList(int index,SystemManage system, String key) {
        
        int start =index * Constant.PAGE_SIZE;
        List<SystemConfig> list;
        if(system != null){
            list = systemConfigDao.list("from SystemConfig sc left outer join fetch sc.systems s where s.id = ? and sc.name like ?",start,Constant.PAGE_SIZE,new Serializable[]{system.getId(),"%"+key+"%"});
            StringBuffer hql=new StringBuffer("from SystemConfig sc ");
            Serializable[] params = new Serializable[list.size()];
            int ii=0;
            for (SystemConfig sc : list) {
                if(ii==0){
                    hql.append(" where sc. id = ? ");    
                }else{
                    hql.append(" or sc. id = ? ");
                }
                params[ii]=sc.getId();
                ii++;
            }
            list=systemConfigDao.list(hql.toString(),params);
        }else{
            list = systemConfigDao.list("from SystemConfig sc where sc.name like ?",start,Constant.PAGE_SIZE,new Serializable[]{"%"+key+"%"});
        }
        
        return list;
    }
 
    public void addSystemConfig(SystemConfig sc) {
        Set<SystemManage> systems = sc.getSystems();
        Set<SystemManage> newSystems=new HashSet<SystemManage>();
        for (SystemManage system : systems) {
            String platform = SystemCommonUtils.getMap().get(String.valueOf(system.getPlatform()));
            String packages = system.getPackageName();
            SystemManage find = systemManageService.getSystem(platform, packages);
            if(find != null){
                newSystems.add(find);
            }
        }
        sc.setSystems(newSystems);
        sc.setUpdatetime(java.lang.System.currentTimeMillis());
        systemConfigDao.save(sc);
    }
 
    public void deleteSystem(long id, SystemManage system) throws NotExistObjectException {
        SystemConfig find = systemConfigDao.find(SystemConfig.class, id);
        if(find==null){
            throw new NotExistObjectException("不存在该子系统");
        }
        find.getSystems().remove(system);
        find.setUpdatetime(java.lang.System.currentTimeMillis());
        systemConfigDao.update(find);
    }
 
    public void addSystem(long id, SystemManage system) throws NotExistObjectException {
        SystemConfig find = systemConfigDao.find(SystemConfig.class, id);
        if(find==null){
            throw new NotExistObjectException("不存在该子系统");
        }
        find.getSystems().add(system);
        find.setUpdatetime(java.lang.System.currentTimeMillis());
        systemConfigDao.update(find);
    }
 
    public SystemConfig getSystemConfig(long id) {
        
        return systemConfigDao.find(SystemConfig.class, id);
    }
 
    public void updateSystemConfig(SystemConfig sc) throws NotExistObjectException {
        SystemConfig find = systemConfigDao.find(SystemConfig.class, sc.getId());
        if(find==null){
            throw new NotExistObjectException("不存在该对象");
        }
        sc.setUpdatetime(java.lang.System.currentTimeMillis());
        Set<SystemManage> newSystems=new HashSet<SystemManage>();
        Set<SystemManage> systems = sc.getSystems();
        for (SystemManage system : systems) {
            String platform = SystemCommonUtils.getMap().get(String.valueOf(system.getPlatform()));
            String packages = system.getPackageName();
            SystemManage sfind = systemManageService.getSystem(platform, packages);
            if(sfind != null){
                newSystems.add(sfind);
            }
        }
        sc.setSystems(newSystems);
        systemConfigDao.update(sc);
    }
 
    public void deleteSystemConfig(long id) {
        systemConfigDao.delete(new SystemConfig(id));
    }
    @Cacheable(value={"childSystemCache"}, key="#p0")
    public String get(String sigkey) throws NotExistObjectException {
        List<SystemConfig> list = systemConfigDao.list("from SystemConfig sc where sc.key = ?", new Serializable[]{sigkey});
        if(list.size() ==0 ){
            throw new NotExistObjectException("不存在该参数"); 
        }
        return list.get(0).getValue();
    }
    @Cacheable(value={"childSystemCache"}, key="#p0+#system.id")
    public String get(String key, SystemManage system) {
        if(system==null || system.getId()==0){
            return "";
        }
        List<SystemConfig> list = systemConfigDao.list("select sc from SystemConfig sc join sc.systems syss where sc.key = ? and syss.id=?", new Serializable[]{key,system.getId()});
        if(list.size() != 0){
            return list.get(0).getValue();
        }
        return "";
    }
 
}