1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<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.tree_name '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
        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    'code',
                        mix.tree_name    '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
            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 'code',
            mix.tree_name '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>