admin
2021-11-19 7511509c68bd2892aad48a0612d497387660214d
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
package com.yeshi.location.app.service.impl.config;
 
import com.yeshi.location.app.dao.config.SystemConfigDao;
import com.yeshi.location.app.dao.config.SystemConfigDao.DaoQuery;
import com.yeshi.location.app.entity.SystemEnum;
import com.yeshi.location.app.entity.config.SystemConfig;
import com.yeshi.location.app.entity.config.SystemConfigKey;
import com.yeshi.location.app.service.inter.config.SystemConfigService;
import com.yeshi.location.app.service.query.config.SystemConfigQuery;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.yeshi.utils.bean.BeanUtil;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
@Service
public class SystemConfigServiceImpl implements SystemConfigService {
 
    @Resource
    private SystemConfigDao systemConfigDao;
 
    @Cacheable(key = "configKey", value = "'getValueCache-'+#system+'-'+#key")
    @Override
    public String getValueCache(SystemEnum system, SystemConfigKey key) {
        DaoQuery daoQuery = new DaoQuery();
        daoQuery.system = system;
        daoQuery.key = key;
        daoQuery.count = 1;
        List<SystemConfig> list = systemConfigDao.list(daoQuery);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0).getValue();
    }
 
    @Override
    public List<SystemConfig> list(SystemConfigQuery systemConfigQuery, int page, int pageSize) {
        DaoQuery daoQuery = new DaoQuery();
        try {
            BeanUtil.copyProperties(systemConfigQuery, daoQuery);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        daoQuery.start = (page - 1) * pageSize;
        daoQuery.count = pageSize;
        return systemConfigDao.list(daoQuery);
    }
 
    @Override
    public long count(SystemConfigQuery systemConfigQuery) {
        DaoQuery daoQuery = new DaoQuery();
        try {
            BeanUtil.copyProperties(systemConfigQuery, daoQuery);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return systemConfigDao.count(daoQuery);
    }
 
    @Override
    public SystemConfig get(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        return systemConfigDao.findOne(query);
    }
 
    @Override
    public void add(SystemConfig systemConfig) throws Exception {
 
        if (systemConfig.getId() == null) {
            systemConfig.setId(systemConfig.getSystem().name() + "-" + systemConfig.getKey().name());
        }
 
 
        //查询主键ID是否存在
        if (systemConfigDao.get(systemConfig.getId()) != null) {
            throw new Exception("已存在");
        }
 
        if (systemConfig.getCreateTime() == null) {
            systemConfig.setCreateTime(new Date());
        }
        //保存
        systemConfigDao.save(systemConfig);
    }
 
    @Override
    public void save(SystemConfig config) {
        if (config.getId() == null) {
            config.setId(config.getSystem().name() + "-" + config.getKey().name());
        }
        if (systemConfigDao.get(config.getId()) != null) {
            update(config);
        } else {
            if (config.getCreateTime() == null) {
                config.setCreateTime(new Date());
            }
            systemConfigDao.save(config);
        }
    }
 
    @Override
    public void update(SystemConfig systemConfig) {
        if (systemConfig.getUpdateTime() == null) {
            systemConfig.setUpdateTime(new Date());
        }
        //更新
        systemConfigDao.updateSelective(systemConfig);
    }
 
    @Override
    public void delete(List<String> idList) {
        for (String id : idList) {
            systemConfigDao.delete(id);
        }
    }
 
 
}