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

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisRun" id="SRaRealTimeAnalysisRunResult">
        <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="deviceType"    column="device_type"    />
        <result property="operationStatus"    column="operation_status"    />
        <result property="judgementBasis"    column="judgement_basis"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="runEtc"    column="run_etc" />
        <result property="goingsOn"  column="goings_on" />
    </resultMap>


    <sql id="selectSRaRealTimeAnalysisRunVo">
        select data_id, factory_id, factory_name, industry_category_name, industry_category_code, district_name, district_code, performance_level, device_name, device_id, device_type, operation_status, judgement_basis, update_time, create_time,run_etc,goings_on,active_power_quantity_tmp,active_power_quantity from s_ra_real_time_analysis_run
    </sql>

    <select id="selectSRaRealTimeAnalysisRunList" resultMap="SRaRealTimeAnalysisRunResult">
        <include refid="selectSRaRealTimeAnalysisRunVo"/>
        <where>
            <if test="factoryName != null  and factoryName != ''"> and factory_name like concat('%', #{factoryName}, '%')</if>
            <if test="industryCategoryCode != null  and industryCategoryCode != ''"> and industry_category_code = #{industryCategoryCode}</if>
            <if test="districtCode != null  and districtCode != ''"> and district_code = #{districtCode}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="operationStatus != null  and operationStatus != ''"> and operation_status = #{operationStatus}</if>
        </where>
        LIMIT  #{pageNum} offset #{pageSize}
    </select>

    <select id="pullDeviceNameOptions" resultType="java.util.Map">
        select
        device_name AS label,
        device_id AS value
        from
        s_ra_real_time_analysis_run_one
        <where>
            <if test="factoryName != null  and factoryName != ''"> and factory_name like concat('%', #{factoryName}, '%')</if>
        </where>
        group by device_id, device_name
    </select>

    <select id="selectListByDate" resultMap="SRaRealTimeAnalysisRunResult">
        <include refid="selectSRaRealTimeAnalysisRunVo"/>
        <where>
            AND judgement_basis = '2'
            <if test="startTime != null">
                AND update_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                AND update_time &lt;= #{endTime}
            </if>
            <if test="updateTime != null ">
                AND update_time = #{updateTime}
            </if>
        </where>
    </select>

    <select id="selectSRaRealTimeAnalysisRunListByDeviceIdAndTime" resultMap="SRaRealTimeAnalysisRunResult">
        <include refid="selectSRaRealTimeAnalysisRunVo"/>
        <where>
            AND judgement_basis = '2'
            <if test="deviceId != null and deviceId.size() != 0">
                AND device_id IN
                <foreach collection="deviceId" index="index" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test=" dateTime!= null">
                AND update_time = #{dateTime}
            </if>
        </where>
    </select>

    <insert id="insertSRaRealTimeAnalysisRun" parameterType="cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisRun">
        <!--@mbg.generated-->
        insert into s_ra_real_time_analysis_run
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dataId != null and dataId != ''">
                data_id,
            </if>
            <if test="factoryId != null and factoryId != ''">
                factory_id,
            </if>
            <if test="factoryName != null and factoryName != ''">
                factory_name,
            </if>
            <if test="industryCategoryName != null and industryCategoryName != ''">
                industry_category_name,
            </if>
            <if test="industryCategoryCode != null and industryCategoryCode != ''">
                industry_category_code,
            </if>
            <if test="districtName != null and districtName != ''">
                district_name,
            </if>
            <if test="districtCode != null and districtCode != ''">
                district_code,
            </if>
            <if test="performanceLevel != null and performanceLevel != ''">
                performance_level,
            </if>
            <if test="deviceName != null and deviceName != ''">
                device_name,
            </if>
            <if test="deviceId != null and deviceId != ''">
                device_id,
            </if>
            <if test="deviceType != null and deviceType != ''">
                device_type,
            </if>
            <if test="operationStatus != null and operationStatus != ''">
                operation_status,
            </if>
            <if test="judgementBasis != null and judgementBasis != ''">
                judgement_basis,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="runEtc != null and runEtc != ''">
                run_etc,
            </if>
            <if test="goingsOn != null and goingsOn != ''">
                goings_on,
            </if>
            <if test="activePowerQuantityTmp != null and activePowerQuantityTmp != ''">
                active_power_quantity_tmp,
            </if>
            <if test="activePowerQuantity != null and activePowerQuantity != ''">
                active_power_quantity,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dataId != null and dataId != ''">
                #{dataId,jdbcType=VARCHAR},
            </if>
            <if test="factoryId != null and factoryId != ''">
                #{factoryId,jdbcType=VARCHAR},
            </if>
            <if test="factoryName != null and factoryName != ''">
                #{factoryName,jdbcType=VARCHAR},
            </if>
            <if test="industryCategoryName != null and industryCategoryName != ''">
                #{industryCategoryName,jdbcType=VARCHAR},
            </if>
            <if test="industryCategoryCode != null and industryCategoryCode != ''">
                #{industryCategoryCode,jdbcType=VARCHAR},
            </if>
            <if test="districtName != null and districtName != ''">
                #{districtName,jdbcType=VARCHAR},
            </if>
            <if test="districtCode != null and districtCode != ''">
                #{districtCode,jdbcType=VARCHAR},
            </if>
            <if test="performanceLevel != null and performanceLevel != ''">
                #{performanceLevel,jdbcType=VARCHAR},
            </if>
            <if test="deviceName != null and deviceName != ''">
                #{deviceName,jdbcType=VARCHAR},
            </if>
            <if test="deviceId != null and deviceId != ''">
                #{deviceId,jdbcType=VARCHAR},
            </if>
            <if test="deviceType != null and deviceType != ''">
                #{deviceType,jdbcType=VARCHAR},
            </if>
            <if test="operationStatus != null and operationStatus != ''">
                #{operationStatus,jdbcType=CHAR},
            </if>
            <if test="judgementBasis != null and judgementBasis != ''">
                #{judgementBasis,jdbcType=CHAR},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="runEtc != null and runEtc != ''">
                #{runEtc,jdbcType=VARCHAR},
            </if>
            <if test="goingsOn != null and goingsOn != ''">
                #{goingsOn,jdbcType=VARCHAR},
            </if>
            <if test="activePowerQuantityTmp != null and activePowerQuantityTmp != ''">
                #{activePowerQuantityTmp,jdbcType=VARCHAR},
            </if>
            <if test="activePowerQuantity != null and activePowerQuantity != ''">
                #{activePowerQuantity,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <insert id="insertSRaRealTimeAnalysisRuns" parameterType="cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisRun">
        INSERT INTO s_ra_real_time_analysis_run (data_id, factory_id, factory_name, industry_category_name, industry_category_code, district_name, district_code, performance_level, device_name, device_id, device_type, operation_status, judgement_basis, update_time,run_etc,goings_on,active_power_quantity_tmp,active_power_quantity)
        VALUES
        <foreach collection="collection" item="item" index="index" separator="," >
            (
                #{item.dataId},#{item.factoryId},#{item.factoryName},#{item.industryCategoryName},#{item.industryCategoryCode},#{item.districtName},#{item.districtCode},#{item.performanceLevel},#{item.deviceName},#{item.deviceId},#{item.deviceType},#{item.operationStatus},#{item.judgementBasis},#{item.updateTime},#{item.runEtc},#{item.goingsOn},#{item.activePowerQuantityTmp},#{item.activePowerQuantity}
            )
        </foreach>
    </insert>

    <select id="selectAnalysisRunByParams" resultMap="SRaRealTimeAnalysisRunResult">
        select * from s_ra_real_time_analysis_run
        <where>
            operation_status in ('1','2')
            <if test="factoryId != null and factoryId != ''">
                and factory_id = #{factoryId,jdbcType=VARCHAR}
            </if>
            <if test="deviceId != null and deviceId != ''">
                and device_id = #{deviceId,jdbcType=VARCHAR}
            </if>
            <if test="createTime != null ">
                and create_time > #{createTime}
            </if>
        </where>
        order by create_time desc limit 1
    </select>

    <select id="threadSelectListByDate" resultMap="SRaRealTimeAnalysisRunResult">
        select data_id, device_id,operation_status, update_time, factory_id,active_power_quantity_tmp,active_power_quantity from s_ra_real_time_analysis_run
        <where>
            AND judgement_basis = '2'
            <if test="startTime != null">
                AND update_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                AND update_time &lt; #{endTime}
            </if>
            <if test="updateTime != null ">
                AND update_time = #{updateTime}
            </if>
        </where>
    </select>

    <select id="analysisCurve" resultType="cn.cecep.talroad.domain.analysis.SRaRealTimeAnalysisRun">
        SELECT
            mr.device_site_id as deviceId,mr.active_power as activePowerQuantityTmp,mr.active_power_quantity activePowerQuantity,mr.data_time as updateTime,bds.name as deviceName
        FROM
            bas_device_site bds
                INNER JOIN mon_elec_real AS mr ON mr.device_site_id = bds.ID
        <where>
            <if test="monitoringStartTime != null">
                AND mr.data_time &gt;= #{monitoringStartTime}
            </if>
            <if test="monitoringEndTime != null">
                AND mr.data_time &lt;= #{monitoringEndTime}
            </if>
            <if test="deviceId != null and deviceId !='' ">
                AND bds.device_id = #{deviceId}
            </if>
        </where>
    </select>

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

