wangliang há 4 anos atrás
pai
commit
9f6b06d1b7

+ 27 - 9
themis-backend/src/main/java/com/qmth/themis/backend/api/TBOrgController.java

@@ -7,6 +7,7 @@ import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dto.response.TBOrgDto;
 import com.qmth.themis.business.entity.TBOrg;
 import com.qmth.themis.business.entity.TBUser;
+import com.qmth.themis.business.enums.FieldUniqueEnum;
 import com.qmth.themis.business.service.TBOrgService;
 import com.qmth.themis.business.util.RedisUtil;
 import com.qmth.themis.business.util.ServletUtil;
@@ -16,6 +17,9 @@ import com.qmth.themis.common.exception.BusinessException;
 import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.ResultUtil;
 import io.swagger.annotations.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.dao.DuplicateKeyException;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
@@ -35,6 +39,7 @@ import java.util.Objects;
 @RestController
 @RequestMapping("/${prefix.url.admin}/org")
 public class TBOrgController {
+    private final static Logger log = LoggerFactory.getLogger(TBOrgController.class);
 
     @Resource
     TBOrgService tbOrgService;
@@ -63,16 +68,29 @@ public class TBOrgController {
         if (Objects.nonNull(tbOrg.getCode()) && Objects.equals(tbOrg.getCode().toUpperCase(), SystemConstant.ADMIN)) {
             throw new BusinessException("admin为超级管理员专属code,请重新输入");
         }
-        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
-        if (Objects.isNull(tbOrg.getId())) {
-            tbOrg.setId(Constants.idGen.next());
-            tbOrg.setCreateId(tbUser.getId());
-        } else {
-            tbOrg.setUpdateId(tbUser.getId());
+        try {
+            TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
+            if (Objects.isNull(tbOrg.getId())) {
+                tbOrg.setId(Constants.idGen.next());
+                tbOrg.setCreateId(tbUser.getId());
+            } else {
+                tbOrg.setUpdateId(tbUser.getId());
+            }
+            redisUtil.setOrg(tbOrg.getId(), tbOrg);
+            redisUtil.setOrgCode(tbOrg.getCode(), tbOrg);
+            tbOrgService.saveOrUpdate(tbOrg);
+        } catch (Exception e) {
+            log.error("请求出错", e);
+            if (e instanceof DuplicateKeyException) {
+                String errorColumn = e.getCause().toString();
+                String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length()).replaceAll("'", "");
+                throw new BusinessException(FieldUniqueEnum.convertToCode(columnStr) + "数据不允许重复插入");
+            } else if (e instanceof BusinessException) {
+                throw new BusinessException(e.getMessage());
+            } else {
+                throw new RuntimeException(e);
+            }
         }
-        redisUtil.setOrg(tbOrg.getId(), tbOrg);
-        redisUtil.setOrgCode(tbOrg.getCode(), tbOrg);
-        tbOrgService.saveOrUpdate(tbOrg);
         return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
     }
 

+ 1 - 1
themis-backend/src/main/java/com/qmth/themis/backend/api/TBUserController.java

@@ -129,7 +129,7 @@ public class TBUserController {
         if (Objects.equals(orgCode.toUpperCase(), SystemConstant.ADMIN)) {
             userList.forEach(s -> {
                 if (!Objects.equals(s.getRemark(), SystemConstant.SYSADMIN)) {
-                    throw new BusinessException("机构code为admin只允许超级管理员登录");
+                    throw new BusinessException("机构编码为admin只允许超级管理员登录");
                 }
             });
         }

+ 3 - 1
themis-business/src/main/java/com/qmth/themis/business/enums/FieldUniqueEnum.java

@@ -29,7 +29,9 @@ public enum FieldUniqueEnum {
 
     t_ie_exam_invigilate_call_recordId_source_Idx("监考视频源"),
     
-    t_e_exam_short_code_Idx("考试口令");
+    t_e_exam_short_code_Idx("考试口令"),
+
+    t_b_org_code_Idx("机构编码");
 
     private String code;
 

+ 1 - 0
themis-business/src/main/resources/db/init.sql

@@ -335,6 +335,7 @@ CREATE TABLE `t_b_org` (
   `create_id` bigint DEFAULT NULL COMMENT '创建人id',
   `update_id` bigint DEFAULT NULL COMMENT '更新人id',
   PRIMARY KEY (`id`)
+  UNIQUE KEY `t_b_org_code_Idx` (`code`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='机构';
 
 -- ----------------------------