<?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.AppReportMonitorMapper">

    <select id="getSceneByCompany" resultType="com.zcxk.charge.api.vo.SceneEntityVo">
        select
            s.id,
            s.SCENE_NAME as "sceneName",
            s.PARENT_SCENE_ID as "parentSceneId"
        from sms_scene_type st
        left join sms_scene s on st.id=s.SCENE_TYPE_ID
        where st.STATUS = 1 and s.STATUS = 1
        <if test="sceneType != null and sceneType !=''">
          and st.SCENE_TYPE_NAME =#{sceneType}
        </if>
        and  s.COMPANY_ORG_ID = #{companyOrgId} and s.PARENT_SCENE_ID=0
        and s.TENANT_ID = #{tenantId}
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="permissonType == 4 or permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>
        order by s.SCENE_NAME
    </select>

    <select id="getDeviceByCompany" resultType="com.zcxk.charge.api.vo.DeviceEntityVo">
        select
            d.id
            ,d.DEVICE_CODE as "deviceCode"
            ,d.DEVICE_NAME as "deviceName"
        from sms_scene_type st
        left join sms_scene s on st.id=s.SCENE_TYPE_ID
        LEFT JOIN sms_scene sc on substring_index(substring_index(sc.parent_scene_ids, ',', 2), ',', -1)=s.id
        left join sms_scene_type sct on sct.id=sc.SCENE_TYPE_ID
        left join sms_device_scene ds on ds.SCENE_ID =sc.id
        left join sms_device d on d.id=ds.DEVICE_ID
        where st.STATUS = 1 and s.STATUS = 1 and ds.`STATUS` =1 and d.`STATUS` =1 and d.ENABLE_STATE =1
        and st.SCENE_TYPE_NAME = '管网'
        <if test="typeName != null and typeName !=''">
            and sct.SCENE_TYPE_NAME =#{typeName}
        </if>
        and  s.COMPANY_ORG_ID = #{companyOrgId}
        and s.TENANT_ID = #{tenantId}
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="permissonType == 4 or permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>
        order by s.SCENE_NAME
    </select>
    <select id="getCompanyBySceneCondition" resultType="java.lang.Integer">
        select
        distinct
         s.COMPANY_ORG_ID
        from sms_scene_type st
        left join sms_scene s on st.id=s.SCENE_TYPE_ID
        where st.STATUS = 1 and s.STATUS = 1
        <if test="sceneType != null and sceneType !=''">
          and st.SCENE_TYPE_NAME =#{sceneType}
        </if>
        <if test="condition != null and condition !=''">
            and s.SCENE_NAME like concat('%',#{condition},'%')
        </if>
        and s.TENANT_ID = #{tenantId}
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="permissonType == 4 or permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>

    <select id="getCompanyByDeviceCondition" resultType="java.lang.Integer">
        select
        distinct
        d.COMPANY_ORG_ID
        from sms_scene_type st
        left join sms_scene s on st.id=s.SCENE_TYPE_ID
        LEFT JOIN sms_scene sc on substring_index(substring_index(sc.parent_scene_ids, ',', 2), ',', -1)=s.id
        left join sms_device_scene ds on ds.SCENE_ID =sc.id
        left join sms_device d on d.id=ds.DEVICE_ID
        where st.STATUS = 1 and s.STATUS = 1 and ds.`STATUS` =1 and d.`STATUS` =1 and d.ENABLE_STATE =1
        and st.SCENE_TYPE_NAME ='管网'
        <if test="condition != null and condition !=''">
            and d.DEVICE_NAME like concat('%',#{condition},'%')
        </if>
        and s.TENANT_ID = #{tenantId}
        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">
            <if test="permissonType == 5 or permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="permissonType == 4 or permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>

    </select>

</mapper>