فهرست منبع

+ 多租户测试

chen.cheng 2 ماه پیش
والد
کامیت
6a35e519ae
14فایلهای تغییر یافته به همراه144 افزوده شده و 154 حذف شده
  1. 3 0
      bd-park/park-backend/park-application/src/main/java/com/huashe/park/application/web/controller/system/SysCustConfigController.java
  2. 1 0
      bd-park/park-backend/park-application/src/main/resources/i18n/messages_en.properties
  3. 2 1
      bd-park/park-backend/park-application/src/main/resources/i18n/messages_zh.properties
  4. 2 2
      bd-park/park-backend/park-core/src/main/java/com/huashe/park/core/mapper/SysCustConfigMapper.java
  5. 0 1
      bd-park/park-backend/park-core/src/main/java/com/huashe/park/core/service/ISysCustConfigService.java
  6. 3 0
      bd-park/park-backend/park-core/src/main/java/com/huashe/park/core/service/impl/ConsUnitInfoServiceImpl.java
  7. 13 23
      bd-park/park-backend/park-core/src/main/java/com/huashe/park/core/service/impl/SysCustConfigServiceImpl.java
  8. 9 4
      bd-park/park-backend/park-core/src/main/resources/mapper/sys/SysCustConfigMapper.xml
  9. 6 5
      bd-park/park-backend/park-infrastructure/src/main/java/com/huashe/park/infrastructure/cfg/mybatis/MybatisInterceptor.java
  10. 4 4
      bd-park/park-backend/park-infrastructure/src/main/java/com/huashe/park/infrastructure/cfg/mybatis/TenantIdInterceptor.java
  11. 37 38
      bd-park/park-backend/park-infrastructure/src/main/java/com/huashe/park/infrastructure/cfg/mybatis/TenantSqlInterceptor.java
  12. 1 1
      common-application/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysConfigMapper.java
  13. 54 70
      common-application/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java
  14. 9 5
      common-application/ruoyi-system/src/main/resources/mapper/system/SysConfigMapper.xml

+ 3 - 0
bd-park/park-backend/park-application/src/main/java/com/huashe/park/application/web/controller/system/SysCustConfigController.java

@@ -1,10 +1,12 @@
 package com.huashe.park.application.web.controller.system;
 
+import com.ruoyi.system.domain.SysConfig;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -52,4 +54,5 @@ public class SysCustConfigController extends BaseController {
         return toAjax(sysCustConfigService.updateConfig(config));
     }
 
+
 }

+ 1 - 0
bd-park/park-backend/park-application/src/main/resources/i18n/messages_en.properties

@@ -39,3 +39,4 @@ cons.unit.has.no.hole=The construction unit has a designed pile hole, which cann
 machine.auth.param.error=SN, NO, and PWD cannot be empty.
 machine.auth.param.none=Authentication failed, please check the parameters.
 machine.not.assign.task=The machine has not been assigned a task.
+cons.biz.param.null=Please configure the CAD parameters first.

+ 2 - 1
bd-park/park-backend/park-application/src/main/resources/i18n/messages_zh.properties

@@ -38,4 +38,5 @@ cons.unit.delete.no.child=存在下级施工单元,不允许删除。
 cons.unit.has.no.hole=施工单元存在设计桩孔,不允许删除。
 machine.auth.param.error=SN、NO、PWD不允许为空。
 machine.auth.param.none=认证失败,请检查参数。
-machine.not.assign.task=抱歉,机械没有分配任务。
+machine.not.assign.task=抱歉,机械没有分配任务。
+cons.biz.param.null=请先配置CAD参数。

+ 2 - 2
bd-park/park-backend/park-core/src/main/java/com/huashe/park/core/mapper/SysCustConfigMapper.java

@@ -14,8 +14,8 @@ public interface SysCustConfigMapper {
     CustSysConfig selectConfigById(Long id);
 
     @IgnoreTenant
-    CustSysConfig checkConfigKeyUnique(String configKey);
+    CustSysConfig checkConfigKeyUnique(CustSysConfig configKey);
 
-    CustSysConfig selectConfig(CustSysConfig param);
+    CustSysConfig selectTenantConfig(CustSysConfig param);
 
 }

