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

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisStop" id="SRaRealTimeAnalysisStopResult">
        <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="runEtc"    column="run_etc"    />
        <result property="goingsOn"    column="goings_on"    />
        <result property="workingProcedure"    column="working_procedure"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="analyseTime"    column="analyse_time"    />
    </resultMap>


    <sql id="selectSRaRealTimeAnalysisStopVo">
        select data_id, factory_id, factory_name, industry_category_name, industry_category_code, district_name, district_code, performance_level, device_name, device_id, run_etc, goings_on, working_procedure, update_time, create_time, analyse_time from s_ra_real_time_analysis_stop
    </sql>

    <select id="selectSRaRealTimeAnalysisStopList" parameterType="cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisStop" resultMap="SRaRealTimeAnalysisStopResult">
        SELECT
        sr.factory_name,
        pf.country_name as district_name,
        bd.work_step AS working_procedure,
        sr.device_name,
        sr.industry_category_name,
        pf.pre_con_level performance_level,
        max(sr.update_time) update_time,
        sr.judgement_basis,
        sr.device_id
        FROM
        s_ra_real_time_analysis_run AS sr
        inner JOIN bas_device AS bd ON sr.device_id = bd.device_id
        LEFT JOIN pc_factory as pf on pf.id=sr.factory_id
        <where>
            sr.judgement_basis='1'
            <if test="sRaRealTimeAnalysisStop.factoryName != null  and sRaRealTimeAnalysisStop.factoryName != ''"> and sr.factory_name like concat('%', #{sRaRealTimeAnalysisStop.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisStop.industryCategoryCode != null  and sRaRealTimeAnalysisStop.industryCategoryCode != ''"> and sr.industry_category_code = #{sRaRealTimeAnalysisStop.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisStop.districtCode != null  and sRaRealTimeAnalysisStop.districtCode != ''"> and pf.country_code = #{sRaRealTimeAnalysisStop.districtCode}</if>
            <if test="sRaRealTimeAnalysisStop.deviceName != null  and sRaRealTimeAnalysisStop.deviceName != ''"> and sr.device_name like concat('%', #{sRaRealTimeAnalysisStop.deviceName}, '%')</if>
            <if test="sRaRealTimeAnalysisStop.deviceId != null  and sRaRealTimeAnalysisStop.deviceId != ''"> and sr.device_id = #{sRaRealTimeAnalysisStop.deviceId}</if>
            <if test="sRaRealTimeAnalysisStop.workingProcedure != null  and sRaRealTimeAnalysisStop.workingProcedure != ''"> and bd.type = #{sRaRealTimeAnalysisStop.workingProcedure}</if>
            <if test="sRaRealTimeAnalysisStop.analyseStartTime != null  and sRaRealTimeAnalysisStop.analyseStartTime != ''"> and sr.update_time >=CAST(#{sRaRealTimeAnalysisStop.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisStop.analyseEndTime != null  and sRaRealTimeAnalysisStop.analyseEndTime != ''"> and sr.update_time &lt;=CAST(#{sRaRealTimeAnalysisStop.analyseEndTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisStop.regionCodes != null and sRaRealTimeAnalysisStop.regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="sRaRealTimeAnalysisStop.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        GROUP BY
        sr.device_id,
        sr.factory_name,
        pf.country_name,
        bd.work_step,
        sr.industry_category_name,
        sr.device_name,
        sr.judgement_basis,
        pf.pre_con_level
        ORDER BY sr.factory_name,sr.device_id desc
    </select>

    <select id="pullWorkingProcedureOptions" resultType="java.util.Map">

        SELECT work_step AS label,work_step AS value
        FROM "bas_device"
        <where>
            work_step !=''
            <if test="industryCategoryCode != null  and industryCategoryCode != ''"> and industry = #{industryCategoryCode}</if>
        </where>
        GROUP BY work_step
    </select>

    <select id="selectSRaRealTimeAnalysisStopListPage" resultMap="SRaRealTimeAnalysisStopResult">
        SELECT
        sr.factory_name,
        pf.country_name as district_name,
        bd.work_step AS working_procedure,
        sr.device_name,
        sr.industry_category_name,
        pf.pre_con_level performance_level,
        max(sr.update_time) update_time,
        sr.judgement_basis,
        sr.device_id
        FROM
        s_ra_real_time_analysis_run AS sr
        inner JOIN bas_device AS bd ON sr.device_id = bd.device_id
        LEFT JOIN pc_factory as pf on pf.id=sr.factory_id
        <where>
            sr.judgement_basis='1'
            <if test="sRaRealTimeAnalysisStop.factoryName != null  and sRaRealTimeAnalysisStop.factoryName != ''"> and sr.factory_name like concat('%', #{sRaRealTimeAnalysisStop.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisStop.industryCategoryCode != null  and sRaRealTimeAnalysisStop.industryCategoryCode != ''"> and sr.industry_category_code = #{sRaRealTimeAnalysisStop.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisStop.districtCode != null  and sRaRealTimeAnalysisStop.districtCode != ''"> and pf.country_code = #{sRaRealTimeAnalysisStop.districtCode}</if>
            <if test="sRaRealTimeAnalysisStop.deviceName != null  and sRaRealTimeAnalysisStop.deviceName != ''"> and sr.device_name like concat('%', #{sRaRealTimeAnalysisStop.deviceName}, '%')</if>
            <if test="sRaRealTimeAnalysisStop.deviceId != null  and sRaRealTimeAnalysisStop.deviceId != ''"> and sr.device_id = #{sRaRealTimeAnalysisStop.deviceId}</if>
            <if test="sRaRealTimeAnalysisStop.workingProcedure != null  and sRaRealTimeAnalysisStop.workingProcedure != ''"> and bd.type = #{sRaRealTimeAnalysisStop.workingProcedure}</if>
            <if test="sRaRealTimeAnalysisStop.analyseStartTime != null  and sRaRealTimeAnalysisStop.analyseStartTime != ''"> and sr.update_time >=CAST(#{sRaRealTimeAnalysisStop.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisStop.analyseEndTime != null  and sRaRealTimeAnalysisStop.analyseEndTime != ''"> and sr.update_time &lt;=CAST(#{sRaRealTimeAnalysisStop.analyseEndTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisStop.regionCodes != null and sRaRealTimeAnalysisStop.regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="sRaRealTimeAnalysisStop.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        GROUP BY
        sr.device_id,
        sr.factory_name,
        pf.country_name,
        bd.work_step,
        sr.industry_category_name,
        sr.device_name,
        sr.judgement_basis,
        pf.pre_con_level
        ORDER BY sr.factory_name,sr.device_id desc
    </select>

    <select id="selectSRaRealTimeAnalysisStopListById" resultMap="SRaRealTimeAnalysisStopResult">
        SELECT
        sr.update_time,
        sr.goings_on,
        sr.operation_status,
        sr.device_id
        FROM
        s_ra_real_time_analysis_run AS sr
        inner JOIN bas_device AS bd ON sr.device_id = bd.device_id
        <where>
            sr.judgement_basis='1'
<!--            <if test="sRaRealTimeAnalysisStop.factoryName != null  and sRaRealTimeAnalysisStop.factoryName != ''"> and sr.factory_name like concat('%', #{sRaRealTimeAnalysisStop.factoryName}, '%')</if>-->
<!--            <if test="sRaRealTimeAnalysisStop.industryCategoryCode != null  and sRaRealTimeAnalysisStop.industryCategoryCode != ''"> and sr.industry_category_code = #{sRaRealTimeAnalysisStop.industryCategoryCode}</if>-->
<!--            <if test="sRaRealTimeAnalysisStop.districtCode != null  and sRaRealTimeAnalysisStop.districtCode != ''"> and sr.district_code = #{sRaRealTimeAnalysisStop.districtCode}</if>-->
<!--            <if test="sRaRealTimeAnalysisStop.deviceName != null  and sRaRealTimeAnalysisStop.deviceName != ''"> and sr.device_name like concat('%', #{sRaRealTimeAnalysisStop.deviceName}, '%')</if>-->
<!--            <if test="sRaRealTimeAnalysisStop.deviceId != null  and sRaRealTimeAnalysisStop.deviceId != ''"> and sr.device_id = #{sRaRealTimeAnalysisStop.deviceId}</if>-->
<!--            <if test="sRaRealTimeAnalysisStop.workingProcedure != null  and sRaRealTimeAnalysisStop.workingProcedure != ''"> and bd.work_step = #{sRaRealTimeAnalysisStop.workingProcedure}</if>-->
            <if test="sRaRealTimeAnalysisStop.analyseStartTime != null  and sRaRealTimeAnalysisStop.analyseStartTime != ''"> and sr.update_time >=CAST(#{sRaRealTimeAnalysisStop.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisStop.analyseEndTime != null  and sRaRealTimeAnalysisStop.analyseEndTime != ''"> and sr.update_time &lt;=CAST(#{sRaRealTimeAnalysisStop.analyseEndTime} AS timestamp) </if>
            <foreach collection="list"  item="chile" close=" )" open="  and sr.device_id in (" separator=",">
                #{chile}
            </foreach>
        </where>
        ORDER BY sr.device_id,sr.update_time asc
    </select>


    <select id="selectDeviceByStatusAndWorkStep" resultType="cn.cecep.talroad.vo.analysis.DeviceStatusWorkStepVO">
        --查询设备工序分类查询状态集合
        select
        bd.work_step,
        t1.device_id
        ,string_agg(operation_status,',') as device_statuses
        from
        (select
        sr.device_id,
        sr.operation_status
        FROM
        s_ra_real_time_analysis_run AS sr
        left join pc_factory pf on sr.factory_id = pf.id
        <where>
            sr.judgement_basis='1'
            <if test="sRaRealTimeAnalysisStop.factoryName != null  and sRaRealTimeAnalysisStop.factoryName != ''"> and sr.factory_name like concat('%', #{sRaRealTimeAnalysisStop.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisStop.industryCategoryCode != null  and sRaRealTimeAnalysisStop.industryCategoryCode != ''"> and sr.industry_category_code = #{sRaRealTimeAnalysisStop.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisStop.districtCode != null  and sRaRealTimeAnalysisStop.districtCode != ''"> and pf.country_code = #{sRaRealTimeAnalysisStop.districtCode}</if>
            <if test="sRaRealTimeAnalysisStop.deviceName != null  and sRaRealTimeAnalysisStop.deviceName != ''"> and sr.device_name like concat('%', #{sRaRealTimeAnalysisStop.deviceName}, '%')</if>
            <if test="sRaRealTimeAnalysisStop.deviceId != null  and sRaRealTimeAnalysisStop.deviceId != ''"> and sr.device_id = #{sRaRealTimeAnalysisStop.deviceId}</if>
            <if test="sRaRealTimeAnalysisStop.workingProcedure != null  and sRaRealTimeAnalysisStop.workingProcedure != ''"> and bd.type = #{sRaRealTimeAnalysisStop.workingProcedure}</if>
            <if test="sRaRealTimeAnalysisStop.analyseStartTime != null  and sRaRealTimeAnalysisStop.analyseStartTime != ''"> and sr.update_time >=CAST(#{sRaRealTimeAnalysisStop.analyseStartTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisStop.analyseEndTime != null  and sRaRealTimeAnalysisStop.analyseEndTime != ''"> and sr.update_time &lt;=CAST(#{sRaRealTimeAnalysisStop.analyseEndTime} AS timestamp) </if>
            <if test="sRaRealTimeAnalysisStop.regionCodes != null and sRaRealTimeAnalysisStop.regionCodes.size() != 0">
                and pf.country_code IN
                <foreach collection="sRaRealTimeAnalysisStop.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        group by device_id ,operation_status
        order by device_id ,operation_status ) t1
        inner JOIN bas_device AS bd ON t1.device_id = bd.device_id
        group by t1.device_id,bd.work_step
    </select>
</mapper>
