xiangpei
2024-07-08 c447386e06ce5927fb6e3ccb22ee673535b3f566
src/main/resources/mapper/DepartmentMapper.xml
@@ -14,21 +14,19 @@
    </sql>
    <insert id="add" parameterType="com.mindskip.xzs.domain.Department" useGeneratedKeys="true" keyProperty="id">
        insert into t_department (name, deleted)
        values (#{name,jdbcType=VARCHAR}, #{deleted,jdbcType=VARCHAR})
        insert into t_department (name, deleted, parent_id)
        values (#{name,jdbcType=VARCHAR}, #{deleted,jdbcType=VARCHAR}, #{parentId})
    </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="adminId != null">
                admin_id = #{adminId},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="deleted != null">
                deleted = #{deleted,jdbcType=VARCHAR},
@@ -127,4 +125,22 @@
        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 FROM t_department WHERE deleted = 0
    </select>
    <select id="getChilds" resultType="integer">
        WITH RECURSIVE temp_table AS (
            SELECT
                id, name, parent_id FROM t_department WHERE id in <foreach collection="deptIds" open="(" item="deptId" close=")" separator=",">#{deptId}</foreach>
            UNION ALL
            SELECT
                so.id, so.name, so.parent_id FROM t_department so INNER JOIN temp_table tb ON so.parent_id = tb.id
        )
        SELECT
            DISTINCT id
        FROM
            temp_table
    </select>
</mapper>