|
@@ -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
|
|
|
}
|
|
|
|