<?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.charge.core.dao.SceneMapper">

    <!--  实体栏位  -->
    <sql id="sceneColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.parent_scene_id as "parentSceneId" ,
         a.parent_scene_ids as "parentSceneIds" ,
         a.scene_name as "sceneName" ,
         a.company_org_id as "companyOrgId" ,
         a.dept_org_id as "deptOrgId" ,
         a.scene_type_id as "sceneTypeId" ,
         a.enable_state as "enableState" ,
         a.run_time as "runTime" ,
         a.address as "address" ,
         a.point_x as "pointX" ,
         a.point_y as "pointY" ,
         a.remark as "remark" ,
         a.status as "status" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy",
         s.scene_type_name as "sceneTypeName"
     </sql>
    <sql id="deviceJoins">
        left join sms_scene_type s on s.id =a.scene_type_id and s.status=1
    </sql>
    <!--  根据主键获取实体   -->
    <select id="findSceneById" resultType="com.zcxk.charge.core.entity.SceneEntity">
        SELECT
        <include refid="sceneColumns"/>
        FROM sms_scene a
        <include refid="deviceJoins"/>
        WHERE a.id = #{id} and a.status=1
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.charge.core.entity.SceneEntity">
        SELECT
        <include refid="sceneColumns"/>
        FROM sms_scene a
        <include refid="deviceJoins"/>
        <where>
            a.status=1
            <if test="scene.companyOrgId!=null and scene.deptOrgId!=null">
               and a.DEPT_ORG_ID = #{scene.deptOrgId} and a.COMPANY_ORG_ID = #{scene.companyOrgId}
            </if>

            <if test="scene.companyOrgId!=null and scene.deptOrgId == null">
                and a.DEPT_ORG_ID is null and a.COMPANY_ORG_ID = #{scene.companyOrgId}
            </if>

            <if test="scene.tenantId != null  and scene.tenantId != ''">and a.tenant_id = #{scene.tenantId}</if>
            <if test="scene.parentSceneId != null ">and a.parent_scene_id = #{scene.parentSceneId}</if>
            <if test="scene.sceneName != null  and scene.sceneName != ''">
                and a.scene_name LIKE concat('%',#{scene.sceneName},'%')
            </if>
            <if test="scene.sceneTypeId != null ">and a.scene_type_id = #{scene.sceneTypeId}</if>
            <if test="scene.sceneTypeName != null ">and s.scene_type_name = #{scene.sceneTypeName}</if>
            <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                <if test="scene.permissonType == 5 or scene.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="scene.permissonType == 4 or scene.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="scene.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
        </where>
        order by  a.date_create
    </select>
    <!--  根据主键获取租户菜单  -->
    <select id="findBySceneTypeIds" resultType="com.zcxk.charge.core.entity.SceneEntity">
        select a.id
        FROM sms_scene a
        <include refid="deviceJoins"/>
        WHERE a.scene_type_id in
        <foreach collection = "Ids" item = "dramaId" open = "(" close = ")" separator = "," >
            #{dramaId}
        </foreach>
    </select>
    <!--  根据主键获取租户菜单  -->
    <select id="findByParentSceneIds" resultType="com.zcxk.charge.core.entity.SceneEntity">
        select a.id
        FROM sms_scene a
        <include refid="deviceJoins"/>
        WHERE a.status=1 and  a.parent_scene_id in
        <foreach collection = "Ids" item = "dramaId" open = "(" close = ")" separator = "," >
            #{dramaId}
        </foreach>
    </select>
    <select id="findByParentIdsLike" resultType="com.zcxk.charge.core.entity.SceneEntity">
        SELECT
        <include refid="sceneColumns"/>
        FROM sms_scene a
        <include refid="deviceJoins"/>
        <where>
            a.status = 1
            <if test="scene.companyOrgId != null ">and a.COMPANY_ORG_ID=#{scene.companyOrgId}</if>
            <if test="scene.tenantId != null  and scene.tenantId != ''">and a.tenant_id = #{scene.tenantId}</if>
            <if test="scene.parentSceneIds != null ">and a.parent_scene_ids LIKE #{scene.parentSceneIds}</if>
            <if test="scene.sceneTypeName != null ">and s.scene_type_name = #{scene.sceneTypeName}</if>
            <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                <if test="scene.permissonType == 5 or scene.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="scene.permissonType == 4 or scene.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="scene.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
        </where>
    </select>
    <!--  根据获取实体 page   -->
    <select id="findPage" resultType="com.zcxk.charge.core.entity.SceneEntity">
        SELECT
        <include refid="sceneColumns"/>
        FROM sms_scene a
        <include refid="deviceJoins"/>
        <where>
            a.status=1 and a.parent_scene_id=0
            <if test="scene.companyOrgId!=null">
                 and a.COMPANY_ORG_ID = #{scene.companyOrgId}
            </if>

            <if test="scene.deptOrgId != null">
                and a.DEPT_ORG_ID = #{scene.deptOrgId}
            </if>
            <if test="scene.tenantId != null  and scene.tenantId != ''">and a.tenant_id = #{scene.tenantId}</if>
            <if test="scene.parentSceneId != null ">and a.parent_scene_id = #{scene.parentSceneId}</if>
            <if test="scene.sceneName != null  and scene.sceneName != ''">
                and a.scene_name LIKE concat('%',#{scene.sceneName},'%')
            </if>
            <if test="scene.sceneTypeId != null ">and a.scene_type_id = #{scene.sceneTypeId}</if>
            <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                <if test="scene.permissonType == 5 or scene.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="scene.permissonType == 4 or scene.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="scene.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
        </where>
        order by  a.date_create  desc
    </select>
    <!--一级场景查询-->
    <select id="selectByTypeName" resultType="com.zcxk.charge.core.entity.SceneEntity">
        SELECT
        <include refid="sceneColumns"/>
        FROM sms_scene a
        <include refid="deviceJoins"/>
        <where>
            a.parent_scene_id=0 and a.status=1 and a.enable_state=1
            <if test="scene.sceneTypeName == null ">
             and (s.scene_type_name='水源' or s.scene_type_name='水厂')
            </if>
            <if test="scene.sceneTypeName != null ">
                and s.scene_type_name=#{scene.sceneTypeName}
            </if>
            <if test="scene.companyOrgId != null ">and a.COMPANY_ORG_ID=#{scene.companyOrgId}</if>
            <if test="scene.tenantId != null  and scene.tenantId != ''">and a.tenant_id = #{scene.tenantId}</if>
            <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                <if test="scene.permissonType == 5 or scene.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="scene.permissonType == 4 or scene.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="scene.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
        </where>
        order by  a.date_create
    </select>
    <!--根据一级场景查询所有场景信息-->
    <select id="findByParentId" resultType="com.zcxk.charge.core.entity.SceneEntity">
        SELECT
        <include refid="sceneColumns"/>
        FROM sms_scene a
        <include refid="deviceJoins"/>
        <where>
            a.status=1
            <if test="scene.parentSceneIds != null ">and a.parent_scene_ids LIKE #{scene.parentSceneIds}</if>
            <if test="scene.tenantId != null  and scene.tenantId != ''">and a.tenant_id = #{scene.tenantId}</if>
            <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                <if test="scene.permissonType == 5 or scene.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="scene.permissonType == 4 or scene.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="scene.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
        </where>
        order by  a.date_create
    </select>
    <!--根据场景id查询一级场景信息-->
    <select id="findParentSceneById" resultType="com.zcxk.charge.core.entity.SceneEntity">
        select t1.scene_name as "sceneName",t2.id as "parentSceneId",t2.scene_name as "parentSceneName"
        from sms_scene t1
        left join sms_scene t2 on substring_index(substring_index(t1.parent_scene_ids, ',', 2), ',', -1)=t2.id
        where t1.id=#{id}
    </select>
    <!--根据用户权限统计一级场景个数-->
    <select id="findSceneAmount" resultType="map">
        select a.sceneTypeName,amount,newAmount from
        (
            select count(a.ID) amount,b.SCENE_TYPE_NAME sceneTypeName
            from sms_scene a INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
            <where>
                a.PARENT_SCENE_ID=0
                and a.STATUS=1
                <if test="scene.tenantId != null  and scene.tenantId != ''">
                    and a.tenant_id = #{scene.tenantId}
                </if>
                <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                    <if test="scene.permissonType == 5 or scene.permissonType == 2">
                        and ( a.DEPT_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        or
                        a.COMPANY_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        )
                    </if>
                    <if test="scene.permissonType == 4 or scene.permissonType == 3">
                        and a.DEPT_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                    </if>
                    <if test="scene.permissonType == 1">
                        and a.COMPANY_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                    </if>
                </if>
                and SCENE_TYPE_NAME in('水源','水厂','泵站')
            </where>
            group by b.SCENE_TYPE_NAME
        ) a left join
        (
            select count(a.ID) newAmount,b.SCENE_TYPE_NAME sceneTypeName
            from sms_scene a INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
            <where>
                a.PARENT_SCENE_ID=0
                and a.STATUS=1
                <if test="months != null">
                    and a.run_time>=date_sub(date_format(curdate(), '%y-%m-1' ), interval #{months}-1 month)
                </if>
                <if test="scene.tenantId != null  and scene.tenantId != ''">
                    and a.tenant_id = #{scene.tenantId}
                </if>
                <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                    <if test="scene.permissonType == 5 or scene.permissonType == 2">
                        and ( a.DEPT_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        or
                        a.COMPANY_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        )
                    </if>
                    <if test="scene.permissonType == 4 or scene.permissonType == 3">
                        and a.DEPT_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                    </if>
                    <if test="scene.permissonType == 1">
                        and a.COMPANY_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                    </if>
                </if>
                and SCENE_TYPE_NAME in('水源','水厂','泵站')
            </where>
            group by b.SCENE_TYPE_NAME
        ) b
        on a.sceneTypeName = b.sceneTypeName
        union all
        select '户表' , ifnull(sum(user_meter_count),0) amount,sum(user_meter_add_count) newAmount
        from sms_month_revenue a
        <where>
            <if test="months != null">
                and a.collect_date>=date_sub(date_format(curdate(), '%y-%m-1' ), interval #{months}-1 month)
            </if>
            <if test="scene.tenantId != null  and scene.tenantId != ''">
                and a.tenant_id = #{scene.tenantId}
            </if>
            <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                <if test="scene.permissonType == 5 or scene.permissonType == 2">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="scene.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <!--报警场景及报警设备查询-->
    <select id="selectAlarmSceneAndDevice" resultType="map">
       select amount, id, scene_id, name, sort from(
        select count(1) amount, a.id,a.id scene_id, a.SCENE_NAME name,2 sort
        from sms_scene a INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
        INNER JOIN sms_alarm_details c on c.PARENT_SCENE_ID=a.ID
        <where>
            a.PARENT_SCENE_ID=0 and a.STATUS=1
            <if test="scene.tenantId != null  and scene.tenantId != ''">
                and a.tenant_id = #{scene.tenantId}
            </if>
            <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and  scene.programItems != null and scene.programItems.size() > 0">
                <if test="scene.permissonType == 5 or scene.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="scene.permissonType == 4 or scene.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="scene.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
            and c.alarm_start_time &gt;= date_format(#{startDate,jdbcType=VARCHAR},'%Y-%m-%d')
            and c.alarm_start_time &lt; date_format(#{endDate,jdbcType=VARCHAR},'%Y-%m-%d')
            and SCENE_TYPE_NAME in('水源','水厂','泵站')
        </where>
        group by a.scene_name, a.id, sort

        union all

        select count(*) amount, id, GROUP_CONCAT(DISTINCT scene_id SEPARATOR ',') scene_id, name,1 sort from
        (
            select d.id,c.PARENT_SCENE_ID scene_id, d.device_name name
            from sms_alarm_details c
            INNER JOIN sms_device d on c.DEVICE_ID=d.ID
            INNER JOIN sms_scene a on  c.PARENT_SCENE_ID=a.ID
            INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
            <where>
                a.PARENT_SCENE_ID=0 and a.STATUS=1
                <if test="scene.tenantId != null  and scene.tenantId != ''">
                    and a.tenant_id = #{scene.tenantId}
                </if>
                <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and scene.programItems != null and scene.programItems.size() > 0">
                    <if test="scene.permissonType == 5 or scene.permissonType == 2">
                        and ( a.DEPT_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        or
                        a.COMPANY_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        )
                    </if>
                    <if test="scene.permissonType == 4 or scene.permissonType == 3">
                        and a.DEPT_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                    </if>
                    <if test="scene.permissonType == 1">
                        and a.COMPANY_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                    </if>
                </if>
                and c.alarm_start_time &gt;= date_format(#{startDate,jdbcType=VARCHAR},'%Y-%m-%d')
                and c.alarm_start_time &lt; date_format(#{endDate,jdbcType=VARCHAR},'%Y-%m-%d')
                and SCENE_TYPE_NAME='管网'
            </where>
            ) t1
            group by name, id, scene_id, sort ) t
            order by amount desc
    </select>

    <select id="selectMonthAlarmTimes" resultType="map">
        select count(1) 数量, DATE_FORMAT(c.DATE_CREATE,'%Y-%m-%d') 日期
            <if test="sort == 2">
                from sms_scene a INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
                INNER JOIN sms_alarm_details c on c.PARENT_SCENE_ID=a.ID
                where a.ID = #{scene.id}
            </if>
            <if test="sort == 1">
                from sms_scene a inner join sms_scene_type b on a.SCENE_TYPE_ID=b.ID
                INNER JOIN sms_alarm_details c on c.PARENT_SCENE_ID=a.ID
                where a.PARENT_SCENE_ID=0 and a.STATUS=1
                and c.device_id = #{scene.id}
                and b.SCENE_TYPE_NAME='管网'
                <if test="scene.tenantId != null  and scene.tenantId != ''">
                    and a.tenant_id = #{scene.tenantId}
                </if>
                <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and scene.programItems != null and scene.programItems.size() > 0">
                    <if test="scene.permissonType == 5 or scene.permissonType == 2">
                        and ( a.DEPT_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        or
                        a.COMPANY_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        )
                    </if>
                    <if test="scene.permissonType == 4 or scene.permissonType == 3">
                        and a.DEPT_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                    </if>
                    <if test="scene.permissonType == 1">
                        and a.COMPANY_ORG_ID in
                        <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                            #{item.orgId}
                        </foreach>
                        and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                    </if>
                </if>
            </if>
            and c.alarm_start_time &gt;= date_format(#{startDate,jdbcType=VARCHAR},'%Y-%m-%d')
            and c.alarm_start_time &lt; date_format(#{endDate,jdbcType=VARCHAR},'%Y-%m-%d')
            group by DATE_FORMAT(c.DATE_CREATE,'%Y-%m-%d')
    </select>

    <select id="selectYearAlarmTimes" resultType="map">
        select count(1) 数量, DATE_FORMAT(c.DATE_CREATE,'%Y-%m') 日期
        <if test="sort == 2">
            from sms_scene a INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
            INNER JOIN sms_alarm_details c on c.PARENT_SCENE_ID=a.ID
            where a.ID = #{scene.id}
        </if>
        <if test="sort == 1">
        from sms_scene a inner join  sms_scene_type b on a.SCENE_TYPE_ID=b.ID
            INNER JOIN sms_alarm_details c on c.PARENT_SCENE_ID=a.ID
            where a.PARENT_SCENE_ID=0 and a.STATUS=1
            and c.device_id = #{scene.id}
            and b.SCENE_TYPE_NAME='管网'
            <if test="scene.tenantId != null  and scene.tenantId != ''">
                and a.tenant_id = #{scene.tenantId}
            </if>
            <if test="scene.userType!=null and scene.userType!=-999 and scene.userType!=-9999 and scene.programItems != null and scene.programItems.size() > 0">
                <if test="scene.permissonType == 5 or scene.permissonType == 2">
                    and ( a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="scene.permissonType == 4 or scene.permissonType == 3">
                    and a.DEPT_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="scene.permissonType == 1">
                    and a.COMPANY_ORG_ID in
                    <foreach collection="scene.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID =0)
                </if>
            </if>
        </if>
        and c.date_create &gt;= date_format(#{startDate,jdbcType=VARCHAR},'%Y-%m-%d')
        and c.date_create &lt; date_format(#{endDate,jdbcType=VARCHAR},'%Y-%m-%d')
        group by DATE_FORMAT(c.DATE_CREATE,'%Y-%m')
    </select>

    <select id="selectAlarmLable" resultType="map">
        select amount 数量, label 标签 from
            (
                select count(1) amount, concat(label,op) label from
                    (
                        select if(d.REMARK is null,e.NAME,d.REMARK) label,CASE WHEN ALARM_CONDITION='&gt;' OR ALARM_CONDITION='&gt;=' then '高'
                        WHEN ALARM_CONDITION='&lt;' OR ALARM_CONDITION='&lt;=' then '低'
                        ELSE '' END op
                        <if test="sort == 2">
                            from sms_scene a INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
                            INNER JOIN sms_alarm_details c on c.PARENT_SCENE_ID=a.ID
                            INNER JOIN sms_device_attribute e on e.ID = c.ATTRIBUTE_ID and e.STATUS=1
                            LEFT JOIN sms_device_parm d on d.ATTRIBUTE_ID = c.ATTRIBUTE_ID and d.DEVICE_ID=c.DEVICE_ID and d.TENANT_ID=c.TENANT_ID and d.PARENT_SCENE_ID=c.PARENT_SCENE_ID and d.STATUS=1
                            INNER JOIN sms_alarm_setting f on f.device_id = c.device_id and f.SCENE_ID=c.SCENE_ID  and f.ATTRIBUTE_ID=c.ATTRIBUTE_ID and f.STATUS=1
                            where a.ID = #{scene.id}
                        </if>
                        <if test="sort == 1">
                            from sms_alarm_details c
                            INNER JOIN sms_device_attribute e on e.ID = c.ATTRIBUTE_ID and e.STATUS=1
                            LEFT JOIN sms_device_parm d on d.ATTRIBUTE_ID = c.ATTRIBUTE_ID and d.DEVICE_ID=c.DEVICE_ID and d.TENANT_ID=c.TENANT_ID and d.PARENT_SCENE_ID=c.PARENT_SCENE_ID and d.STATUS=1
                            INNER JOIN sms_alarm_setting f on f.device_id = c.device_id and f.SCENE_ID=c.SCENE_ID and f.ATTRIBUTE_ID=c.ATTRIBUTE_ID and f.STATUS=1
                            where c.device_id = #{scene.id}
                        </if>
                        and c.date_create &gt;= date_format(#{startDate,jdbcType=VARCHAR},'%Y-%m-%d')
                        and c.date_create &lt; date_format(#{endDate,jdbcType=VARCHAR},'%Y-%m-%d')
                    ) t1
                group by concat(label,op)
                union all
                select count(1) amount, label from
                (
                    select '离线' label
                    <if test="sort == 2">
                        from sms_scene a INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
                        INNER JOIN sms_alarm_details c on c.PARENT_SCENE_ID=a.ID
                        where a.ID = #{scene.id} and alarm_type='状态报警'
                    </if>
                    <if test="sort == 1">
                        from sms_alarm_details c
                        where c.device_id = #{scene.id} and alarm_type='状态报警'
                    </if>
                    and c.alarm_start_time &gt;= date_format(#{startDate,jdbcType=VARCHAR},'%Y-%m-%d')
                    and c.alarm_start_time &lt; date_format(#{endDate,jdbcType=VARCHAR},'%Y-%m-%d')
                ) t1
                group by label
            ) t2
        order by amount desc
    </select>


    <select id="selectAlarmDeviceIdBySenceId" resultType="map">
        select DISTINCT c.device_id from sms_scene a INNER JOIN sms_scene_type b on a.SCENE_TYPE_ID=b.ID
        INNER JOIN sms_alarm_details c on c.PARENT_SCENE_ID=a.ID
        where a.ID = #{id}
    </select>
</mapper>