+ 0 - 1
bd-park/park-backend/park-core/src/main/java/com/huashe/park/core/service/ISysCustConfigService.java

@@ -11,7 +11,6 @@ public interface ISysCustConfigService {
 
     public int updateConfig(CustSysConfig config);
 
-    String selectConfigByKey(String configKey);
 
     CustSysConfig selectConfigObjByKey(String configKey);
 

+ 3 - 0
bd-park/park-backend/park-core/src/main/java/com/huashe/park/core/service/impl/ConsUnitInfoServiceImpl.java

@@ -269,6 +269,9 @@ public class ConsUnitInfoServiceImpl implements IConsUnitInfoService {
         File file = new File(filePath);
 
         CustSysConfig custSysConfig = sysCustConfigService.selectConfigObjByKey(cadForestCfg.getCadBizParam());
+        if (ObjectUtils.isEmpty(custSysConfig)) {
+            throw new ServiceException(MessageUtils.message("cons.biz.param.null"));
+        }
         HttpRequest form = HttpRequest.post(cadForestCfg.getCadServer() + "/upload").form("dwg_file", file)
             .form("param", custSysConfig.getCfgJson());
         String json = form.execute().body();

+ 13 - 23
bd-park/park-backend/park-core/src/main/java/com/huashe/park/core/service/impl/SysCustConfigServiceImpl.java

@@ -1,10 +1,10 @@
 package com.huashe.park.core.service.impl;
 
+import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import com.huashe.common.constant.CacheConstants;
-import com.huashe.common.text.Convert;
 import com.huashe.common.utils.StringUtils;
 import com.huashe.park.core.mapper.SysCustConfigMapper;
 import com.huashe.park.core.redis.RedisProxy;
@@ -28,38 +28,21 @@ public class SysCustConfigServiceImpl implements ISysCustConfigService {
     public int updateConfig(CustSysConfig config) {
         CustSysConfig temp = sysCustConfigMapper.selectConfigById(config.getConfigId());
         if (!StringUtils.equals(temp.getConfigKey(), config.getConfigKey())) {
-            redisProxy.deleteObject(getCacheKey(temp.getConfigKey()));
+            redisProxy.deleteObject(getCacheKey(temp));
         }
 
         int row = sysCustConfigMapper.updateConfig(config);
         if (row > 0) {
-            redisProxy.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
+            redisProxy.setCacheObject(getCacheKey(config), config.getConfigValue());
         }
         return row;
     }
 
     @Override
-    public String selectConfigByKey(String configKey) {
-        String configValue = Convert.toStr(redisProxy.getCacheObject(getCacheKey(configKey)));
-        if (StringUtils.isNotEmpty(configValue)) {
-            return configValue;
-        }
-        CustSysConfig config = new CustSysConfig();
-        config.setConfigKey(configKey);
-        CustSysConfig retConfig = sysCustConfigMapper.selectConfig(config);
-        if (StringUtils.isNotNull(retConfig)) {
-            redisProxy.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
-            return retConfig.getConfigValue();
-        }
-        return StringUtils.EMPTY;
-    }
-
-    @Override
     public CustSysConfig selectConfigObjByKey(String configKey) {
-
         CustSysConfig config = new CustSysConfig();
         config.setConfigKey(configKey);
-        return sysCustConfigMapper.selectConfig(config);
+        return sysCustConfigMapper.selectTenantConfig(config);
     }
 
     @Override
@@ -70,11 +53,18 @@ public class SysCustConfigServiceImpl implements ISysCustConfigService {
     @Override
     public boolean checkConfigKeyUnique(CustSysConfig config) {
         Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId();
-        CustSysConfig info = sysCustConfigMapper.checkConfigKeyUnique(config.getConfigKey());
-        return !StringUtils.isNotNull(info) || info.getConfigId() == configId;
+        CustSysConfig info = sysCustConfigMapper.checkConfigKeyUnique(config);
+        return !StringUtils.isNotNull(info) || info.getConfigId().equals(configId);
     }
 
     private String getCacheKey(String configKey) {
         return CacheConstants.SYS_CONFIG_KEY + configKey;
     }
+
+    private String getCacheKey(CustSysConfig configKey) {
+        if (!ObjectUtils.isEmpty(configKey.getTenantId())) {
+            return CacheConstants.SYS_CONFIG_KEY + configKey.getConfigKey() + ":" + configKey.getTenantId();
+        }
+        return CacheConstants.SYS_CONFIG_KEY + configKey.getConfigKey();
+    }
 }

+ 9 - 4
bd-park/park-backend/park-core/src/main/resources/mapper/sys/SysCustConfigMapper.xml

@@ -12,10 +12,11 @@
             <if test="configKey !=null and configKey != ''">
                 and config_key = #{configKey}
             </if>
+            <if test="configType == 'Y'.toString() and tenantId  != null">and tenant_id = #{tenantId}</if>
         </where>
     </sql>
 
-    <select id="selectConfig" parameterType="CustSysConfig" resultType="CustSysConfig">
+    <select id="selectTenantConfig" parameterType="CustSysConfig" resultType="CustSysConfig">
         select *
         from sys_config
         <include refid="sqlwhereSearch"/>
@@ -30,7 +31,7 @@
             <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
             <if test="remark != null">remark = #{remark},</if>
             <if test="cfgJson != null">cfg_json = #{cfgJson},</if>
-            <if test="tenantId  != null">tenant_id = #{tenantId},</if>
+            <if test="configType == 'Y'.toString() and tenantId  != null">tenant_id = #{tenantId},</if>
             update_time = sysdate()
         </set>
         where config_id = #{configId}
@@ -41,8 +42,12 @@
         from sys_config where config_id = #{configId}
     </select>
 
-    <select id="checkConfigKeyUnique" parameterType="String" resultType="CustSysConfig">
+    <select id="checkConfigKeyUnique" parameterType="CustSysConfig" resultType="CustSysConfig">
         select * from sys_config
-        where config_key = #{configKey} limit 1
+        where config_key = #{configKey}
+        <if test="configType != 'Y'.toString() and tenantId !=null">
+            and tenant_id = #{tenantId}
+        </if>
+        limit 1
     </select>
 </mapper> 

+ 6 - 5
bd-park/park-backend/park-infrastructure/src/main/java/com/huashe/park/infrastructure/cfg/mybatis/MybatisInterceptor.java

@@ -51,22 +51,22 @@ public class MybatisInterceptor extends BaseInterceptor implements Interceptor {
                     injectCreateModifyInfo(item, sqlCommandType);
                 }
             }
+            return invocation.proceed();
         }
         // 处理集合类型参数(List/数组)
         if (parameterObject instanceof Collection) {
             for (Object item : (Collection<?>) parameterObject) {
                 injectCreateModifyInfo(item, sqlCommandType);
             }
+            return invocation.proceed();
         }
-        else if (parameterObject.getClass().isArray()) {
+        if (parameterObject.getClass().isArray()) {
             for (Object item : (Object[]) parameterObject) {
                 injectCreateModifyInfo(item, sqlCommandType);
             }
+            return invocation.proceed();
         }
-        else {
-            injectCreateModifyInfo(parameterObject, sqlCommandType);
-        }
-
+        injectCreateModifyInfo(parameterObject, sqlCommandType);
         return invocation.proceed();
     }
 
@@ -108,6 +108,7 @@ public class MybatisInterceptor extends BaseInterceptor implements Interceptor {
                 }
                 field.setAccessible(false);
             });
