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

    <resultMap type="cn.cecep.talroad.domain.SRaRealTimeAnalysisRunOne" id="SRaRealTimeAnalysisRunOneResult">
        <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"    />
        <result property="activePowerQuantityTmp"    column="active_power_quantity_tmp"    />
        <result property="activePowerQuantity"    column="active_power_quantity"    />
    </resultMap>


    <sql id="selectSRaRealTimeAnalysisRunOneVo">
        select data_id, factory_id, factory_name, pc.industry_permission_name ,pc.industry_permission_name as industry_category_name, industry_category_code, pc.country_name as district_name, pc.country_code as district_code, pc.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_one
    </sql>

    <select id="selectSRaRealTimeAnalysisRunOneList"  resultMap="SRaRealTimeAnalysisRunOneResult">
        <include refid="selectSRaRealTimeAnalysisRunOneVo"/> as ro
        LEFT JOIN (SELECT industry_permission_name,id,pre_con_level as performance_level,country_code,country_name from pc_factory ) as pc on ro.factory_id=pc.id
        <where>
            <if test="sRaRealTimeAnalysisRunOne.factoryName != null  and sRaRealTimeAnalysisRunOne.factoryName != ''"> and factory_name like concat('%', #{sRaRealTimeAnalysisRunOne.factoryName}, '%')</if>
            <if test="sRaRealTimeAnalysisRunOne.industryCategoryCode != null  and sRaRealTimeAnalysisRunOne.industryCategoryCode != ''"> and pc.industry_permission_name = #{sRaRealTimeAnalysisRunOne.industryCategoryCode}</if>
            <if test="sRaRealTimeAnalysisRunOne.districtCode != null  and sRaRealTimeAnalysisRunOne.districtCode != ''"> and pc.country_code = #{sRaRealTimeAnalysisRunOne.districtCode}</if>
            <if test="sRaRealTimeAnalysisRunOne.deviceId != null  and sRaRealTimeAnalysisRunOne.deviceId != ''"> and device_id = #{sRaRealTimeAnalysisRunOne.deviceId}</if>
            <if test="sRaRealTimeAnalysisRunOne.deviceType != null  and sRaRealTimeAnalysisRunOne.deviceType != ''"> and device_type = #{sRaRealTimeAnalysisRunOne.deviceType}</if>
            <if test="sRaRealTimeAnalysisRunOne.operationStatus != null  and sRaRealTimeAnalysisRunOne.operationStatus != ''"> and operation_status = #{sRaRealTimeAnalysisRunOne.operationStatus}</if>
            <if test="sRaRealTimeAnalysisRunOne.regionCodes != null and sRaRealTimeAnalysisRunOne.regionCodes.size() != 0">
                and pc.country_code IN
                <foreach collection="sRaRealTimeAnalysisRunOne.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        order by device_id asc
    </select>


    <update id="updateBatchSelective" parameterType="java.util.List">
        <!--@mbg.generated-->
        update s_ra_real_time_analysis_run_one
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="factory_id = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.factoryId != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.factoryId,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="factory_name = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.factoryName != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.factoryName,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="industry_category_name = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.industryCategoryName != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.industryCategoryName,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="industry_category_code = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.industryCategoryCode != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.industryCategoryCode,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="district_name = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.districtName != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.districtName,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="district_code = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.districtCode != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.districtCode,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="performance_level = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.performanceLevel != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.performanceLevel,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="device_name = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.deviceName != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.deviceName,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="device_id = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.deviceId != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.deviceId,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="device_type = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.deviceType != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.deviceType,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="operation_status = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.operationStatus != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.operationStatus,jdbcType=CHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="judgement_basis = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.judgementBasis != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.judgementBasis,jdbcType=CHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.updateTime != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then CAST( #{item.updateTime} AS timestamp)
                    </if>
                </foreach>
            </trim>
            <trim prefix="create_time = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.createTime != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then CAST(#{item.createTime} AS timestamp)
                    </if>
                </foreach>
            </trim>
            <trim prefix="run_etc = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.runEtc != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.runEtc,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="goings_on = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.goingsOn != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.goingsOn,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="active_power_quantity_tmp = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.activePowerQuantityTmp != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.activePowerQuantityTmp,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="active_power_quantity = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.activePowerQuantity != null">
                        when device_id = #{item.deviceId,jdbcType=VARCHAR} then #{item.activePowerQuantity,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
        </trim>
        where device_id in
        <foreach close=")" collection="list" item="item" open="(" separator=", ">
            #{item.deviceId,jdbcType=VARCHAR}
        </foreach>
    </update>
    <insert id="batchInsert"  parameterType="cn.cecep.talroad.domain.SRaRealTimeAnalysisRunOne">
        <!--@mbg.generated-->
        insert into s_ra_real_time_analysis_run_one
        (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)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.dataId,jdbcType=VARCHAR}, #{item.factoryId,jdbcType=VARCHAR}, #{item.factoryName,jdbcType=VARCHAR},
            #{item.industryCategoryName,jdbcType=VARCHAR}, #{item.industryCategoryCode,jdbcType=VARCHAR},
            #{item.districtName,jdbcType=VARCHAR}, #{item.districtCode,jdbcType=VARCHAR}, #{item.performanceLevel,jdbcType=VARCHAR},
            #{item.deviceName,jdbcType=VARCHAR}, #{item.deviceId,jdbcType=VARCHAR}, #{item.deviceType,jdbcType=VARCHAR},
            #{item.operationStatus,jdbcType=CHAR}, #{item.judgementBasis,jdbcType=CHAR}, #{item.updateTime},
            #{item.createTime}, #{item.runEtc,jdbcType=VARCHAR}, #{item.goingsOn,jdbcType=VARCHAR},
            #{item.activePowerQuantityTmp,jdbcType=VARCHAR}, #{item.activePowerQuantity,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <select id="selectAllDeviceId" resultType="java.lang.String">
        SELECT device_id FROM s_ra_real_time_analysis_run_one WHERE device_type = #{deviceType}
    </select>
    <select id="selectAnalysisRunByParams" resultMap="SRaRealTimeAnalysisRunOneResult">
        select * from s_ra_real_time_analysis_run_one where factory_id = #{factoryId} and device_id = #{deviceId} limit 1
    </select>
</mapper>
