<?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.pm.subsystem.mapper.VideoDeviceMapper">

    <resultMap type="pd" id="CameraResult">
        <result property="id"    column="id"    />
        <result property="cameraCode"    column="camera_code"    />
        <result property="cameraName"    column="camera_name"    />
        <result property="monitorArea"    column="monitor_area"    />
        <result property="building"    column="building"    />
        <result property="floor"    column="floor"    />
        <result property="locationDetail"    column="location_detail"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="port"    column="port"    />
        <result property="brand"    column="brand"    />
        <result property="model"    column="model"    />
        <result property="resolution"    column="resolution"    />
        <result property="frameRate"    column="frame_rate"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="ptzSupport"    column="ptz_support"    />
        <result property="infraredSupport"    column="infrared_support"    />
        <result property="recordingStatus"    column="recording_status"    />
        <result property="storagePath"    column="storage_path"    />
        <result property="lastOnlineTime"    column="last_online_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectCameraVo">
        select id, camera_code, camera_name, monitor_area, building, floor, location_detail,
               ip_address, port, brand, model, resolution, frame_rate, device_status,
               ptz_support, infrared_support, recording_status, storage_path,
               last_online_time, create_time, update_time
        from camera_device
    </sql>

    <select id="selectCameraList" parameterType="pd" resultMap="CameraResult">
        <include refid="selectCameraVo"/>
        <where>
            <if test="cameraCode != null and cameraCode != ''"> and camera_code like concat('%', #{cameraCode}, '%')</if>
            <if test="cameraName != null and cameraName != ''"> and camera_name like concat('%', #{cameraName}, '%')</if>
            <if test="monitorArea != null and monitorArea != ''"> and monitor_area like concat('%', #{monitorArea}, '%')</if>
            <if test="building != null and building != ''"> and building = #{building}</if>
            <if test="floor != null and floor != ''"> and floor = #{floor}</if>
            <if test="deviceStatus != null"> and device_status = #{deviceStatus}</if>
            <if test="ptzSupport != null"> and ptz_support = #{ptzSupport}</if>
            <if test="recordingStatus != null"> and recording_status = #{recordingStatus}</if>
            <if test="beginTime != null and beginTime != ''"> and date_format(create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
            <if test="endTime != null and endTime != ''"> and date_format(create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectCameraById" parameterType="pd" resultMap="CameraResult">
        <include refid="selectCameraVo"/>
        where id = #{id}
    </select>

    <insert id="insertCamera" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into camera_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cameraCode != null and cameraCode != ''">camera_code,</if>
            <if test="cameraName != null and cameraName != ''">camera_name,</if>
            <if test="monitorArea != null and monitorArea != ''">monitor_area,</if>
            <if test="building != null and building != ''">building,</if>
            <if test="floor != null and floor != ''">floor,</if>
            <if test="locationDetail != null and locationDetail != ''">location_detail,</if>
            <if test="ipAddress != null and ipAddress != ''">ip_address,</if>
            <if test="port != null">port,</if>
            <if test="brand != null and brand != ''">brand,</if>
            <if test="model != null and model != ''">model,</if>
            <if test="resolution != null and resolution != ''">resolution,</if>
            <if test="frameRate != null">frame_rate,</if>
            <if test="deviceStatus != null">device_status,</if>
            <if test="ptzSupport != null">ptz_support,</if>
            <if test="infraredSupport != null">infrared_support,</if>
            <if test="recordingStatus != null">recording_status,</if>
            <if test="storagePath != null and storagePath != ''">storage_path,</if>
            <if test="lastOnlineTime != null">last_online_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cameraCode != null and cameraCode != ''">#{cameraCode},</if>
            <if test="cameraName != null and cameraName != ''">#{cameraName},</if>
            <if test="monitorArea != null and monitorArea != ''">#{monitorArea},</if>
            <if test="building != null and building != ''">#{building},</if>
            <if test="floor != null and floor != ''">#{floor},</if>
            <if test="locationDetail != null and locationDetail != ''">#{locationDetail},</if>
            <if test="ipAddress != null and ipAddress != ''">#{ipAddress},</if>
            <if test="port != null">#{port},</if>
            <if test="brand != null and brand != ''">#{brand},</if>
            <if test="model != null and model != ''">#{model},</if>
            <if test="resolution != null and resolution != ''">#{resolution},</if>
            <if test="frameRate != null">#{frameRate},</if>
            <if test="deviceStatus != null">#{deviceStatus},</if>
            <if test="ptzSupport != null">#{ptzSupport},</if>
            <if test="infraredSupport != null">#{infraredSupport},</if>
            <if test="recordingStatus != null">#{recordingStatus},</if>
            <if test="storagePath != null and storagePath != ''">#{storagePath},</if>
            <if test="lastOnlineTime != null">#{lastOnlineTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateCamera" parameterType="pd">
        update camera_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="cameraName != null and cameraName != ''">camera_name = #{cameraName},</if>
            <if test="monitorArea != null and monitorArea != ''">monitor_area = #{monitorArea},</if>
            <if test="building != null and building != ''">building = #{building},</if>
            <if test="floor != null and floor != ''">floor = #{floor},</if>
            <if test="locationDetail != null and locationDetail != ''">location_detail = #{locationDetail},</if>
            <if test="ipAddress != null and ipAddress != ''">ip_address = #{ipAddress},</if>
            <if test="port != null">port = #{port},</if>
            <if test="brand != null and brand != ''">brand = #{brand},</if>
            <if test="model != null and model != ''">model = #{model},</if>
            <if test="resolution != null and resolution != ''">resolution = #{resolution},</if>
            <if test="frameRate != null">frame_rate = #{frameRate},</if>
            <if test="deviceStatus != null">device_status = #{deviceStatus},</if>
            <if test="ptzSupport != null">ptz_support = #{ptzSupport},</if>
            <if test="infraredSupport != null">infrared_support = #{infraredSupport},</if>
            <if test="recordingStatus != null">recording_status = #{recordingStatus},</if>
            <if test="storagePath != null and storagePath != ''">storage_path = #{storagePath},</if>
            <if test="lastOnlineTime != null">last_online_time = #{lastOnlineTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        <where>
            <if test="id != null">id = #{id}</if>
            <if test="cameraCode != null and cameraCode != ''">or camera_code = #{cameraCode}</if>
        </where>
    </update>

    <delete id="deleteCameraById" parameterType="Long">
        delete from camera_device where id = #{id}
    </delete>

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

    <!-- 联动规则相关查询 -->
    <select id="selectLinkageRuleList" parameterType="pd" resultType="pd">
        select id, rule_name, trigger_device_code, trigger_device_name, trigger_condition,
        linkage_camera_code, linkage_camera_name, linkage_action, delay_time,
        rule_status, create_time, update_time
        from video_linkage_rule
        <where>
            <if test="ruleName != null and ruleName != ''"> and rule_name like concat('%', #{ruleName}, '%')</if>
            <if test="triggerDevice != null and triggerDevice != ''"> and trigger_device_name like concat('%', #{triggerDevice}, '%')</if>
            <if test="linkageCamera != null and linkageCamera != ''"> and linkage_camera_name like concat('%', #{linkageCamera}, '%')</if>
            <if test="ruleStatus != null"> and rule_status = #{ruleStatus}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectLinkageRuleById" parameterType="Long" resultType="pd">
        select id, rule_name, trigger_device_code, trigger_device_name, trigger_condition,
               linkage_camera_code, linkage_camera_name, linkage_action, delay_time,
               rule_status, create_time, update_time
        from video_linkage_rule
        where id = #{id}
    </select>

    <insert id="insertLinkageRule" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into video_linkage_rule (rule_name, trigger_device_code, trigger_device_name, trigger_condition,
                                        linkage_camera_code, linkage_camera_name, linkage_action, delay_time,
                                        rule_status, create_time, update_time)
        values (#{ruleName}, #{triggerDeviceCode}, #{triggerDeviceName}, #{triggerCondition},
                #{linkageCameraCode}, #{linkageCameraName}, #{linkageAction}, #{delayTime},
                #{ruleStatus}, #{createTime}, #{updateTime})
    </insert>

    <update id="updateLinkageRule" parameterType="pd">
        update video_linkage_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="ruleName != null and ruleName != ''">rule_name = #{ruleName},</if>
            <if test="triggerDeviceCode != null and triggerDeviceCode != ''">trigger_device_code = #{triggerDeviceCode},</if>
            <if test="triggerDeviceName != null and triggerDeviceName != ''">trigger_device_name = #{triggerDeviceName},</if>
            <if test="triggerCondition != null and triggerCondition != ''">trigger_condition = #{triggerCondition},</if>
            <if test="linkageCameraCode != null and linkageCameraCode != ''">linkage_camera_code = #{linkageCameraCode},</if>
            <if test="linkageCameraName != null and linkageCameraName != ''">linkage_camera_name = #{linkageCameraName},</if>
            <if test="linkageAction != null and linkageAction != ''">linkage_action = #{linkageAction},</if>
            <if test="delayTime != null">delay_time = #{delayTime},</if>
            <if test="ruleStatus != null">rule_status = #{ruleStatus},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLinkageRuleById" parameterType="Long">
        delete from video_linkage_rule where id = #{id}
    </delete>

    <!-- 联动执行记录查询 -->
    <select id="selectLinkageRecordList" parameterType="pd" resultType="pd">
        select r.id, r.rule_id, r.trigger_device_code, r.linkage_camera_code, r.linkage_action,
        r.execute_time, r.execute_result, r.create_time,
        l.rule_name
        from video_linkage_record r
        left join video_linkage_rule l on r.rule_id = l.id
        <where>
            <if test="triggerDeviceCode != null and triggerDeviceCode != ''"> and r.trigger_device_code = #{triggerDeviceCode}</if>
            <if test="linkageCameraCode != null and linkageCameraCode != ''"> and r.linkage_camera_code = #{linkageCameraCode}</if>
            <if test="executeResult != null and executeResult != ''"> and r.execute_result = #{executeResult}</if>
            <if test="beginTime != null and beginTime != ''"> and date_format(r.execute_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
            <if test="endTime != null and endTime != ''"> and date_format(r.execute_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        </where>
        order by r.execute_time desc
    </select>

    <insert id="insertLinkageRecord" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into video_linkage_record (rule_id, trigger_device_code, linkage_camera_code, linkage_action,
                                          execute_time, execute_result, create_time)
        values (#{ruleId}, #{triggerDeviceCode}, #{linkageCameraCode}, #{linkageAction},
                #{executeTime}, #{executeResult}, #{createTime})
    </insert>

    <!-- 录像文件查询 -->
    <select id="selectRecordingFileList" parameterType="pd" resultType="pd">
        select id, camera_code, camera_name, file_name, file_path, file_size,
        start_time, end_time, duration, status, create_time
        from recording_file
        <where>
            <if test="cameraCode != null and cameraCode != ''"> and camera_code = #{cameraCode}</if>
            <if test="cameraName != null and cameraName != ''"> and camera_name like concat('%', #{cameraName}, '%')</if>
            <if test="status != null"> and status = #{status}</if>
            <if test="beginTime != null and beginTime != ''"> and date_format(start_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
            <if test="endTime != null and endTime != ''"> and date_format(start_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        </where>
        order by start_time desc
    </select>

    <insert id="insertRecordingFile" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into recording_file (camera_code, camera_name, file_name, file_path, file_size,
                                    start_time, end_time, duration, status, create_time)
        values (#{cameraCode}, #{cameraName}, #{fileName}, #{filePath}, #{fileSize},
                #{startTime}, #{endTime}, #{duration}, #{status}, #{createTime})
    </insert>

    <!-- 预置位查询 -->
    <select id="selectPresetList" parameterType="pd" resultType="pd">
        select id, camera_code, preset_id, preset_name, position_x, position_y, zoom_level, create_time
        from camera_preset
        <where>
            <if test="cameraCode != null and cameraCode != ''"> and camera_code = #{cameraCode}</if>
        </where>
        order by preset_id
    </select>

    <insert id="insertPreset" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into camera_preset (camera_code, preset_id, preset_name, position_x, position_y, zoom_level, create_time)
        values (#{cameraCode}, #{presetId}, #{presetName}, #{positionX}, #{positionY}, #{zoomLevel}, #{createTime})
    </insert>

    <delete id="deletePresetById" parameterType="Long">
        delete from camera_preset where id = #{id}
    </delete>

    <!-- 云台控制记录 -->
    <select id="selectPtzControlList" parameterType="pd" resultType="pd">
        select id, camera_code, command, speed, control_user, control_time, control_result, create_time
        from ptz_control_record
        <where>
            <if test="cameraCode != null and cameraCode != ''"> and camera_code = #{cameraCode}</if>
            <if test="controlUser != null and controlUser != ''"> and control_user = #{controlUser}</if>
            <if test="beginTime != null and beginTime != ''"> and date_format(control_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
            <if test="endTime != null and endTime != ''"> and date_format(control_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        </where>
        order by control_time desc
    </select>

    <insert id="insertPtzControl" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into ptz_control_record (camera_code, command, speed, control_user, control_time, control_result, create_time)
        values (#{cameraCode}, #{command}, #{speed}, #{controlUser}, #{controlTime}, #{controlResult}, #{createTime})
    </insert>

    <!-- 摄像机统计数据 -->
    <select id="selectCameraStatistics" parameterType="pd" resultType="pd">
        select
            count(*) as totalCameras,
            count(case when device_status = 1 then 1 end) as onlineCameras,
            count(case when device_status = 0 then 1 end) as offlineCameras,
            count(case when device_status = 2 then 1 end) as faultCameras,
            count(case when recording_status = 1 then 1 end) as recordingCameras,
            count(case when ptz_support = 1 then 1 end) as ptzCameras,
            count(case when infrared_support = 1 then 1 end) as infraredCameras
        from camera_device
    </select>

</mapper>