<?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.ruoyi.project.training.spec.mapper.TStSuccessorMapper">

    <resultMap type="TStSuccessor" id="TStSuccessorResult">
        <result property="id"    column="id"    />
        <result property="staffId"    column="staff_id"    />
        <result property="remarks"    column="remarks"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="deptId"    column="dept_id"    />
        <result property="actualpost"    column="actualpost"    />
        <result property="mentorStaffId"    column="mentor_staff_id"    />
        <result property="state"    column="state"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTStSuccessorVo">
        select d.id, d.staff_id,t.name as staffName,m.name as mentorStaffName, d.remarks, d.del_flag, d.creater_code,
        d.createdate, d.updater_code, d.updatedate, d.dept_id, d.actualpost, d.mentor_staff_id, d.state ,s.dept_name
        from t_st_successor d
        left join sys_dept s on s.dept_id = d.dept_id
        left join t_staffmgr t on t.staffid = d.staff_id
        left join t_staffmgr m on m.staffid = d.mentor_staff_id
    </sql>

    <!-- 根据学员id查询继任者清单详细信息 -->
    <select id="selectTStSuccessorBySuccessorId" parameterType="Long" resultMap="TStSuccessorResult">
        <include refid="selectTStSuccessorVo"/>
        where d.staff_id = #{staffId}
    </select>

    <!-- 根据staffId修改“进行中”的继任者计划状态为“因离职中止” -->
    <update id="updateStateByStaffId" parameterType="Long">
        update t_st_successor set state = 2 where (staff_id = #{staffId} or mentor_staff_id = #{staffId}) and state = 0
    </update>

    <!--根据导师ID查询学员列表-->
    <select id="selectSuccessorListByMentorId" parameterType="TStSuccessor" resultMap="TStSuccessorResult">
        <include refid="selectTStSuccessorVo"/>
        <where>
            <if test="mentorStaffId != null  and mentorStaffId != ''"> and mentor_staff_id = #{mentorStaffId}</if>
            and d.del_flag = 0
            and d.state != 2
        </where>
        <!-- 数据范围过滤 -->
    </select>

    <!--根据学员ID查询导师列表-->
    <select id="selectMentorListByStaffId" parameterType="String" resultMap="TStSuccessorResult">
        <include refid="selectTStSuccessorVo"/>
        <where>
            <if test="mentorStaffId != null  and mentorStaffId != ''"> and staff_id = #{staffId}</if>
            and d.del_flag = 0
            and d.state != 2
        </where>
        <!-- 数据范围过滤 -->
    </select>

    <select id="selectTStSuccessorList" parameterType="TStSuccessor" resultMap="TStSuccessorResult">
        <include refid="selectTStSuccessorVo"/>
        <where>
            <if test="staffId != null  and staffId != ''"> and staff_id = #{staffId}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="mentorStaffId != null  and mentorStaffId != ''"> and mentor_staff_id = #{mentorStaffId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTStSuccessorById" parameterType="Long" resultMap="TStSuccessorResult">
        <include refid="selectTStSuccessorVo"/>
        where d.id = #{id}
    </select>

    <insert id="insertTStSuccessor" parameterType="TStSuccessor">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_st_successor.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_st_successor
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="staffId != null">staff_id,</if>
            <if test="remarks != null">remarks,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="actualpost != null">actualpost,</if>
            <if test="mentorStaffId != null">mentor_staff_id,</if>
            <if test="state != null">state,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="staffId != null">#{staffId},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="actualpost != null">#{actualpost},</if>
            <if test="mentorStaffId != null">#{mentorStaffId},</if>
            <if test="state != null">#{state},</if>
         </trim>
    </insert>

    <update id="updateTStSuccessor" parameterType="TStSuccessor">
        update t_st_successor
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="actualpost != null">actualpost = #{actualpost},</if>
            <if test="mentorStaffId != null">mentor_staff_id = #{mentorStaffId},</if>
            <if test="state != null">state = #{state},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTStSuccessorById" parameterType="Long">
        update t_st_successor set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTStSuccessorByIds" parameterType="String">
        update t_st_successor set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
