<?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.boge.modules.dept.dao.mapper.SysDeptMapper">

    <insert id="saveDeptRelation">
        replace into sys_user_dept values
        <foreach collection="depts"  item="dept" separator=",">
           (#{user},#{dept})
        </foreach>
    </insert>
    <delete id="delDeptRelation">
        delete from sys_user_dept where user_id = #{user}
    </delete>

    <update id="updateSubCount">
        update sys_dept set sub_count =
        (select m.count from (select count(*) count from sys_dept where pid = #{pid}) as m)
        where dept_id = #{pid}
    </update>
    <select id="findAllChild" resultType="java.lang.String">
        SELECT
            max(t3.childId) as deptIds
        from
            (
                select *,
                       if( find_in_set(t1.pid, @p) > 0,@p := concat(@p,',',id),0 ) as childId
                from
                        (select dept_id as id, pid from sys_dept t order by id) t1,
                        (select @p := #{pid}) t2
            ) t3
        where childId != '0'
    </select>
    <select id="getDeptRelation" resultType="java.util.Map">
        select * from sys_user_dept where dept_id in (
        <foreach collection="deptIds" separator=","  item="dept">
            #{dept}
        </foreach>
            )
    </select>
</mapper>