<?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.zcxk.dao.OnlineMonitorMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.dto.MonitorDataCollectDto" id="monitorDataMap">
        <result property="sceneId" column="scene_id" jdbcType="INTEGER"/>
        <result property="sceneName" column="scene_name" jdbcType="VARCHAR"/>
        <result property="sceneTypeName" column="scene_type_name" jdbcType="VARCHAR"/>
        <result property="pointX" column="point_x" jdbcType="VARCHAR"/>
        <result property="pointY" column="point_y" jdbcType="VARCHAR"/>
        <result property="address" column="address" jdbcType="VARCHAR"/>
        <result property="sceneState" column="scene_state" jdbcType="INTEGER"/>
        <result property="companyOrgId" column="companyOrgId" jdbcType="INTEGER"/>
        <collection property="MonitorDataEntities" ofType="com.zcxk.dto.MonitorDataDto" javaType="list">
            <result property="deviceId" column="device_id" jdbcType="INTEGER"/>
            <result property="attributeId" column="attribute_id" jdbcType="INTEGER"/>
            <result property="attributeName" column="attribute_name" jdbcType="VARCHAR"/>
            <result property="unit" column="unit" jdbcType="VARCHAR"/>
            <result property="isAlarm" column="is_Alarm" jdbcType="VARCHAR"/>
            <result property="attributeType" column="attribute_type" jdbcType="VARCHAR"/>
            <result property="deviceCode" column="device_code" jdbcType="VARCHAR"/>
            <result property="latestValue" column="latest_value" jdbcType="VARCHAR"/>
            <result property="alarmRange" column="alarm_range" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>
    <!-- 设备结果集 -->
    <resultMap type="com.zcxk.dto.MonitorDataCollectDto" id="deviceDataMap">
        <result property="sceneId" column="scene_id" jdbcType="INTEGER"/>
        <result property="sceneName" column="scene_name" jdbcType="VARCHAR"/>
        <result property="deviceId" column="device_id" jdbcType="INTEGER"/>
        <result property="deviceCode" column="device_code" jdbcType="VARCHAR"/>
        <result property="deviceName" column="device_name" jdbcType="VARCHAR"/>
        <result property="pointX" column="point_x" jdbcType="VARCHAR"/>
        <result property="pointY" column="point_y" jdbcType="VARCHAR"/>
        <result property="address" column="address" jdbcType="VARCHAR"/>
        <result property="deviceState" column="device_state" jdbcType="INTEGER"/>
        <result property="sceneTypeName" column="scene_type_name" jdbcType="VARCHAR"/>
        <collection property="MonitorDataEntities" ofType="com.zcxk.dto.MonitorDataDto" javaType="list">
            <result property="attributeId" column="attribute_id" jdbcType="INTEGER"/>
            <result property="attributeName" column="attribute_name" jdbcType="VARCHAR"/>
            <result property="unit" column="unit" jdbcType="VARCHAR"/>
            <result property="isAlarm" column="is_Alarm" jdbcType="VARCHAR"/>
            <result property="attributeType" column="attribute_type" jdbcType="VARCHAR"/>
            <result property="latestValue" column="latest_value" jdbcType="VARCHAR"/>
            <result property="alarmRange" column="alarm_range" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>
    <!-- 管网图层设备结果集 -->
    <resultMap type="com.zcxk.dto.PipeNetLayerDto" id="layerMap">
        <result property="sceneId" column="scene_id" jdbcType="INTEGER"/>
        <result property="sceneName" column="scene_name" jdbcType="VARCHAR"/>
        <result property="sceneTypeName" column="scene_type_name" jdbcType="VARCHAR"/>
        <collection property="deviceDataList" ofType="com.zcxk.dto.MonitorDataCollectDto" javaType="list">
            <result property="deviceId" column="device_id" jdbcType="INTEGER"/>
            <result property="deviceCode" column="device_code" jdbcType="VARCHAR"/>
            <result property="deviceName" column="device_name" jdbcType="VARCHAR"/>
            <result property="pointX" column="point_x" jdbcType="VARCHAR"/>
            <result property="pointY" column="point_y" jdbcType="VARCHAR"/>
            <result property="address" column="address" jdbcType="VARCHAR"/>
            <result property="deviceState" column="device_state" jdbcType="INTEGER"/>
            <collection property="MonitorDataEntities" ofType="com.zcxk.dto.MonitorDataDto" javaType="list">
                <result property="attributeId" column="attribute_id" jdbcType="INTEGER"/>
                <result property="attributeName" column="attribute_name" jdbcType="VARCHAR"/>
                <result property="unit" column="unit" jdbcType="VARCHAR"/>
                <result property="isAlarm" column="is_Alarm" jdbcType="VARCHAR"/>
                <result property="alarmRange" column="alarm_range" jdbcType="VARCHAR"/>
            </collection>
        </collection>
    </resultMap>

    <sql id="Base_Column_List">
        t1.id scene_Id,
        t1.scene_name ,
        t3.scene_type_name,
        t1.point_x ,
        t1.point_y,
        t1.address ,
        t5.device_id,
        t5.attribute_id ,
        ifnull(t5.remark, t9.`name`) attribute_name,
        t9.unit ,
        case when t5.attribute_id is  null then null when t6.id is null then 0 else 1 end is_alarm,
        t5.parm_type attribute_type,
        if (t5.device_id is null, null, t4.device_code) device_code,
        concat(t11.alarm_condition,cast(t11.alarm_value as char)+0) alarm_range
    </sql>

    <sql id="sceneDeviceJoins">
        inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
        left join sms_device_scene t2 on t2.parent_scene_id=t1.id and t2. status = 1
        left join sms_device t4 on t2.device_id=t4.id and t4. status = 1 and t4. enable_state = 1
        left join sms_device_parm t5 on t5.scene_id = t2.scene_id and t5.device_id=t2.device_id and t5. status = 1
        <if test="type == 3 ">
            and t5.is_suspension = 1
        </if>
        <if test="type == 4 ">
            and t5.is_map = 1
        </if>
        left join sms_device_attribute t9 on t5.attribute_id=t9.id and t9.status = 1
        left join sms_alarm_details t6 on t6.scene_id=t2.scene_id and t6.device_id=t2.device_id
                  and t6.attribute_id=t5.attribute_id and t6.status = 1 and t6.state=1
        left join sms_alarm_setting t11 on t6.alarm_setting_id=t11.id and t11.status = 1 and t11.alarm_condition!='='
    </sql>

    <sql id="sceneAlarmJoins">
        left join (
            select parent_scene_id
            <if test="flag == null or flag ==0">
                ,count(1) alarm_count
            </if>
            <if test="flag == 1">
                ,sum(case when alarm_type='参数报警' then 1 else 0 end) alarm_count
                ,sum(case when alarm_type='状态报警' then 1 else 0 end) offline_alarm_count
            </if>
            from sms_alarm_details a1
            where a1.`status` = 1 and a1.state = 1
            <if test="sceneIds != null and sceneIds.size() > 0">
                and a1.scene_id  in
                <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            group by parent_scene_id
        )t7 on t7.parent_scene_id=t1.id
        <if test="flag == 1">
            left join (
                select parent_scene_id,count(distinct device_id) setting_device_count
                from sms_alarm_setting
                where alarm_type='状态报警' and status=1
                group by parent_scene_id
            )t10 on t7.parent_scene_id=t10.parent_scene_id
        </if>
    </sql>

    <sql id="deviceInnerJoins">
        inner join sms_scene_type t3 on t1.scene_type_id=t3.id and t3. status = 1
        inner join sms_device_scene t2 on t2.parent_scene_id=t1.id and t2. status = 1
        inner join sms_device t6 on t2.device_id=t6.id and t6. status = 1 and t6.enable_state = 1
        inner join sms_scene t4 on t2.scene_id=t4.id and t4. status = 1
        inner join sms_scene_type t5 on t4.scene_type_id=t5.id and t5. status = 1
    </sql>

    <sql id="deviceLeftJoins">
        inner join sms_scene_type t3 on t1.scene_type_id=t3.id and t3. status = 1
        left join sms_device_scene t2 on t2.parent_scene_id=t1.id and t2. status = 1
        left join sms_device t6 on t2.device_id=t6.id and t6. status = 1 and t6.enable_state = 1
        left join sms_scene t4 on t2.scene_id=t4.id and t4. status = 1
        left join sms_scene_type t5 on t4.scene_type_id=t5.id and t5. status = 1 and t5.scene_type_name = #{sceneTypeName}
    </sql>

    <!--查询实时报警信息-->
    <select id="selectAlarmDetails" resultType="com.zcxk.dto.AlarmDetailsDto">
        select
        t1.scene_name as "sceneName",
        t6.alarm_content as "alarmContent",
        t6.alarm_start_time as "alarmStartTime"
        from sms_scene t1
        inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
        inner join sms_alarm_details t6 on t1.id=t6.parent_scene_id and t6.`status`=1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t6.state=1
        <if test="sceneTypeName != null and sceneTypeName != ''">
            and t3.scene_type_name = #{sceneTypeName}
        </if>
        <if test="companyOrgId != null and companyOrgId !=''">
            and t6.company_org_id=#{companyOrgId}
        </if>
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="sceneName != null and sceneName != ''">
            and t1.scene_name  like concat('%',#{sceneName},'%')
        </if>
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id = #{tenantId}
        </if>
        order by t6.alarm_start_time desc
    </select>
    <!--查询实时报警数量-->
    <select id="selectAlarmCount" resultType="java.util.Map">
        select
        t1.scene_name as "sceneName",
        count(1) as "alalmCount"
        from sms_scene t1
        inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
        inner join sms_alarm_details t6 on t1.id=t6.parent_scene_id and t6.`status`=1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = #{sceneTypeName} and t6.state=1
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="sceneName != null and sceneName != ''">
            and t1.scene_name  like concat('%',#{sceneName},'%')
        </if>
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id = #{tenantId}
        </if>
        group by t1.scene_name
        order by alalmCount desc
    </select>
    <!--查询地图悬浮数据-->
    <select id="selectMapSuspension" resultMap="monitorDataMap">
        select
        <include refid="Base_Column_List"/>
        <if test="flag == null or flag ==0">
            ,if (t7.alarm_count>0, 1, 0) scene_state,
        </if>
        <if test="flag == 1">
            ,case when t7.offline_alarm_count=t10.setting_device_count then 2
            when t7.alarm_count>0 or t7.offline_alarm_count>0 then 1 else 0 end scene_state,
        </if>
        t1.company_org_id as "companyOrgId"
        from sms_scene t1
        <include refid="sceneDeviceJoins"/>
        <include refid="sceneAlarmJoins"/>
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1
        <choose>
            <when test="sceneTypeName != null and sceneTypeName !=''">
                and t3.scene_type_name = #{sceneTypeName}
            </when>
            <otherwise>
                and t3.scene_type_name in('水源','水厂','泵站')
            </otherwise>
        </choose>
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id = #{tenantId}
        </if>
        order by t3.scene_type_name,t1.scene_name,t5.seq
    </select>
    <!--查询地图参数数据-->
    <select id="selectMapParam" resultMap="monitorDataMap">
        select
        <include refid="Base_Column_List"/>
        <if test="flag == null or flag == 0">
            ,if (t7.alarm_count>0, 1, 0) scene_state
        </if>
        <if test="flag == 1">
            ,case when t7.offline_alarm_count=t10.setting_device_count then 2
            when t7.alarm_count>0 or t7.offline_alarm_count>0 then 1 else 0 end scene_state
        </if>
        from sms_scene t1
        <include refid="sceneDeviceJoins"/>
        <include refid="sceneAlarmJoins"/>
        where t1.parent_scene_id = 0 and t1. status = 1 and t1.id = #{sceneId}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by t1.scene_name,t5.seq
    </select>
    <!--分页查询实时数据列表-->
    <select id="selectPage" resultType="com.zcxk.dto.OnlineDataDto">
        select t1.id scene_id,t1.scene_name,t1.address,t3.scene_type_name
        from sms_scene t1
        inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
        left join(
            select parent_scene_id,max(last_update_time) last_update_time
            from sms_device_scene a1
            left join sms_device a2 on a2.id=a1.device_id
            where
            <if test="onlineDataDto.sceneIds != null and onlineDataDto.sceneIds.size() > 0">
                a1.scene_id  in
                <foreach collection="onlineDataDto.sceneIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="onlineDataDto.sceneIds == null or onlineDataDto.sceneIds.size() == 0">
                a1.scene_id is null
            </if>
            group by parent_scene_id
        )t4 on t1.id=t4.parent_scene_id
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = #{onlineDataDto.sceneTypeName}
        <if test="onlineDataDto.sceneIds != null and onlineDataDto.sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="onlineDataDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="onlineDataDto.sceneIds == null or onlineDataDto.sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="onlineDataDto.sceneName != null and onlineDataDto.sceneName != ''">
            and t1.scene_name  like concat('%',#{onlineDataDto.sceneName},'%')
        </if>
        <if test="onlineDataDto.tenantId != null and onlineDataDto.tenantId != ''">
            and t1.tenant_id = #{onlineDataDto.tenantId}
        </if>
        order by t4.last_update_time desc
    </select>
    <!--查询设备报警分析-->
    <select id="selectAlarmStats" resultType="java.util.Map">
        select
        t4.id,
        t4.device_name as "deviceName",
        count(1) as "alarmCount",
        sum(timestampdiff(minute,t6.alarm_start_time,ifnull(t6.alarm_end_time,now()))) as "alarmDuration"
        from sms_scene t1
        inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
        inner join sms_alarm_details t6 on t1.id=t6.parent_scene_id and t6.`status`=1
        inner join sms_device t4 on t4.id=t6.device_id and t4.`status`=1 and t4. enable_state = 1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t1.id = #{id}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="days != null">
            and t6.alarm_start_time>DATE(NOW() + INTERVAL-#{days}+1 DAY)
        </if>
        group by t4.id,t4.device_name
    </select>
    <!--查询场景对应参数数据-->
    <select id="selectSceneParam" resultMap="monitorDataMap">
        select
        <include refid="Base_Column_List"/>,t8.latest_value
        from sms_scene t1
        <include refid="sceneDeviceJoins"/>
        left join (
            select c1.device_id,c1.attribute_id,c1.latest_value
            from sms_month_report c1
            inner join (
            select b1.device_id,b1.attribute_id,max(b1.collect_date) collect_date
            from sms_month_report b1
            where b1.year = year(now() + interval-1 day) and b1.month = month(now() + interval-1 day) and b1.day = day(now() + interval-1 day)
            group by b1.device_id,b1.attribute_id )c2 on c1.device_id=c2.device_id and  c1.attribute_id=c2.attribute_id and c1.collect_date=c2.collect_date
        )t8 on t8.device_id=t2.device_id and t8.attribute_id =t5.attribute_id
        where t1.parent_scene_id = 0 and t1. status = 1
        <if test="type == 1 ">
            and t5.parm_type in(3,4,5,6)
        </if>
        <if test="type == 2 ">
            and t5.parm_type in(2,3,4,5,6,7,8,9,10,11,12,13,14,20)
        </if>
        <if test="type == 5 ">
            and t5.parm_type in(3,4,12,13,14)
        </if>
        <if test="sceneTypeName != null and sceneTypeName != '' ">
            and t3.scene_type_name = #{sceneTypeName}
        </if>
        <if test="sceneId != null and sceneId != '' ">
            and t1.id = #{sceneId}
        </if>
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id = #{tenantId}
        </if>
        order by t1.scene_name,t5.seq
    </select>
    <!--查询场景对应参数数据-->
    <select id="selectSceneParamForLastMonth" resultMap="monitorDataMap">
        select
        <include refid="Base_Column_List"/>,t8.latest_value
        from sms_scene t1
        <include refid="sceneDeviceJoins"/>
        left join (
        select c1.device_id,c1.attribute_id,c1.latest_value
        from sms_month_report c1
        inner join (
        select b1.device_id,b1.attribute_id,max(b1.collect_date) collect_date
        from sms_month_report b1
        where PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( b1.COLLECT_DATE, '%Y%m' ) ) =1
        group by b1.device_id,b1.attribute_id )c2 on c1.device_id=c2.device_id and  c1.attribute_id=c2.attribute_id and c1.collect_date=c2.collect_date
        )t8 on t8.device_id=t2.device_id and t8.attribute_id =t5.attribute_id
        where t1.parent_scene_id = 0 and t1. status = 1
        <if test="type == 1 ">
            and t5.parm_type in(3,4,5,6)
        </if>
        <if test="type == 2 ">
            and t5.parm_type in(2,3,4,5,6,7,8,9,10,11,12,13,14)
        </if>
        <if test="sceneTypeName != null and sceneTypeName != '' ">
            and t3.scene_type_name = #{sceneTypeName}
        </if>
        <if test="sceneId != null and sceneId != '' ">
            and t1.id = #{sceneId}
        </if>
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id = #{tenantId}
        </if>
        order by t1.scene_name,t5.seq
    </select>
    <!--按管网名称分类查询设备数量-->
    <select id="selectDeviceCount" resultType="java.util.Map">
        select  t1.id sceneId,t1.scene_name sceneName,sum(if(t5.id is null,0,1)) deviceCount,sum(if(t7.device_id is null,0,1)) alarmCount
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        left join(
            select a1.scene_id, a1.device_id from sms_alarm_details a1 where a1.`status` = 1 and a1.state = 1 group by a1.scene_id, a1.device_id
        )t7 on t7.scene_id=t2.scene_id and t7.device_id=t2.device_id
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = '管网' and t5.scene_type_name = #{sceneTypeName}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="sceneId != null">
            and t1.id = #{sceneId}
        </if>
        <if test="deviceName != null and deviceName != ''">
            and t6.device_name like concat('%', #{deviceName},'%')
        </if>
        group by t1.id,t1.scene_name
    </select>
    <!--查询设备地图参数数据-->
    <select id="selectDeviceMapParam" resultMap="deviceDataMap">
        select distinct t1.id device_id,t1.device_code,t1.device_name,t1.point_x,point_y,t1.address,t2.attribute_id, ifnull(t2.remark, t3.`name`) attribute_name,t3.unit,t2.parm_type attribute_type,
         t2.seq,case when t2.attribute_id is  null then null when t6.id is null then 0 else 1 end is_alarm,concat(t11.alarm_condition,cast(t11.alarm_value as char)+0) alarm_range
        <if test="flag == null or flag ==0">
            ,if (t4.alarm_count>0, 1, 0) device_state
        </if>
        <if test="flag == 1">
            ,case when t4.offline_alarm_count>0 then 2 when t4.alarm_count>0 then 1 else 0 end device_state
        </if>
        from sms_device  t1
        left join sms_device_parm t2 on t2.device_id=t1.id and t2.is_map=1 and t2.status=1
        left join sms_device_attribute t3 on t2.attribute_id=t3.id and t3.status=1
        left join (
            select a1.device_id
            <if test="flag == null or flag ==0">
                ,count(1) alarm_count
            </if>
            <if test="flag == 1">
                ,sum(case when alarm_type='参数报警' then 1 else 0 end) alarm_count
                ,sum(case when alarm_type='状态报警' then 1 else 0 end) offline_alarm_count
            </if>
            from sms_alarm_details a1 where a1.device_id= #{deviceId} and a1.`status` = 1 and a1.state = 1
        )t4 on t4.device_id=t1.id
        left join sms_alarm_details t6 on t6.device_id=t1.id and t6.attribute_id=t2.attribute_id and t6.status = 1 and t6.state=1
        left join sms_alarm_setting t11 on t6.alarm_setting_id=t11.id and t11.status = 1 and t11.alarm_condition!='='
        where t1. status = 1 and t1. enable_state = 1 and t1.id= #{deviceId}
        order by t2.seq
    </select>
    <!--统计设备合格数据-->
    <select id="statsDeviceQualified" resultType="com.zcxk.dto.DeviceCountStatsDto">
        select t5.scene_type_name type,count(1) totalCount,sum(if(t7.device_id is null,0,1)) alarmCount
        <if test="flag == null or flag ==0">
            ,count(1)-sum(if(t7.device_id is null,0,1)) normalCount
        </if>
        <if test="flag == 1">
            ,sum(if(t8.device_id is null,0,1)) offlineCount
            ,count(1)-sum(if(t7.device_id is null,0,1))-sum(if(t8.device_id is null,0,1)) normalCount
        </if>
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        left join(
            select a1.scene_id, a1.device_id from sms_alarm_details a1 where a1.`status` = 1 and a1.state = 1
            <if test="(sceneTypeName != null and sceneTypeName !='') or flag == 1">
                and a1.alarm_type = '参数报警'
            </if>
            group by a1.scene_id, a1.device_id
        )t7 on t7.scene_id=t2.scene_id and t7.device_id=t2.device_id
        <if test="flag == 1">
            left join(
                select a1.scene_id, a1.device_id from sms_alarm_details a1 where a1.`status` = 1 and a1.state = 1 and a1.alarm_type = '状态报警'
                group by a1.scene_id, a1.device_id
            )t8 on t8.scene_id=t2.scene_id and t8.device_id=t2.device_id
        </if>
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = '管网'
        <choose>
            <when test="sceneTypeName != null and sceneTypeName !=''">
                and t5.scene_type_name = #{sceneTypeName}
            </when>
            <otherwise>
                and t5.scene_type_name in('压力','流量','水质')
            </otherwise>
        </choose>
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="sceneId != null">
            and t1.id = #{sceneId}
        </if>
        <if test="deviceName != null and deviceName != ''">
            and t6.device_name like concat('%', #{deviceName},'%')
        </if>
        group by t5.scene_type_name
    </select>
    <!--统计设备属性合格数据-->
    <select id="statsParmQualified" resultType="com.zcxk.dto.DeviceCountStatsDto">
        select case t7.parm_type when '13' then '压力' when '7' then 'PH' when '9' then '浊度' when '11' then '余氯' end type,
        count(t8.device_id) alarmCount
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        inner join sms_device_parm t7 on  t7.scene_id=t2.scene_id and  t7.device_id=t2.device_id and t7.status=1
        <if test="sceneTypeName == '压力'">
            and t7.parm_type in(13)
        </if>
        <if test="sceneTypeName == '水质'">
            and t7.parm_type in(7,9,11)
        </if>
        left join sms_alarm_details t8 on t8.attribute_id=t7.attribute_id and t8.device_id=t7.device_id and t8.scene_id=t7.scene_id and t8.`status` = 1 and t8.state = 1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = '管网' and t5.scene_type_name = #{sceneTypeName}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="sceneId != null">
            and t1.id = #{sceneId}
        </if>
        <if test="deviceName != null and deviceName != ''">
            and t6.device_name like concat('%', #{deviceName},'%')
        </if>
        group by t7.parm_type
    </select>
    <!-- 查询设备参数-->
    <select id="statsDeviceParm" resultMap="deviceDataMap">
        SELECT t6.id device_id,t6.device_code,t6.device_name,t7.attribute_id,t7.parm_type attribute_type,t3.scene_type_name,t10.unit,
         case when t7.attribute_id is  null then null when t8.id is null then 0 else 1 end is_alarm
        <if test="type == 4 ">,t9.latest_value</if>
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        left join sms_device_parm t7 on  t7.scene_id=t2.scene_id and  t7.device_id=t2.device_id and t7.status=1
        <if test="type == 1 ">
            and t7.parm_type in(13)
        </if>
        <if test="type == 2 ">
            and t7.parm_type in(14,16)
        </if>
        <if test="type == 3">
            and t7.parm_type in(7,9,11,18)
        </if>
        <if test="type == 4 ">
            and t7.parm_type in(16)
            left join (
                select c1.device_id,c1.attribute_id,c1.latest_value
                from sms_month_report c1
                inner join (
                select b1.device_id,b1.attribute_id,max(b1.collect_date) collect_date
                from sms_month_report b1
                where b1.year = year(now() + interval-1 day) and b1.month = month(now() + interval-1 day) and b1.day = day(now() + interval-1 day)
                group by b1.device_id,b1.attribute_id )c2 on c1.device_id=c2.device_id and  c1.attribute_id=c2.attribute_id and c1.collect_date=c2.collect_date
            )t9 on t9.device_id=t2.device_id and t9.attribute_id =t7.attribute_id
        </if>
        <if test="type == 5 ">
            and t7.parm_type in(14)
        </if>
        left join sms_alarm_details t8 on t8.attribute_id=t7.attribute_id and t8.device_id=t7.device_id and t8.scene_id=t7.scene_id and t8.`status` = 1 and t8.state = 1
        left join sms_device_attribute t10 on t10.id=t7.attribute_id and t10.status=1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = '管网' and t5.scene_type_name = #{sceneTypeName}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="sceneId != null">
            and t1.id = #{sceneId}
        </if>
        <if test="deviceIds != null and deviceIds.size() > 0">
            and t6.id  in
            <foreach collection="deviceIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="deviceName != null and deviceName != ''">
            and t6.device_name like concat('%', #{deviceName},'%')
        </if>
    </select>
    <!--分页查询管网实时数据列表-->
    <select id="selectPipeNetPage" resultType="com.zcxk.dto.OnlineDataDto">
        select t1.id scene_id,t1.scene_name,t6.id device_id,t6.device_code,t6.device_name,t6.address,if (t7.alarm_count>0, 1, 0) deviceState
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        left join(
            select a1.scene_id, a1.device_id ,count(1) alarm_count from sms_alarm_details a1 where a1.`status` = 1 and a1.state = 1 group by a1.scene_id, a1.device_id
        )t7 on t7.scene_id=t2.scene_id and t7.device_id=t2.device_id
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = '管网' and t5.scene_type_name = #{onlineDataDto.sceneTypeName}
        <if test="onlineDataDto.sceneIds != null and onlineDataDto.sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="onlineDataDto.sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="onlineDataDto.sceneIds == null or onlineDataDto.sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="onlineDataDto.sceneId != null">
            and t1.id = #{onlineDataDto.sceneId}
        </if>
        <if test="onlineDataDto.deviceName != null and onlineDataDto.deviceName != ''">
            and t6.device_name like concat('%', #{onlineDataDto.deviceName},'%')
        </if>
        order by t6.last_update_time desc
    </select>
    <!--查询管网地图图层及设备-->
    <select id="selectPipeNetLayer" resultMap="layerMap">
        select t4.id scene_id,t4.scene_name,t5.scene_type_name,t6.id device_id,t6.device_code,t6.device_name,t6.point_x,t6.point_y,t6.address,concat(t11.alarm_condition,cast(t11.alarm_value as char)+0) alarm_range
        ,t8.attribute_id ,ifnull(t8.remark, t9.`name`) attribute_name,t9.unit,case when t8.attribute_id is null then null when t10.id is null then 0 else 1 end is_alarm
        <if test="flag == null or flag ==0">
            ,if (t7.alarm_count>0, 1, 0) device_state
        </if>
        <if test="flag == 1">
            ,case when t7.offline_alarm_count>0 then 2 when t7.alarm_count>0 then 1 else 0 end device_state
        </if>
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        left join(
            select a1.scene_id, a1.device_id
            <if test="flag == null or flag ==0">
                ,count(1) alarm_count
            </if>
            <if test="flag == 1">
                ,sum(case when alarm_type='参数报警' then 1 else 0 end) alarm_count
                ,sum(case when alarm_type='状态报警' then 1 else 0 end) offline_alarm_count
            </if>
            from sms_alarm_details a1 where a1.`status` = 1 and a1.state = 1 group by a1.scene_id, a1.device_id
        )t7 on t7.scene_id=t2.scene_id and t7.device_id=t2.device_id
        left join sms_device_parm t8 on t8.device_id=t2.device_id and t8. status = 1 and t8.is_suspension = 1
        left join sms_device_attribute t9 on t8.attribute_id=t9.id and t9.status = 1
        left join sms_alarm_details t10 on t10.scene_id=t2.scene_id and t10.device_id=t2.device_id and t10.attribute_id=t8.attribute_id and t10.status = 1 and t10.state=1
        left join sms_alarm_setting t11 on t10.alarm_setting_id=t11.id and t11.status = 1 and t11.alarm_condition!='='
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = '管网'
        <choose>
            <when test="sceneTypeName != null and sceneTypeName !=''">
                and t5.scene_type_name = #{sceneTypeName}
            </when>
            <otherwise>
                and t5.scene_type_name in('压力','流量','水质')
            </otherwise>
        </choose>
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        order by t4.scene_name,t8.seq
    </select>
    <!--查询昨日水厂制水总量-->
    <select id="selectTotalYieldWater" resultType="java.lang.Double">
        select sum(sum_value)/10000 yieldWater
        from sms_scene t1
        <include refid="sceneDeviceJoins"/>
        inner join sms_month_report t8 on t8.device_id=t2.device_id and t8.attribute_id =t5.attribute_id
            and t8.year = year(now() + interval-1 day) and t8.month = month(now() + interval-1 day) and t8.day = day(now() + interval-1 day)
        where t1.parent_scene_id = 0 and t1. status = 1 and t5.parm_type in(3) and t3.scene_type_name = '水厂'
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id = #{tenantId}
        </if>
    </select>
    <!--查询实时数据列表-->
    <select id="selectList" resultType="com.zcxk.dto.OnlineDataDto">
        select t1.id scene_id,t1.scene_name,t1.address,t3.scene_type_name
            ,case when t7.offline_alarm_count=t10.setting_device_count then 2
            when t7.alarm_count>0 or t7.offline_alarm_count>0 then 1 else 0 end scene_state
        from sms_scene t1
        inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
        left join(
            select parent_scene_id,max(last_update_time) last_update_time
            from sms_device_scene a1
            left join sms_device a2 on a2.id=a1.device_id
            where
            <if test="sceneIds != null and sceneIds.size() > 0">
                a1.scene_id  in
                <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="sceneIds == null or sceneIds.size() == 0">
                a1.scene_id is null
            </if>
            group by parent_scene_id
        )t4 on t1.id=t4.parent_scene_id
        left join (
            select parent_scene_id
                ,sum(case when alarm_type='参数报警' then 1 else 0 end) alarm_count
                ,sum(case when alarm_type='状态报警' then 1 else 0 end) offline_alarm_count
            from sms_alarm_details a1
            where a1.`status` = 1 and a1.state = 1
            <if test="sceneIds != null and sceneIds.size() > 0">
                and a1.scene_id  in
                <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            group by parent_scene_id
        )t7 on t7.parent_scene_id=t1.id
        left join (
            select parent_scene_id,count(distinct device_id) setting_device_count
            from sms_alarm_setting
            where alarm_type='状态报警' and status=1
            group by parent_scene_id
        )t10 on t7.parent_scene_id=t10.parent_scene_id
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = #{sceneTypeName}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="tenantId != null and tenantId != ''">
            and t1.tenant_id = #{tenantId}
        </if>
        <if test="sceneId != null and sceneId != ''">
            and t1.id = #{sceneId}
        </if>
        <if test="companyOrgId != null and companyOrgId != ''">
            and t1.company_org_id = #{companyOrgId}
        </if>
        order by  field(scene_state,1,0,2),t4.last_update_time desc
    </select>
    <!--查询管网实时数据列表-->
    <select id="selectPipeNetList" resultType="com.zcxk.dto.OnlineDataDto">
        select t1.id scene_id,t1.scene_name,t6.id device_id,t6.device_code,t6.device_name,t6.address
        ,case when t7.offline_alarm_count>0 then 2 when t7.alarm_count>0 then 1 else 0 end device_state
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        left join(
            select a1.scene_id, a1.device_id
            ,sum(case when alarm_type='参数报警' then 1 else 0 end) alarm_count
            ,sum(case when alarm_type='状态报警' then 1 else 0 end) offline_alarm_count
         from sms_alarm_details a1 where a1.`status` = 1 and a1.state = 1 group by a1.scene_id, a1.device_id
        )t7 on t7.scene_id=t2.scene_id and t7.device_id=t2.device_id
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = '管网' and t5.scene_type_name = #{sceneTypeName}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        <if test="deviceId != null and deviceId != ''">
            and t6.id = #{deviceId}
        </if>
        <if test="companyOrgId != null and companyOrgId != ''">
            and t1.company_org_id = #{companyOrgId}
        </if>
        order by field(device_state,1,0,2),t6.last_update_time desc
    </select>
    <!--查找场景下的设备-->
    <select id="selectSceneDevice" resultType="com.zcxk.dto.ranking.SceneDeviceDto">
        select  t6.id deviceId,
                t6.device_code deviceCode,
                t6.device_name deviceName,
                t8.attribute_id attributeId,
                ifnull(t8.remark, t9.`name`) attribute_name
        from sms_scene t1
        <include refid="deviceInnerJoins"/>
        left join sms_device_parm t8 on t8.scene_id = t2.scene_id and t8.device_id=t2.device_id and t8. status = 1 and t8.PARM_TYPE = #{parmType}
        left join sms_device_attribute t9 on t8.attribute_id=t9.id and t9.status = 1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1
        and t3.scene_type_name = #{firstSceneTypeName}
        and t5.scene_type_name = #{secondSceneTypeName}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sceneIds == null or sceneIds.size() == 0">
            and t1.id is null
        </if>
        order by t4.scene_name,t8.seq
    </select>
    <!--查询场景实时状态数量-->
    <select id="selectStateCount" resultType="java.util.Map">
        select sceneState,count(1) sceneCount
        from(
            select case when t7.offline_alarm_count=t10.setting_device_count then 2
                    when t7.alarm_count>0 or t7.offline_alarm_count>0 then 1 else 0 end sceneState
            from sms_scene t1
            inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
            <include refid="sceneAlarmJoins"/>
            where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1
            <if test="sceneTypeName != null and sceneTypeName != ''">
                and t3.scene_type_name = #{sceneTypeName}
            </if>
            <if test="sceneIds != null and sceneIds.size() > 0">
                and t1.id  in
                <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="sceneIds == null or sceneIds.size() == 0">
                and t1.id is null
            </if>
        )tab1
        group by sceneState
    </select>
    <!--查询设备报警分析-->
    <select id="selectAlarmStatsInfo" resultType="com.zcxk.vo.AlarmDetailsVo">
        select
        t4.id,
        t4.device_name as "deviceName",
        t6.ALARM_START_TIME,
        t6.ALARM_END_TIME
        from sms_scene t1
        inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
        inner join sms_alarm_details t6 on t1.id=t6.parent_scene_id and t6.`status`=1
        inner join sms_device t4 on t4.id=t6.device_id and t4.`status`=1 and t4. enable_state = 1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t1.id = #{id}
        <if test="sceneIds != null and sceneIds.size() > 0">
            and t1.id  in
            <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and
        (
         (t6.ALARM_START_TIME  >  #{startTime} and #{endTime} > t6.ALARM_START_TIME )
         or
         (t6.ALARM_END_TIME    >  #{startTime} and #{endTime} > t6.ALARM_END_TIME  )
        )
    </select>
</mapper>