<?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.inspect.patrol.mapper.PatrolVideotapeMapper">

    <resultMap type="PatrolVideotape" id="PatrolVideotapeResult">
        <result property="videotapeId" column="videotape_id"/>
        <result property="monitorId" column="monitor_id"/>
        <result property="videotapeDuration" column="videotape_duration"/>
        <result property="thumbnailUrlPath" column="thumbnail_url_path"/>
        <result property="videotapeUrlPath" column="videotape_url_path"/>
        <result property="videotapeType" column="videotape_type"/>
        <result property="deviceId" column="device_id"/>
        <result property="alertType" column="alert_type"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectPatrolVideotapeVo">
        select videotape_id,
               monitor_id,
               videotape_duration,
               thumbnail_url_path,
               videotape_url_path,
               videotape_type,
               device_pos_id,
               alert_type,
               create_time
        from patrol_videotape
    </sql>

    <select id="selectPatrolVideotapeList" parameterType="PatrolVideotapeListModel" resultMap="PatrolVideotapeResult">
        <include refid="selectPatrolVideotapeVo"/>
        <where>
            <if test="monitorId != null ">and monitor_id = #{monitorId}</if>
            <if test="videotapeType != null  and videotapeType != ''">and videotape_type = #{videotapeType}</if>
            <if test="deviceId != null ">and device_id = #{deviceId}</if>
            <if test="alertType != null  and alertType != ''">and alert_type = #{alertType}</if>
            <if test="startTime != null  and startTime != ''">and create_time &gt;=
                str_to_date(#{startTime},'%Y%m%d%H%i%s')
            </if>
            <if test="endTime != null  and endTime != ''">and create_time &lt;= str_to_date(#{endTime},'%Y%m%d%H%i%s')
            </if>
        </where>
    </select>

    <select id="selectPatrolVideotapeByVideotapeId" parameterType="Long" resultMap="PatrolVideotapeResult">
        <include refid="selectPatrolVideotapeVo"/>
        where videotape_id = #{videotapeId}
    </select>

    <insert id="insertPatrolVideotape" parameterType="PatrolVideotape">
        insert into patrol_videotape
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="videotapeId != null">videotape_id,</if>
            <if test="monitorId != null">monitor_id,</if>
            <if test="videotapeDuration != null">videotape_duration,</if>
            <if test="thumbnailUrlPath != null">thumbnail_url_path,</if>
            <if test="videotapeUrlPath != null">videotape_url_path,</if>
            <if test="videotapeType != null">videotape_type,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="alertType != null">alert_type,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="videotapeId != null">#{videotapeId},</if>
            <if test="monitorId != null">#{monitorId},</if>
            <if test="videotapeDuration != null">#{videotapeDuration},</if>
            <if test="thumbnailUrlPath != null">#{thumbnailUrlPath},</if>
            <if test="videotapeUrlPath != null">#{videotapeUrlPath},</if>
            <if test="videotapeType != null">#{videotapeType},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="alertType != null">#{alertType},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updatePatrolVideotape" parameterType="PatrolVideotape">
        update patrol_videotape
        <trim prefix="SET" suffixOverrides=",">
            <if test="monitorId != null">monitor_id = #{monitorId},</if>
            <if test="videotapeDuration != null">videotape_duration = #{videotapeDuration},</if>
            <if test="thumbnailUrlPath != null">thumbnail_url_path = #{thumbnailUrlPath},</if>
            <if test="videotapeUrlPath != null">videotape_url_path = #{videotapeUrlPath},</if>
            <if test="videotapeType != null">videotape_type = #{videotapeType},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="alertType != null">alert_type = #{alertType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where videotape_id = #{videotapeId}
    </update>

    <delete id="deletePatrolVideotapeByVideotapeId" parameterType="Long">
        delete
        from patrol_videotape
        where videotape_id = #{videotapeId}
    </delete>

    <delete id="deletePatrolVideotapeByVideotapeIds" parameterType="String">
        delete from patrol_videotape where videotape_id in
        <foreach item="videotapeId" collection="array" open="(" separator="," close=")">
            #{videotapeId}
        </foreach>
    </delete>
</mapper>