+            return;
         }
         if (SqlCommandType.UPDATE == sqlCommandType) {
             ReflectionUtils.doWithFields(target.getClass(), field -> {

+ 4 - 4
bd-park/park-backend/park-infrastructure/src/main/java/com/huashe/park/infrastructure/cfg/mybatis/TenantIdInterceptor.java

@@ -42,15 +42,15 @@ public class TenantIdInterceptor extends BaseInterceptor implements Interceptor
             for (Object item : (Collection<?>) parameterObject) {
                 injectTenantId(item, tenantId);
             }
+            return invocation.proceed();
         }
-        else if (parameterObject.getClass().isArray()) {
+        if (parameterObject.getClass().isArray()) {
             for (Object item : (Object[]) parameterObject) {
                 injectTenantId(item, tenantId);
             }
+            return invocation.proceed();
         }
-        else {
-            injectTenantId(parameterObject, tenantId);
-        }
+        injectTenantId(parameterObject, tenantId);
 
         return invocation.proceed();
     }

+ 37 - 38
bd-park/park-backend/park-infrastructure/src/main/java/com/huashe/park/infrastructure/cfg/mybatis/TenantSqlInterceptor.java

@@ -16,10 +16,8 @@ import org.apache.ibatis.plugin.Signature;
 import org.apache.ibatis.reflection.MetaObject;
 import org.apache.ibatis.reflection.SystemMetaObject;
 import org.springframework.stereotype.Component;
-import org.springframework.util.ReflectionUtils;
 
 import com.huashe.common.annotation.mybatis.Tenant;
-import com.huashe.common.annotation.mybatis.TenantField;
 import com.huashe.park.common.animations.mybatis.MethodMetadataCache;
 
 import net.sf.jsqlparser.expression.Expression;
@@ -51,38 +49,41 @@ public class TenantSqlInterceptor extends BaseInterceptor implements Interceptor
         MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
         Method mapperMethod = getMapperMethod(mappedStatement);
         // 判断是否需要添加租户条件
-        if (ObjectUtils.allNotNull(mapperMethod, getLoginUser().getUser().getTenantId())) {
-            // 解析 SQL 语法树
-            Statement stmt = CCJSqlParserUtil.parse(originalSql);
-            if (stmt instanceof Select) {
-                Select select = (Select) stmt;
-                SelectBody selectBody = select.getSelectBody();
-                if (selectBody instanceof PlainSelect) {
-                    PlainSelect plainSelect = (PlainSelect) selectBody;
-                    // 构建租户条件表达式(tenant_id = ?)
-                    EqualsTo tenantCondition = new EqualsTo();
-                    tenantCondition.setLeftExpression(handleColumn(mapperMethod));
-                    tenantCondition.setRightExpression(new LongValue(getLoginUser().getUser().getTenantId()));
-
-                    // 修改 WHERE 条件
-                    Expression where = plainSelect.getWhere();
-                    if (where == null) {
-                        // 没有 WHERE 子句时直接添加
-                        plainSelect.setWhere(tenantCondition);
-                    }
-                    else {
-                        // 已有 WHERE 子句时用 AND 连接
-                        AndExpression and = new AndExpression(where, tenantCondition);
-                        plainSelect.setWhere(and);
-                    }
+        if (!ObjectUtils.allNotNull(mapperMethod, getLoginUser().getUser().getTenantId())) {
+            return invocation.proceed();
+        }
+        // 解析 SQL 语法树
+        Statement stmt = CCJSqlParserUtil.parse(originalSql);
+        if (!(stmt instanceof Select)) {
+            return invocation.proceed();
+        }
+        Select select = (Select) stmt;
+        SelectBody selectBody = select.getSelectBody();
+        if (!(selectBody instanceof PlainSelect)) {
+            return invocation.proceed();
+        }
+        PlainSelect plainSelect = (PlainSelect) selectBody;
+        // 构建租户条件表达式(tenant_id = ?)
+        EqualsTo tenantCondition = new EqualsTo();
+        tenantCondition.setLeftExpression(handleColumn(mapperMethod));
+        tenantCondition.setRightExpression(new LongValue(getLoginUser().getUser().getTenantId()));
 
-                    // 生成修改后的 SQL
-                    String modifiedSql = plainSelect.toString();
-                    metaObject.setValue("delegate.boundSql.sql", modifiedSql);
-                }
-            }
+        // 修改 WHERE 条件
+        Expression where = plainSelect.getWhere();
+        if (where == null) {
+            // 没有 WHERE 子句时直接添加
+            plainSelect.setWhere(tenantCondition);
+        }
+        else {
+            // 已有 WHERE 子句时用 AND 连接
+            AndExpression and = new AndExpression(where, tenantCondition);
+            plainSelect.setWhere(and);
         }
 
+        // 生成修改后的 SQL
+        String modifiedSql = plainSelect.toString();
+        metaObject.setValue("delegate.boundSql.sql", modifiedSql);
+
         return invocation.proceed();
     }
 
@@ -101,15 +102,13 @@ public class TenantSqlInterceptor extends BaseInterceptor implements Interceptor
 
     private Column handleColumn(Method mapperMethod) {
         Tenant annotation = mapperMethod.getAnnotation(Tenant.class);
-        if (ObjectUtils.isNotEmpty(annotation)) {
-            if (ObjectUtils.isNotEmpty(annotation.tableAlias())) {
-                return new Column(String.format("%s.%s", annotation.tableAlias(), annotation.field()));
-            }
-            return new Column(annotation.field());
-        }
-        else {
+        if (!ObjectUtils.isNotEmpty(annotation)) {
             return new Column("tenant_id");
         }
+        if (ObjectUtils.isNotEmpty(annotation.tableAlias())) {
+            return new Column(String.format("%s.%s", annotation.tableAlias(), annotation.field()));
+        }
+        return new Column(annotation.field());
     }
 
     @Override

+ 1 - 1
common-application/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysConfigMapper.java

@@ -43,7 +43,7 @@ public interface SysConfigMapper
      * @param configKey 参数键名
      * @return 参数配置信息
      */
-    public SysConfig checkConfigKeyUnique(String configKey);
+    public SysConfig checkConfigKeyUnique(SysConfig configKey);
 
     /**
      * 新增参数配置

+ 54 - 70
common-application/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java

@@ -1,9 +1,5 @@
 package com.ruoyi.system.service.impl;
 
-import java.util.Collection;
-import java.util.List;
-import javax.annotation.PostConstruct;
-
 import com.huashe.common.annotation.DataSource;
 import com.huashe.common.constant.CacheConstants;
 import com.huashe.common.constant.UserConstants;
@@ -11,21 +7,25 @@ import com.huashe.common.enums.DataSourceType;
 import com.huashe.common.exception.ServiceException;
 import com.huashe.common.text.Convert;
 import com.huashe.common.utils.StringUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
 import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.system.domain.SysConfig;
 import com.ruoyi.system.mapper.SysConfigMapper;
 import com.ruoyi.system.service.ISysConfigService;
+import org.apache.commons.lang3.ObjectUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import java.util.Collection;
+import java.util.List;
 
 /**
  * 参数配置 服务层实现
- * 
+ *
  * @author ruoyi
  */
 @Service
-public class SysConfigServiceImpl implements ISysConfigService
-{
+public class SysConfigServiceImpl implements ISysConfigService {
     @Autowired
     private SysConfigMapper configMapper;
 
@@ -36,45 +36,40 @@ public class SysConfigServiceImpl implements ISysConfigService
      * 项目启动时,初始化参数到缓存
      */
     @PostConstruct
-    public void init()
-    {
+    public void init() {
         loadingConfigCache();
     }
 
     /**
      * 查询参数配置信息
-     * 
+     *
      * @param configId 参数配置ID
      * @return 参数配置信息
      */
     @Override
     @DataSource(DataSourceType.MASTER)
-    public SysConfig selectConfigById(Long configId)
-    {
+    public SysConfig selectConfigById(Long configId) {
         SysConfig config = new SysConfig();
         config.setConfigId(configId);
         return configMapper.selectConfig(config);
     }
 
     /**
-     * 根据键名查询参数配置信息
-     * 
+     * 根据键名查询参数配置信息,如果是租户的配置,需要在key上追加租户ID 【key:[tenantId]】
+     *
      * @param configKey 参数key
      * @return 参数键值
      */
     @Override
-    public String selectConfigByKey(String configKey)
-    {
+    public String selectConfigByKey(String configKey) {
         String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
-        if (StringUtils.isNotEmpty(configValue))
-        {
+        if (StringUtils.isNotEmpty(configValue)) {
             return configValue;
         }
         SysConfig config = new SysConfig();
         config.setConfigKey(configKey);
         SysConfig retConfig = configMapper.selectConfig(config);
-        if (StringUtils.isNotNull(retConfig))
-        {
+        if (StringUtils.isNotNull(retConfig)) {
             redisCache.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
             return retConfig.getConfigValue();
         }
@@ -83,15 +78,13 @@ public class SysConfigServiceImpl implements ISysConfigService
 
     /**
      * 获取验证码开关
-     * 
+     *
      * @return true开启,false关闭
      */
     @Override
-    public boolean selectCaptchaEnabled()
-    {
+    public boolean selectCaptchaEnabled() {
         String captchaEnabled = selectConfigByKey("sys.account.captchaEnabled");
-        if (StringUtils.isEmpty(captchaEnabled))
-        {
+        if (StringUtils.isEmpty(captchaEnabled)) {
             return true;
         }
         return Convert.toBool(captchaEnabled);
@@ -99,69 +92,60 @@ public class SysConfigServiceImpl implements ISysConfigService
 
     /**
      * 查询参数配置列表
-     * 
+     *
      * @param config 参数配置信息
      * @return 参数配置集合
      */
     @Override
-    public List<SysConfig> selectConfigList(SysConfig config)
-    {
+    public List<SysConfig> selectConfigList(SysConfig config) {
         return configMapper.selectConfigList(config);
     }
 
     /**
      * 新增参数配置
-     * 
+     *
      * @param config 参数配置信息
      * @return 结果
      */
     @Override
-    public int insertConfig(SysConfig config)
-    {
+    public int insertConfig(SysConfig config) {
         int row = configMapper.insertConfig(config);
-        if (row > 0)
-        {
-            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
+        if (row > 0) {
+            redisCache.setCacheObject(getCacheKey(config), config.getConfigValue());
         }
         return row;
     }
 
     /**
      * 修改参数配置
-     * 
+     *
      * @param config 参数配置信息
      * @return 结果
      */
     @Override
-    public int updateConfig(SysConfig config)
-    {
+    public int updateConfig(SysConfig config) {
         SysConfig temp = configMapper.selectConfigById(config.getConfigId());
-        if (!StringUtils.equals(temp.getConfigKey(), config.getConfigKey()))
-        {
-            redisCache.deleteObject(getCacheKey(temp.getConfigKey()));
+        if (!StringUtils.equals(temp.getConfigKey(), config.getConfigKey())) {
+            redisCache.deleteObject(getCacheKey(temp));
         }
 
         int row = configMapper.updateConfig(config);
-        if (row > 0)
-        {
-            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
+        if (row > 0) {
+            redisCache.setCacheObject(getCacheKey(config), config.getConfigValue());
         }
         return row;
     }
 
     /**
      * 批量删除参数信息
-     * 
+     *
      * @param configIds 需要删除的参数ID
      */
     @Override
-    public void deleteConfigByIds(Long[] configIds)
-    {
-        for (Long configId : configIds)
-        {
+    public void deleteConfigByIds(Long[] configIds) {
+        for (Long configId : configIds) {
             SysConfig config = selectConfigById(configId);
-            if (StringUtils.equals(UserConstants.YES, config.getConfigType()))
-            {
+            if (StringUtils.equals(UserConstants.YES, config.getConfigType())) {
                 throw new ServiceException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey()));
             }
             configMapper.deleteConfigById(configId);
@@ -173,12 +157,10 @@ public class SysConfigServiceImpl implements ISysConfigService
      * 加载参数缓存数据
      */
     @Override
-    public void loadingConfigCache()
-    {
+    public void loadingConfigCache() {
         List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
-        for (SysConfig config : configsList)
-        {
-            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
+        for (SysConfig config : configsList) {
+            redisCache.setCacheObject(getCacheKey(config), config.getConfigValue());
         }
     }
 
@@ -186,8 +168,7 @@ public class SysConfigServiceImpl implements ISysConfigService
      * 清空参数缓存数据
      */
     @Override
-    public void clearConfigCache()
-    {
+    public void clearConfigCache() {
         Collection<String> keys = redisCache.keys(CacheConstants.SYS_CONFIG_KEY + "*");
         redisCache.deleteObject(keys);
     }
@@ -196,25 +177,22 @@ public class SysConfigServiceImpl implements ISysConfigService
      * 重置参数缓存数据
      */
     @Override
-    public void resetConfigCache()
-    {
+    public void resetConfigCache() {
         clearConfigCache();
         loadingConfigCache();
     }
 
     /**
      * 校验参数键名是否唯一
-     * 
+     *
      * @param config 参数配置信息
      * @return 结果
      */
     @Override
-    public boolean checkConfigKeyUnique(SysConfig config)
-    {
+    public boolean 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())
-        {
+        SysConfig info = configMapper.checkConfigKeyUnique(config);
+        if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue()) {
             return UserConstants.NOT_UNIQUE;
         }
         return UserConstants.UNIQUE;
@@ -222,12 +200,18 @@ public class SysConfigServiceImpl implements ISysConfigService
 
     /**
      * 设置cache key
-     * 
+     *
      * @param configKey 参数键
      * @return 缓存键key
      */
-    private String getCacheKey(String configKey)
-    {
+    private String getCacheKey(String configKey) {
         return CacheConstants.SYS_CONFIG_KEY + configKey;
     }
+
+    private String getCacheKey(SysConfig configKey) {
+        if (!ObjectUtils.isEmpty(configKey.getTenantId())) {
+            return CacheConstants.SYS_CONFIG_KEY + configKey.getConfigKey() + ":" + configKey.getTenantId();
+        }
+        return CacheConstants.SYS_CONFIG_KEY + configKey.getConfigKey();
+    }
 }

+ 9 - 5
common-application/ruoyi-system/src/main/resources/mapper/system/SysConfigMapper.xml

@@ -71,9 +71,13 @@
         where config_id = #{configId}
     </select>
 
-    <select id="checkConfigKeyUnique" parameterType="String" resultMap="SysConfigResult">
+    <select id="checkConfigKeyUnique" parameterType="SysConfig" resultMap="SysConfigResult">
         <include refid="selectConfigVo"/>
-        where config_key = #{configKey} limit 1
+        where config_key = #{configKey}
+        <if test="configType != 'Y'.toString() and tenantId !=null">
+            and tenant_id = #{tenantId}
+        </if>
+        limit 1
     </select>
 
     <insert id="insertConfig" parameterType="SysConfig">
@@ -84,7 +88,7 @@
         <if test="configType != null and configType != '' ">config_type,</if>
         <if test="createBy != null and createBy != ''">create_by,</if>
         <if test="remark != null and remark != ''">remark,</if>
-        <if test="configType != 'Y' and tenantId != null and tenantId != ''">tenant_id,</if>
+        <if test="configType != 'Y'.toString() and tenantId != null and tenantId != ''">tenant_id,</if>
         create_time
         )values(
         <if test="configName != null and configName != ''">#{configName},</if>
@@ -93,7 +97,7 @@
         <if test="configType != null and configType != ''">#{configType},</if>
         <if test="createBy != null and createBy != ''">#{createBy},</if>
         <if test="remark != null and remark != ''">#{remark},</if>
-        <if test="configType != 'Y' and tenantId != null and tenantId != ''">#{tenantId},</if>
+        <if test="configType != 'Y'.toString() and tenantId != null and tenantId != ''">#{tenantId},</if>
         sysdate()
         )
     </insert>
@@ -107,7 +111,7 @@
             <if test="configType != null and configType != ''">config_type = #{configType},</if>
             <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
             <if test="remark != null">remark = #{remark},</if>
-            <if test="configType != 'Y' and tenantId != null and tenantId != ''">tenant_id = #{tenantId},</if>
+            <if test="configType == 'Y'.toString() and tenantId != null and tenantId != ''">tenant_id = null,</if>
             update_time = sysdate()
         </set>
         where config_id = #{configId}