<?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.mapper.TTrainingParticipantsMapper">

    <resultMap type="TTrainingParticipants" id="TTrainingParticipantsResult">
        <result property="id" column="id"/>
        <result property="staffId" column="staff_id"/>
        <result property="companyId" column="company_id"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="score" column="score"/>
        <result property="operationScore" column="operation_score"/>
        <result property="trainingPlace" column="training_place"/>
        <result property="trainingDuration" column="training_duration"/>
        <result property="trainingCost" column="training_cost"/>
        <result property="budget" column="budget"/>
        <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="companyItem" column="item"/>
        <result property="frequency" column="frequency"/>
        <result property="courseCode" column="course_code"/>
    </resultMap>

    <sql id="selectTTrainingParticipantsVo">
        select d.id,
               d.staff_id,
               d.company_id,
               s.item,
               s.frequency,
               s.course_code,
               d.start_date,
               d.end_date,
               d.score,
               d.operation_score,
               d.training_place,
               d.training_duration,
               d.training_cost,
               d.budget,
               d.remarks,
               d.del_flag,
               d.creater_code,
               d.createdate,
               d.updater_code,
               d.updatedate
        from t_training_participants d
                 left join t_training_companylevel s on d.company_id = s.id
    </sql>

    <select id="selectTTrainingParticipantsList" parameterType="TTrainingParticipants"
            resultMap="TTrainingParticipantsResult">
        <include refid="selectTTrainingParticipantsVo"/>
        <where>
            <if test="staffId != null ">and d.staff_id = #{staffId}</if>
            <if test="companyId != null ">and company_id = #{companyId}</if>
            and d.del_flag = 0
        </where>
        order by s.course_code
    </select>

    <select id="selectTTrainingParticipantsListTask" parameterType="TTrainingParticipants"
            resultMap="TTrainingParticipantsResult">
        <include refid="selectTTrainingParticipantsVo"/>
        <where>
            <if test="staffId != null ">and d.staff_id = #{staffId}</if>
            <if test="companyId != null ">and company_id = #{companyId}</if>
            and d.del_flag = 0
        </where>
        order by end_date
    </select>


    <select id="selectTTrainingParticipantsById" parameterType="Long" resultMap="TTrainingParticipantsResult">
        <include refid="selectTTrainingParticipantsVo"/>
        where id = #{id}
    </select>

    <insert id="insertTTrainingParticipants" parameterType="TTrainingParticipants">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_training_participants.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_training_participants
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="staffId != null">staff_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="score != null">score,</if>
            <if test="operationScore != null">operation_score,</if>
            <if test="trainingPlace != null">training_place,</if>
            <if test="trainingDuration != null">training_duration,</if>
            <if test="trainingCost != null">training_cost,</if>
            <if test="budget != null">budget,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="staffId != null">#{staffId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="score != null">#{score},</if>
            <if test="operationScore != null">#{operationScore},</if>
            <if test="trainingPlace != null">#{trainingPlace},</if>
            <if test="trainingDuration != null">#{trainingDuration},</if>
            <if test="trainingCost != null">#{trainingCost},</if>
            <if test="budget != null">#{budget},</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>
        </trim>
    </insert>

    <update id="updateTTrainingParticipants" parameterType="TTrainingParticipants">
        update t_training_participants
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="score != null">score = #{score},</if>
            <if test="operationScore != null">operation_score = #{operationScore},</if>
            <if test="trainingPlace != null">training_place = #{trainingPlace},</if>
            <if test="trainingDuration != null">training_duration = #{trainingDuration},</if>
            <if test="trainingCost != null">training_cost = #{trainingCost},</if>
            <if test="budget != null">budget = #{budget},</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>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTTrainingParticipantsByStaffAndCom" parameterType="TTrainingParticipants">
        update t_training_participants
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="score != null">score = #{score},</if>
            <if test="operationScore != null">operation_score = #{operationScore},</if>
            <if test="trainingPlace != null">training_place = #{trainingPlace},</if>
            <if test="trainingDuration != null">training_duration = #{trainingDuration},</if>
            <if test="trainingCost != null">training_cost = #{trainingCost},</if>
            <if test="budget != null">budget = #{budget},</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>
        </trim>
        where staff_id = #{staffId} and company_id = #{companyId}
    </update>

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

    <update id="deleteTTrainingParticipantsByCompanyId" parameterType="TTrainingParticipants">
        update t_training_participants
        set del_flag = 2
        where company_id = #{companyId}
          and staff_id = #{staffId}
          and del_flag = 0
    </update>

    <update id="deleteTTrainingParticipantsAll">
        update t_training_participants
        set del_flag = 2
        where del_flag = 0
    </update>

    <delete id="deleteTParticipantsByCompanyId">
        delete from t_training_participants
        where company_id = #{companyId}
    </delete>

    <delete id="duplicateParticipants">
        DELETE
        FROM T_TRAINING_PARTICIPANTS
        where rowid !=( select max(rowid)
        from T_TRAINING_PARTICIPANTS b
        where b.STAFF_ID = T_TRAINING_PARTICIPANTS.STAFF_ID
				and b.COMPANY_ID = T_TRAINING_PARTICIPANTS.COMPANY_ID
				and b.START_DATE = T_TRAINING_PARTICIPANTS.START_DATE
        )
    </delete>

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

</mapper>
