<?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.fastbee.sip.mapper.SipDeviceChannelMapper">

    <resultMap type="com.fastbee.sip.domain.SipDeviceChannel" id="SipDeviceChannelResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="tenantName" column="tenant_name"/>
        <result property="productId" column="product_id"/>
        <result property="productName" column="product_name"/>
        <result property="userId" column="user_id"/>
        <result property="deviceSipId" column="device_sip_id"/>
        <result property="channelSipId" column="channel_sip_id"/>
        <result property="channelName" column="channel_name"/>
        <result property="registerTime" column="register_time"/>
        <result property="deviceType" column="device_type"/>
        <result property="channelType" column="channel_type"/>
        <result property="cityCode" column="city_code"/>
        <result property="civilCode" column="civil_code"/>
        <result property="manufacture" column="manufacture"/>
        <result property="model" column="model"/>
        <result property="owner" column="owner"/>
        <result property="block" column="block"/>
        <result property="address" column="address"/>
        <result property="parentId" column="parent_id"/>
        <result property="ipAddress" column="ip_address"/>
        <result property="port" column="port"/>
        <result property="password" column="password"/>
        <result property="ptzType" column="ptz_type"/>
        <result property="ptzTypeText" column="ptz_type_text"/>
        <result property="status" column="status"/>
        <result property="longitude" column="longitude"/>
        <result property="latitude" column="latitude"/>
        <result property="streamId" column="stream_id"/>
        <result property="subCount" column="sub_count"/>
        <result property="parental" column="parental"/>
        <result property="hasAudio" column="has_audio"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <resultMap type="com.fastbee.sip.model.vo.SipDeviceChannelVO" id="SipDeviceChannelVOResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="tenantName" column="tenant_name"/>
        <result property="productId" column="product_id"/>
        <result property="productName" column="product_name"/>
        <result property="userId" column="user_id"/>
        <result property="deviceSipId" column="device_sip_id"/>
        <result property="channelSipId" column="channel_sip_id"/>
        <result property="channelName" column="channel_name"/>
        <result property="registerTime" column="register_time"/>
        <result property="deviceType" column="device_type"/>
        <result property="channelType" column="channel_type"/>
        <result property="cityCode" column="city_code"/>
        <result property="civilCode" column="civil_code"/>
        <result property="manufacture" column="manufacture"/>
        <result property="model" column="model"/>
        <result property="owner" column="owner"/>
        <result property="block" column="block"/>
        <result property="address" column="address"/>
        <result property="parentId" column="parent_id"/>
        <result property="ipAddress" column="ip_address"/>
        <result property="port" column="port"/>
        <result property="password" column="password"/>
        <result property="ptzType" column="ptz_type"/>
        <result property="ptzTypeText" column="ptz_type_text"/>
        <result property="status" column="status"/>
        <result property="longitude" column="longitude"/>
        <result property="latitude" column="latitude"/>
        <result property="streamId" column="stream_id"/>
        <result property="subCount" column="sub_count"/>
        <result property="parental" column="parental"/>
        <result property="hasAudio" column="has_audio"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="streamPush" column="stream_push"/>
        <result property="streamRecord" column="stream_record"/>
        <result property="videoRecord" column="vide_record"/>
        <result property="reDeviceId" column="re_device_id"/>
        <result property="playUrl" column="play_url"/>
        <result property="reSceneModelId" column="re_scene_model_id"/>
        <result property="reDeviceName" column="re_device_name"/>
        <result property="reSceneModelName" column="re_scene_model_name"/>
    </resultMap>

    <sql id="selectSipDeviceChannelVo">
        select id,
               tenant_id,
               tenant_name,
               product_id,
               product_name,
               user_id,
               device_sip_id,
               channel_sip_id,
               channel_name,
               register_time,
               device_type,
               channel_type,
               city_code,
               civil_code,
               manufacture,
               model,
               owner,
               block,
               address,
               parent_id,
               ip_address,
               port,
               password,
               ptz_type,
               ptz_type_text,
               status,
               longitude,
               latitude,
               stream_id,
               sub_count,
               parental,
               has_audio,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from sip_device_channel
    </sql>

    <select id="selectSipDeviceChannelVOPage" parameterType="com.fastbee.sip.model.vo.SipDeviceChannelVO" resultMap="SipDeviceChannelVOResult">
        select s.id,
        s.tenant_id,
        s.tenant_name,
        s.product_id,
        s.product_name,
        s.user_id,
        s.device_sip_id,
        s.channel_sip_id,
        s.channel_name,
        s.register_time,
        s.device_type,
        s.channel_type,
        s.city_code,
        s.civil_code,
        s.manufacture,
        s.model,
        s.owner,
        s.block,
        s.address,
        s.parent_id,
        s.ip_address,
        s.port,
        s.password,
        s.ptz_type,
        s.ptz_type_text,
        s.status,
        s.longitude,
        s.latitude,
        s.stream_id,
        s.sub_count,
        s.parental,
        s.has_audio,
        s.del_flag,
        s.create_by,
        s.create_time,
        s.update_by,
        s.update_time,
        s.remark
        from sip_device_channel s
        <where>
            <if test="sipDeviceChannelVO.tenantId != null ">
                AND (s.tenant_id = #{sipDeviceChannelVO.tenantId} OR
                     s.tenant_id IN
                        (SELECT t.dept_id
                         FROM sys_dept t
                         WHERE ${findInSetStr})
                    )
            </if>
            <if test="sipDeviceChannelVO.tenantName != null  and sipDeviceChannelVO.tenantName != ''">and s.tenant_name like concat('%', #{sipDeviceChannelVO.tenantName}, '%')</if>
            <if test="sipDeviceChannelVO.productId != null ">and s.product_id = #{sipDeviceChannelVO.productId}</if>
            <if test="sipDeviceChannelVO.productName != null  and sipDeviceChannelVO.productName != ''">and s.product_name like concat('%', #{sipDeviceChannelVO.productName},'%')</if>
            <if test="sipDeviceChannelVO.userId != null ">and s.user_id = #{sipDeviceChannelVO.userId}</if>
            <if test="sipDeviceChannelVO.deviceSipId != null  and sipDeviceChannelVO.deviceSipId != ''">and s.device_sip_id = #{sipDeviceChannelVO.deviceSipId}</if>
            <if test="sipDeviceChannelVO.channelSipId != null  and sipDeviceChannelVO.channelSipId != ''">and s.channel_sip_id = #{sipDeviceChannelVO.channelSipId}</if>
            <if test="sipDeviceChannelVO.channelName != null  and sipDeviceChannelVO.channelName != ''">and s.channel_name like concat('%', #{sipDeviceChannelVO.channelName},'%')</if>
            <if test="sipDeviceChannelVO.registerTime != null ">and s.register_time = #{sipDeviceChannelVO.registerTime}</if>
            <if test="sipDeviceChannelVO.deviceType != null  and sipDeviceChannelVO.deviceType != ''">and s.device_type = #{sipDeviceChannelVO.deviceType}</if>
            <if test="sipDeviceChannelVO.channelType != null  and sipDeviceChannelVO.channelType != ''">and s.channel_type = #{sipDeviceChannelVO.channelType}</if>
            <if test="sipDeviceChannelVO.cityCode != null  and sipDeviceChannelVO.cityCode != ''">and s.city_code = #{sipDeviceChannelVO.cityCode}</if>
            <if test="sipDeviceChannelVO.civilCode != null  and sipDeviceChannelVO.civilCode != ''">and s.civil_code = #{sipDeviceChannelVO.civilCode}</if>
            <if test="sipDeviceChannelVO.manufacture != null  and sipDeviceChannelVO.manufacture != ''">and s.manufacture = #{sipDeviceChannelVO.manufacture}</if>
            <if test="sipDeviceChannelVO.model != null  and sipDeviceChannelVO.model != ''">and s.model = #{sipDeviceChannelVO.model}</if>
            <if test="sipDeviceChannelVO.owner != null  and sipDeviceChannelVO.owner != ''">and s.owner = #{sipDeviceChannelVO.owner}</if>
            <if test="sipDeviceChannelVO.block != null  and sipDeviceChannelVO.block != ''">and s.block = #{sipDeviceChannelVO.block}</if>
            <if test="sipDeviceChannelVO.address != null  and sipDeviceChannelVO.address != ''">and s.address = #{sipDeviceChannelVO.address}</if>
            <if test="sipDeviceChannelVO.parentId != null  and sipDeviceChannelVO.parentId != ''">and s.parent_id = #{sipDeviceChannelVO.parentId}</if>
            <if test="sipDeviceChannelVO.ipAddress != null  and sipDeviceChannelVO.ipAddress != ''">and s.ip_address = #{sipDeviceChannelVO.ipAddress}</if>
            <if test="sipDeviceChannelVO.port != null ">and s.port = #{sipDeviceChannelVO.port}</if>
            <if test="sipDeviceChannelVO.password != null  and sipDeviceChannelVO.password != ''">and s.password = #{sipDeviceChannelVO.password}</if>
            <if test="sipDeviceChannelVO.ptzType != null ">and s.ptz_type = #{sipDeviceChannelVO.ptzType}</if>
            <if test="sipDeviceChannelVO.ptzTypeText != null  and sipDeviceChannelVO.ptzTypeText != ''">and s.ptz_type_text = #{sipDeviceChannelVO.ptzTypeText}</if>
            <if test="sipDeviceChannelVO.status != null ">and s.status = #{sipDeviceChannelVO.status}</if>
            <if test="sipDeviceChannelVO.longitude != null ">and s.longitude = #{sipDeviceChannelVO.longitude}</if>
            <if test="sipDeviceChannelVO.latitude != null ">and s.latitude = #{sipDeviceChannelVO.latitude}</if>
            <if test="sipDeviceChannelVO.streamId != null  and streamId != ''">and s.stream_id = #{sipDeviceChannelVO.streamId}</if>
            <if test="sipDeviceChannelVO.subCount != null ">and s.sub_count = #{sipDeviceChannelVO.subCount}</if>
            <if test="sipDeviceChannelVO.parental != null ">and s.parental = #{sipDeviceChannelVO.parental}</if>
            <if test="sipDeviceChannelVO.hasAudio != null ">and s.has_audio = #{sipDeviceChannelVO.hasAudio}</if>
            and s.del_flag = '0'
        </where>
    </select>

    <update id="updateSipDeviceChannel" parameterType="SipDeviceChannel">
        update sip_device_channel
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="tenantName != null and tenantName != ''">tenant_name = #{tenantName},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="deviceSipId != null">device_sip_id = #{deviceSipId},</if>
            <if test="channelSipId != null and channelSipId != ''">channel_sip_id = #{channelSipId},</if>
            <if test="channelName != null and channelName != ''">channel_name = #{channelName},</if>
            <if test="registerTime != null">register_time = #{registerTime},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="channelType != null">channel_type = #{channelType},</if>
            <if test="cityCode != null and cityCode != ''">city_code = #{cityCode},</if>
            <if test="civilCode != null and civilCode != ''">civil_code = #{civilCode},</if>
            <if test="manufacture != null and manufacture != ''">manufacture = #{manufacture},</if>
            <if test="model != null and model != ''">model = #{model},</if>
            <if test="owner != null and owner != ''">owner = #{owner},</if>
            <if test="block != null and block != ''">block = #{block},</if>
            <if test="address != null and address != ''">address = #{address},</if>
            <if test="parentId != null and parentId != ''">parent_id = #{parentId},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="port != null">port = #{port},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="ptzType != null">ptz_type = #{ptzType},</if>
            <if test="ptzTypeText != null and ptzTypeText != ''">ptz_type_text = #{ptzTypeText},</if>
            <if test="status != null">status = #{status},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="streamId != null and streamId != ''">stream_id = #{streamId},</if>
            <if test="subCount != null">sub_count = #{subCount},</if>
            <if test="parental != null">parental = #{parental},</if>
            <if test="hasAudio != null">has_audio = #{hasAudio},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="createBy != null and createBy != ''">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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where channel_sip_id = #{channelSipId}
    </update>

    <select id="selectSipDeviceChannelByDeviceSipId" parameterType="String" resultMap="SipDeviceChannelResult">
        <include refid="selectSipDeviceChannelVo"/>
        where device_sip_id = #{deviceSipId} and del_flag = '0'
    </select>

    <select id="selectSipDeviceChannelByChannelSipId" parameterType="String" resultMap="SipDeviceChannelResult">
        <include refid="selectSipDeviceChannelVo"/>
        where channel_sip_id = #{channelSipId} and del_flag = '0'
    </select>

    <select id="selectChannelWithCivilCodeAndLength" resultMap="SipDeviceChannelResult">
        <include refid="selectSipDeviceChannelVo"/>
        where device_sip_id = #{deviceSipId} and del_flag = '0'
        <if test='parentId != null and length != null' > and parent_id = #{parentId} or left(channel_sip_id, LENGTH(#{parentId})) = #{parentId} and length(channel_sip_id) = #{length} </if>
        <if test='parentId == null and length != null' > and parent_id = #{parentId} or length(channel_sip_id) = #{length} </if>
        <if test='parentId == null and length == null' > and parent_id = #{parentId} </if>
        <if test='parentId != null and length == null' > and parent_id = #{parentId} or left(channel_sip_id, LENGTH(#{parentId})) = #{parentId} </if>
    </select>

    <select id="selectChannelByCivilCode" resultMap="SipDeviceChannelResult">
        <include refid="selectSipDeviceChannelVo"/>
        where device_sip_id = #{deviceSipId} and length(channel_sip_id)>14 and civil_code=#{parentId} and del_flag = '0'
    </select>

    <select id="selectChannelWithoutCiviCode" resultMap="SipDeviceChannelResult">
        <include refid="selectSipDeviceChannelVo"/>
        where device_sip_id=#{deviceSipId} and civil_code not in (select civil_code from sip_device_channel where del_flag = '0' and device_sip_id=#{deviceSipId} group by civil_code)
    </select>

    <select id="getChannelMinLength" resultType="Integer">
        select min(length(channel_sip_id)) as minLength
        from sip_device_channel
        where device_sip_id = #{deviceSipId} and del_flag = '0'
    </select>

    <update id="updateChannelStreamId" parameterType="SipDeviceChannel">
        update sip_device_channel
        <trim prefix="SET" suffixOverrides=",">
            <if test="streamId != null and streamId != ''">stream_id = #{streamId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSipDeviceChannelByDeviceId" parameterType="String">
        update
        sip_device_channel set del_flag = '1'
        where device_sip_id = #{deviceSipId}
    </delete>

    <select id="getBindingChannel" resultType="com.fastbee.sip.domain.BindingChannel">
        select s.channel_sip_id as channelId,
               d.device_id as deviceId,
               d.device_name as deviceName,
               m.scene_model_id as sceneModelId,
               m.scene_model_name as sceneModelName
        from sip_device_channel s
        left join iot_device d on s.channel_sip_id = d.channel_id
        left join scene_model m on m.channel_id = s.channel_sip_id
        where s.del_flag = '0' and d.del_flag = '0' and m.del_flag = '0'

    </select>

    <select id="selectDeviceRelSipDeviceChannelList" resultType="com.fastbee.sip.domain.SipDeviceChannel">
        select sc.id, sr.re_device_id, sc.channel_name, sc.device_sip_id, sc.channel_sip_id, sc.status
        from iot_device d left join iot_sip_relation sr on d.device_id = sr.re_device_id
                          left join sip_device_channel sc on sr.channel_id = sc.channel_sip_id
        where d.del_flag = 0
          and sc.del_flag = 0
          and d.serial_number = #{serialNumber}
    </select>

    <select id="selectSceneRelSipDeviceChannelList" resultType="com.fastbee.sip.domain.SipDeviceChannel">
        select sc.id, sr.re_device_id, sc.channel_name, sc.device_sip_id, sc.channel_sip_id, sc.status
        from iot_sip_relation sr
                 left join sip_device_channel sc on sr.channel_id = sc.channel_sip_id
        where sc.del_flag = 0
          and sr.re_scene_model_id = #{sceneModelId}
    </select>
</mapper>
