<?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.PatrolPresetPosMapper">

    <resultMap type="PatrolPresetPos" id="PatrolPresetPosResult">
        <result property="presetPosId" column="preset_pos_id"/>
        <result property="presetPosCode" column="preset_pos_code"/>
        <result property="presetPosName" column="preset_pos_name"/>
        <result property="patrolPointId" column="patrol_point_id"/>
        <result property="channelId" column="channel_id"/>
        <result property="coordinateType" column="coordinate_type"/>
        <result property="stopTime" column="stop_time"/>
        <result property="channelImg" column="channel_img"/>
        <result property="description" column="description"/>
        <result property="isEnable" column="is_enable"/>
        <result property="createTime" column="create_time"/>
        <result property="algSubtypeCode" column="alg_subtype_code"/>
        <result property="eqpBookId" column="eqp_book_id"/>
        <result property="channelCode" column="channel_code"/>
        <result property="channelType" column="channel_type"/>
        <result property="patrolDeviceName" column="patrol_device_name"/>
        <result property="patrolDeviceCode" column="patrol_device_code"/>
        <result property="videoNvrCode" column="video_nvr_code"/>
        <result property="purpose" column="purpose"/>
    </resultMap>
    <resultMap type="PatrolPresetPosSlim" id="PatrolPresetPosResultSlim">
        <result property="presetPosId" column="preset_pos_id"/>
        <result property="presetPosCode" column="preset_pos_code"/>
        <result property="presetPosName" column="preset_pos_name"/>
        <result property="patrolPointId" column="patrol_point_id"/>
        <result property="patrolDeviceName" column="patrol_device_name"/>
        <result property="patrolDeviceCode" column="patrol_device_code"/>
        <result property="algSubtypeCode" column="alg_subtype_code"/>
        <result property="channelId" column="channel_id"/>
        <result property="isEnable" column="is_enable"/>
        <result property="channelCode" column="channel_code"/>
        <result property="channelType" column="channel_type"/>
        <result property="videoNvrCode" column="video_nvr_code"/>
        <result property="host" column="host"/>
        <result property="port" column="port"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="address" column="address"/>
    </resultMap>
    <resultMap type="com.inspect.patrol.vo.CameraVo" id="CameraVo">
        <result property="eqpBookId" column="eqp_book_id"/>
        <result property="channelCode" column="channel_code"/>
        <result property="channelName" column="channel_name"/>
        <result property="channelId" column="channel_id"/>
        <result property="patrolDeviceCode" column="patrol_device_code"/>
        <result property="patrolDeviceName" column="patrol_device_name"/>
        <result property="useMode" column="use_mode"/>
        <result property="areaId" column="area_id"/>
    </resultMap>
    <sql id="selectPatrolPresetPosVo">
        select preset_pos_id,
               preset_pos_code,
               preset_pos_name,
               patrol_point_id,
               channel_id,
               coordinate_type,
               stop_time,
               channel_img,
               description,
               is_enable,
               create_time,
               alg_subtype_code,
               eqp_book_id,
               channel_code,
               channel_type,
               patrol_device_code,
               video_nvr_code
        from patrol_preset_pos
    </sql>

    <select id="selectPatrolPresetPosList" parameterType="PatrolPresetPos" resultMap="PatrolPresetPosResult">
        <include refid="selectPatrolPresetPosVo"/>
        <where>
            <if test="presetPosCode != null  and presetPosCode != ''">and preset_pos_code = #{presetPosCode}</if>
            <if test="presetPosName != null  and presetPosName != ''">and preset_pos_name like concat('%',
                #{presetPosName}, '%')
            </if>
            <if test="patrolPointId != null ">and patrol_point_id = #{patrolPointId}</if>
            <if test="channelId != null  and channelId != ''">and channel_id = #{channelId}</if>
            <if test="coordinateType != null  and coordinateType != ''">and coordinate_type = #{coordinateType}</if>
            <if test="stopTime != null ">and stop_time = #{stopTime}</if>
            <if test="channelImg != null  and channelImg != ''">and channel_img = #{channelImg}</if>
            <if test="description != null  and description != ''">and description = #{description}</if>
            <if test="isEnable != null  and isEnable != ''">and is_enable = #{isEnable}</if>
            <if test="patrolDeviceCode != null  and patrolDeviceCode != ''">and patrol_device_code = #{patrolDeviceCode}</if>
            <if test="channelCode != null  and channelCode != ''">and channel_code = #{channelCode}</if>
            <if test="videoNvrCode != null  and videoNvrCode != ''">and video_nvr_code = #{videoNvrCode}</if>
        </where>
    </select>
    <select id="selectPatrolPresetPosListByIds" parameterType="String" resultMap="PatrolPresetPosResult">
        <include refid="selectPatrolPresetPosVo"/>
        where preset_pos_id in
        <foreach item="presetPosId" collection="array" open="(" separator="," close=")">
            #{presetPosId}
        </foreach>
    </select>
    <select id="selectPatrolPresetPosByPresetPosId" parameterType="String" resultMap="PatrolPresetPosResult">
        <include refid="selectPatrolPresetPosVo"/>
        where preset_pos_id = #{presetPosId}
    </select>

    <select id="selectCameraByChannelType" parameterType="String" resultMap="CameraVo">
        select a.eqp_book_id,a.area_id,a.patrol_device_code, a.patrol_device_name, a.use_mode, b.channel_code,b.channel_id,b.channel_name
        from basedata_eqpbook a
                 left join basedata_eqpbook_channel b on a.eqp_book_id = b.eqp_book_id
        where b.channel_type = #{channelType}
          and a.type = '10'
        and b.online_state = '1'
        order by a.eqp_book_id,b.channel_code,b.channel_name
    </select>
    <select id="selectListByDeviceId" parameterType="String" resultMap="PatrolPresetPosResult">
        select a.preset_pos_id,
               a.preset_pos_code,
               a.preset_pos_name,
               a.alg_subtype_code,
               a.eqp_book_id,
               a.channel_code,
               a.channel_type
        from basedata_patrolpoint b
                 left join patrol_preset_pos a on a.patrol_point_id = b.patrol_point_id
        where b.device_id = #{deviceId}
          and a.patrol_point_id is not null
    </select>
    <select id="selectPatrolPresetPosByPatrolPointId" parameterType="String" resultMap="PatrolPresetPosResult">
        select a.preset_pos_id,
               a.preset_pos_name,
               a.alg_subtype_code,
               a.eqp_book_id,
               a.channel_code,
               a.channel_type,
               a.channel_img,
               c.channel_name patrol_device_name,
               p.purpose purpose
        from patrol_preset_pos a
            left join basedata_patrolpoint p on a.patrol_point_id = p.patrol_point_id
--                  left join basedata_eqpbook b on a.eqp_book_id = b.eqp_book_id
                 left join basedata_eqpbook_channel c on c.channel_id = a.channel_id
        where a.patrol_point_id = #{patrolPointId}
    </select>
    <select id="selectPresetCodeByChannelCode" parameterType="PatrolPresetPos" resultMap="PatrolPresetPosResult">
        select a.preset_pos_code,a.preset_pos_name
        from patrol_preset_pos a
        where a.patrol_device_code = #{patrolDeviceCode}
        and a.channel_code in
            (select b.channel_code from basedata_eqpbook_channel b
            where b.eqp_book_id = a.eqp_book_id )

    </select>

    <select id="selectPresetCodeSlimByPatrolPointId" parameterType="PatrolPresetPosSlim" resultMap="PatrolPresetPosResultSlim">
        select a.preset_pos_id,a.preset_pos_code,a.preset_pos_name,a.patrol_point_id,a.channel_id,a.patrol_device_code,
               a.alg_subtype_code,a.channel_code,a.channel_type,a.video_nvr_code,b.host,b.port,b.username,b.password,b.address
        from patrol_preset_pos a
        left join basedata_eqpbook_channel b on a.channel_code = b.channel_code and a.channel_id = b.channel_id
        where a.patrol_point_id = #{patrolPointId} and a.is_enable = #{isEnable}
    </select>

    <insert id="insertPatrolPresetPos" parameterType="PatrolPresetPos" useGeneratedKeys="true" keyProperty="presetPosId">
        insert into patrol_preset_pos
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="presetPosId != null and presetPosId != ''">preset_pos_id,</if>
            <if test="presetPosCode != null and presetPosCode != ''">preset_pos_code,</if>
            <if test="presetPosName != null">preset_pos_name,</if>
            <if test="patrolPointId != null">patrol_point_id,</if>
            <if test="channelId != null">channel_id,</if>
            <if test="coordinateType != null">coordinate_type,</if>
            <if test="stopTime != null">stop_time,</if>
            <if test="channelImg != null">channel_img,</if>
            <if test="description != null">description,</if>
            <if test="isEnable != null">is_enable,</if>
            <if test="createTime != null">create_time,</if>
            <if test="algSubtypeCode != null">alg_subtype_code,</if>
            <if test="eqpBookId != null">eqp_book_id,</if>
            <if test="channelCode != null">channel_code,</if>
            <if test="channelType != null">channel_type,</if>
            <if test="patrolDeviceCode != null">patrol_device_code,</if>
            <if test="videoNvrCode != null">video_nvr_code,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="presetPosId != null and presetPosId != ''">#{presetPosId},</if>
            <if test="presetPosCode != null and presetPosCode != ''">#{presetPosCode},</if>
            <if test="presetPosName != null">#{presetPosName},</if>
            <if test="patrolPointId != null">#{patrolPointId},</if>
            <if test="channelId != null">#{channelId},</if>
            <if test="coordinateType != null">#{coordinateType},</if>
            <if test="stopTime != null">#{stopTime},</if>
            <if test="channelImg != null">#{channelImg},</if>
            <if test="description != null">#{description},</if>
            <if test="isEnable != null">#{isEnable},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="algSubtypeCode != null">#{algSubtypeCode},</if>
            <if test="eqpBookId != null">#{eqpBookId},</if>
            <if test="channelCode != null">#{channelCode},</if>
            <if test="channelType != null">#{channelType},</if>
            <if test="patrolDeviceCode != null">#{patrolDeviceCode},</if>
            <if test="videoNvrCode != null">#{videoNvrCode},</if>
        </trim>
    </insert>

    <update id="updatePatrolPresetPos" parameterType="PatrolPresetPos">
        update patrol_preset_pos
        <trim prefix="SET" suffixOverrides=",">
            <!--            <if test="presetPosCode != null and presetPosCode != ''">preset_pos_code = #{presetPosCode},</if>-->
            <if test="presetPosName != null">preset_pos_name = #{presetPosName},</if>
            <if test="patrolPointId != null">patrol_point_id = #{patrolPointId},</if>
            <!--            <if test="channelId != null">channel_id = #{channelId},</if>-->
            <if test="coordinateType != null">coordinate_type = #{coordinateType},</if>
            <if test="stopTime != null">stop_time = #{stopTime},</if>
            <if test="channelImg != null">channel_img = #{channelImg},</if>
            <if test="description != null">description = #{description},</if>
            <!--            <if test="isEnable != null">is_enable = #{isEnable},</if>-->
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="algSubtypeCode != null">alg_subtype_code = #{algSubtypeCode},</if>
            <if test="videoNvrCode != null">video_nvr_code = #{videoNvrCode},</if>
        </trim>
        where preset_pos_id = #{presetPosId}
    </update>

    <delete id="deletePatrolPresetPosByPresetPosId" parameterType="Long">
        delete
        from patrol_preset_pos
        where preset_pos_id = #{presetPosId}
    </delete>
    <delete id="deleteBasePresetByPresetPosId" parameterType="Long">
        delete
        from basedata_patrolpoint_preset a
        where a.patrolpoint_preset_id =
              (select b.patrolpoint_preset_id
               from basedata_patrolpoint_preset b
                        left join patrol_preset_pos c on c.preset_pos_code = b.preset_code
                   and a.eqp_book_id = b.eqp_book_id and c.channel_id = b.channel_id and
                                                         b.patrol_point_id = c.patrol_point_id
                  where c.preset_pos_id = #{presetPosId} limit 1
              )
    </delete>

    <update id="deletePatrolPresetPosByPresetPosIds" parameterType="String">
        delete from patrol_preset_pos
        where preset_pos_id in
        <foreach item="presetPosId" collection="array" open="(" separator="," close=")">
            #{presetPosId}
        </foreach>
    </update>

    <delete id="deleteBasedataPresetByPresetPosIds" parameterType="String">
        delete from basedata_patrolpoint_preset where patrol_point_id in
          (select patrol_point_id from patrol_preset_pos where preset_pos_id in
        <foreach item="presetPosId" collection="array" open="(" separator="," close=")">
            #{presetPosId}
        </foreach>
            )
    </delete>
</mapper>