<?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.fs.company.mapper.CompanyDeptMapper">
    
    <resultMap type="CompanyDept" id="CompanyDeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="deptName"    column="dept_name"    />
        <result property="orderNum"    column="order_num"    />
        <result property="leader"    column="leader"    />
        <result property="phone"    column="phone"    />
        <result property="email"    column="email"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectCompanyDeptVo">
        select dept_id, parent_id, company_id, ancestors, dept_name, order_num, leader, phone, email, status, del_flag, create_by, create_time, update_by, update_time from company_dept
    </sql>

    <select id="selectCompanyDeptList" parameterType="CompanyDept" resultMap="CompanyDeptResult">
        <include refid="selectCompanyDeptVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="leader != null  and leader != ''"> and leader = #{leader}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="email != null  and email != ''"> and email = #{email}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectCompanyDeptById" parameterType="Long" resultMap="CompanyDeptResult">
        <include refid="selectCompanyDeptVo"/>
        where dept_id = #{deptId}
    </select>
        
    <insert id="insertCompanyDept" parameterType="CompanyDept" useGeneratedKeys="true" keyProperty="deptId">
        insert into company_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="leader != null">leader,</if>
            <if test="phone != null">phone,</if>
            <if test="email != null">email,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="leader != null">#{leader},</if>
            <if test="phone != null">#{phone},</if>
            <if test="email != null">#{email},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateCompanyDept" parameterType="CompanyDept">
        update company_dept
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="leader != null">leader = #{leader},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where dept_id = #{deptId}
    </update>

    <delete id="deleteCompanyDeptById" parameterType="Long">
        delete from company_dept where dept_id = #{deptId}
    </delete>

    <delete id="deleteCompanyDeptByIds" parameterType="String">
        delete from company_dept where dept_id in 
        <foreach item="deptId" collection="array" open="(" separator="," close=")">
            #{deptId}
        </foreach>
    </delete>

    <select id="selectDeptListByRoleId" resultType="Integer">
        select d.dept_id
        from company_dept d
        left join company_role_dept rd on d.dept_id = rd.dept_id
        where rd.role_id = #{roleId}
        <if test="deptCheckStrictly">
            and d.dept_id not in (select d.parent_id from company_dept d inner join company_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
        </if>
        order by d.parent_id, d.order_num
    </select>
    <select id="checkDeptExistUser" parameterType="Long" resultType="int">
		select count(1) from company_user where dept_id = #{deptId} and del_flag = '0'
	</select>

    <select id="hasChildByDeptId" parameterType="Long" resultType="int">
		select count(1) from company_dept
		where del_flag = '0' and parent_id = #{deptId} limit 1
	</select>

    <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
		select count(*) from company_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)
	</select>


    <update id="updateDeptChildren" parameterType="java.util.List">
        update company_dept set ancestors =
        <foreach collection="depts" item="item" index="index"
                 separator=" " open="case dept_id" close="end">
            when #{item.deptId} then #{item.ancestors}
        </foreach>
        where dept_id in
        <foreach collection="depts" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.deptId}
        </foreach>
    </update>

    <update id="updateDeptStatus" parameterType="CompanyDept">
        update company_dept
        <set>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where dept_id in (${ancestors})
    </update>

    <select id="selectChildrenDeptById" parameterType="Long" resultMap="CompanyDeptResult">
        select * from company_dept where find_in_set(#{deptId}, ancestors)
    </select>

</mapper>