Roc 1 week ago
parent
commit
a9727825de
2 changed files with 71 additions and 39 deletions
  1. 23 13
      models/ai_summary/ai_summary_classify.go
  2. 48 26
      models/sandbox/sandbox_classify.go

+ 23 - 13
models/ai_summary/ai_summary_classify.go

@@ -148,19 +148,29 @@ type AiSummaryClassifyDeleteCheckReq struct {
 }
 
 func GetAiSummaryInfoCountByClassifyId(classifyId int) (count int, err error) {
-	sql := ` SELECT COUNT(1) AS count FROM ai_summary AS a
-				WHERE a.classify_id IN(
-				SELECT t.ai_summary_classify_id FROM 
-				(
-				SELECT rd.*
-				FROM (SELECT * FROM ai_summary_classify WHERE parent_id IS NOT NULL) rd,
-					 (SELECT @pid := ?) pd 
-				WHERE FIND_IN_SET(parent_id, @pid) > 0 
-				  AND @pid := CONCAT(@pid, ',', ai_summary_classify_id) 
-				UNION SELECT * FROM ai_summary_classify WHERE ai_summary_classify_id = @pid 
-				)AS t
-				) `
-	err = global.DEFAULT_DmSQL.Raw(sql, classifyId).Scan(&count).Error
+	var pars []interface{}
+	var sql string
+	sql = `WITH RECURSIVE ai_summary_classify_cte (ai_summary_classify_id, parent_id) AS (
+				SELECT ai_summary_classify_id, parent_id
+				FROM ai_summary_classify
+				WHERE parent_id = ?
+				UNION ALL
+				SELECT c.ai_summary_classify_id, c.parent_id
+				FROM ai_summary_classify c
+				INNER JOIN ai_summary_classify_cte ct ON c.parent_id = ct.ai_summary_classify_id
+			)
+			SELECT COUNT(1) AS count
+			FROM ai_summary a
+			WHERE a.classify_id IN (
+				SELECT ai_summary_classify_id
+				FROM ai_summary_classify_cte
+				UNION
+				SELECT ai_summary_classify_id
+				FROM ai_summary_classify
+				WHERE ai_summary_classify_id = ?
+			)`
+	pars = append(pars, classifyId, classifyId)
+	err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
 	return
 }
 

+ 48 - 26
models/sandbox/sandbox_classify.go

@@ -106,19 +106,31 @@ type SandboxClassifyDeleteCheckReq struct {
 }
 
 func GetSandboxInfoCountByClassifyId(classifyId int) (count int, err error) {
-	sql := ` SELECT COUNT(1) AS count FROM sandbox AS a
-				WHERE a.sandbox_classify_id IN(
-				SELECT t.sandbox_classify_id FROM 
-				(
-				SELECT rd.*
-				FROM (SELECT * FROM sandbox_classify WHERE parent_id IS NOT NULL) rd,
-					 (SELECT @pid := ?) pd 
-				WHERE FIND_IN_SET(parent_id, @pid) > 0 
-				  AND @pid := CONCAT(@pid, ',', sandbox_classify_id) 
-				UNION SELECT * FROM sandbox_classify WHERE sandbox_classify_id = @pid 
-				)AS t
-				) AND a.is_delete = 0 `
-	err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
+	var sql string
+	var pars []interface{}
+	sql = `WITH RECURSIVE classify_tree AS (
+    -- 基础查询:获取起始节点
+    SELECT sandbox_classify_id, parent_id, 1 as level
+    FROM sandbox_classify
+    WHERE sandbox_classify_id = ?
+    
+    UNION ALL
+    
+    -- 递归查询:获取所有子节点
+    SELECT c.sandbox_classify_id, c.parent_id, t.level + 1
+    FROM sandbox_classify c
+    INNER JOIN classify_tree t ON c.parent_id = t.sandbox_classify_id
+    WHERE c.parent_id IS NOT NULL
+)
+SELECT COUNT(1) AS count 
+FROM sandbox a
+WHERE a.sandbox_classify_id IN (
+    SELECT sandbox_classify_id 
+    FROM classify_tree
+) 
+AND a.is_delete = 0`
+	pars = append(pars, classifyId, classifyId)
+	err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
 	return
 }
 
@@ -133,19 +145,29 @@ type DeleteSandboxClassifyReq struct {
 }
 
 func DeleteSandboxClassify(classifyId int) (err error) {
-	sql := ` DELETE FROM sandbox_classify
-				WHERE sandbox_classify_id IN(
-				SELECT t.sandbox_classify_id FROM
-				(
-				SELECT rd.*
-				FROM (SELECT * FROM sandbox_classify WHERE parent_id IS NOT NULL) rd,
-				(SELECT @pid := ?) pd
-				WHERE FIND_IN_SET(parent_id, @pid) > 0
-				AND @pid := CONCAT(@pid, ',', sandbox_classify_id)
-				UNION SELECT * FROM sandbox_classify WHERE sandbox_classify_id = @pid
-				)AS t
-				) `
-	err = global.DmSQL["data"].Exec(sql, classifyId).Error
+	var pars []interface{}
+	var sql string
+	sql = `WITH RECURSIVE sandbox_classify_cte (sandbox_classify_id, parent_id) AS (
+			SELECT sandbox_classify_id, parent_id
+			FROM sandbox_classify
+			WHERE parent_id = ?
+			UNION ALL
+			SELECT c.sandbox_classify_id, c.parent_id
+			FROM sandbox_classify c
+			INNER JOIN sandbox_classify_cte ct ON c.parent_id = ct.sandbox_classify_id
+		)
+		DELETE FROM sandbox_classify
+		WHERE sandbox_classify_id IN (
+			SELECT sandbox_classify_id
+			FROM sandbox_classify_cte
+			UNION
+			SELECT sandbox_classify_id
+			FROM sandbox_classify
+			WHERE sandbox_classify_id = ?
+		)`
+	pars = append(pars, classifyId, classifyId)
+	err = global.DmSQL["data"].Exec(sql, pars...).Error
+
 	return
 }