<?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.lz.modules.app.dao.DepartmentsDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.lz.modules.app.entity.DepartmentsEntity" id="departmentsMap">
        <result property="id" column="id"/>
        <result property="isDelete" column="is_delete"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="departmentId" column="department_id"/>
        <result property="departmentParentId" column="department_parent_id"/>
        <result property="memberCount" column="member_count"/>
        <result property="departmentName" column="department_name"/>
        <result property="chatId" column="chat_id"/>
        <result property="leaderEmployeeId" column="leader_employee_id"/>
        <result property="leaderOpenId" column="leader_open_id"/>
        <result property="status" column="status"/>
        <result column="level" property="level"/>
    </resultMap>

    <select id="getDepartmentByDepartmentId" resultType="com.lz.modules.app.entity.DepartmentsEntity">
        select * from lz_departments where is_delete=0 and department_id = #{departmentId}
	</select>

    <select id="getDepartmentsByparentId" resultType="com.lz.modules.app.dto.DepartmentsDto">
        select id, department_id,department_parent_id,member_count,department_name from lz_departments where department_parent_id=#{parentId}
    </select>

    <update id="updateDepartment">
        UPDATE lz_departments
        <set>
            update_time = now(),
            <if test="departmentParentId != null and departmentParentId != '' ">
                department_parent_id = #{departmentParentId,jdbcType=VARCHAR},
            </if>
            <if test="memberCount != null">
                member_count = #{memberCount,jdbcType=INTEGER},
            </if>
            <if test="departmentName != null and departmentName != '' ">
                department_name = #{departmentName,jdbcType=VARCHAR},
            </if>
            <if test="chatId != null and chatId != '' ">
                chat_id = #{chatId,jdbcType=VARCHAR},
            </if>
            <if test="leaderEmployeeId != null and leaderEmployeeId != '' ">
                leader_employee_id = #{leaderEmployeeId,jdbcType=VARCHAR},
            </if>
            <if test="leaderOpenId != null and leaderOpenId != '' ">
                leader_open_id = #{leaderOpenId,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
        </set>
        WHERE is_delete = 0 AND department_id = #{departmentId ,jdbcType=BIGINT}
    </update>

    <update id="updateDepartmentStatus">
        UPDATE lz_departments set status = 0 WHERE is_delete = 0
    </update>

    <insert id="addDepartmentBatch">
        INSERT INTO
        lz_departments(department_id,department_parent_id,member_count,department_name,chat_id,leader_employee_id,leader_open_id,status)
        VALUES
        <foreach collection="departments" item="department" separator=",">
            (#{department.departmentId},#{department.departmentParentId},#{department.memberCount},#{department.departmentName},#{department.chatId},#{department.leaderEmployeeId},#{department.leaderOpenId},#{department.status})
        </foreach>
    </insert>



    <select id="selectAll" resultType="com.lz.modules.app.entity.DepartmentsEntity">
        select * from lz_departments where is_delete=0
    </select>

    <select id="selectEntityByParentDepartmentId" resultType="com.lz.modules.app.entity.DepartmentsEntity">
         select * from lz_departments where is_delete=0 and department_parent_id = #{parentId}
    </select>
    <select id="selectByDepartmentId" resultType="com.lz.modules.app.entity.DepartmentsEntity">
         select * from lz_departments where is_delete=0  and department_id =#{departmentId}
    </select>
    <select id="selectStaffAllDepartments" resultType="com.lz.modules.app.dto.StaffDepartmentDto">
        select d.department_name department1 ,
         c.department_name department2 ,
         b.department_name department3,
         a.department_name department4   from lz_departments a left join lz_departments b on a.department_parent_id = b.department_id
        left join lz_departments c on b.department_parent_id = c.department_id
        left join lz_departments d on d.department_id = c.department_parent_id
        where a.department_id = #{departmentId}
    </select>


    <insert id="addDepartment" useGeneratedKeys="true" keyProperty="id" parameterType="DepartmentsEntity">
        INSERT INTO
        lz_departments(department_id,department_parent_id,member_count,department_name,chat_id,leader_employee_id,leader_open_id,status)
        VALUES
        (#{departments.departmentId},#{departments.departmentParentId},#{departments.memberCount},#{departments.departmentName},#{departments.chatId},#{departments.leaderEmployeeId},#{departments.leaderOpenId},#{departments.status})

    </insert>


    <select id="selectUserAllDepartmentIds" resultType="com.lz.modules.app.dto.DepartmentInfos">
        select
            d1.department_id dd1,d1.department_name dn1 ,
            d2.department_id dd2,d2.department_name dn2,
            d3.department_id dd3,d3.department_name dn3 ,
            d4.department_id dd4,d4.department_name dn4,
            d5.department_id dd5,d5.department_name dn5
        from lz_departments d1
            left join lz_departments d2 on d1.department_parent_id = d2.department_id
            left join lz_departments d3 on d2.department_parent_id = d3.department_id
            left join lz_departments d4 on d3.department_parent_id = d4.department_id
            left join lz_departments d5 on d4.department_parent_id = d5.department_id
        where d1.department_id = #{departmentId}
    </select>


    <select id="selectDepartmentIdsByparentIds" resultType="String">
        select department_id from lz_departments where is_delete=0
        <if test="parentIds !=null and parentIds.size>0">
            and department_parent_id IN
            <foreach collection="parentIds" item="id" open="(" close=")"
                     separator=",">
                #{id}
            </foreach>
        </if>

    </select>


    <select id="selectDeparmentLevel1" resultType="com.lz.modules.app.entity.DepartmentsEntity">
        select * from lz_departments where department_parent_id in (
        select department_id from lz_departments where department_parent_id = 1 )
    </select>




    <select id="selectAllChildDepartmentIds" resultType="com.lz.modules.app.dto.DepartmentInfos">
        select
            d1.department_id dd1,d1.department_name dn1 ,
            d2.department_id dd2,d2.department_name dn2,
            d3.department_id dd3,d3.department_name dn3 ,
            d4.department_id dd4,d4.department_name dn4,
            d5.department_id dd5,d5.department_name dn5
        from lz_departments d1
            left join lz_departments d2 on d1.department_id = d2.department_parent_id
            left join lz_departments d3 on d2.department_id = d3.department_parent_id
            left join lz_departments d4 on d3.department_id = d4.department_parent_id
            left join lz_departments d5 on d4.department_id = d5.department_parent_id
        where d1.department_id = #{departmentId}
    </select>

    <select id="selectEntityByDepartmentIds" resultType="com.lz.modules.app.entity.DepartmentsEntity">
        select * from lz_departments where department_id in (
        <foreach collection="dIds" item="item" separator=",">
            #{item}
        </foreach>
        )
    </select>

    <select id="selectEntityByParentDepartmentIds" resultType="com.lz.modules.app.entity.DepartmentsEntity">
        select * from lz_departments where department_parent_id in (
        <foreach collection="dIds" item="item" separator=",">
            #{item.departmentId}
        </foreach>
        )
    </select>

    <select id="selectParentDepartmentByDepartmentId" resultType="com.lz.modules.app.entity.DepartmentsEntity">
        select
            *
        from lz_departments
        where department_id =
        (select department_parent_id as department_id from lz_departments where department_id = #{departmentId} and is_delete=0 limit 1)
        and is_delete=0 limit 1
    </select>


</mapper>
