<?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.bms.mapper.BmsDeviceMapper">
    
    <resultMap type="BmsDevice" id="BmsDeviceResult">
        <result property="deviceId"          column="device_id"          />
        <result property="deviceCode"        column="device_code"        />
        <result property="deviceName"        column="device_name"        />
        <result property="deviceType"        column="device_type"        />
        <result property="status"            column="status"             />
        <result property="batteryCapacity"   column="battery_capacity"   />
        <result property="ratedVoltage"      column="rated_voltage"      />
        <result property="currentVoltage"    column="current_voltage"    />
        <result property="currentCurrent"    column="current_current"    />
        <result property="remainingCapacity" column="remaining_capacity" />
        <result property="temperature"       column="temperature"        />
        <result property="lastCommTime"      column="last_comm_time"     />
        <result property="delFlag"           column="del_flag"           />
        <result property="version"           column="version"            />
        <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"             />
    </resultMap>

    <sql id="selectBmsDeviceVo">
        select device_id, device_code, device_name, device_type, status, battery_capacity, 
               rated_voltage, current_voltage, current_current, remaining_capacity, temperature, 
               last_comm_time, del_flag, version, create_by, create_time, update_by, update_time, remark 
        from bms_device
    </sql>

    <select id="selectBmsDeviceList" parameterType="BmsDevice" resultMap="BmsDeviceResult">
        <include refid="selectBmsDeviceVo"/>
        <where>  
            <if test="deviceCode != null  and deviceCode != ''"> and device_code like concat('%', #{deviceCode}, '%')</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="batteryCapacity != null "> and battery_capacity = #{batteryCapacity}</if>
            <if test="ratedVoltage != null "> and rated_voltage = #{ratedVoltage}</if>
            <if test="currentVoltage != null "> and current_voltage = #{currentVoltage}</if>
            <if test="currentCurrent != null "> and current_current = #{currentCurrent}</if>
            <if test="remainingCapacity != null "> and remaining_capacity = #{remainingCapacity}</if>
            <if test="temperature != null "> and temperature = #{temperature}</if>
            <if test="lastCommTime != null "> and last_comm_time = #{lastCommTime}</if>
        </where>
        order by device_id desc
    </select>
    
    <select id="selectBmsDeviceByCode" parameterType="String" resultMap="BmsDeviceResult">
        <include refid="selectBmsDeviceVo"/>
        where device_code = #{deviceCode} and del_flag = '0'
    </select>

    <update id="updateBmsDeviceStatus">
        update bms_device set status = #{status} where device_id in
        <foreach item="deviceId" collection="deviceIds" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </update>

</mapper>