<?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.ruoyi.business.mapper.HwMonitorUnitMapper">
    <resultMap type="HwMonitorUnit" id="HwMonitorUnitResult">
        <result property="monitorUnitId"    column="monitor_unit_id"    />
        <result property="monitorUnitName"    column="monitor_unit_name"    />
        <result property="sceneId"    column="scene_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="monitorUnitTypeId"    column="monitor_unit_type_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="monitorAddress"    column="monitor_address"    />
        <result property="monitorPic"    column="monitor_pic"    />
        <result property="preserveTime"    column="preserve_time"    />
        <result property="remark"    column="remark"    />
        <result property="monitorUnitStatus"    column="monitor_unit_status"    />
        <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="monitorUnitField"    column="monitor_unit_field"    />
    </resultMap>
    <resultMap type="HwMonitorUnitVo" id="HwMonitorUnitVoResult" >
        <result property="sceneName" column="scene_name"/>
        <result property="sceneId" column="scene_id"/>
        <result property="sum" column="sum"/>
        <result property="err" column="err"/>
    </resultMap>
    <resultMap type="AllNumsVo" id="AllNumsVoResult">
        <result property="sceneId" column="scene_id"/>
        <result property="subSum" column="sub_sum"/>
        <result property="sum" column="sum"/>
    </resultMap>
    <resultMap type="AlarmStatsVo" id="AlarmStatsResult" >
        <result property="monitorUnitId" column="monitor_unit_id"/>
        <result property="monitorUnitName" column="monitor_unit_name"/>
        <result property="sum" column="sum"/>
    </resultMap>
    <resultMap type="SubDeviceSumVo" id="SubDeviceSumVoResult">
        <result property="SubSum" column="sub_sum"/>
        <result property="DeviceNum" column="device_num"/>
        <result property="OnlineDeviceNum" column="online_device_num"/>
    </resultMap>
    <resultMap type="com.ruoyi.business.domain.VO.ElectronicNumVo" id="ElectronicNumVoResult">
        <result property="electronicFenceId" column="electronic_fence_id"/>
        <result property="electronicFenceName" column="electronic_fence_name"/>
        <result property="areaShapeFlag" column="area_shape_flag"/>
        <result property="areaRange" column="area_range"/>
        <result property="fenceType" column="fence_type"/>
        <result property="num" column="num"/>
        <result property="alarmNum" column="alarm_num"/>
    </resultMap>
    <resultMap id="MonitorUnitNumVoResult" type="MonitorUnitNumVo">
        <result property="monitorUnitId" column="monitor_unit_id"/>
        <result property="num" column="num"/>
    </resultMap>

    <sql id="selectHwMonitorUnitVo">
        select monitor_unit_id, monitor_unit_name, scene_id, parent_id, monitor_unit_type_id, area_id, monitor_address, monitor_pic, preserve_time, remark, monitor_unit_status, create_by, create_time, update_by, update_time, monitor_unit_field from hw_monitor_unit
    </sql>
    <select id="selectHaveEleDevice" parameterType="Long" resultMap="MonitorUnitNumVoResult">
        SELECT monitor_unit_id,count(*) as num from hw_device where monitor_unit_id
        = (select monitor_unit_id from hw_monitor_unit where monitor_unit_id =
        (select target_id  from hw_fence_target where target_type = '2'and
        electronic_fence_id = (select electronic_fence_id from hw_electronic_fence
        where scene_id = #{sceneId}) )) GROUP BY monitor_unit_id
    </select>
    <select id="selectMonitorUnitHaveEle" parameterType="Long" resultMap="MonitorUnitNumVoResult">
        select monitor_unit_id,count(*) as num from hw_device where
        monitor_unit_id = (select target_id  from hw_fence_target where
         target_type = '2'and electronic_fence_id =
         (select electronic_fence_id from hw_electronic_fence where scene_id = #{sceneId}) )
        GROUP BY monitor_unit_id
    </select>
    <select id="selectHwMonitorUnitList" parameterType="HwMonitorUnit" resultMap="HwMonitorUnitResult">
        <include refid="selectHwMonitorUnitVo"/>
        <where>
            <if test="monitorUnitName != null  and monitorUnitName != ''"> and monitor_unit_name like concat('%', #{monitorUnitName}, '%')</if>
            <if test="sceneId != null "> and scene_id = #{sceneId}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="monitorUnitTypeId != null "> and monitor_unit_type_id = #{monitorUnitTypeId}</if>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="monitorAddress != null  and monitorAddress != ''"> and monitor_address = #{monitorAddress}</if>
            <if test="monitorPic != null  and monitorPic != ''"> and monitor_pic = #{monitorPic}</if>
            <if test="preserveTime != null "> and preserve_time = #{preserveTime}</if>
            <if test="monitorUnitStatus != null "> and monitor_unit_status = #{monitorUnitStatus}</if>
            <if test="monitorUnitField != null  and monitorUnitField != ''"> and monitor_unit_field = #{monitorUnitField}</if>
        </where>
    </select>

    <select id="selectHwMonitorUnitByMonitorUnitId" parameterType="Long" resultMap="HwMonitorUnitResult">
        <include refid="selectHwMonitorUnitVo"/>
        where monitor_unit_id = #{monitorUnitId}
    </select>
    <!--查询最子集监控单元以场景进行分组-->
    <select id="selectAllNums" resultMap="AllNumsVoResult">
        select scene_id,count(*) as sub_sum  from
        (select monitor_unit_id,scene_id from hw_monitor_unit
         where monitor_unit_id not in (select parent_id from hw_monitor_unit where parent_id is not null))as temp
        GROUP BY scene_id
    </select>
    <select id="selectSubSetNums" resultType="String">

      select count(*)from hw_monitor_unit
         where monitor_unit_id not in (select parent_id from hw_monitor_unit where parent_id is not null)
    </select>
    <select id="selectDeviceNums" resultType="String">
        select count(*) as sum  from hw_device
where device_status = '1'
    </select>
<!--    通过场景分组，查询设备数量 todo 已解决 -->
    <select id="selectsum" resultMap="AllNumsVoResult">
select scene_id,count(*) as sum  from hw_device
where device_status = '1'GROUP BY scene_id
    </select>
<!-- 通过id查询子集并返回-->
    <select id="selectHwMonitorUnitByParent" parameterType="Long" resultMap="HwMonitorUnitResult">
        <include refid="selectHwMonitorUnitVo"/>
        where parent_id = #{monitorUnitId}
    </select>
    <select id="selectReleatedDeviceIdNums" parameterType="Long" resultType="Integer">
        select count(*) from hw_device where device_id = #{deviceId}
    </select>

<!--    查询该监控单元是否为最小单元-->
    <select id="selectHwMonitorUnitIsParent" parameterType="Long" resultType="String">
         SELECT count(*) from hw_monitor_unit where parent_id = #{monitorUnitId} GROUP BY parent_id
    </select>
<!--    todo 查询最小单元详细信息根据主键 byid 已解决-->
    <select id="selectHwmonitorUnitSub" resultType="Long" resultMap="HwMonitorUnitResult">
        <include refid="selectHwMonitorUnitVo"/>
        where monitor_unit_id = #{monitorUnitId}
    </select>
    <!--查询id与name的结果集，通过id与selectMonitorPercentage相连-->
    <select id="selectSceneNameById" resultMap="HwMonitorUnitVoResult">
        select scene_id,scene_name from hw_scene
    </select>
<!--    分页查询，在impl实现分页-->
    <select id="selectLimitSubMonitorUnit" resultMap="HwMonitorUnitResult">
        select * from hw_monitor_unit where monitor_unit_id
         not in (select parent_id from hw_monitor_unit where parent_id is not null)
    </select>
<!--    查询最子集监控单元，使用sceneid进行分组-->
    <select id="selectMonitorPercentage" resultMap="HwMonitorUnitVoResult">
     select scene_id,count(*) as sum  from (select monitor_unit_id,scene_id
      from hw_monitor_unit  where monitor_unit_id not in
      (select parent_id from hw_monitor_unit where parent_id is not null))as temp
      GROUP BY scene_id order by sum desc
    </select>
<!--    先通过报警信息表查询报警的监控单元id与数量，在查询监控单元的名字-->
    <select id="selectAlarmStats" resultMap="AlarmStatsResult">
select b.monitor_unit_id,b.monitor_unit_name,sum from
(select monitor_unit_id,count(*)as sum from hw_alarm_info GROUP BY monitor_unit_id
 )as a left join hw_monitor_unit b on a.monitor_unit_id = b.monitor_unit_id order BY sum
    </select>
<!--    sub_sum 最子集监控单元数量，device_num 设备数量 online_device_num在线设备数量  todo 传一个sceneid 监控单元和设备数量拆开查询 设备拿全部在后端判断 orderby 主键 -->
    <select id="selectSubDeviceSum" parameterType="Long" resultType="Integer">
    select count(*) from (select monitor_unit_id,scene_id
      from hw_monitor_unit  where monitor_unit_id not in
      (select parent_id from hw_monitor_unit where parent_id is not null))as temp  where scene_id = #{sceneId}
    </select>


<!--    先查询监控单元/设备的总量与报警未处理数量，再通过场景id与电子围栏相连接-->
    <select id="selectElectronicNum" resultMap="ElectronicNumVoResult">
        select a.electronic_fence_id,a.electronic_fence_name,a.area_shape_flag,a.area_range,a.fence_type,b.num,b.alarm_num
        from hw_electronic_fence as a left join ( select scene_id,count(*)as num,count(case when handle_status = '0' then 1 end ) as alarm_num from hw_alarm_info GROUP BY scene_id) as b on a.scene_id = b.scene_id
    </select>
    <select id="selectEleInScene" parameterType="Long" resultType="List">
select electronic_fence_id from hw_electronic_fence where scene_id = #{sceneId}
    </select>
    <select id="selectEleType2" parameterType="Long" resultType="String">
select count(*) from hw_device where monitor_unit_id = (select target_id from hw_fence_target where target_type='2' and electronic_fence_id = #{electronicFenceId})
   </select>
    <select id="selectEleType3" parameterType="Long" resultType="String">
        select count(*) from hw_device where scene_id = #{sceneId}
    </select>

    <select id="selectDeviceType2Nums" parameterType="Long" resultType="Integer">
    select count(*) from hw_device
    where scene_id = #{sceneId}and device_type = '2' GROUP BY device_id
    </select>
    <insert id="insertHwMonitorUnit" parameterType="HwMonitorUnit" useGeneratedKeys="true" keyProperty="monitorUnitId">
        insert into hw_monitor_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="monitorUnitName != null and monitorUnitName != ''">monitor_unit_name,</if>
            <if test="sceneId != null">scene_id,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="monitorUnitTypeId != null">monitor_unit_type_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="monitorAddress != null">monitor_address,</if>
            <if test="monitorPic != null">monitor_pic,</if>
            <if test="preserveTime != null">preserve_time,</if>
            <if test="remark != null">remark,</if>
            <if test="monitorUnitStatus != null">monitor_unit_status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="monitorUnitField != null">monitor_unit_field,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="monitorUnitName != null and monitorUnitName != ''">#{monitorUnitName},</if>
            <if test="sceneId != null">#{sceneId},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="monitorUnitTypeId != null">#{monitorUnitTypeId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="monitorAddress != null">#{monitorAddress},</if>
            <if test="monitorPic != null">#{monitorPic},</if>
            <if test="preserveTime != null">#{preserveTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="monitorUnitStatus != null">#{monitorUnitStatus},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="monitorUnitField != null">#{monitorUnitField},</if>
         </trim>
    </insert>

    <update id="updateHwMonitorUnit" parameterType="HwMonitorUnit">
        update hw_monitor_unit
        <trim prefix="SET" suffixOverrides=",">
            <if test="monitorUnitName != null and monitorUnitName != ''">monitor_unit_name = #{monitorUnitName},</if>
            <if test="sceneId != null">scene_id = #{sceneId},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="monitorUnitTypeId != null">monitor_unit_type_id = #{monitorUnitTypeId},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="monitorAddress != null">monitor_address = #{monitorAddress},</if>
            <if test="monitorPic != null">monitor_pic = #{monitorPic},</if>
            <if test="preserveTime != null">preserve_time = #{preserveTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="monitorUnitStatus != null">monitor_unit_status = #{monitorUnitStatus},</if>
            <if test="createBy != null">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="monitorUnitField != null">monitor_unit_field = #{monitorUnitField},</if>
        </trim>
        where monitor_unit_id = #{monitorUnitId}
    </update>

    <delete id="deleteHwMonitorUnitByMonitorUnitId" parameterType="Long">
        delete from hw_monitor_unit where monitor_unit_id = #{monitorUnitId}
    </delete>

    <delete id="deleteHwMonitorUnitByMonitorUnitIds" parameterType="String">
        delete from hw_monitor_unit where monitor_unit_id in
        <foreach item="monitorUnitId" collection="array" open="(" separator="," close=")">
            #{monitorUnitId}
        </foreach>
    </delete>
</mapper>
