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

    <resultMap type="BaseDeviceParamVal" id="BaseDeviceParamValResult">
            <result property="recordId"    column="record_id"    />
            <result property="paramCode"    column="param_code"    />
            <result property="deviceCode"    column="device_code"    />
            <result property="deviceId"    column="device_id"    />
            <result property="paramName"    column="param_name"    />
            <result property="paramValue"    column="param_value"    />
            <result property="collectTime"    column="collect_time"    />
            <result property="recordTime"    column="record_time"    />
    </resultMap>

    <sql id="selectBaseDeviceParamValVo">
        select record_id, param_code, device_code, device_id, param_name, param_value,
               collect_time, record_time
            from base_device_param_val
    </sql>

    <select id="selectBaseDeviceParamValList" parameterType="BaseDeviceParamVal" resultMap="BaseDeviceParamValResult">
        <include refid="selectBaseDeviceParamValVo"/>
        <where>
                        <if test="paramCode != null  and paramCode != ''"> and param_code = #{paramCode}</if>
                        <if test="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>
                        <if test="deviceId != null "> and device_id = #{deviceId}</if>
                        <if test="paramName != null  and paramName != ''"> and param_name like concat(concat('%', #{paramName}), '%')</if>
                        <if test="paramValue != null  and paramValue != ''"> and param_value = #{paramValue}</if>
                        <if test="collectTime != null "> and collect_time = #{collectTime}</if>
                        <if test="recordTime != null "> and record_time = #{recordTime}</if>
        </where>
    </select>

    <select id="selectBaseDeviceParamValByRecordId" parameterType="Long" resultMap="BaseDeviceParamValResult">
            <include refid="selectBaseDeviceParamValVo"/>
            where record_id = #{recordId}
    </select>

    <insert id="insertBaseDeviceParamVal" parameterType="BaseDeviceParamVal">
            <selectKey keyProperty="recordId" resultType="long" order="BEFORE">
                SELECT seq_base_device_param_val.NEXTVAL as recordId FROM DUAL
            </selectKey>
        insert into base_device_param_val
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="recordId != null">record_id,</if>
                <if test="paramCode != null">param_code,</if>
                <if test="deviceCode != null">device_code,</if>
                <if test="deviceId != null">device_id,</if>
                <if test="paramName != null">param_name,</if>
                <if test="paramValue != null">param_value,</if>
                <if test="collectTime != null">collect_time,</if>
                <if test="recordTime != null">record_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="recordId != null">#{recordId},</if>
                <if test="paramCode != null">#{paramCode},</if>
                <if test="deviceCode != null">#{deviceCode},</if>
                <if test="deviceId != null">#{deviceId},</if>
                <if test="paramName != null">#{paramName},</if>
                <if test="paramValue != null">#{paramValue},</if>
                <if test="collectTime != null">#{collectTime},</if>
                <if test="recordTime != null">#{recordTime},</if>
        </trim>
    </insert>

    <update id="updateBaseDeviceParamVal" parameterType="BaseDeviceParamVal">
        update base_device_param_val
        <trim prefix="SET" suffixOverrides=",">
                    <if test="paramCode != null">param_code = #{paramCode},</if>
                    <if test="deviceCode != null">device_code = #{deviceCode},</if>
                    <if test="deviceId != null">device_id = #{deviceId},</if>
                    <if test="paramName != null">param_name = #{paramName},</if>
                    <if test="paramValue != null">param_value = #{paramValue},</if>
                    <if test="collectTime != null">collect_time = #{collectTime},</if>
                    <if test="recordTime != null">record_time = #{recordTime},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteBaseDeviceParamValByRecordId" parameterType="Long">
        delete from base_device_param_val where record_id = #{recordId}
    </delete>

    <delete id="deleteBaseDeviceParamValByRecordIds" parameterType="String">
        delete from base_device_param_val where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>

    <!-- 最新参数聚合查询：每设备每参数取最新一条记录，限制返回200条 -->
    <select id="selectLatestBaseDeviceParamValList" parameterType="BaseDeviceParamVal" resultMap="BaseDeviceParamValResult">
        SELECT * FROM (
            SELECT
                record_id, param_code, device_code, device_id, param_name, param_value, collect_time, record_time,
                ROW_NUMBER() OVER (ORDER BY device_code, param_code) AS row_num
            FROM (
                SELECT
                    record_id, param_code, device_code, device_id, param_name, param_value, collect_time, record_time,
                    ROW_NUMBER() OVER (PARTITION BY device_code, param_code ORDER BY NVL(record_time, collect_time) DESC) AS rn
                FROM base_device_param_val
                <where>
                    <if test="deviceCode != null and deviceCode != ''">and device_code = #{deviceCode}</if>
                    <if test="paramCode != null and paramCode != ''">and param_code = #{paramCode}</if>
                    <if test="deviceId != null">and device_id = #{deviceId}</if>
                    <if test="paramName != null and paramName != ''">and param_name like '%' || #{paramName} || '%'</if>
                </where>
            ) WHERE rn = 1
        ) WHERE row_num &lt;= 200
    </select>
</mapper>