From 82c1b82a6d46620a5ae493353c37d40da691f716 Mon Sep 17 00:00:00 2001
From: lyh <925863403@qq.com>
Date: 星期四, 11 九月 2025 10:57:03 +0800
Subject: [PATCH] 优化sql

---
 lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/mapper/xml/PermissionStreamNewMapper.xml |  149 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 116 insertions(+), 33 deletions(-)

diff --git a/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/mapper/xml/PermissionStreamNewMapper.xml b/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/mapper/xml/PermissionStreamNewMapper.xml
index de844de..8ef3bdf 100644
--- a/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/mapper/xml/PermissionStreamNewMapper.xml
+++ b/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/mapper/xml/PermissionStreamNewMapper.xml
@@ -3,44 +3,127 @@
 <mapper namespace="org.jeecg.modules.dnc.mapper.PermissionStreamNewMapper">
     <select id="loadProductMix" resultType="org.jeecg.modules.dnc.entity.ProductMix">
         SELECT DISTINCT
-            mix.id,
-            mix.tree_code 'code',
-                mix.name,
-            mix.parent_id,
-            mix.tree_type AS 'type',
-                mix.extend
-        FROM
-            nc_product_mix mix -- 杩炴帴鏉冮檺琛紝绛涢�夐儴闂ㄧ浉鍏宠褰�
-                LEFT JOIN nc_permission_stream_new nps_depart ON mix.id = nps_depart.business_id
-                AND nps_depart.delete_flag = '0'
-                        <if test="productIdList != null and productIdList.size() > 0">
-                            AND nps_depart.depart_id IN
-                            <foreach collection="productIdList" item="productId" index="index" open="(" close=")" separator=",">
-                                #{productId}
-                            </foreach>
-                        </if>
-                AND nps_depart.user_id IS NULL -- 杩炴帴鏉冮檺琛紝绛涢�夌敤鎴风浉鍏宠褰�
-                LEFT JOIN nc_permission_stream_new nps_user ON mix.id = nps_user.business_id
-                AND nps_user.user_id = #{userId}
-                AND nps_user.delete_flag = '0'
-        WHERE
-            nps_depart.business_id IS NOT NULL
-          AND nps_user.business_id IS NOT NULL;
-    </select>
-    <select id="loadProductMixAll" resultType="org.jeecg.modules.dnc.entity.ProductMix">
-        SELECT DISTINCT
         mix.id,
-        mix.tree_code 'code',
-        mix.name,
+        mix.tree_code AS 'code',
+        mix.tree_name AS 'name',
         mix.parent_id,
         mix.tree_type AS 'type',
