zjj
3 天以前 d5540413a0163383b53d33aea1be93390ea9149c
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
package com.vincent.rsf.server.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.common.utils.GsonUtils;
import com.vincent.rsf.framework.common.DateUtils;
import com.vincent.rsf.server.system.entity.Config;
import com.vincent.rsf.server.system.enums.ConfigType;
import com.vincent.rsf.server.system.enums.StatusType;
import com.vincent.rsf.server.system.mapper.ConfigMapper;
import com.vincent.rsf.server.system.service.ConfigService;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * Created by vincent on 8/30/2024
 */
@Service("configService")
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {
 
    public static final Map<String, Config> CONFIG_CACHE = new ConcurrentHashMap<>();
 
    @PostConstruct
    public void init() {
        List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getStatus, StatusType.ENABLE.val));
        for (Config config : list) {
            CONFIG_CACHE.put(config.getFlag(), config);
        }
    }
 
    @Override
    @SuppressWarnings("unchecked")
    public <T> T getVal(String key, Class<T> clazz) {
        Config config = CONFIG_CACHE.get(key);
        if (config == null) {
            List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
            config = list.stream().findFirst().orElse(null);
            if (null == config) {
                return null;
            }
        }
        String val = config.getVal();
        switch (ConfigType.query(config.getType())) {
            case BOOLEAN:
                if (val.equals("1") || val.trim().equalsIgnoreCase("TRUE")) {
                    return (T) Boolean.TRUE;
                }
                return (T) Boolean.FALSE;
            case NUMBER:
                if (clazz == Integer.class) {
                    return (T) Integer.valueOf(val);
                } else if (clazz == Short.class) {
                    return (T) Short.valueOf(val);
                } else if (clazz == Long.class) {
                    return (T) Long.valueOf(val);
                } else if (clazz == Double.class) {
                    return (T) Double.valueOf(val);
                }
                throw new UnsupportedOperationException("Unsupported type: " + clazz.getName());
            case STRING:
                return (T) val;
            case JSON:
                return GsonUtils.fromJson(val, clazz);
            case DATE:
                return (T) DateUtils.convert(val);
            default:
                return null;
        }
    }
 
    @Override
    public <T> boolean setVal(String key, T val) {
        Config config = CONFIG_CACHE.get(key);
        if (config == null) {
            List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
            config = list.stream().findFirst().orElse(null);
            if (null == config) {
                return false;
            }
        }
 
        ConfigType configType = ConfigType.query(config.getType());
        switch (configType) {
            case BOOLEAN:
                if (!(val instanceof Boolean)) {
                    throw new IllegalArgumentException("Expected Boolean value for key: " + key);
                }
                config.setVal((Boolean) val ? "TRUE" : "FALSE");
                break;
            case NUMBER:
                if (val instanceof Integer || val instanceof Short || val instanceof Long || val instanceof Double) {
                    config.setVal(String.valueOf(val));
                } else {
                    throw new IllegalArgumentException("Expected a numeric value for key: " + key);
                }
                break;
            case STRING:
                if (!(val instanceof String)) {
                    throw new IllegalArgumentException("Expected a String value for key: " + key);
                }
                config.setVal((String) val);
                break;
            case JSON:
                config.setVal(GsonUtils.toJson(val));
                break;
            case DATE:
                if (!(val instanceof Date)) {
                    throw new IllegalArgumentException("Expected a Date value for key: " + key);
                }
                config.setVal(DateUtils.convert((Date) val));
                break;
            default:
                throw new UnsupportedOperationException("Unsupported ConfigType: " + configType);
        }
 
        return this.updateById(config);
    }
 
}