Browse Source

fix:去除新增分类时的根节点id

zqbao 4 months ago
parent
commit
d97e7687b5

+ 2 - 2
controllers/edb_monitor/edb_monitor_classify.go

@@ -79,7 +79,7 @@ func (c *EdbMonitorClassifyController) Add() {
 		br.Msg = "分类名称不能为空"
 		return
 	}
-	if req.Level <= 0 || (req.ParentId > 0 && req.RootId <= 0) || req.ClassifyId > 0 {
+	if req.Level <= 0 || req.ClassifyId > 0 {
 		br.Msg = "参数错误"
 		return
 	}
@@ -134,7 +134,7 @@ func (c *EdbMonitorClassifyController) Edit() {
 		br.Msg = "分类名称不能为空"
 		return
 	}
-	if req.Level <= 0 || (req.ParentId > 0 && req.RootId <= 0) {
+	if req.Level <= 0 {
 		br.Msg = "参数错误"
 		return
 	}

+ 0 - 1
models/edb_monitor/request/edb_monitor_classify.go

@@ -5,7 +5,6 @@ type EdbMonitorClassifySaveReq struct {
 	ClassifyName string
 	Level        int
 	ParentId     int
-	RootId       int
 }
 
 type EdbMonitorClassifyDeleteReq struct {

+ 24 - 17
services/edb_monitor/edb_monitor_classify.go

@@ -27,30 +27,37 @@ func SaveEdbMonitorClassify(req request.EdbMonitorClassifySaveReq) (msg string,
 		err = fmt.Errorf("get parent classify err:%w", er)
 		return
 	}
+	var parentClassify *edbmonitor.EdbMonitorClassify
+	if req.ParentId > 0 {
+		parentClassify, er = edbmonitor.GetEdbMonitorClassifyById(req.ParentId)
+		if er != nil {
+			if er.Error() == utils.ErrNoRow() {
+				msg = "父级分类不存在"
+				err = errors.New("parent classify not exist")
+				return
+			}
+			msg = "分类信息保存失败"
+			err = fmt.Errorf("get parent classify err:%w", er)
+			return
+		}
+	}
+	var rootId int
+	if parentClassify != nil {
+		rootId = parentClassify.RootId
+	}
 	var parentId []int
-	if req.ParentId != req.RootId {
+	if req.ParentId != rootId {
 		if req.ParentId > 0 {
 			parentId = append(parentId, req.ParentId)
 		}
-		if req.RootId > 0 {
-			parentId = append(parentId, req.RootId)
+		if rootId > 0 {
+			parentId = append(parentId, rootId)
 		}
 	} else {
 		if req.ParentId > 0 {
 			parentId = append(parentId, req.ParentId)
 		}
 	}
-	count, er := edbmonitor.GetEdbMonitorClassifyCountByIdList(parentId)
-	if er != nil {
-		msg = "分类信息保存失败"
-		err = fmt.Errorf("get classify count err:%w", er)
-		return
-	}
-	if len(parentId) != count {
-		msg = "父级分类不存在"
-		err = errors.New("parent classify not exist")
-		return
-	}
 
 	if req.ClassifyId > 0 {
 		classifyInfo, er := edbmonitor.GetEdbMonitorClassifyById(req.ClassifyId)
@@ -96,8 +103,8 @@ func SaveEdbMonitorClassify(req request.EdbMonitorClassifySaveReq) (msg string,
 			}
 		}
 
-		if classifyInfo.RootId != req.RootId {
-			classifyInfo.RootId = req.RootId
+		if classifyInfo.RootId != rootId {
+			classifyInfo.RootId = rootId
 			updateCols = append(updateCols, "root_id")
 
 		}
@@ -125,7 +132,7 @@ func SaveEdbMonitorClassify(req request.EdbMonitorClassifySaveReq) (msg string,
 		tmp := new(edbmonitor.EdbMonitorClassify)
 		tmp.ClassifyName = req.ClassifyName
 		tmp.ParentId = req.ParentId
-		tmp.RootId = req.RootId
+		tmp.RootId = rootId
 		tmp.Level = req.Level
 		tmp.Sort = maxSort + 1
 		tmp.CreateTime = time.Now()