-        mix.extend
-        FROM
-        nc_product_mix mix -- 杩炴帴鏉冮檺琛紝绛涢�夐儴闂ㄧ浉鍏宠褰�
-        LEFT JOIN nc_permission_stream_new nps_user ON mix.id = nps_user.business_id
+        mix.extend,
+        mix.create_time
+        FROM nc_product_mix mix
+        WHERE EXISTS (
+        SELECT 1
+        FROM nc_permission_stream_new nps_depart
+        WHERE nps_depart.business_id = mix.id
+        AND nps_depart.delete_flag = '0'
+        AND nps_depart.user_id IS NULL
+        <if test="productIdList != null and productIdList.size() > 0">
+            AND nps_depart.depart_id IN
+            <foreach collection="productIdList" item="productId" open="(" close=")" separator=",">
+                #{productId}
+            </foreach>
+        </if>
+        )
+        AND EXISTS (
+        SELECT 1
+        FROM nc_permission_stream_new nps_user
+        WHERE nps_user.business_id = mix.id
         AND nps_user.user_id = #{userId}
         AND nps_user.delete_flag = '0'
+        )
+        ORDER BY mix.tree_type, mix.create_time ASC;
+    </select>
+    <select id="loadProductMixAll" resultType="org.jeecg.modules.dnc.entity.ProductMix">
+        SELECT DISTINCT mix.id,
+                        mix.tree_code ,
+                        mix.tree_name ,
+                        mix.parent_id,
+                        mix.tree_type AS 'type',
+                        mix.extend,
+                        mix.create_time
+        FROM nc_product_mix mix -- 杩炴帴鏉冮檺琛紝绛涢�夐儴闂ㄧ浉鍏宠褰�
+                 LEFT JOIN nc_permission_stream_new nps_user ON mix.id = nps_user.business_id
+            AND nps_user.user_id = #{userId}
+            AND nps_user.delete_flag = '0'
+        WHERE nps_user.business_id IS NOT NULL
+        order by mix.tree_type, mix.create_time asc
+    </select>
+    <select id="loadProductMixByBusinessId" resultType="org.jeecg.modules.dnc.entity.ProductMix">
+        WITH CTE_Hierarchy AS (
+        SELECT
+            CAST(id AS VARCHAR(36)) AS id,
+            CAST(parent_id AS VARCHAR(36)) AS parent_id,
+            1 AS LEVEL,
+            CAST('#' + id + '#' AS VARCHAR(MAX)) AS visit_path
+        FROM
+            nc_product_mix
         WHERE
-         nps_user.business_id IS NOT NULL;
+            id = #{businessId}
+            AND tree_type = #{businessType}
+        UNION ALL
+        -- 鍚戜笂閫掑綊鐖惰妭鐐癸紙甯﹀惊鐜娴嬶級
+        SELECT
+            CAST(p.id AS VARCHAR(36)),
+            CAST(p.parent_id AS VARCHAR(36)),
+            h.level + 1,
+            CAST(h.visit_path + '#' + p.id + '#' AS VARCHAR(MAX))
+        FROM
+            nc_product_mix p
+            INNER JOIN CTE_Hierarchy h ON CAST(p.id AS VARCHAR(36)) = h.parent_id
+        WHERE
+            p.tree_type = #{businessType}
+            AND h.visit_path NOT LIKE '%#' + p.id + '#%'
+            AND h.level &lt; 1000
+        UNION ALL
+        -- 鍚戜笅閫掑綊瀛愯妭鐐癸紙甯﹀惊鐜娴嬶級
+        SELECT
+            CAST(c.id AS VARCHAR(36)),
+            CAST(c.parent_id AS VARCHAR(36)),
+            h.level + 1,
+            CAST(h.visit_path + '#' + c.id + '#' AS VARCHAR(MAX))
+        FROM
+            nc_product_mix c
+            INNER JOIN CTE_Hierarchy h ON CAST(c.parent_id AS VARCHAR(36)) = h.id
+        WHERE
+            c.tree_type = #{businessType}
+            AND h.visit_path NOT LIKE '%#' + c.id + '#%'
+            AND h.level &lt; 1000
+        ),
+        ExclusionCTE AS (
+        SELECT DISTINCT id
+            FROM CTE_Hierarchy
+        WHERE LEVEL BETWEEN 1 AND 1000
+        )
+        SELECT DISTINCT
+            mix.id,
+            mix.tree_code,
+            mix.tree_name,
+            mix.parent_id,
+            mix.tree_type AS 'type',
+            mix.extend,
+            mix.create_time
+        FROM
+            nc_product_mix mix
+        LEFT JOIN nc_permission_stream_new nps_depart ON mix.id = nps_depart.business_id
+            AND nps_depart.delete_flag = '0'
+                <if test="productIdList != null and productIdList.size() > 0">
+                    AND nps_depart.depart_id IN
+                    <foreach collection="productIdList" item="productId" index="index" open="(" close=")" separator=",">
+                        #{productId}
+                    </foreach>
+                </if>
+            AND nps_depart.user_id IS NULL
+        LEFT JOIN nc_permission_stream_new nps_user ON mix.id = nps_user.business_id
+            AND nps_user.user_id = #{userId}
+            AND nps_user.delete_flag = '0'
+        WHERE
+            nps_depart.business_id IS NOT NULL
+            AND nps_user.business_id IS NOT NULL
+            AND mix.tree_type = #{businessType}
+            AND NOT EXISTS (SELECT 1 FROM ExclusionCTE e WHERE e.id = mix.id)
+        ORDER BY
+            mix.tree_type,
+            mix.create_time ASC
+            OPTION (MAXRECURSION 0);
     </select>
 </mapper>

--
Gitblit v1.9.3