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

    <resultMap type="TStYearplan" id="TStYearplanResult">
        <result property="id"    column="id"    />
        <result property="staffId"    column="staff_id"    />
        <result property="name"    column="name"    />
        <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="approveStatus"    column="approve_status"    />
        <result property="deptName" column="dept_name" />
        <result property="planYear"    column="plan_year"    />
        <result property="staffName"    column="nick_name"    />

    </resultMap>

    <sql id="selectTStYearplanVo">
        select d.id, d.staff_id,u.nick_name, d.name,d.plan_year, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.approve_status ,s.dept_name
        from t_st_yearplan d
          left join sys_dept s on s.dept_id = d.dept_id and s.del_flag = 0
          left join sys_user u on d.staff_id = u.staffid and u.del_flag = 0
          left join T_ST_SUCCESSOR suc on d.STAFF_ID = suc.STAFF_ID and suc.del_flag = 0
    </sql>

    <select id="selectTStYearplanList" parameterType="TStYearplan" resultMap="TStYearplanResult">
        <include refid="selectTStYearplanVo"/>
        <where>
            <if test="staffId != null  and staffId != ''"> and d.staff_id = #{staffId}</if>
            <if test="name != null  and name != ''"> and d.name like concat(concat('%', #{name}), '%')</if>
            <if test="createrCode != null  and createrCode != ''"> and d.creater_code = #{createrCode}</if>
            <if test="createdate != null "> and d.createdate = #{createdate}</if>
            <if test="updaterCode != null  and updaterCode != ''"> and d.updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and d.updatedate = #{updatedate}</if>
            <if test="deptId != null "> and d.dept_id = #{deptId}</if>
            <if test="approveStatus != null "> and d.approve_status = #{approveStatus}</if>
            <if test="planYear != null "> and d.plan_year = #{planYear}</if>
            <if test="mentorStaffId != null  and mentorStaffId != ''"> and suc.mentor_staff_id = #{mentorStaffId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTStYearplanById" parameterType="Long" resultMap="TStYearplanResult">
        <include refid="selectTStYearplanVo"/>
        where d.id = #{id}
    </select>

    <select id="selectTStYearPlanExist" parameterType="TStYearplan" resultMap="TStYearplanResult">
        select
            d.id
        from t_st_yearplan d
        where d.staff_id = #{staffId} and d.plan_year = #{planYear}
          and d.del_flag = 0
    </select>

    <insert id="insertTStYearplan" parameterType="TStYearplan">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_st_yearplan.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_st_yearplan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="staffId != null">staff_id,</if>
            <if test="name != null">name,</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="approveStatus != null">approve_status,</if>
            <if test="planYear != null">plan_year,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="staffId != null">#{staffId},</if>
            <if test="name != null">#{name},</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="approveStatus != null">#{approveStatus},</if>
            <if test="planYear != null">#{planYear},</if>
        </trim>
    </insert>

    <update id="updateTStYearplan" parameterType="TStYearplan">
        update t_st_yearplan
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="name != null">name = #{name},</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="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="planYear != null">plan_year = #{planYear},</if>
        </trim>
        where id = #{id}
    </update>

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

    <update id="deleteTStYearplanByStaffId" parameterType="String">
        update t_st_yearplan set del_flag = 2 where staff_id = #{staffId}
    </update>


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

</mapper>
