<?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.TTrainingDeviceMapper">
    
    <resultMap type="TTrainingDevice" id="TTrainingDeviceResult">
        <result property="id"    column="id"    />
        <result property="staffId"    column="staff_id"    />
        <result property="regularId"    column="regular_id"    />
        <result property="startDate"    column="start_date"    />
        <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="courseCode"    column="courseid"    />
        <result property="item"    column="course"    />
        <result property="planTrainingdate"    column="course_startdate"    />
        <result property="lecturer"    column="trainer"    />
        <result property="lecturerName"    column="name"    />
        <result property="hour"    column="duration"    />
        <result property="year"    column="years"    />
        <result property="supplementary"    column="supplementary"    />
        <result property="trainingType"    column="training_type"    />
    </resultMap>

    <sql id="selectTTrainingDeviceVo">
        select d.id, t.training_type, s.name, t.courseid, t.course, t.course_startdate, t.trainer, t.duration, t.years, d.staff_id, d.regular_id, d.start_date, d.remarks, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.supplementary from t_training_device d
        left join t_training t on t.id = d.regular_id
        left join t_staffmgr s on s.staffid = t.trainer
    </sql>

    <select id="selectTTrainingDeviceList" parameterType="TTrainingDevice" resultMap="TTrainingDeviceResult">
        <include refid="selectTTrainingDeviceVo"/>
        <where>  
            <if test="staffId != null  and staffId != ''"> and staff_id = #{staffId}</if>
            <if test="regularId != null "> and d.regular_id = #{regularId}</if>
            <if test="startDate != null "> and start_date = #{startDate}</if>
            <if test="year != null  and year != ''"> and t.years = #{year}</if>
            <if test="trainingType != null and trainingType != ''"> and t.training_type = #{trainingType}</if>
            and d.del_flag = 0
        </where>
    </select>
    
    <select id="selectTTrainingDeviceById" parameterType="Long" resultMap="TTrainingDeviceResult">
        <include refid="selectTTrainingDeviceVo"/>
        where d.id = #{id}
    </select>
        
    <insert id="insertTTrainingDevice" parameterType="TTrainingDevice">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_training_device.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_training_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="staffId != null">staff_id,</if>
            <if test="regularId != null">regular_id,</if>
            <if test="startDate != null">start_date,</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="supplementary != null">supplementary,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="staffId != null">#{staffId},</if>
            <if test="regularId != null">#{regularId},</if>
            <if test="startDate != null">#{startDate},</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="supplementary != null">#{supplementary},</if>
         </trim>
    </insert>

    <update id="updateTTrainingDevice" parameterType="TTrainingDevice">
        update t_training_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="regularId != null">regular_id = #{regularId},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="startDate == null">start_date = NULL,</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="supplementary != null">supplementary = #{supplementary},</if>
        </trim>
        where id = #{id}
    </update>

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

    <update id="deleteTTrainingDevice" parameterType="TTrainingDevice">
        update t_training_device set del_flag = 2 where regular_id = #{regularId}
    </update>

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