xiangpei
2025-05-14 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403
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
<?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="com.mindskip.xzs.repository.DepartmentMapper">
    <resultMap id="BaseResultMap" type="com.mindskip.xzs.domain.Department">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="deleted" jdbcType="VARCHAR" property="deleted"/>
        <result column="parent_id" property="parentId"/>
        <result column="special" property="special"/>
        <result column="level" property="level"/>
    </resultMap>
 
    <sql id="Base_Column_List">
        id
        , name,deleted,parent_id,special,level
    </sql>
 
    <insert id="add" parameterType="com.mindskip.xzs.domain.Department" useGeneratedKeys="true" keyProperty="id">
        insert into t_department (name, deleted, parent_id,special,level)
        values (#{name,jdbcType=VARCHAR}, #{deleted,jdbcType=VARCHAR}, #{parentId}, #{special.value},#{level})
    </insert>
 
    <update id="update" parameterType="com.mindskip.xzs.domain.Department">
        update t_department
        <set>
            parent_id = #{parentId},
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="deleted != null">
                deleted = #{deleted,jdbcType=VARCHAR},
            </if>
            <if test="special != null and special != ''">
                special = #{special.value},
            </if>
            <if test="level != null">
                level = #{level},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
 
    <select id="gets" resultMap="BaseResultMap">
        SELECT
            td.id,
            td.name,
            td.parent_id,
            td.special,
            td.level
        FROM
            t_department td
        WHERE
            td.deleted = 0
            <if test="deptId != null and deptId.size() > 0">
                AND td.id in <foreach collection="deptId" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach>
            </if>
    </select>
 
    <select id="getById" resultMap="BaseResultMap">
        SELECT
            td.id,
            td.name,
            td.parent_id,
            td.special,
            td.level
        FROM
            t_department td
        WHERE
              td.id = #{id} AND td.deleted = 0
    </select>
 
    <resultMap id="pageResult" type="com.mindskip.xzs.viewmodel.admin.department.DepartmentResponseVM">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection column="id" property="adminIds" ofType="integer" select="selectUserIdsByDeptId"></collection>
        <collection column="id" property="adminNames" ofType="integer" select="selectUserNamesByDeptId"></collection>
    </resultMap>
 
    <select id="selectUserIdsByDeptId" resultType="integer">
        SELECT
               tu.id
        FROM
             t_user_department tud
                 INNER JOIN t_user tu ON tud.user_id = tu.id AND tud.department_id = #{deptId} AND tu.status = 1 AND deleted = 0 AND tud.dept_admin = 1
    </select>
 
    <select id="selectUserNamesByDeptId" resultType="string">
        SELECT
            tu.real_name
        FROM
            t_user_department tud
                INNER JOIN t_user tu ON tud.user_id = tu.id AND tud.department_id = #{deptId} AND tu.status = 1 AND deleted = 0 AND tud.dept_admin = 1
    </select>
 
    <select id="page" resultMap="pageResult"
            parameterType="com.mindskip.xzs.viewmodel.admin.department.DepartmentResponseVM">
        SELECT
            td.id,
            td.name,
            td.parent_id,
            td.special,
            td.level
        FROM
             t_department td
        <where>
            AND td.deleted = 0
            <if test="id != null ">
                AND td.id= #{id}
            </if>
            <if test="name != null ">
                AND td.name like concat('%',#{name},'%')
            </if>
 
        </where>
    </select>
 
    <select id="getName" resultMap="BaseResultMap">
        SELECT
            td.id,
            td.name,
            td.parent_id,
            td.special,
            td.level,
            td.admin_id as adminId,
            tu.real_name as adminName
        FROM
            t_department td
                LEFT JOIN t_user tu ON td.admin_id = tu.id AND tu.deleted = 0
        WHERE
            td.name = #{name} AND td.deleted = 0
    </select>
 
    <select id="selectByAdminId" resultType="integer">
        SELECT id FROM t_department WHERE admin_id = #{userId} AND deleted = 0
    </select>
 
    <select id="countByAdminId" resultType="integer">
        SELECT count(*) FROM t_department WHERE admin_id = #{userId} AND deleted = 0 AND id != #{id}
    </select>
 
    <select id="list" resultType="com.mindskip.xzs.domain.vo.CascaderDataVO">
        SELECT id as value, name as label, parent_id, special, level FROM t_department WHERE deleted = 0
    </select>
 
    <select id="getChilds" resultType="integer">
        WITH RECURSIVE temp_table AS (
            SELECT
                id, name, parent_id,special,level FROM t_department WHERE id in <foreach collection="deptIds" open="(" item="deptId" close=")" separator=",">#{deptId}</foreach> AND deleted = 0
            UNION ALL
            SELECT
                so.id, so.name, so.parent_id, so.special, so.level FROM t_department so INNER JOIN temp_table tb ON so.parent_id = tb.id AND so.deleted = 0
        )
        SELECT
            DISTINCT id
        FROM
            temp_table
    </select>
 
    <select id="getFather" resultType="integer">
        WITH RECURSIVE temp_table AS (
        SELECT
        id, name, parent_id,special,level FROM t_department WHERE id = #{deptId} AND deleted = 0
        UNION ALL
        SELECT
        so.id, so.name, so.parent_id, so.special, so.level FROM t_department so INNER JOIN temp_table tb ON so.id = tb.parent_id AND so.deleted = 0
        )
        SELECT
        DISTINCT id
        FROM
        temp_table
    </select>
 
    <select id="getLevelDeptList" resultType="com.mindskip.xzs.domain.vo.CascaderDataVO">
        SELECT id as value, name as label, parent_id, special, level FROM t_department WHERE level = #{level} AND deleted = 0
    </select>
 
    <select id="page1" resultType="com.mindskip.xzs.vo.SubjectVO">
        SELECT
               id, name
        FROM
             t_subject
        WHERE
              deleted = 0
              <if test="query.name != null and query.name != ''">
                  AND name like concat('%', #{query.name}  , '%')
              </if>
    </select>
 
</mapper>