<?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.huatek.busi.mapper.BusiVideoMapper">
    <resultMap id="BaseResultMap" type="com.huatek.busi.entity.BusiVideo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="site_id" jdbcType="BIGINT" property="siteId"/>
        <result column="eqm_code" jdbcType="VARCHAR" property="eqmCode"/>
        <result column="type" jdbcType="VARCHAR" property="type"/>
        <result column="task_name" jdbcType="VARCHAR" property="taskName"/>
        <result column="site_name" jdbcType="VARCHAR" property="siteName"/>
        <result column="task_code" jdbcType="VARCHAR" property="taskCode"/>
        <result column="rtsp_url" jdbcType="VARCHAR" property="rtspUrl"/>
        <result column="create_user" jdbcType="BIGINT" property="createUser"/>
        <result column="status" jdbcType="VARCHAR" property="status"/>
        <result column="del_flag" jdbcType="VARCHAR" property="delFlag"/>
        <result column="create_name" jdbcType="VARCHAR" property="createName"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="sort" jdbcType="INTEGER" property="sort"/>
    </resultMap>

    <sql id="Base_Column_List">
  	t.id AS id,
    t.site_id AS siteId,
    t.eqm_code AS eqmCode,
    t.type AS TYPE,
    t.task_name AS taskName,
    t.task_code AS taskCode,
    t.rtsp_url AS rtspUrl,
    t.create_user AS createUser,
    t.status AS STATUS,
    t.del_flag AS delFlag,
    t.create_name AS createName,
    t.create_date AS createDate,
    t.sort AS sort
  </sql>


    <sql id="Base_Column_List2">
  	t.id AS id,
    t.site_id AS siteId,
    s.site_name AS siteName,
    t.eqm_code AS eqmCode,
    st.equipment_name as equipmentName,
    t.type AS TYPE,
    t.task_name AS taskName,

    CASE
        WHEN t.type = 'blq_hwcw'
        THEN
        (SELECT
          sensor_name
        FROM
          busi_arrester_sensor sensor
        WHERE sensor.sensor_code = t.task_code)
          WHEN t.type = 'gis_hwcw'
          THEN
          (SELECT
            sensor_name
          FROM
            busi_gis_sensor sensor
          WHERE sensor.sensor_code = t.task_code)
            WHEN t.type = 'byq_yxgk'
            THEN
            (SELECT
              sensor_name
            FROM
              busi_transformer_sensor sensor
            WHERE sensor.sensor_code = t.task_code)
            END AS sensorName,
    t.task_code AS taskCode,
    t.rtsp_url AS rtspUrl,
    t.http_url AS httpUrl,
    t.create_user AS createUser,
    t.status AS STATUS,
    t.del_flag AS delFlag,
    t.create_name AS createName,
    t.create_date AS createDate,
    t.sort AS sort
  </sql>

    <select id="findBusiVideoDtoPage" parameterType="com.huatek.busi.dto.BusiVideoDto"
            resultType="com.huatek.busi.vo.BusiVideoVo">
        SELECT
        <include refid="Base_Column_List2"/>
        FROM
        busi_video t left join busi_site s on t.site_id = s.id
        left join busi_external_equipment st on t.eqm_code = st.equipment_id
        where t.del_flag='0'
        <if test="siteId != null and siteId != ''">
            and t.site_id = #{siteId}
        </if>
        <if test="type != null and type != ''">
            and t.type = #{type}
        </if>
        <if test="startDate != null">
            <![CDATA[  and DATE_FORMAT(t.create_date, '%Y-%m-%d') >= DATE_FORMAT(#{startDate}, '%Y-%m-%d')]]>
        </if>
        <if test="endDate != null">
            <![CDATA[  and DATE_FORMAT(t.create_date, '%Y-%m-%d') <=  DATE_FORMAT(#{endDate} , '%Y-%m-%d')]]>
        </if>
        order by t.create_date DESC
    </select>


    <select id="queryBusiVideoById" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiVideoVo">
        SELECT
        <include refid="Base_Column_List2"/>
        FROM
        busi_video t left join busi_site s on t.site_id = s.id
        left join busi_external_equipment st on t.eqm_code = st.equipment_id
        where t.del_flag='0' and t.id=#{id}
    </select>

    <!-- 逻辑删除 -->
    <update id="deleteBusiVideo" parameterType="java.util.List">
        update busi_video set del_flag='1'
        where id in
        <foreach collection="idList" index="index" item="id" separator="," open="(" close=")">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </update>

    <select id="getRtspUrl" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiVideoVo">
        SELECT
        <include refid="Base_Column_List"/>
        FROM busi_video t
        WHERE t.task_code = #{sensorId}
        LIMIT 1
    </select>

</mapper>
