<?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="cn.cecep.talroad.mapper.analysis.SRaRealTimeAnalysisCoMapper">

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisCo" id="SRaRealTimeAnalysisCoResult">
        <result property="dataId"    column="data_id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="factoryName"    column="factory_name"    />
        <result property="industryCategoryName"    column="industry_category_name"    />
        <result property="industryCategoryCode"    column="industry_category_code"    />
        <result property="districtName"    column="district_name"    />
        <result property="districtCode"    column="district_code"    />
        <result property="performanceLevel"    column="performance_level"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceId"    column="device_id"    />
        <result property="diffuse"    column="diffuse"    />
        <result property="remark"    column="remark"    />
        <result property="workingProcedure"    column="working_procedure"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="analyseTime"    column="analyse_time"    />
        <result property="coalEmissions"    column="coal_emissions"    />
    </resultMap>


    <sql id="selectSRaRealTimeAnalysisCoVo">
        select data_id, factory_id, factory_name, industry_category_name, industry_category_code, district_name, district_code, performance_level, device_name, device_id, diffuse, remark, working_procedure, update_time, create_time, analyse_time,coal_emissions from s_ra_real_time_analysis_co
    </sql>

    <select id="selectSRaRealTimeAnalysisCoList"  resultMap="SRaRealTimeAnalysisCoResult">
        SELECT
        factory_name,
        industry_category_name,
        pf.country_name as district_name,
        pf.pre_con_level as performance_level,
        bd.work_step working_procedure,
        device_name ,
        co.device_id
        FROM
        "s_ra_real_time_analysis_co" as co LEFT JOIN (SELECT id,pre_con_level,country_code,country_name from  pc_factory ) as pf on pf.id=co.factory_id
        LEFT JOIN bas_device as bd on bd.device_id=co.device_id
        <where>
            <if test="sRaRealTimeAnalysisCo.factoryName != null  and sRaRealTimeAnalysisCo.factoryName != ''"> and co.factory_name like concat('%', #{sRaRealTimeAnalysisCo.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.industryCategoryCode != null  and sRaRealTimeAnalysisCo.industryCategoryCode != ''"> and co.industry_category_code = #{sRaRealTimeAnalysisCo.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisCo.districtCode != null  and sRaRealTimeAnalysisCo.districtCode != ''"> and pf.country_code = #{sRaRealTimeAnalysisCo.districtCode}</if>
            <if test="sRaRealTimeAnalysisCo.deviceName != null  and sRaRealTimeAnalysisCo.deviceName != ''"> and co.device_name like concat('%', #{sRaRealTimeAnalysisCo.deviceName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.deviceId != null  and sRaRealTimeAnalysisCo.deviceId != ''"> and co.device_id = #{sRaRealTimeAnalysisCo.deviceId}</if>
            <if test="sRaRealTimeAnalysisCo.workingProcedure != null  and sRaRealTimeAnalysisCo.workingProcedure != ''">
                and bd.type=#{sRaRealTimeAnalysisCo.workingProcedure}
            </if>
            <if test="sRaRealTimeAnalysisCo.analyseStartTime != null  and sRaRealTimeAnalysisCo.analyseStartTime != ''"> and co.analyse_time >=CAST(#{sRaRealTimeAnalysisCo.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.analyseEndTime != null  and sRaRealTimeAnalysisCo.analyseEndTime != ''"> and co.analyse_time &lt;=CAST(#{sRaRealTimeAnalysisCo.analyseEndTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.regionCodes != null and sRaRealTimeAnalysisCo.regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="sRaRealTimeAnalysisCo.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        GROUP BY
        co.factory_name,
        co.industry_category_name,
        pf.country_name,
        pf.pre_con_level,
        bd.work_step,
        co.device_name,
        co.device_id
        order by co.factory_name,co.device_id desc
    </select>

    <select id="pullWorkingProcedureOptions" resultType="java.util.Map">
        select
        working_procedure AS label,
        working_procedure AS value
        from
        s_ra_real_time_analysis_co
        <where>
            <if test="industryCategoryCode != null  and industryCategoryCode != ''"> and industry_category_code = #{industryCategoryCode}</if>
        </where>
        group by working_procedure
    </select>

    <select id="selectSRaRealTimeAnalysisCoListByDeviceIdAndTime"
            resultType="cn.cecep.talroad.domain.GasReleaseRealStatus">
        SELECT
        diffuse_status as status,
            analyse_time as time
        FROM
            s_ra_real_time_analysis_co
            <where>
                <if test="deviceId != null and deviceId != ''">
                    AND device_id = #{deviceId}
                </if>
                <if test="startTime != null">
                    AND analyse_time &gt;= #{startTime}
                </if>
                <if test="endTime != null">
                    AND analyse_time &lt;= #{endTime}
                </if>
            </where>
            ORDER BY analyse_time ASC
    </select>

    <select id="selectSRaRealTimeAnalysisCoListPage" resultMap="SRaRealTimeAnalysisCoResult">
        SELECT
        factory_name,
        industry_category_name,
        pf.country_name as district_name,
        pf.pre_con_level as performance_level,
        bd.work_step working_procedure,
        device_name ,
        co.device_id
        FROM
        "s_ra_real_time_analysis_co" as co LEFT JOIN (SELECT id,pre_con_level,country_code,country_name from  pc_factory ) as pf on pf.id=co.factory_id
        LEFT JOIN bas_device as bd on bd.device_id=co.device_id
        <where>
            <if test="sRaRealTimeAnalysisCo.factoryName != null  and sRaRealTimeAnalysisCo.factoryName != ''"> and co.factory_name like concat('%', #{sRaRealTimeAnalysisCo.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.industryCategoryCode != null  and sRaRealTimeAnalysisCo.industryCategoryCode != ''"> and co.industry_category_code = #{sRaRealTimeAnalysisCo.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisCo.districtCode != null  and sRaRealTimeAnalysisCo.districtCode != ''"> and pf.country_code = #{sRaRealTimeAnalysisCo.districtCode}</if>
            <if test="sRaRealTimeAnalysisCo.deviceName != null  and sRaRealTimeAnalysisCo.deviceName != ''"> and co.device_name like concat('%', #{sRaRealTimeAnalysisCo.deviceName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.deviceId != null  and sRaRealTimeAnalysisCo.deviceId != ''"> and co.device_id = #{sRaRealTimeAnalysisCo.deviceId}</if>
            <if test="sRaRealTimeAnalysisCo.workingProcedure != null  and sRaRealTimeAnalysisCo.workingProcedure != ''">
                and bd.type=#{sRaRealTimeAnalysisCo.workingProcedure}
            </if>
            <if test="sRaRealTimeAnalysisCo.analyseStartTime != null  and sRaRealTimeAnalysisCo.analyseStartTime != ''"> and co.analyse_time >=CAST(#{sRaRealTimeAnalysisCo.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.analyseEndTime != null  and sRaRealTimeAnalysisCo.analyseEndTime != ''"> and co.analyse_time &lt;=CAST(#{sRaRealTimeAnalysisCo.analyseEndTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.regionCodes != null and sRaRealTimeAnalysisCo.regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="sRaRealTimeAnalysisCo.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        GROUP BY
        co.factory_name,
        co.industry_category_name,
        pf.country_name,
        pf.pre_con_level,
        bd.work_step,
        co.device_name,
        co.device_id
        order by co.factory_name,co.device_id desc
    </select>

    <select id="analysisCurveEndTime" resultType="java.lang.String">
        select max(diffuse_end_time) analyse_time from s_ra_real_time_analysis_co
        <where>
            <if test="monitoringStartTime != null">
                AND diffuse_end_time &gt;= #{monitoringStartTime}
            </if>
            <if test="monitoringEndTime != null">
                AND diffuse_end_time &lt;= #{monitoringEndTime}
            </if>
            <if test="deviceId != null and deviceId !='' ">
                AND device_id = #{deviceId}
            </if>
        </where>
    </select>

    <select id="selectSRaRealTimeAnalysisCoListById" resultMap="SRaRealTimeAnalysisCoResult">
        SELECT
        device_id,remark,diffuse_start_time,diffuse_status,diffuse_end_time, coal_emissions
        FROM
        "s_ra_real_time_analysis_co"
        <where>
<!--            <if test="sRaRealTimeAnalysisCo.factoryName != null  and sRaRealTimeAnalysisCo.factoryName != ''"> and factory_name like concat('%', #{sRaRealTimeAnalysisCo.factoryName}, '%')</if>-->
<!--            <if test="sRaRealTimeAnalysisCo.industryCategoryCode != null  and sRaRealTimeAnalysisCo.industryCategoryCode != ''"> and industry_category_code = #{sRaRealTimeAnalysisCo.industryCategoryCode}</if>-->
<!--            <if test="sRaRealTimeAnalysisCo.districtCode != null  and sRaRealTimeAnalysisCo.districtCode != ''"> and district_code = #{sRaRealTimeAnalysisCo.districtCode}</if>-->
<!--            <if test="sRaRealTimeAnalysisCo.deviceName != null  and sRaRealTimeAnalysisCo.deviceName != ''"> and device_name like concat('%', #{sRaRealTimeAnalysisCo.deviceName}, '%')</if>-->
<!--            <if test="sRaRealTimeAnalysisCo.deviceId != null  and sRaRealTimeAnalysisCo.deviceId != ''"> and device_id = #{sRaRealTimeAnalysisCo.deviceId}</if>-->
<!--            <if test="sRaRealTimeAnalysisCo.workingProcedure != null  and sRaRealTimeAnalysisCo.workingProcedure != ''"> and working_procedure = #{sRaRealTimeAnalysisCo.workingProcedure}</if>-->
            <if test="sRaRealTimeAnalysisCo.analyseStartTime != null  and sRaRealTimeAnalysisCo.analyseStartTime != ''"> and analyse_time >=CAST(#{sRaRealTimeAnalysisCo.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.analyseEndTime != null  and sRaRealTimeAnalysisCo.analyseEndTime != ''"> and analyse_time &lt;=CAST(#{sRaRealTimeAnalysisCo.analyseEndTime} AS timestamp) </if>
            <foreach collection="list"  item="chile" close=" )" open="  and device_id in (" separator=",">
                #{chile}
            </foreach>
        </where>
        ORDER BY device_id,diffuse_end_time asc
    </select>

    <select id="selectDeviceTypeCount" resultType="cn.cecep.talroad.vo.analysis.CoDeviceTypeCountVO">
        select
        count(*) filter (where t2."type" = 'GLMQ') as GLMQCount,
        count(*) filter (where t2."type" = 'GLMQG') as GLMQGCount,
        count(*) filter (where t2."type" = 'ZL') as ZLCount,
        count(*) filter (where t2."type" = 'MQGW') as MQGWCount
        from bas_device t2
        inner join
        (select
        t1.device_id
        from s_ra_real_time_analysis_co t1 inner join pc_factory pf on t1.factory_id = pf.id
        <where>
            <if test="sRaRealTimeAnalysisCo.factoryName != null  and sRaRealTimeAnalysisCo.factoryName != ''"> and factory_name like concat('%', #{sRaRealTimeAnalysisCo.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.industryCategoryCode != null  and sRaRealTimeAnalysisCo.industryCategoryCode != ''"> and industry_category_code = #{sRaRealTimeAnalysisCo.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisCo.districtCode != null  and sRaRealTimeAnalysisCo.districtCode != ''"> and pf.country_code = #{sRaRealTimeAnalysisCo.districtCode}</if>
            <if test="sRaRealTimeAnalysisCo.deviceName != null  and sRaRealTimeAnalysisCo.deviceName != ''"> and device_name like concat('%', #{sRaRealTimeAnalysisCo.deviceName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.deviceId != null  and sRaRealTimeAnalysisCo.deviceId != ''"> and device_id = #{sRaRealTimeAnalysisCo.deviceId}</if>
            <if test="sRaRealTimeAnalysisCo.analyseStartTime != null  and sRaRealTimeAnalysisCo.analyseStartTime != ''"> and analyse_time >=CAST(#{sRaRealTimeAnalysisCo.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.analyseEndTime != null  and sRaRealTimeAnalysisCo.analyseEndTime != ''"> and analyse_time &lt;=CAST(#{sRaRealTimeAnalysisCo.analyseEndTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.regionCodes != null and sRaRealTimeAnalysisCo.regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="sRaRealTimeAnalysisCo.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        group by t1.device_id) t1 on t1.device_id = t2.device_id
        left join pc_factory pf on t2.factory_id = pf.id
        <where>
            <if test="sRaRealTimeAnalysisCo.workingProcedure != null  and sRaRealTimeAnalysisCo.workingProcedure != ''"> and t2.type = #{sRaRealTimeAnalysisCo.workingProcedure}</if>
        </where>
    </select>

    <select id="selectDeviceTypeDiffuseStatusCount" resultType="cn.cecep.talroad.vo.analysis.CoDeviceTypeDiffuseStatusVO">
        --查询时间段内设备各个状态数量
        select
        t2.TYPE as deviceType,
        t1.diffuse_status,
        t1.device_id
        from bas_device t2
        inner join
        (select
        t1.device_id,t1.diffuse_status
        from s_ra_real_time_analysis_co t1 inner join pc_factory pf on t1.factory_id = pf.id
        <where>
            <if test="sRaRealTimeAnalysisCo.factoryName != null  and sRaRealTimeAnalysisCo.factoryName != ''"> and factory_name like concat('%', #{sRaRealTimeAnalysisCo.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.industryCategoryCode != null  and sRaRealTimeAnalysisCo.industryCategoryCode != ''"> and industry_category_code = #{sRaRealTimeAnalysisCo.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisCo.districtCode != null  and sRaRealTimeAnalysisCo.districtCode != ''"> and pf.country_code = #{sRaRealTimeAnalysisCo.districtCode}</if>
            <if test="sRaRealTimeAnalysisCo.deviceName != null  and sRaRealTimeAnalysisCo.deviceName != ''"> and device_name like concat('%', #{sRaRealTimeAnalysisCo.deviceName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.deviceId != null  and sRaRealTimeAnalysisCo.deviceId != ''"> and device_id = #{sRaRealTimeAnalysisCo.deviceId}</if>
            <if test="sRaRealTimeAnalysisCo.analyseStartTime != null  and sRaRealTimeAnalysisCo.analyseStartTime != ''"> and analyse_time >=CAST(#{sRaRealTimeAnalysisCo.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.analyseEndTime != null  and sRaRealTimeAnalysisCo.analyseEndTime != ''"> and analyse_time &lt;=CAST(#{sRaRealTimeAnalysisCo.analyseEndTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.regionCodes != null and sRaRealTimeAnalysisCo.regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="sRaRealTimeAnalysisCo.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        group by t1.device_id,t1.diffuse_status order by t1.device_id,t1.diffuse_status) t1 on t1.device_id = t2.device_id
        left join pc_factory pf on t2.factory_id = pf.id
        <where>
            <if test="sRaRealTimeAnalysisCo.workingProcedure != null  and sRaRealTimeAnalysisCo.workingProcedure != ''"> and t2.type = #{sRaRealTimeAnalysisCo.workingProcedure}</if>
        </where>
        group by t2.TYPE,t1.diffuse_status,t1.device_id order by t2.type,t1.diffuse_status,t1.device_id
    </select>

    <select id="selectDeviceDiffuseStatusFactoryList" resultType="cn.cecep.talroad.vo.analysis.CoDeviceDiffuseStatusFactoryVO">
        --查询时间段内异常状态的企业列表
        select
        t2.TYPE as deviceType,
        t1.diffuse_status,
        t1.factory_name
        from bas_device t2
        inner join
        (select
        t1.device_id,t1.factory_name,t1.diffuse_status
        from s_ra_real_time_analysis_co t1 inner join pc_factory pf on t1.factory_id = pf.id
        <where>
            <if test="sRaRealTimeAnalysisCo.factoryName != null  and sRaRealTimeAnalysisCo.factoryName != ''"> and factory_name like concat('%', #{sRaRealTimeAnalysisCo.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.industryCategoryCode != null  and sRaRealTimeAnalysisCo.industryCategoryCode != ''"> and industry_category_code = #{sRaRealTimeAnalysisCo.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisCo.districtCode != null  and sRaRealTimeAnalysisCo.districtCode != ''"> and pf.country_code = #{sRaRealTimeAnalysisCo.districtCode}</if>
            <if test="sRaRealTimeAnalysisCo.deviceName != null  and sRaRealTimeAnalysisCo.deviceName != ''"> and device_name like concat('%', #{sRaRealTimeAnalysisCo.deviceName}, '%')</if>
            <if test="sRaRealTimeAnalysisCo.deviceId != null  and sRaRealTimeAnalysisCo.deviceId != ''"> and device_id = #{sRaRealTimeAnalysisCo.deviceId}</if>
            <if test="sRaRealTimeAnalysisCo.analyseStartTime != null  and sRaRealTimeAnalysisCo.analyseStartTime != ''"> and analyse_time >=CAST(#{sRaRealTimeAnalysisCo.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.analyseEndTime != null  and sRaRealTimeAnalysisCo.analyseEndTime != ''"> and analyse_time &lt;=CAST(#{sRaRealTimeAnalysisCo.analyseEndTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisCo.regionCodes != null and sRaRealTimeAnalysisCo.regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="sRaRealTimeAnalysisCo.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        group by t1.device_id,t1.factory_name,t1.diffuse_status order by t1.device_id,t1.factory_name,t1.diffuse_status) t1 on t1.device_id = t2.device_id
        <where>
            <if test="sRaRealTimeAnalysisCo.workingProcedure != null  and sRaRealTimeAnalysisCo.workingProcedure != ''"> and t2.type = #{sRaRealTimeAnalysisCo.workingProcedure}</if>
        </where>
        group by t2.TYPE,t1.diffuse_status,t1.factory_name order by t2.type,t1.diffuse_status,t1.factory_name
    </select>
</mapper>
