<?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.flow.dao.StaffRoleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lz.modules.flow.entity.StaffRole">
        <id column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="staff_id" property="staffId"/>
        <result column="department_level" property="departmentLevel"/>
        <result column="type_role_ids" property="typeRoleIds"/>
        <result column="staff_name" property="staffName"/>
        <result column="evaluation_group_id" property="evaluationGroupId"/>
        <result column="department_id" property="departmentId"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id AS id, is_delete AS isDelete, gmt_create AS gmtCreate, gmt_modified AS gmtModified, staff_id AS staffId, department_level AS departmentLevel, type_role_ids AS typeRoleIds, staff_name AS staffName, evaluation_group_id AS evaluationGroupId, department_id AS departmentId
    </sql>




    <select id="selectStaffRoleById" resultType="StaffRole" >
        select * from lz_staff_role  where id=#{id} and is_delete = 0 limit 1
    </select>


    <insert id="insertStaffRole" parameterType="StaffRole" useGeneratedKeys="true" keyProperty="id" >
        insert into lz_staff_role(
        <if test="staffId != null">staff_id, </if>
        <if test="departmentLevel != null">department_level, </if>
        <if test="typeRoleIds != null">type_role_ids, </if>
        <if test="staffName != null">staff_name, </if>
        <if test="evaluationGroupId != null">evaluation_group_id, </if>
        <if test="departmentId != null">department_id, </if>
        is_delete,
        gmt_create,
        gmt_modified
        )values(
        <if test="staffId != null">#{ staffId}, </if>
        <if test="departmentLevel != null">#{ departmentLevel}, </if>
        <if test="typeRoleIds != null">#{ typeRoleIds}, </if>
        <if test="staffName != null">#{ staffName}, </if>
        <if test="evaluationGroupId != null">#{ evaluationGroupId}, </if>
        <if test="departmentId != null">#{ departmentId}, </if>
        0,
        now(),
        now()
        )
    </insert>


    <update id="updateStaffRoleById" parameterType="StaffRole" >
        update
        lz_staff_role
        <trim prefix="set" suffixOverrides=",">
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="gmtCreate != null">gmt_create = #{gmtCreate},</if>
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="departmentLevel != null">department_level = #{departmentLevel},</if>
            <if test="typeRoleIds != null">type_role_ids = #{typeRoleIds},</if>
            <if test="staffName != null">staff_name = #{staffName},</if>
            <if test="evaluationGroupId != null">evaluation_group_id = #{evaluationGroupId},</if>
            <if test="departmentId != null">department_id = #{departmentId}</if>
        </trim>
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="updateCoverStaffRoleById" parameterType="StaffRole" >
        update
            lz_staff_role
        set
            is_delete = #{isDelete},
            gmt_create = #{gmtCreate},
            staff_id = #{staffId},
            department_level = #{departmentLevel},
            type_role_ids = #{typeRoleIds},
            staff_name = #{staffName},
            evaluation_group_id = #{evaluationGroupId},
            department_id = #{departmentId}
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="deleteStaffRoleById" parameterType="java.lang.Long">
        delete from  lz_staff_role  where id=#{id} limit 1
    </update>





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

    <select id="selectByRole" resultType="com.lz.modules.flow.entity.StaffRole">
         select * from lz_staff_role  where  is_delete = 0 and department_level = #{departmentLevel}
    </select>

    <select id="selectByCondition" resultType="com.lz.modules.flow.entity.StaffRole">
        select * from lz_staff_role where is_delete = 0
        <if test="params.name != null and params.name != '' ">
            and staff_id in (select id from lz_staff where name like concat('%',#{params.name},'%'))
        </if>
         order by id desc
    </select>



    <select id="selectByGroupId" resultType="com.lz.modules.flow.entity.StaffRole">
          select * from lz_staff_role where is_delete = 0 and evaluation_group_id = #{id}
    </select>

    <select id="selectStaffRolesByStaffId" resultType="com.lz.modules.flow.entity.StaffRole">
          select * from lz_staff_role where is_delete = 0 and staff_id in (
            <foreach collection="list" item="item" separator=",">
                #{item}
            </foreach>
          )
    </select>



    <select id="selectByEvaluationGroupId" resultType="com.lz.modules.flow.entity.StaffRole">
        select sr.id,
            sr.is_delete,
            sr.gmt_modified,
            sr.gmt_create,
            sr.staff_id,
            sr.department_level,
            sr.type_role_ids,
            sr.staff_name,
            evg.evaluation_group_id,
            evg.is_select
         from lz_staff_role_evaluation_group evg left join lz_staff_role sr on evg.role_id = sr.id
        where evg.evaluation_group_id=#{id} and evg.is_delete = 0 and  sr.is_delete = 0
    </select>

    <select id="selectByRoleLevel" resultType="com.lz.modules.flow.entity.StaffRole">
        select * from lz_staff_role where is_delete = 0 and department_level = #{name}
    </select>


    <select id="selectPageByRoleLevel" resultType="com.lz.modules.flow.entity.StaffRole">
        select * from lz_staff_role where is_delete = 0 and department_level = #{name}
    </select>


    <select id="selectStaffRolesByStaffIdDepartmentLevel" resultType="com.lz.modules.flow.entity.StaffRole">
        select * from lz_staff_role where is_delete = 0 and department_level = #{departmentLevel} and staff_id =#{staffId}
    </select>

    <select id="selectAllByStaffId" resultType="com.lz.modules.flow.entity.StaffRole">
        select * from lz_staff_role where is_delete = 0 and staff_id =#{staffId}
    </select>


    <select id="selectAllMenus" resultType="com.lz.modules.flow.entity.StaffMenu">
            select * from lz_staff_menu  where id in (
        select menu_id from lz_staff_role_menu where role_id in (
        select id from lz_staff_role where staff_id = #{userId} and is_delete=0) and is_delete = 0 ) and is_delete = 0
    </select>

    <select id="selectAllStaffRoleByDepartmentLevel" resultType="com.lz.modules.flow.entity.StaffRole">
         select * from lz_staff_role where is_delete = 0 and department_level in
        <foreach collection="departmentLevels" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="selectStaffRolesByStaffIdDepartmentLevelList"  resultType="com.lz.modules.flow.entity.StaffRole">
        select * from lz_staff_role where is_delete = 0 and staff_id = #{staffId} and department_level in
        <foreach collection="departmentLevels" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="selectMastRoles" resultType="StaffRole" >
        select * from lz_staff_role  where department_level='MASTER_PM' and is_delete = 0
    </select>

    <select id="selectAllGroupManageRoles" resultType="StaffRole" >
        select * from lz_staff_role where evaluation_group_id=0 and is_delete=0
    </select>

    <select id="selectMenusByParentId" resultType="com.lz.modules.flow.entity.StaffMenu">
         select * from lz_staff_menu  where is_delete=0 and parent_id=#{parentId}
    </select>


</mapper>

