<?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.huashe.park.core.mapper.BdCameraInfoMapper">

    <resultMap type="BdCameraInfo" id="BdCameraInfoResult">
        <result property="id" column="id"/>
        <result property="cameraName" column="camera_name"/>
        <result property="cameraStream" column="camera_stream"/>
        <result property="liveStream" column="live_stream"/>
        <result property="channelCode" column="channel_code"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <sql id="selectBdCameraInfoVo">
        select id,
               camera_name,
               camera_stream,
               live_stream,
               channel_code,
               update_time,
               create_time,
               create_by,
               update_by
        from bd_camera_info
    </sql>

    <select id="selectBdCameraInfoList" parameterType="BdCameraInfo" resultMap="BdCameraInfoResult">
        <include refid="selectBdCameraInfoVo"/>
        <where>
            <if test="cameraName != null  and cameraName != ''">and camera_name like concat('%', #{cameraName}, '%')
            </if>
        </where>
    </select>

    <select id="selectBdCameraFence" resultType="VideoFence">
        SELECT fence.id,
               fence.defence_name,
               ST_AsText(poly) poly,
               camera.camera_name,
               camera.camera_stream,
               camera.live_stream
        FROM bd_fence_info fence
                 INNER JOIN bd_camera_info camera ON fence.location_id = camera.id
            AND fence.fence_type = 3
    </select>

    <select id="selectBdCameraInfoById" parameterType="Long" resultMap="BdCameraInfoResult">
        <include refid="selectBdCameraInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertBdCameraInfo" parameterType="BdCameraInfo" useGeneratedKeys="true" keyProperty="id">
        insert into bd_camera_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cameraName != null">camera_name,</if>
            <if test="cameraStream != null">camera_stream,</if>
            <if test="liveStream != null">live_stream,</if>
            <if test="channelCode != null">channel_code,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cameraName != null">#{cameraName},</if>
            <if test="cameraStream != null">#{cameraStream},</if>
            <if test="liveStream != null">#{liveStream},</if>
            <if test="channelCode != null">#{channelCode},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>

    <update id="updateBdCameraInfo" parameterType="BdCameraInfo">
        update bd_camera_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="cameraName != null">camera_name = #{cameraName},</if>
            <if test="cameraStream != null">camera_stream = #{cameraStream},</if>
            <if test="liveStream != null">live_stream = #{liveStream},</if>
            <if test="channelCode != null">channel_code = #{channelCode},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBdCameraInfoById" parameterType="Long">
        delete
        from bd_camera_info
        where id = #{id}
    </delete>

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