<?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.DepartmentsStaffRelateDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.lz.modules.app.entity.DepartmentsStaffRelateEntity" id="departmentsStaffRelateMap">
        <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="staffId" column="staff_id"/>
        <result column="level" property="level"/>
        <result column="is_leader" property="isLeader"/>
    </resultMap>



    <insert id="addDepartmentStaffRelateBatch">
        INSERT INTO lz_departments_staff_relate(department_id, staff_id)
        VALUES
        <foreach collection="relates" item="relate" separator=",">
            (#{relate.departmentId}, #{relate.staffId})
        </foreach>
    </insert>

    <update id="deleteAllRelates">
        update lz_departments_staff_relate set is_delete=1
    </update>

    <select id="getRelateInfoByStaffId" resultType="String">
        select department_id from lz_departments_staff_relate where is_delete=0 and staff_id = #{staffId}
	</select>

    <select id="getRelateByStaffIdAndDepartmentId" resultType="java.lang.String">
        select department_id from lz_departments_staff_relate where is_delete=0 and staff_id = #{staffId} and department_id=#{departmentId} limit 1
    </select>


    <update id="updateByStaffId">
        update lz_departments_staff_relate set is_delete=0, update_time=now(), department_id=#{departmentId}, is_leader=#{staffId.isLeader} where is_delete=0 and staff_id = #{staffId.id}
    </update>

    <select id="selectByDepartmentIds" resultType="com.lz.modules.app.entity.DepartmentsStaffRelateEntity">
        select * from lz_departments_staff_relate where is_delete=0  and  department_id in
        <foreach collection="departmentIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="selectByStaffIds" resultType="com.lz.modules.app.entity.DepartmentsStaffRelateEntity">
        select * from ( select * from lz_departments_staff_relate where is_delete=0  and  staff_id in
        <foreach collection="staffIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
        order by level desc) t
        group by  staff_id
    </select>
    <select id="selectAll" resultType="com.lz.modules.app.entity.DepartmentsStaffRelateEntity">
        select * from lz_departments_staff_relate where is_delete = 0
    </select>

    <select id="selectLastDepartmentByStaffId"
            resultType="com.lz.modules.app.entity.DepartmentsStaffRelateEntity">
        select * from ( select * from lz_departments_staff_relate where is_delete=0  and  staff_id  = #{staffId} order by level desc) t  group by  staff_id
    </select>

    <select id="selectLeaderByDepartmentId"
            resultType="com.lz.modules.app.entity.DepartmentsStaffRelateEntity">
        select * from lz_departments_staff_relate where is_delete=0  and  department_id  = #{departmentId} and  is_leader = 1 limit 1
    </select>

    <select id="selectDepartmentByDepartmentId"
            resultType="com.lz.modules.app.entity.DepartmentsStaffRelateEntity">
        select * from lz_departments_staff_relate where is_delete=0  and  department_id  = #{departmentId}  limit 1
    </select>


    <select id="selectByStaffId" resultType="com.lz.modules.app.entity.DepartmentsStaffRelateEntity">
        select * from lz_departments_staff_relate where is_delete=0 and staff_id = #{staffId} limit 1
    </select>


    <select id="getDepartmentNameByStaffIds" resultType="com.lz.modules.app.dto.ReportProgressListDto">
        SELECT department_name,staff_id from lz_departments_staff_relate r
        LEFT JOIN
        lz_departments d
        on r.department_id = d.department_id
        where r.is_delete=0 and d.is_delete=0
        and r.staff_id in
        <foreach collection="staffIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>

    </select>

    <select id="selectStaffIdsByDepartments" resultType="String">
        SELECT staff_id from lz_departments_staff_relate where is_delete = 0 and
        department_id in
        <foreach collection="deparmentIds" item="department_id" open="(" close=")"
                 separator=",">
            #{department_id}
        </foreach>
    </select>

    <select id="selectLeadersByDepartmentId"
            resultType="com.lz.modules.app.entity.StaffEntity">
        select staff.* from lz_staff as staff join lz_departments_staff_relate as relate on staff.id = relate.staff_id
          where relate.is_delete=0  and  relate.department_id  = #{depId} and  relate.is_leader = 1
    </select>
</mapper>
