Lius
2025-06-10 96be94fc3c33c49e15b538bebbea455e839a7a7b
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?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.system.mapper.MdcProductionMapper">
 
    <!--递归查询所有子节点-->
    <select id="recursionChildren" resultType="java.lang.String">
        WITH temp ( id ) AS (
            SELECT
                id
            FROM
                mdc_production
            WHERE
                id = #{ productionId } UNION ALL
            SELECT
                a.id
            FROM
                mdc_production a
                    INNER JOIN temp ON a.parent_id = temp.id
        ) SELECT
            *
        FROM
            temp
    </select>
 
    <!--根据用户id和车间id获取用户拥有的车间id-->
    <select id="findFirstProduction" resultType="java.lang.String">
        SELECT TOP 1 t2.id FROM mdc_user_production t1 LEFT JOIN mdc_production t2 ON t1.pro_id = t2.id WHERE t1.user_id = #{ userId } AND t2.parent_id = #{productionId}
    </select>
 
    <!--根据用户id查询用户工段权限-->
    <select id="findThreeProductionId" resultType="java.lang.String">
        SELECT TOP 1 t2.id id FROM mdc_user_production t1 LEFT JOIN mdc_production t2 ON t1.pro_id = t2.id WHERE t1.user_id = #{userId} AND t2.org_type = '3'
    </select>
    <select id="findAllProductionId" resultType="org.jeecg.modules.system.entity.MdcProduction">
        SELECT
            t1.*
        FROM
            mdc_production t1
                LEFT JOIN mdc_user_production t2 ON t1.id = t2.pro_id
                LEFT JOIN sys_user t3 on t3.ID=t2.user_id
        WHERE
            t3.id = #{userId}
          AND t1.org_type = '3'
    </select>
 
    <select id="recursionChildrenByList" resultType="java.lang.String">
        WITH temp (id) AS (
            -- 初始查询,获取指定 id 的记录
            SELECT
                id
            FROM
                mdc_production
            WHERE
                id IN
            <foreach collection = "productionIds" item = "id" index = "index" open = "(" close = ")" separator = ",">
                 #{id}
            </foreach>
            UNION ALL
            -- 递归查询,查找子节点
            SELECT
                a.id
            FROM
                mdc_production a
                    INNER JOIN temp ON a.parent_id = temp.id
        )
-- 最终查询,使用 DISTINCT 去重
        SELECT DISTINCT
            id
        FROM
            temp;
    </select>
 
    <select id="findChildren" resultType="java.lang.String">
        WITH temp ( id ) AS (
            SELECT
                id
            FROM
                mdc_production
            WHERE
                id IN
            <foreach collection="mdcProductionIds" item = "id" index = "index" open = "(" close = ")" separator = ",">
                #{id}
            </foreach>
            AND mdc_flag = '1' UNION ALL
            SELECT
                a.id
            FROM
                mdc_production a
                    INNER JOIN temp ON a.parent_id = temp.id
            WHERE
                a.mdc_flag = '1'
        ) SELECT
            *
        FROM
            temp
    </select>
 
    <select id="loadProductionOptions" resultType="org.jeecg.modules.system.entity.MdcProduction">
        SELECT
            t1.*
        FROM
            mdc_production t1
                LEFT JOIN mdc_user_production t2 ON t1.id = t2.pro_id
        WHERE
            t2.user_id = #{userId} AND t1.mdc_flag = '1'
        <choose>
            <when test="productionId != null and productionId != ''">
                AND t1.org_type = '3' AND t1.parent_id = #{productionId}
            </when>
            <otherwise>
                AND t1.org_type = '2'
            </otherwise>
        </choose>
        ORDER BY t1.production_order
    </select>
 
    <select id="findTeamValue" resultType="java.lang.String">
        SELECT DISTINCT
            t1.team_code deamCode
        FROM
            mdc_equipment t1
            LEFT JOIN mdc_production_equipment t2 ON t1.id = t2.equipment_id
        WHERE t2.production_id IN
        <foreach collection="productionList" item = "productionId" index = "index" open = "(" close= ")" separator = ",">
            #{productionId}
        </foreach>
    </select>
 
    <select id="findProIdsByUId" resultType="java.lang.String">
        SELECT
            pro_id
        FROM
            mdc_user_production
        WHERE
            pro_id IN
            <foreach collection="allProductionIds" item = "productionId" index = "index" open = "(" close= ")" separator = ",">
                #{productionId}
            </foreach>
          AND user_id = #{userId}
    </select>
 
    <select id="findChildByProId" resultType="java.lang.String">
        WITH temp ( id ) AS (
            SELECT
                id
            FROM
                mdc_production
            WHERE
                id = #{ productionId }
              AND mdc_flag = '1' UNION ALL
            SELECT
                a.id
            FROM
                mdc_production a
                    INNER JOIN temp ON a.parent_id = temp.id
            WHERE
                a.mdc_flag = '1'
        ) SELECT
            *
        FROM
            temp
    </select>
 
    <select id="findProName" resultType="java.lang.String">
        WITH production_hierarchy AS (
            SELECT
                t3.id,
                t3.parent_id,
                t3.production_name,
                t3.org_type,
                0 AS level
            FROM
                mdc_equipment t1
                    JOIN
                mdc_production_equipment t2 ON t1.id = t2.equipment_id
                    JOIN
                mdc_production t3 ON t2.production_id = t3.id
            WHERE
                t1.equipment_id = #{equipmentId}
 
            UNION ALL
            SELECT
                t4.id,
                t4.parent_id,
                t4.production_name,
                t4.org_type,
                ph.level + 1
            FROM
                production_hierarchy ph
                    JOIN
                mdc_production t4 ON ph.parent_id = t4.id
            WHERE
                ph.parent_id IS NOT NULL
        )
        SELECT TOP 1
            production_name
        FROM
            production_hierarchy
        WHERE
            org_type = 2
        ORDER BY
            level ASC
    </select>
</mapper>