<?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.yeejoin.equipmanage.mapper.VideoMapper">
    <resultMap id="Video" type="com.yeejoin.equipmanage.common.entity.vo.PageVideoVO">
        <result column="id" property="id"></result>
        <result column="code" property="code"></result>
        <result column="name" property="name"></result>
        <result column="region" property="region"></result>
        <result column="address" property="address"></result>
        <result column="url" property="url"></result>
        <result column="img" property="img"></result>

    </resultMap>

    <resultMap type="com.yeejoin.equipmanage.common.entity.vo.PageSecurityVideoVO" id="menuTree">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="name" property="name"/>
        <result column="full_name" property="fullName"/>
        <collection property="children" ofType="com.yeejoin.equipmanage.common.entity.vo.PageSecurityVideoVO"
                    column="id" select="getMenuChildren"/>
    </resultMap>

    <select id="pageVideo" resultMap="Video">
        SELECT
        wlv.id,
        wlv.code,
        wlv.url AS url,
        wlv.name,
        GROUP_CONCAT(war.full_name) as region,
        wlv.address,
        wlv.status,
        wlv.img,
        wlv.type,
        wlv.biz_org_name
        FROM
        wl_video as wlv
        left join wl_video_source as vis on vis.video_id = wlv.id
        left join wl_warehouse_structure as war on vis.source_id = war.id
        where 1=1
        <if test="video!=null and video.name!=null and video.name!=''">
            and wlv.name like concat('%',#{video.name},'%')
        </if>
        <if test="video!=null and video.code!=null and video.code!=''">
            and wlv.code like concat('%',#{video.code},'%')
        </if>
        <if test="video!=null and video.typeCode!=null and video.typeCode!=''">
            and wlv.type_code like concat('%',#{video.typeCode},'%')
        </if>
        <if test="video!=null and video.bizOrgCode!=null and video.bizOrgCode!=''">
            and wlv.biz_org_code LIKE CONCAT(#{video.bizOrgCode}, '%')
        </if>
        group by  wlv.id
        order by wlv.create_date desc
    </select>

    <select id="pageBuildingVideo" resultType="com.yeejoin.equipmanage.common.entity.vo.BuildingVideoVO">
        SELECT
        v.id AS id,
        v.name AS name,
        v.token AS token,
        v.url AS url,
        v.code AS code,
        getBuildingNames(vc.source_id) as location,
        v.address,
        v.preset_position as presetPosition
        FROM wl_video v
        JOIN wl_video_source vc ON vc.video_id = v.id
        <where>
            <if test="dto.buildingId!=null and dto.buildingId!=''">
                FIND_IN_SET(vc.source_id, getBuildChildIdsByRootId(#{dto.buildingId}))
            </if>
            <if test="dto.bizOrgCode!=null and dto.bizOrgCode!=''">
                and v.biz_org_code LIKE CONCAT(#{dto.bizOrgCode}, '%')
            </if>
            <if test="dto.equipmentName!=null and dto.equipmentName!=''">
                and v.name like concat('%',#{dto.equipmentName},'%')
            </if>
            <if test="dto.code!=null and dto.code!=''">
                and v.code like concat('%',#{dto.code},'%')
            </if>
        </where>
        order by v.create_date
    </select>

    <select id="getFireEquipByVideoId" resultType="com.yeejoin.equipmanage.common.entity.vo.BuildingListVO">
            SELECT
                wle.*,
                equd.`name` AS equipmentName,
                wws.full_name AS equipmentAddress
            FROM
                wl_equipment_specific wle
                JOIN wl_video_equipment_specific ve ON wle.id = ve.equipment_specific_id
                LEFT JOIN wl_equipment_detail equd ON equd.id = wle.equipment_detail_id
                LEFT JOIN wl_stock_detail wsd ON wsd.equipment_detail_id = equd.id
				LEFT JOIN wl_warehouse_structure wws ON wws.id = wsd.warehouse_structure_id
            WHERE
                ve.video_id = #{videoId}
    </select>

    <select id="getPowerEquipByVideoId" resultType="com.yeejoin.equipmanage.common.entity.vo.BuildingListVO">
        SELECT
            wle.id as id,
            wle.`code` AS code,
            wle.`name` AS equipmentName,
            wle.address AS equipmentAddress
        FROM
            f_equipment wle
            JOIN wl_video_important_equipment ve ON wle.id = ve.important_equipment_id
        WHERE
            ve.video_id = #{videoId}
    </select>
    <select id="getVideoBySpeId" resultType="com.yeejoin.equipmanage.common.entity.vo.AlamVideoVO">
         select
        vid.id,
        vid.token,
        vid.name,
        vid.url,
        vid.code,
        vid.preset_position as presetPosition
        from
        wl_video_equipment_specific as ves
        left join wl_video as vid on ves.video_id = vid.id
        where equipment_specific_id = #{id}
    </select>
    <select id="getVideoById" resultType="com.yeejoin.equipmanage.common.entity.vo.AlamVideoVO">
    SELECT
		v.`name`,
		v.`code`,
		v.url,
		v.token,
		CONCAT_WS(' ', ws.full_name, v.address) AS address,
		v.img,
		v.preset_position as presetPosition
	FROM
		wl_video v
	LEFT JOIN wl_video_source vs ON vs.video_id = v.id
	LEFT JOIN wl_warehouse_structure ws ON ws.id = vs.source_id
	WHERE
		v.id = #{id}
    </select>

    <select id="pageColaBuildingVideo" resultType="com.yeejoin.equipmanage.common.entity.vo.BuildingVideoVO">
        SELECT
        sa.equipment_specific_id AS id,
        wed.name AS name,
        wed.code AS code,
        wws.full_name AS location,
        sa.equipment_specific_id AS specificId
        FROM
        (
        SELECT
        *
        FROM
        wl_stock_detail wsd
        WHERE
        wsd.warehouse_structure_id IN
        <foreach collection="dto.buildIdList" item="dto.buildIdList" index="index" open="(" close=")" separator=",">
            #{dto.buildIdList}
        </foreach>
        ) sa,
        wl_equipment_detail wed,
        wl_equipment we,
        wl_equipment_category wec,
        wl_warehouse_structure wws
        WHERE
        sa.equipment_detail_id = wed.id
        AND wws.id = sa.warehouse_structure_id
        AND wed.equipment_id = we.id
        AND wec.id = we.category_id
        and wec.code IN
        <foreach collection="dto.monitorCodes" item="dto.monitorCodes" index="index" open="(" close=")" separator=",">
            #{dto.monitorCodes}
        </foreach>
        <if test="dto.equipmentName!=null and dto.equipmentName!=''">
            and wed.name like concat('%',#{dto.equipmentName},'%')
        </if>
        <if test="dto.code!=null and dto.code!=''">
            and wed.code like concat('%',#{dto.code},'%')
        </if>
        <if test="dto.orgCode!=null and dto.orgCode!=''">
            and sa.org_code = #{dto.orgCode}
        </if>
    </select>

    <select id="listColaBuildingVideo" resultType="com.yeejoin.equipmanage.common.entity.vo.BuildingVideoVO">
        SELECT
        wsd.equipment_specific_id AS id,
        wed.name AS name,
        wed.code AS code,
        wws.full_name AS location,
        wsd.equipment_specific_id AS specificId
        FROM
        wl_stock_detail wsd,
        wl_equipment_detail wed,
        wl_equipment we,
        wl_equipment_category wec,
        wl_warehouse_structure wws
        WHERE
        wsd.equipment_detail_id = wed.id
        AND wed.equipment_id = we.id
        AND wec.id = we.category_id
        AND wws.id = wsd.warehouse_structure_id
        and wec.code in
        <foreach collection="dto.monitorCodes" item="dto.monitorCodes" index="index" open="(" close=")" separator=",">
            #{dto.monitorCodes}
        </foreach>
        <if test="dto.equipmentName!=null and dto.equipmentName!=''">
            and wed.name like concat('%',#{dto.equipmentName},'%')
        </if>
        <if test="dto.code!=null and dto.code!=''">
            and wed.code like concat('%',#{dto.code},'%')
        </if>
        <if test="dto.orgCode!=null and dto.orgCode!=''">
            and wsd.org_code = #{dto.orgCode}
        </if>
    </select>

    <select id="countColaBuildVideo" resultType="java.lang.Long">
        SELECT
        count(1)
        FROM
        wl_stock_detail wsd,
        wl_equipment_detail wed,
        wl_equipment we,
        wl_equipment_category wec,
        wl_warehouse_structure wws
        WHERE
        wsd.equipment_detail_id = wed.id
        AND wed.equipment_id = we.id
        AND wec.id = we.category_id
        AND wws.id = wsd.warehouse_structure_id
        and wec.code in
        <foreach collection="dto.monitorCodes" item="dto.monitorCodes" index="index" open="(" close=")" separator=",">
            #{dto.monitorCodes}
        </foreach>
    </select>

    <select id="findAllParents" resultMap="menuTree">
        SELECT
            wws.id AS id,
            wws.parent_id AS parentId,
            wws.name AS name,
            wws.full_name AS fullName
        FROM
            wl_warehouse_structure wws
        WHERE
            wws.parent_id = 0
    </select>
    <select id="getMenuChildren" resultMap="menuTree">
        SELECT
            wws.id AS id,
            wws.parent_id AS parentId,
            wws.name AS name,
            wws.full_name AS fullName
        FROM
            wl_warehouse_structure wws
        WHERE
            wws.parent_id = #{id}
    </select>

    <select id="getSecurityOrFireEquipment" resultType="com.yeejoin.equipmanage.common.entity.vo.BuildingListVO">
        SELECT
		wes.id AS id,
		wed.name AS equipmentName,
		wed.code AS code,
		wws.full_name AS equipmentAddress
		FROM
		wl_equipment_specific wes
		LEFT JOIN wl_equipment_detail wed ON wed.id = wes.equipment_detail_id
		LEFT JOIN wl_stock_detail wsd ON wsd.equipment_detail_id = wed.id
		LEFT JOIN wl_warehouse_structure wws ON wws.id = wsd.warehouse_structure_id
		WHERE
		wes.id = #{id}
    </select>

    <select id="selectBuildList" resultType="String">
        select source_name from wl_source_scene
        where video_in_scene like CONCAT('%',#{id},'%' )
    </select>

    <select id="selectImportantEquipment" resultType="com.yeejoin.equipmanage.common.entity.vo.EquipmentSpecificVo">
        select id,code from f_equipment
    </select>





    <select id="pageVideoList" resultType="com.yeejoin.equipmanage.common.entity.vo.VideoListVo">
        SELECT
        wle.id ,
        wle.`code` ,
        wle.url,
        wle.position3d,
        wle.token,
        wle.`name` ,
        wle.address ,
        wle.longitude,
        <if test='longitude!=null and latitude!=null '>
        Round(st_distance(point(wle.longitude,wle.latitude),point(#{longitude},#{latitude}))*111195,1) AS distance,
        </if>
        wle.latitude
        FROM wl_video wle
        where   wle.longitude is not null and  wle.latitude is not null
        <if test='longitude!=null and latitude!=null and distance!=null'>
             and distance &lt;= #{distance}
        </if>
        <if test='longitude!=null and latitude!=null '>
        ORDER BY distance
        </if>
        limit #{pageNum},#{pageSize}
    </select>


    <select id="pageVideoCount" resultType="int">
        SELECT

        count( wle.id ) num

        FROM wl_video wle
        where   wle.longitude is not null and  wle.latitude is not null
        <if test='distance!=null'>
            and Round(st_distance(point(wle.longitude,wle.latitude),point(#{longitude},#{latitude}))*111195,1) &lt;=
            #{distance}
        </if>
    </select>

    <sql id="videoField" >
        wlv.id,
        wlv.code,
        wlv.name,
        wlv.address,
        wlv.status,
        wlv.type,
        wlv.ip,
        wlv.port,
        wlv.token,
        wlv.url,
        wlv.img,
        wlv.longitude,
        wlv.latitude,
    </sql>

    <select id="listByType" resultType="com.yeejoin.equipmanage.common.entity.vo.VideoListVo">
        SELECT
        <include refid="videoField"/>
        GROUP_CONCAT(war.full_name) as region
        FROM
        wl_video as wlv
        left join wl_video_source as vis on vis.video_id = wlv.id
        left join wl_warehouse_structure as war on vis.source_id = war.id
        where 1=1
        <if test="type!=null and type!=''">
            and wlv.type = #{type}
        </if>
        group by  wlv.id
    </select>
    
    <select id="listByCode" resultType="com.yeejoin.equipmanage.common.entity.Video">
       SELECT
        <include refid="videoField"/>
        wlv.code
        FROM
        wl_video as wlv
        where wlv.code = #{code}
    </select>


</mapper>
