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
     
    
        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
        
            and t5.is_suspension = 1
         
        
            and t5.is_map = 1
         
        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 (
            select parent_scene_id,count(1) alarm_count
            from sms_alarm_details a1
            where a1.`status` = 1 and a1.state = 1
            
                and a1.scene_id  in
                
                    #{item}
                 
             
            group by parent_scene_id
        )t7 on t7.parent_scene_id=t1.id
     
    
        inner join sms_scene_type t3 on t1.scene_type_id=t3.id
        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
        inner join sms_scene_type t5 on t4.scene_type_id=t5.id and t5. status = 1
     
    
        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}
     
    
    
        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 t3.scene_type_name = #{sceneTypeName} and t6.state=1
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.scene_name  like concat('%',#{sceneName},'%')
         
        
            and t1.tenant_id = #{tenantId}
         
        order by t6.alarm_start_time desc
     
    
    
        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
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.scene_name  like concat('%',#{sceneName},'%')
         
        
            and t1.tenant_id = #{tenantId}
         
        group by t1.scene_name
        order by alalmCount desc
     
    
    
        select
         ,if (t7.alarm_count>0, 1, 0) scene_state
        from sms_scene t1
         
         
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1
        
            
                and t3.scene_type_name = #{sceneTypeName}
             
            
                and t3.scene_type_name in('水源','水厂','泵站')
             
         
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.tenant_id = #{tenantId}
         
        order by t3.scene_type_name,t1.scene_name,t5.seq
     
    
    
        select
         
        from sms_scene t1
         
         
        where t1.parent_scene_id = 0 and t1. status = 1 and t1.id = #{sceneId}
        
            and t1.id  in
            
                #{item}
             
         
        order by t1.scene_name,t5.seq
     
    
    
        select t1.id scene_id,t1.scene_name,t1.address
        from sms_scene t1
        inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
        where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = #{onlineDataDto.sceneTypeName}
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.scene_name  like concat('%',#{onlineDataDto.sceneName},'%')
         
        
            and t1.tenant_id = #{onlineDataDto.tenantId}
         
        order by  t1.date_create
     
    
    
        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}
        
            and t1.id  in
            
                #{item}
             
         
        
            and t6.alarm_start_time>DATE(NOW() + INTERVAL-#{days}+1 DAY)
         
        group by t4.id,t4.device_name
     
    
    
        select
         ,t8.latest_value
        from sms_scene t1
         
        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
        
            and t5.parm_type in(3,4,5,6)
         
        
            and t5.parm_type in(2,3,4,5,6,7,8,9,10,11,12,13,14)
         
        
            and t3.scene_type_name = #{sceneTypeName}
         
        
            and t1.id = #{sceneId}
         
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.tenant_id = #{tenantId}
         
        order by t1.scene_name,t5.seq
     
    
    
        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
         
        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}
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.id = #{sceneId}
         
        
            and t6.device_name like concat('%', #{deviceName},'%')
         
        group by t1.id,t1.scene_name
     
    
    
        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.seq,case when t2.attribute_id is  null then null when t6.id is null then 0 else 1 end is_alarm, if (t4.alarm_count>0, 1, 0) device_state
        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,count(1) alarm_count 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
        where t1. status = 1 and t1. enable_state = 1 and t1.id= #{deviceId}
        order by t2.seq
     
    
    
        select t5.scene_type_name type,count(1) totalCount,sum(if(t7.device_id is null,0,1)) alarmCount,count(1)-sum(if(t7.device_id is null,0,1)) normalCount
        from sms_scene t1
         
        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}
             
            
                and t5.scene_type_name in('压力','流量','水质')
             
         
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.id = #{sceneId}
         
        
            and t6.device_name like concat('%', #{deviceName},'%')
         
        group by t5.scene_type_name
     
    
    
        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
         
        inner join sms_device_parm t7 on  t7.scene_id=t2.scene_id and  t7.device_id=t2.device_id and t7.status=1
        
            and t7.parm_type in(13)
         
        
            and t7.parm_type in(7,9,11)
         
        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}
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.id = #{sceneId}
         
        
            and t6.device_name like concat('%', #{deviceName},'%')
         
        group by t7.parm_type
     
    
    
        SELECT t6.id device_id,t6.device_code,t6.device_name,t7.attribute_id,t7.parm_type attribute_type,
         case when t7.attribute_id is  null then null when t8.id is null then 0 else 1 end is_alarm
        ,t9.latest_value 
        from sms_scene t1
         
        left join sms_device_parm t7 on  t7.scene_id=t2.scene_id and  t7.device_id=t2.device_id and t7.status=1
        
            and t7.parm_type in(13)
         
        
            and t7.parm_type in(14,16)
         
        
            and t7.parm_type in(7,9,11,18)
         
        
            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
         
        
            and t7.parm_type in(14)
         
        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}
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.id = #{sceneId}
         
        
            and t6.id  in
            
                #{item}
             
         
        
            and t6.device_name like concat('%', #{deviceName},'%')
         
     
    
    
        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
         
        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}
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        
            and t1.id = #{onlineDataDto.sceneId}
         
        
            and t6.device_name like concat('%', #{onlineDataDto.deviceName},'%')
         
        order by t6.last_update_time desc
     
    
    
        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,if (t7.alarm_count>0, 1, 0) device_state
        from sms_scene t1
         
        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 = #{sceneTypeName}
             
            
                and t5.scene_type_name in('压力','流量','水质')
             
         
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.id is null
         
        order by t4.scene_name,t6.date_create
     
    
    
        select sum(sum_value) yieldWater
        from sms_scene t1
         
        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 = '水厂'
        
            and t1.id  in
            
                #{item}
             
         
        
            and t1.tenant_id = #{tenantId}