add
heheng
2025-06-10 942bdeee0b6fcc92b35e788c851d39c5182a8e40
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
package com.gkhy.exam.system.service.impl;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.constant.CacheConstant;
import com.gkhy.exam.common.constant.UserConstant;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.RedisUtils;
import com.gkhy.exam.common.utils.StringUtils;
import com.gkhy.exam.system.domain.SysConfig;
import com.gkhy.exam.system.mapper.SysConfigMapper;
import com.gkhy.exam.system.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * <p>
 * 系统配置表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2023-11-13 08:39:55
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {
    @Autowired
    private RedisUtils redisUtils;
 
    @Override
    public SysConfig selectConfigById(Long configId) {
        return baseMapper.selectById(configId);
    }
 
    @Override
    public String selectConfigByKey(String configKey) {
        String redisKey=getCacheKey(configKey);
        String configValue= (String) redisUtils.get(redisKey);
        if(StrUtil.isNotEmpty(configValue)){
            return configValue;
        }
        SysConfig retConfig=baseMapper.getConfig(new SysConfig().setConfigKey(configKey));
        if(ObjectUtil.isNotNull(retConfig)){
            redisUtils.set(redisKey,retConfig.getConfigValue());
            return retConfig.getConfigValue();
        }
        return StrUtil.EMPTY;
    }
 
    @Override
    public boolean selectCaptchaEnabled() {
        String captchaEnabled = selectConfigByKey("sys.account.captchaEnabled");
        if (StringUtils.isEmpty(captchaEnabled))
        {
            return true;
        }
        return Convert.toBool(captchaEnabled);
    }
 
    @Override
    public CommonPage selectConfigList(SysConfig config) {
        PageUtils.startPage();
        List<SysConfig> sysConfigList=baseMapper.selectConfigList(config);
        return CommonPage.restPage(sysConfigList);
    }
 
    @Override
    public int insertConfig(SysConfig config) {
        int row=baseMapper.insert(config);
        if(row>0){
            redisUtils.set(getCacheKey(config.getConfigKey()),config.getConfigValue());
        }
        return row;
    }
 
    @Override
    public int updateConfig(SysConfig config) {
        SysConfig temp=selectConfigById(config.getId());
        if(ObjectUtil.equals(temp.getConfigKey(),config.getConfigKey())){
            redisUtils.del(getCacheKey(temp.getConfigKey()));
        }
        int row=baseMapper.updateById(config);
        if(row>0){
            redisUtils.set(getCacheKey(config.getConfigKey()),config.getConfigValue());
        }
        return row;
    }
 
    @Override
    public void deleteConfigByIds(Long[] configIds) {
        for(Long configId:configIds){
            SysConfig config=selectConfigById(configId);
            if(ObjectUtil.equals(UserConstant.YES,config.getType())){
                throw new ApiException(String.format("内置参数[%1$s]不能删除",config.getConfigKey()));
            }
            baseMapper.deleteById(configId);
            redisUtils.del(getCacheKey(config.getConfigKey()));
        }
    }
 
    @Override
    public void resetConfigCache() {
 
    }
 
    @Override
    public boolean checkConfigKeyUnique(SysConfig config) {
        return false;
    }
 
 
 
    private String getCacheKey(String configKey){
        return CacheConstant.SYS_CONFIG_KEY+configKey;
    }
}