<?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.aucma.base.mapper.BaseDeviceParamMapper">

    <resultMap type="BaseDeviceParam" id="BaseDeviceParamResult">
        <result property="objId" column="obj_id"/>
        <result property="paramCode" column="param_code"/>
        <result property="paramName" column="param_name"/>
        <result property="paramNetwork" column="param_network"/>
        <result property="paramAddress" column="param_address"/>
        <result property="paramType" column="param_type"/>
        <result property="deviceCode" column="device_code"/>
        <result property="readFrequency" column="read_frequency"/>
        <result property="isFlag" column="is_flag"/>
        <result property="createdBy" column="created_by"/>
        <result property="createdTime" column="created_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
        <result property="deviceName" column="device_name"/>
        <result property="productLineName" column="PRODUCT_LINE_NAME"/>
        <result property="upperLimit" column="upper_limit"/>
        <result property="lowerLimit" column="lower_limit"/>
        <result property="alertLevel" column="alert_level"/>
        <result property="alertEnabled" column="alert_enabled"/>
    </resultMap>

    <sql id="selectBaseDeviceParamVo">
        select dp.obj_id,
               dp.param_code,
               dp.param_name,
               dp.param_network,
               dp.param_address,
               dp.param_type,
               dp.device_code,
               dp.read_frequency,
               dp.upper_limit,
               dp.lower_limit,
               dp.alert_level,
               dp.alert_enabled,
               dp.is_flag,
               dp.created_by,
               dp.created_time,
               dp.updated_by,
               dp.updated_time,
               dl.DEVICE_NAME,
               pl.PRODUCT_LINE_NAME
        from base_deviceparam dp
                 left join BASE_DEVICELEDGER dl on dl.DEVICE_CODE = dp.DEVICE_CODE
                 left join BASE_PRODUCTLINE pl on pl.PRODUCT_LINE_CODE = dl.PRODUCT_LINE_CODE
    </sql>

    <select id="selectBaseDeviceParamList" parameterType="BaseDeviceParam" resultMap="BaseDeviceParamResult">
        <include refid="selectBaseDeviceParamVo"/>
        <where>
            <if test="paramCode != null  and paramCode != ''">and dp.param_code = #{paramCode}</if>
            <if test="paramName != null  and paramName != ''">and dp.param_name like concat(concat('%', #{paramName}), '%')</if>
            <if test="paramNetwork != null  and paramNetwork != ''">and dp.param_network = #{paramNetwork}</if>
            <if test="paramAddress != null  and paramAddress != ''">and dp.param_address = #{paramAddress}</if>
            <if test="paramType != null  and paramType != ''">and dp.param_type = #{paramType}</if>
            <if test="deviceCode != null  and deviceCode != ''">and dp.device_code = #{deviceCode}</if>
            <if test="readFrequency != null ">and dp.read_frequency = #{readFrequency}</if>
            <if test="isFlag != null ">and dp.is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and dp.created_by = #{createdBy}</if>
            <if test="createdTime != null ">and dp.created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and dp.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and dp.updated_time = #{updatedTime}</if>
        </where>
    </select>

    <select id="countByParamCode" resultType="int">
        select count(1)
        from base_deviceparam
        where param_code = #{paramCode}
        <if test="excludeObjId != null">
            and obj_id &lt;&gt; #{excludeObjId}
        </if>
    </select>

    <select id="selectBaseDeviceParamByObjId" parameterType="Long" resultMap="BaseDeviceParamResult">
        <include refid="selectBaseDeviceParamVo"/>
        where dp.obj_id = #{objId}
    </select>
    <select id="selectBaseDeviceParamByParamCode" parameterType="String" resultMap="BaseDeviceParamResult">
        select dp.obj_id,
               dp.param_code,
               dp.param_name
        from base_deviceparam dp
        where dp.param_code = #{paramCode}
    </select>

    <insert id="insertBaseDeviceParam" parameterType="BaseDeviceParam">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_base_deviceparam.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into base_deviceparam
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="paramCode != null">param_code,</if>
            <if test="paramName != null">param_name,</if>
            <if test="paramNetwork != null">param_network,</if>
            <if test="paramAddress != null">param_address,</if>
            <if test="paramType != null">param_type,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="readFrequency != null">read_frequency,</if>
            <if test="upperLimit != null">upper_limit,</if>
            <if test="lowerLimit != null">lower_limit,</if>
            <if test="alertLevel != null">alert_level,</if>
            <if test="alertEnabled != null">alert_enabled,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedTime != null">updated_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="paramCode != null">#{paramCode},</if>
            <if test="paramName != null">#{paramName},</if>
            <if test="paramNetwork != null">#{paramNetwork},</if>
            <if test="paramAddress != null">#{paramAddress},</if>
            <if test="paramType != null">#{paramType},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="readFrequency != null">#{readFrequency},</if>
            <if test="upperLimit != null">#{upperLimit},</if>
            <if test="lowerLimit != null">#{lowerLimit},</if>
            <if test="alertLevel != null">#{alertLevel},</if>
            <if test="alertEnabled != null">#{alertEnabled},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateBaseDeviceParam" parameterType="BaseDeviceParam">
        update base_deviceparam
        <trim prefix="SET" suffixOverrides=",">
            <if test="paramCode != null">param_code = #{paramCode},</if>
            <if test="paramName != null">param_name = #{paramName},</if>
            <if test="paramNetwork != null">param_network = #{paramNetwork},</if>
            <if test="paramAddress != null">param_address = #{paramAddress},</if>
            <if test="paramType != null">param_type = #{paramType},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="readFrequency != null">read_frequency = #{readFrequency},</if>
            <if test="upperLimit != null">upper_limit = #{upperLimit},</if>
            <if test="lowerLimit != null">lower_limit = #{lowerLimit},</if>
            <if test="alertLevel != null">alert_level = #{alertLevel},</if>
            <if test="alertEnabled != null">alert_enabled = #{alertEnabled},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="createdBy != null">created_by = #{createdBy},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedBy != null">updated_by = #{updatedBy},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteBaseDeviceParamByObjId" parameterType="Long">
        delete
        from base_deviceparam
        where obj_id = #{objId}
    </delete>

    <delete id="deleteBaseDeviceParamByObjIds" parameterType="String">
        delete from base_deviceparam where obj_id in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>

    <select id="selectByParamCodes" resultMap="BaseDeviceParamResult">
        select obj_id, param_code, param_name, param_network, param_address,
               param_type, device_code, read_frequency, is_flag,
               created_by, created_time, updated_by, updated_time
        from base_deviceparam
        where param_code in
        <foreach item="paramCode" collection="list" open="(" separator="," close=")">
            #{paramCode}
        </foreach>
    </select>

    <insert id="batchInsertDeviceParam" parameterType="java.util.List">
        <foreach item="item" collection="list" open="BEGIN" close="END;" separator="">
            INSERT INTO base_deviceparam (obj_id, param_code, param_name, param_network, param_address,
                param_type, device_code, read_frequency, is_flag, created_by, created_time)
            VALUES (seq_base_deviceparam.NEXTVAL, 
                #{item.paramCode,jdbcType=VARCHAR}, 
                #{item.paramName,jdbcType=VARCHAR}, 
                #{item.paramNetwork,jdbcType=VARCHAR}, 
                #{item.paramAddress,jdbcType=VARCHAR},
                #{item.paramType,jdbcType=VARCHAR}, 
                #{item.deviceCode,jdbcType=VARCHAR}, 
                #{item.readFrequency,jdbcType=NUMERIC}, 
                #{item.isFlag,jdbcType=NUMERIC}, 
                #{item.createdBy,jdbcType=VARCHAR}, 
                #{item.createdTime,jdbcType=TIMESTAMP});
        </foreach>
    </insert>

    <update id="batchUpdateDeviceParam" parameterType="java.util.List">
        <foreach item="item" collection="list" open="BEGIN " close=" END;" separator=" ">
            UPDATE base_deviceparam
            SET param_name = #{item.paramName,jdbcType=VARCHAR},
                param_network = #{item.paramNetwork,jdbcType=VARCHAR},
                param_address = #{item.paramAddress,jdbcType=VARCHAR},
                param_type = #{item.paramType,jdbcType=VARCHAR},
                device_code = #{item.deviceCode,jdbcType=VARCHAR},
                read_frequency = #{item.readFrequency,jdbcType=NUMERIC},
                is_flag = #{item.isFlag,jdbcType=NUMERIC},
                updated_by = #{item.updatedBy,jdbcType=VARCHAR},
                updated_time = #{item.updatedTime,jdbcType=TIMESTAMP}
            WHERE param_code = #{item.paramCode,jdbcType=VARCHAR};
        </foreach>
    </update>
</mapper>