#
vincentlu
2025-05-13 ebd2f4397a92c6a5096de1b86d59154363344720
zy-acs-manager/src/main/java/com/zy/acs/manager/system/service/impl/ConfigServiceImpl.java
@@ -4,15 +4,16 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.acs.common.utils.GsonUtils;
import com.zy.acs.framework.common.DateUtils;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.system.entity.Config;
import com.zy.acs.manager.system.enums.ConfigType;
import com.zy.acs.manager.system.mapper.ConfigMapper;
import com.zy.acs.manager.system.service.ConfigService;
import com.zy.acs.framework.exception.CoolException;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
 * Created by vincent on 8/30/2024
@@ -20,18 +21,31 @@
@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) {
        List<Config> list = this.list(new LambdaQueryWrapper<Config>().eq(Config::getFlag, key));
        Config config = list.stream().findFirst().orElse(null);
        if (null == config) {
            return null;
        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().toUpperCase().equals("TRUE")) {
                if (val.equals("1") || val.trim().equalsIgnoreCase("TRUE")) {
                    return (T) Boolean.TRUE;
                }
                return (T) Boolean.FALSE;
@@ -57,4 +71,52 @@
        }
    }
    @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);
    }
}