<?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.xinelu.manage.mapper.trainingorderitemdirectory.TrainingOrderItemDirectoryMapper">

    <resultMap type="TrainingOrderItemDirectory" id="TrainingOrderItemDirectoryResult">
        <result property="id" column="id"/>
        <result property="trainingOrderDetailsId" column="training_order_details_id"/>
        <result property="trainingOrderNo" column="training_order_no"/>
        <result property="trainingItemDirectoryId" column="training_item_directory_id"/>
        <result property="itemDirectoryName" column="item_directory_name"/>
        <result property="itemDirectoryCode" column="item_directory_code"/>
        <result property="itemDirectoryTitle" column="item_directory_title"/>
        <result property="itemDirectoryVideoDuration" column="item_directory_video_duration"/>
        <result property="itemDirectoryIntroduce" column="item_directory_introduce"/>
        <result property="itemDirectoryWatchStatus" column="item_directory_watch_status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectTrainingOrderItemDirectoryVo">
        select id,
               training_order_details_id,
               training_order_no,
               training_item_directory_id,
               item_directory_name,
               item_directory_code,
               item_directory_title,
               item_directory_url,
               item_directory_video_duration,
               item_directory_introduce,
               item_directory_watch_status,
               create_by,
               create_time,
               update_by,
               update_time
        from training_order_item_directory
    </sql>

    <select id="selectTrainingOrderItemDirectoryList" parameterType="TrainingOrderItemDirectory"
            resultMap="TrainingOrderItemDirectoryResult">
        <include refid="selectTrainingOrderItemDirectoryVo"/>
        <where>
            <if test="trainingOrderDetailsId != null ">
                and training_order_details_id = #{trainingOrderDetailsId}
            </if>
            <if test="trainingOrderNo != null  and trainingOrderNo != ''">
                and training_order_no = #{trainingOrderNo}
            </if>
            <if test="trainingItemDirectoryId != null ">
                and training_item_directory_id = #{trainingItemDirectoryId}
            </if>
            <if test="itemDirectoryName != null  and itemDirectoryName != ''">
                and item_directory_name like concat('%', #{itemDirectoryName}, '%')
            </if>
            <if test="itemDirectoryCode != null  and itemDirectoryCode != ''">
                and item_directory_code = #{itemDirectoryCode}
            </if>
            <if test="itemDirectoryTitle != null  and itemDirectoryTitle != ''">
                and item_directory_title = #{itemDirectoryTitle}
            </if>
            <if test="itemDirectoryIntroduce != null  and itemDirectoryIntroduce != ''">
                and item_directory_introduce = #{itemDirectoryIntroduce}
            </if>
            <if test="itemDirectoryWatchStatus != null  and itemDirectoryWatchStatus != ''">
                and item_directory_watch_status = #{itemDirectoryWatchStatus}
            </if>
        </where>
    </select>

    <select id="selectTrainingOrderItemDirectoryById" parameterType="Long"
            resultMap="TrainingOrderItemDirectoryResult">
        <include refid="selectTrainingOrderItemDirectoryVo"/>
        where id = #{id}
    </select>

    <insert id="insertTrainingOrderItemDirectory" parameterType="TrainingOrderItemDirectory" useGeneratedKeys="true"
            keyProperty="id">
        insert into training_order_item_directory
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trainingOrderDetailsId != null">training_order_details_id,
            </if>
            <if test="trainingOrderNo != null">training_order_no,
            </if>
            <if test="trainingItemDirectoryId != null">training_item_directory_id,
            </if>
            <if test="itemDirectoryName != null">item_directory_name,
            </if>
            <if test="itemDirectoryCode != null">item_directory_code,
            </if>
            <if test="itemDirectoryTitle != null">item_directory_title,
            </if>
            <if test="itemDirectoryUrl != null">item_directory_url,
            </if>
            <if test="itemDirectoryVideoDuration != null">item_directory_video_duration,
            </if>
            <if test="itemDirectoryIntroduce != null">item_directory_introduce,
            </if>
            <if test="itemDirectoryWatchStatus != null">item_directory_watch_status,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="trainingOrderDetailsId != null">#{trainingOrderDetailsId},
            </if>
            <if test="trainingOrderNo != null">#{trainingOrderNo},
            </if>
            <if test="trainingItemDirectoryId != null">#{trainingItemDirectoryId},
            </if>
            <if test="itemDirectoryName != null">#{itemDirectoryName},
            </if>
            <if test="itemDirectoryCode != null">#{itemDirectoryCode},
            </if>
            <if test="itemDirectoryTitle != null">#{itemDirectoryTitle},
            </if>
            <if test="itemDirectoryUrl != null">#{itemDirectoryUrl},
            </if>
            <if test="itemDirectoryVideoDuration != null">#{itemDirectoryVideoDuration},
            </if>
            <if test="itemDirectoryIntroduce != null">#{itemDirectoryIntroduce},
            </if>
            <if test="itemDirectoryWatchStatus != null">#{itemDirectoryWatchStatus},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateTrainingOrderItemDirectory" parameterType="TrainingOrderItemDirectory">
        update training_order_item_directory
        <trim prefix="SET" suffixOverrides=",">
            <if test="trainingOrderDetailsId != null">training_order_details_id =
                #{trainingOrderDetailsId},
            </if>
            <if test="trainingOrderNo != null">training_order_no =
                #{trainingOrderNo},
            </if>
            <if test="trainingItemDirectoryId != null">training_item_directory_id =
                #{trainingItemDirectoryId},
            </if>
            <if test="itemDirectoryName != null">item_directory_name =
                #{itemDirectoryName},
            </if>
            <if test="itemDirectoryCode != null">item_directory_code =
                #{itemDirectoryCode},
            </if>
            <if test="itemDirectoryTitle != null">item_directory_title =
                #{itemDirectoryTitle},
            </if>
            <if test="itemDirectoryUrl != null">item_directory_url =
                #{itemDirectoryUrl},
            </if>
            <if test="itemDirectoryVideoDuration != null">item_directory_video_duration =
                #{itemDirectoryVideoDuration},
            </if>
            <if test="itemDirectoryIntroduce != null">item_directory_introduce =
                #{itemDirectoryIntroduce},
            </if>
            <if test="itemDirectoryWatchStatus != null">item_directory_watch_status =
                #{itemDirectoryWatchStatus},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTrainingOrderItemDirectoryById" parameterType="Long">
        delete
        from training_order_item_directory
        where id = #{id}
    </delete>

    <delete id="deleteTrainingOrderItemDirectoryByIds" parameterType="String">
        delete from training_order_item_directory where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <insert id="insertTrainingOrderItemDirectoryList" parameterType="java.util.List">
        insert into training_order_item_directory(
        training_order_details_id,
        training_order_no,
        training_item_directory_id,
        item_directory_name,
        item_directory_code,
        item_directory_title,
        item_directory_url,
        item_directory_video_duration,
        item_directory_introduce,
        item_directory_watch_status,
        create_by,
        create_time,
        update_by,
        update_time
        ) values
        <foreach item="trainingOrderItemDirectory" index="index" collection="list" separator=",">
            (
            #{trainingOrderItemDirectory.trainingOrderDetailsId},
            #{trainingOrderItemDirectory.trainingOrderNo},
            #{trainingOrderItemDirectory.trainingItemDirectoryId},
            #{trainingOrderItemDirectory.itemDirectoryName},
            #{trainingOrderItemDirectory.itemDirectoryCode},
            #{trainingOrderItemDirectory.itemDirectoryTitle},
            #{trainingOrderItemDirectory.itemDirectoryUrl},
            #{trainingOrderItemDirectory.itemDirectoryVideoDuration},
            #{trainingOrderItemDirectory.itemDirectoryIntroduce},
            #{trainingOrderItemDirectory.itemDirectoryWatchStatus},
            #{trainingOrderItemDirectory.createBy},
            #{trainingOrderItemDirectory.createTime},
            #{trainingOrderItemDirectory.updateBy},
            #{trainingOrderItemDirectory.updateTime}
            )
        </foreach>
    </insert>

    <update id="updatePersonTrainingOrderItemDirectory" parameterType="TrainingOrderItemDirectory">
        UPDATE training_order_item_directory
        SET item_directory_watch_status = #{itemDirectoryWatchStatus},
            update_time = now()
        WHERE training_order_no = #{trainingOrderNo}
          AND training_item_directory_id = #{trainingItemDirectoryId}
    </update>

    <select id="getTrainingOrderItemDirectoryList"
            resultType="com.xinelu.manage.vo.trainingorderitemdirectory.TrainingOrderItemDirectoryInfoVO">
        SELECT
        t.nurse_station_id,
        t.nurse_station_person_id,
        t.nurse_person_name,
        t.training_order_status,
        tod.training_item_id,
        tod.training_item_type,
        tod.training_order_id,
        toid.training_item_directory_id,
        toid.training_order_no,
        toid.item_directory_watch_status,
        (SELECT watch_time FROM training_item_watch_record WHERE training_item_directory_id =
        toid.training_item_directory_id AND training_order_id = tod.training_order_id) watch_time
        FROM
        training_order_item_directory toid
        LEFT JOIN training_order_details tod ON toid.training_order_no = tod.training_order_no
        LEFT JOIN training_order t ON tod.training_order_no = t.training_order_no
        <where>
            <if test="nurseStationPersonId != null ">
                and t.nurse_station_person_id = #{nurseStationPersonId}
            </if>
            <if test="trainingOrderStatus != null  and trainingOrderStatus != ''">
                and t.training_order_status = #{trainingOrderStatus}
            </if>
            <if test="trainingItemId != null ">
                and tod.training_item_id = #{trainingItemId}
            </if>
            <if test="trainingItemDirectoryIdList != null and trainingItemDirectoryIdList.size() > 0">
                and toid.training_item_directory_id in
                <foreach item="trainingItemDirectoryId" collection="trainingItemDirectoryIdList" open="(" separator=","
                         close=")">
                    #{trainingItemDirectoryId}
                </foreach>
            </if>
            AND t.del_flag = 0
            AND tod.del_flag = 0
        </where>
        GROUP BY
        toid.training_item_directory_id
    </select>
</mapper>
