Browse Source

1、租户字典数据label的校验

dongpo 6 months ago
parent
commit
2866b8dbf9

+ 2 - 0
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java

@@ -79,6 +79,8 @@ public interface ErrorCodeConstants {
     ErrorCode DICT_DATA_NOT_ENABLE = new ErrorCode(1_002_007_002, "字典数据({})不处于开启状态,不允许选择");
     ErrorCode DICT_DATA_VALUE_DUPLICATE = new ErrorCode(1_002_007_003, "已经存在该值的字典数据");
     ErrorCode DICT_DATA_CAN_NOT_DELETE_SYSTEM_DATA = new ErrorCode(1_002_007_004, "不能删除内置数据");
+    ErrorCode DICT_DATA_LABEL_DUPLICATE = new ErrorCode(1_002_007_005, "已经存在该标签的字典数据");
+    ErrorCode DICT_DATA_CAN_NOT_UPDATE_LABEL_SYSTEM_DATA = new ErrorCode(1_002_007_006, "不能修改内置数据的标签名");
     // ========== 通知公告 1-002-008-000 ==========
     ErrorCode NOTICE_NOT_FOUND = new ErrorCode(1_002_008_001, "当前通知公告不存在");
     // ========== 短信渠道 1-002-011-000 ==========

+ 27 - 1
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dictTenant/DictDataTenantServiceImpl.java

@@ -27,6 +27,7 @@ import java.util.Collection;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
@@ -81,6 +82,8 @@ public class DictDataTenantServiceImpl implements DictDataTenantService {
         validateDictTypeExists(createReqVO.getDictType());
         // 校验字典数据的值的唯一性
         // validateDictDataValueUnique(null, createReqVO.getDictType(), createReqVO.getValue());
+        // 校验字典数据的label的唯一性
+        validateDictDataLabelUnique(null, createReqVO.getDictType(), createReqVO.getLabel());
 
         // 插入字典类型
         DictDataTenantDO dictData = BeanUtils.toBean(createReqVO, DictDataTenantDO.class);
@@ -104,11 +107,19 @@ public class DictDataTenantServiceImpl implements DictDataTenantService {
     @Override
     public void updateDictData(DictDataTenantUpdateReqVO updateReqVO) {
         // 校验自己存在
-        validateDictDataExists(updateReqVO.getId());
+        DictDataTenantDO dictDataTenantDO = validateDictDataExists(updateReqVO.getId());
+        // 内置角色label不能修改
+        if (dictDataTenantDO.getDataType() != null && dictDataTenantDO.getDataType() == 1) {
+            if (!Objects.equals(dictDataTenantDO.getLabel(), updateReqVO.getLabel())) {
+                throw exception(DICT_DATA_CAN_NOT_UPDATE_LABEL_SYSTEM_DATA);
+            }
+        }
         // 校验字典类型有效
         // validateDictTypeExists(updateReqVO.getDictType());
         // 校验字典数据的值的唯一性
         // validateDictDataValueUnique(updateReqVO.getId(), updateReqVO.getDictType(), updateReqVO.getValue());
+        // 校验字典数据的label的唯一性
+        validateDictDataLabelUnique(updateReqVO.getId(), dictDataTenantDO.getDictType(), updateReqVO.getLabel());
 
         // 更新字典类型
         DictDataTenantDO updateObj = BeanUtils.toBean(updateReqVO, DictDataTenantDO.class);
@@ -148,6 +159,21 @@ public class DictDataTenantServiceImpl implements DictDataTenantService {
         }
     }
 
+
+    private void validateDictDataLabelUnique(Long id, String dictType, String label) {
+        DictDataTenantDO dictData = dictDataTenantMapper.selectByDictTypeAndLabel(dictType, label);
+        if (dictData == null) {
+            return;
+        }
+        // 如果 id 为空,说明不用比较是否为相同 id 的字典数据
+        if (id == null) {
+            throw exception(DICT_DATA_LABEL_DUPLICATE);
+        }
+        if (!dictData.getId().equals(id)) {
+            throw exception(DICT_DATA_LABEL_DUPLICATE);
+        }
+    }
+
     @VisibleForTesting
     public DictDataTenantDO validateDictDataExists(Long id) {
         DictDataTenantDO dictData = dictDataTenantMapper.selectById(id);