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

    <resultMap type="TStPlan" id="TStPlanResult">
        <result property="id"    column="id"    />
        <result property="staffId"    column="staff_id"    />
        <result property="staffName"    column="nick_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="startDate"    column="start_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="classHour"    column="class_hour"    />
        <result property="classContent"    column="class_content"    />
        <result property="studyState"    column="study_state"    />
        <result property="deptName" column="dept_name" />
        <result property="score" column="score" />
        <result property="overallComment" column="overall_comment" />
        <result property="approveStatus" column="APPROVE_STATUS" />
        <result property="dateOfCompletion" column="date_of_completion" />
        <result property="yearPlanId" column="year_plan_id" />
    </resultMap>

    <sql id="selectTStPlanVo">
        select
            d.year_plan_id,
            d.date_of_completion,
            d.overall_comment,
            d.score,
            d.id,
            d.staff_id,
            d.APPROVE_STATUS,
            u.nick_name,
            d.plant_name,
            d.del_flag,
            d.createdate,
            d.updatedate,
            d.dept_id,
            d.start_date,
            d.end_date,
            d.class_hour,
            d.class_content,
            d.study_state,
            u1.nick_name as updateBy,
            u2.nick_name as createBy,
            s.dept_name,
            suc.MENTOR_STAFF_ID
        from t_st_plan d
        left join sys_dept s on s.dept_id = d.dept_id
        left join sys_user u on d.staff_id = u.staffid
        LEFT JOIN SYS_USER u1 on d.updater_code = u1.USER_ID
        LEFT JOIN SYS_USER u2 on d.creater_code = u2.USER_ID
        left join T_ST_SUCCESSOR suc on d.STAFF_ID = suc.STAFF_ID
        left join T_ST_YEARPlAN yp on d.year_plan_id = yp.id
    </sql>

    <!-- 根据staffId修改“未完成”的继任者计划状态为“因离职中止” -->
    <update id="updateStudyStateByStaffId" parameterType="Long">
        update t_st_plan set study_state = 4 where (staff_id = #{staffId} or staff_id in (select staff_id from T_ST_SUCCESSOR where mentor_staff_id = #{staffId})) and study_state in (0,2,3)
    </update>

    <!-- 查询季度培训计划列表 -->
    <select id="selectSeasonalTStPlanList" parameterType="TStFeedback" resultMap="TStPlanResult">
        <include refid="selectTStPlanVo"/>
        <where>
            <if test="successorId != null  and successorId != ''">
                and d.staff_id = #{successorId}
            </if>
            <if test="mentorId != null  and mentorId != ''">
                and suc.MENTOR_STAFF_ID = #{mentorId}
            </if>
            <if test="feedbackYear != null  and feedbackYear != ''">
                and extract(year from d.start_date) = #{feedbackYear}
            </if>
            <if test="feedbackSeason != null  and feedbackSeason != ''">
                and floor((extract(month from d.start_date) + 2) / 3) &lt;= #{feedbackSeason}
                and floor((extract(month from d.end_date) + 2) / 3) &gt;= #{feedbackSeason}
            </if>
            and d.del_flag = 0
            and d.APPROVE_STATUS = 1
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by d.start_date
    </select>

    <!-- 查询月度培训计划列表 -->
    <select id="selectMonthlyTStPlanList" parameterType="TStFeedback" resultMap="TStPlanResult">
        <include refid="selectTStPlanVo"/>
        <where>
            <if test="successorId != null  and successorId != ''">
                and d.staff_id = #{successorId}
            </if>
            <if test="mentorId != null  and mentorId != ''">
                and suc.MENTOR_STAFF_ID = #{mentorId}
            </if>
            <if test="feedbackYear != null  and feedbackYear != ''">
                and extract(year from d.start_date) = #{feedbackYear}
            </if>
            <if test="feedbackMonth != null  and feedbackMonth != ''">
                and extract(month from d.start_date) &lt;= #{feedbackMonth}
                and extract(month from d.end_date) &gt;= #{feedbackMonth}
            </if>
            and d.del_flag = 0
            and d.APPROVE_STATUS = 1
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by d.start_date
    </select>

    <select id="selectTStPlan" parameterType="TStPlan" resultMap="TStPlanResult">
        <include refid="selectTStPlanVo"/>
        <where>
            <if test="yearPlanId != null  and yearPlanId != ''"> and d.year_plan_id = #{yearPlanId}</if>
            <if test="staffId != null  and staffId != ''"> and d.staff_id = #{staffId}</if>
            <if test="plantName != null  and plantName != ''"> and plant_name = #{plantName}</if>
            <if test="startDate != null "> and start_date = #{startDate}</if>
            <if test="endDate != null "> and end_date = #{endDate}</if>
            <if test="classHour != null  and classHour != ''"> and class_hour = #{classHour}</if>
            <if test="studyState != null  and studyState != ''"> and study_state = #{studyState}</if>
            <if test="mentorStaffId != null  and mentorStaffId != ''"> and mentor_staff_id = #{mentorStaffId}</if>
            and d.del_flag = 0
        </where>
    </select>

    <select id="selectTStPlanList" parameterType="TStPlan" resultMap="TStPlanResult">
        <include refid="selectTStPlanVo"/>
        <where>
            <if test="yearPlanId != null  and yearPlanId != ''"> and d.year_plan_id = #{yearPlanId}</if>
            <if test="year != null  and year != ''"> and yp.plan_year = #{year}</if>
            <if test="staffId != null  and staffId != ''"> and d.staff_id = #{staffId}</if>
            <if test="plantName != null  and plantName != ''"> and plant_name like concat(concat('%', #{plantName}), '%')</if>
            <if test="startDate != null "> and start_date &gt;= #{startDate}</if>
            <if test="endDate != null "> and end_date &lt;= #{endDate}</if>
            <if test="classHour != null  and classHour != ''"> and class_hour = #{classHour}</if>
            <if test="studyState != null  and studyState != ''"> and study_state = #{studyState}</if>
            <if test="mentorStaffId != null  and mentorStaffId != ''"> and mentor_staff_id = #{mentorStaffId}</if>
            and d.del_flag = 0
        </where>
        order by start_date
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTStPlanById" parameterType="Long" resultMap="TStPlanResult">
        <include refid="selectTStPlanVo"/>
        where d.id = #{id}
    </select>

    <insert id="insertTStPlan" parameterType="TStPlan">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_st_plan.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_st_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="staffId != null">staff_id,</if>
            <if test="plantName != null">plant_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="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="classHour != null">class_hour,</if>
            <if test="classContent != null">class_content,</if>
            <if test="studyState != null">study_state,</if>
            <if test="approveStatus != null">APPROVE_STATUS,</if>
            <if test="yearPlanId != null">year_plan_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="staffId != null">#{staffId},</if>
            <if test="plantName != null">#{plantName},</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="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="classHour != null">#{classHour},</if>
            <if test="classContent != null">#{classContent},</if>
            <if test="studyState != null">#{studyState},</if>
            <if test="approveStatus != null">#{approveStatus},</if>
            <if test="yearPlanId != null">#{yearPlanId},</if>
         </trim>
    </insert>

    <update id="updateTStPlan" parameterType="TStPlan">
        update t_st_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="plantName != null">plant_name = #{plantName},</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="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="classHour != null">class_hour = #{classHour},</if>
            <if test="classContent != null">class_content = #{classContent},</if>
            <if test="studyState != null">study_state = #{studyState},</if>
            <if test="score != null">
                score = #{score},
                <!-- 导师反馈的同时更新实际完成培训日期为当前日期 -->
                date_of_completion = SYSDATE,
            </if>
            <if test="overallComment != null">overall_comment = #{overallComment},</if>
            <if test="approveStatus != null">approve_status = #{approveStatus},</if>
            <if test="dateOfCompletion != null">date_of_completion = #{dateOfCompletion}</if>
        </trim>
        where id = #{id}
    </update>

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

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

</mapper>
