<?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.xzzn.ems.mapper.EmsCoolingDataMapper">

    <resultMap type="EmsCoolingData" id="EmsCoolingDataResult">
        <result property="id"    column="id"    />
        <result property="dataUpdateTime"    column="data_update_time"    />
        <result property="systemName"    column="system_name"    />
        <result property="workMode"    column="work_mode"    />
        <result property="currentTemperature"    column="current_temperature"    />
        <result property="heatingStartPoint"    column="heating_start_point"    />
        <result property="coolingStartPoint"    column="cooling_start_point"    />
        <result property="highTempAlarmPoint"    column="high_temp_alarm_point"    />
        <result property="heatingStopPoint"    column="heating_stop_point"    />
        <result property="coolingStopPoint"    column="cooling_stop_point"    />
        <result property="lowTempAlarmPoint"    column="low_temp_alarm_point"    />
        <result property="gsTemp"    column="gs_temp"    />
        <result property="hsTemp"    column="hs_temp"    />
        <result property="gsPressure"    column="gs_pressure"    />
        <result property="hsPressure"    column="hs_pressure"    />
        <result property="lysTemp"    column="lys_temp"    />
        <result property="vb01Kd"    column="vb01_kd"    />
        <result property="vb02Kd"    column="vb02_kd"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="siteId"    column="site_id"    />
        <result property="deviceId"    column="device_id"    />
    </resultMap>

    <sql id="selectEmsCoolingDataVo">
        select id, system_name, work_mode, current_temperature, heating_start_point, cooling_start_point, high_temp_alarm_point, heating_stop_point, cooling_stop_point, low_temp_alarm_point, gs_temp, hs_temp, gs_pressure, hs_pressure, lys_temp, vb01_kd, vb02_kd, create_by, create_time, update_by, update_time, remark, site_id, device_id from ems_cooling_data
    </sql>

    <select id="selectEmsCoolingDataList" parameterType="EmsCoolingData" resultMap="EmsCoolingDataResult">
        <include refid="selectEmsCoolingDataVo"/>
        <where>
            <if test="dataUpdateTime != null "> and data_update_time = #{dataUpdateTime}</if>
            <if test="systemName != null  and systemName != ''"> and system_name like concat('%', #{systemName}, '%')</if>
            <if test="workMode != null  and workMode != ''"> and work_mode = #{workMode}</if>
            <if test="currentTemperature != null "> and current_temperature = #{currentTemperature}</if>
            <if test="heatingStartPoint != null "> and heating_start_point = #{heatingStartPoint}</if>
            <if test="coolingStartPoint != null "> and cooling_start_point = #{coolingStartPoint}</if>
            <if test="highTempAlarmPoint != null "> and high_temp_alarm_point = #{highTempAlarmPoint}</if>
            <if test="heatingStopPoint != null "> and heating_stop_point = #{heatingStopPoint}</if>
            <if test="coolingStopPoint != null "> and cooling_stop_point = #{coolingStopPoint}</if>
            <if test="lowTempAlarmPoint != null "> and low_temp_alarm_point = #{lowTempAlarmPoint}</if>
            <if test="gsTemp != null "> and gs_temp = #{gsTemp}</if>
            <if test="hsTemp != null "> and hs_temp = #{hsTemp}</if>
            <if test="gsPressure != null "> and gs_pressure = #{gsPressure}</if>
            <if test="hsPressure != null "> and hs_pressure = #{hsPressure}</if>
            <if test="lysTemp != null "> and lys_temp = #{lysTemp}</if>
            <if test="vb01Kd != null "> and vb01_kd = #{vb01Kd}</if>
            <if test="vb02Kd != null "> and vb02_kd = #{vb02Kd}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
        </where>
    </select>

    <select id="selectEmsCoolingDataById" parameterType="Long" resultMap="EmsCoolingDataResult">
        <include refid="selectEmsCoolingDataVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsCoolingData" parameterType="EmsCoolingData" useGeneratedKeys="true" keyProperty="id">
        insert into ems_cooling_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dataUpdateTime != null">data_update_time,</if>
            <if test="systemName != null">system_name,</if>
            <if test="workMode != null">work_mode,</if>
            <if test="currentTemperature != null">current_temperature,</if>
            <if test="heatingStartPoint != null">heating_start_point,</if>
            <if test="coolingStartPoint != null">cooling_start_point,</if>
            <if test="highTempAlarmPoint != null">high_temp_alarm_point,</if>
            <if test="heatingStopPoint != null">heating_stop_point,</if>
            <if test="coolingStopPoint != null">cooling_stop_point,</if>
            <if test="lowTempAlarmPoint != null">low_temp_alarm_point,</if>
            <if test="gsTemp != null">gs_temp,</if>
            <if test="hsTemp != null">hs_temp,</if>
            <if test="gsPressure != null">gs_pressure,</if>
            <if test="hsPressure != null">hs_pressure,</if>
            <if test="lysTemp != null">lys_temp,</if>
            <if test="vb01Kd != null">vb01_kd,</if>
            <if test="vb02Kd != null">vb02_kd,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="siteId != null">site_id,</if>
            <if test="deviceId != null and deviceId != ''">device_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dataUpdateTime != null">#{dataUpdateTime},</if>
            <if test="systemName != null">#{systemName},</if>
            <if test="workMode != null">#{workMode},</if>
            <if test="currentTemperature != null">#{currentTemperature},</if>
            <if test="heatingStartPoint != null">#{heatingStartPoint},</if>
            <if test="coolingStartPoint != null">#{coolingStartPoint},</if>
            <if test="highTempAlarmPoint != null">#{highTempAlarmPoint},</if>
            <if test="heatingStopPoint != null">#{heatingStopPoint},</if>
            <if test="coolingStopPoint != null">#{coolingStopPoint},</if>
            <if test="lowTempAlarmPoint != null">#{lowTempAlarmPoint},</if>
            <if test="gsTemp != null">#{gsTemp},</if>
            <if test="hsTemp != null">#{hsTemp},</if>
            <if test="gsPressure != null">#{gsPressure},</if>
            <if test="hsPressure != null">#{hsPressure},</if>
            <if test="lysTemp != null">#{lysTemp},</if>
            <if test="vb01Kd != null">#{vb01Kd},</if>
            <if test="vb02Kd != null">#{vb02Kd},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="siteId != null">#{siteId},</if>
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
        </trim>
    </insert>

    <update id="updateEmsCoolingData" parameterType="EmsCoolingData">
        update ems_cooling_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="dataUpdateTime != null">data_update_time = #{dataUpdateTime},</if>
            <if test="systemName != null">system_name = #{systemName},</if>
            <if test="workMode != null">work_mode = #{workMode},</if>
            <if test="currentTemperature != null">current_temperature = #{currentTemperature},</if>
            <if test="heatingStartPoint != null">heating_start_point = #{heatingStartPoint},</if>
            <if test="coolingStartPoint != null">cooling_start_point = #{coolingStartPoint},</if>
            <if test="highTempAlarmPoint != null">high_temp_alarm_point = #{highTempAlarmPoint},</if>
            <if test="heatingStopPoint != null">heating_stop_point = #{heatingStopPoint},</if>
            <if test="coolingStopPoint != null">cooling_stop_point = #{coolingStopPoint},</if>
            <if test="lowTempAlarmPoint != null">low_temp_alarm_point = #{lowTempAlarmPoint},</if>
            <if test="gsTemp != null">gs_temp = #{gsTemp},</if>
            <if test="hsTemp != null">hs_temp = #{hsTemp},</if>
            <if test="gsPressure != null">gs_pressure = #{gsPressure},</if>
            <if test="hsPressure != null">hs_pressure = #{hsPressure},</if>
            <if test="lysTemp != null">lys_temp = #{lysTemp},</if>
            <if test="vb01Kd != null">vb01_kd = #{vb01Kd},</if>
            <if test="vb02Kd != null">vb02_kd = #{vb02Kd},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsCoolingDataById" parameterType="Long">
        delete from ems_cooling_data where id = #{id}
    </delete>

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

    <select id="getCoolingDataList" parameterType="String" resultMap="EmsCoolingDataResult">
        SELECT  t.*
        FROM ( SELECT  *, ROW_NUMBER() OVER (PARTITION BY device_id  ORDER BY update_time DESC ) AS rn
                 FROM ems_cooling_data
                 WHERE site_id = #{siteId}
             ) t
        WHERE  rn = 1
        ORDER BY system_name
    </select>
</mapper>