| | |
| | | package com.zy.system.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.zy.common.entity.Parameter; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import com.zy.core.network.fake.FakeConfigKeys; |
| | | import com.zy.system.entity.Config; |
| | | import com.zy.system.mapper.ConfigMapper; |
| | | import com.zy.system.service.ConfigService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | @Service("configService") |
| | | public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService { |
| | | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | @Override |
| | | public String getConfigValue(String code, String defaultValue) { |
| | | if (code == null || code.trim().isEmpty()) { |
| | | return defaultValue; |
| | | } |
| | | Config config = getOne(new QueryWrapper<Config>().eq("code", code).last("limit 1")); |
| | | if (config == null || config.getValue() == null || config.getValue().trim().isEmpty()) { |
| | | return defaultValue; |
| | | } |
| | | return config.getValue(); |
| | | } |
| | | |
| | | @Override |
| | | public long getConfigLongValue(String code, long defaultValue) { |
| | | String value = getConfigValue(code, String.valueOf(defaultValue)); |
| | | try { |
| | | long parsed = Long.parseLong(value.trim()); |
| | | return parsed >= 0 ? parsed : defaultValue; |
| | | } catch (Exception e) { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public boolean saveConfigValue(String code, String value) { |
| | | Config config = getOne(new QueryWrapper<Config>().eq("code", code).last("limit 1")); |
| | | if (config == null) { |
| | | config = new Config(code, code, value, (short) 1, (short) 1); |
| | | if (FakeConfigKeys.ALL_KEYS.contains(code)) { |
| | | config.setSelectType("fake"); |
| | | } |
| | | return save(config); |
| | | } |
| | | config.setValue(value); |
| | | if (FakeConfigKeys.ALL_KEYS.contains(code) && (config.getSelectType() == null || config.getSelectType().trim().isEmpty())) { |
| | | config.setSelectType("fake"); |
| | | } |
| | | return updateById(config); |
| | | } |
| | | |
| | | @Override |
| | | public void refreshSystemConfigCache() { |
| | | HashMap<String, String> systemConfigMap = new HashMap<>(); |
| | | List<Config> configList = list(new QueryWrapper<>()); |
| | | for (Config config : configList) { |
| | | systemConfigMap.put(config.getCode(), config.getValue()); |
| | | } |
| | | redisUtil.set(RedisKeyType.SYSTEM_CONFIG_MAP.key, systemConfigMap); |
| | | Parameter.reset(); |
| | | } |
| | | } |