<?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.evobms.project.system.mapper.BmsDevicesMapper">
    
    <resultMap type="BmsDevices" id="BmsDevicesResult">
        <result property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
         <result property="deviceSn"    column="device_sn"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceType"    column="device_type"    />
        <result property="status"    column="status"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="firmwareVersion"    column="firmware_version"    />
        <result property="lastOnline"    column="last_online"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
    </resultMap>

    <sql id="selectBmsDevicesVo">
        select id, device_id,device_sn, device_name, device_type, status, ip_address, firmware_version, last_online, update_time, update_by, create_time, create_by from bms_devices
    </sql>

    <select id="selectBmsDevicesList" parameterType="BmsDevices" resultMap="BmsDevicesResult">
        <include refid="selectBmsDevicesVo"/>
        <where>  
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="deviceSn != null  and deviceSn != ''"> and device_sn like concat('%', #{deviceSn}, '%')</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="ipAddress != null  and ipAddress != ''"> and ip_address = #{ipAddress}</if>
            <if test="firmwareVersion != null  and firmwareVersion != ''"> and firmware_version = #{firmwareVersion}</if>
            <if test="lastOnline != null "> and last_online = #{lastOnline}</if>
        </where>
    </select>
    
    <select id="selectBmsDevicesById" parameterType="String" resultMap="BmsDevicesResult">
        <include refid="selectBmsDevicesVo"/>
        where id = #{id}
    </select>

    <insert id="insertBmsDevices" parameterType="BmsDevices">
        insert into bms_devices
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="deviceSn != null and deviceSn != ''">device_sn,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="deviceType != null and deviceType != ''">device_type,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="ipAddress != null">ip_address,</if>
            <if test="firmwareVersion != null">firmware_version,</if>
            <if test="lastOnline != null">last_online,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
             <if test="deviceSn != null and deviceSn != ''">device_sn,</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="deviceType != null and deviceType != ''">#{deviceType},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="ipAddress != null">#{ipAddress},</if>
            <if test="firmwareVersion != null">#{firmwareVersion},</if>
            <if test="lastOnline != null">#{lastOnline},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
         </trim>
    </insert>

    <update id="updateBmsDevices" parameterType="BmsDevices">
        update bms_devices
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
             <if test="deviceSn != null and deviceSn != ''">device_sn = #{deviceSn},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="deviceType != null and deviceType != ''">device_type = #{deviceType},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="firmwareVersion != null">firmware_version = #{firmwareVersion},</if>
            <if test="lastOnline != null">last_online = #{lastOnline},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBmsDevicesById" parameterType="String">
        delete from bms_devices where id = #{id}
    </delete>

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