|
|
@@ -0,0 +1,94 @@
|
|
|
+package com.fs.tenant.config.service.impl;
|
|
|
+
|
|
|
+import com.fs.common.constant.Constants;
|
|
|
+import com.fs.common.constant.UserConstants;
|
|
|
+import com.fs.common.core.redis.RedisCache;
|
|
|
+import com.fs.common.exception.ServiceException;
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.system.domain.SysConfig;
|
|
|
+import com.fs.system.mapper.SysConfigMapper;
|
|
|
+import com.fs.tenant.config.SysConfigCacheDelegate;
|
|
|
+import com.fs.tenant.config.service.TenantSysConfigService;
|
|
|
+import com.fs.tenant.dict.helper.TenantDictContextHelper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 多租户 sys_config 读写:统一切库 / Redis 租户上下文,查询走 @Cacheable,更新后驱逐并回写值缓存。
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class TenantSysConfigServiceImpl implements TenantSysConfigService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantDictContextHelper contextHelper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysConfigCacheDelegate configCacheDelegate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysConfigMapper configMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SysConfig getConfigByKey(Long tenantId, String configKey) {
|
|
|
+ if (StringUtils.isEmpty(configKey)) {
|
|
|
+ throw new ServiceException("参数键名不能为空");
|
|
|
+ }
|
|
|
+ if (tenantId == null) {
|
|
|
+ return contextHelper.executeInMaster(() -> configCacheDelegate.loadFromDb(configKey));
|
|
|
+ }
|
|
|
+ return contextHelper.executeInTenant(tenantId, () -> configCacheDelegate.loadFromDb(configKey));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int updateConfigByKey(SysConfig config) {
|
|
|
+ if (config == null || StringUtils.isEmpty(config.getConfigKey())) {
|
|
|
+ throw new ServiceException("参数键名不能为空");
|
|
|
+ }
|
|
|
+ Long tenantId = config.getTenantId();
|
|
|
+ if (tenantId == null) {
|
|
|
+ return contextHelper.executeInMaster(() -> saveOrUpdateInContext(config));
|
|
|
+ }
|
|
|
+ return contextHelper.executeInTenant(tenantId, () -> saveOrUpdateInContext(config));
|
|
|
+ }
|
|
|
+
|
|
|
+ private int saveOrUpdateInContext(SysConfig config) {
|
|
|
+ if (UserConstants.NOT_UNIQUE.equals(checkConfigKeyUnique(config))) {
|
|
|
+ throw new ServiceException("参数键名'" + config.getConfigKey() + "'已存在");
|
|
|
+ }
|
|
|
+ int row;
|
|
|
+ if (config.getConfigId() != null) {
|
|
|
+ row = configMapper.updateConfig(config);
|
|
|
+ } else {
|
|
|
+ row = configMapper.insertConfig(config);
|
|
|
+ }
|
|
|
+ if (row > 0) {
|
|
|
+ syncRedisAfterWrite(config.getConfigKey(), config.getConfigValue());
|
|
|
+ }
|
|
|
+ return row;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void syncRedisAfterWrite(String configKey, String configValue) {
|
|
|
+ configCacheDelegate.evict(configKey);
|
|
|
+ if (StringUtils.isNotEmpty(configValue)) {
|
|
|
+ redisCache.setCacheObject(valueCacheKey(configKey), configValue);
|
|
|
+ } else {
|
|
|
+ redisCache.deleteObject(valueCacheKey(configKey));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String checkConfigKeyUnique(SysConfig config) {
|
|
|
+ Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId();
|
|
|
+ SysConfig info = configMapper.checkConfigKeyUnique(config.getConfigKey());
|
|
|
+ if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue()) {
|
|
|
+ return UserConstants.NOT_UNIQUE;
|
|
|
+ }
|
|
|
+ return UserConstants.UNIQUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String valueCacheKey(String configKey) {
|
|
|
+ return Constants.SYS_CONFIG_KEY + configKey;
|
|
|
+ }
|
|
|
+}
|