<?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.pwt.web.mapper.PwtBatteryMapper">
    
    <resultMap type="PwtBattery" id="PwtBatteryResult">
        <result property="batteryId"    column="battery_id"    />
        <result property="batteryName"    column="battery_name"    />
        <result property="batteryType"    column="battery_type"    />
        <result property="batteryFactory"    column="battery_factory"    />
        <result property="batterySerial"    column="battery_serial"    />
        <result property="productionTime"    column="production_time"    />
        <result property="vin"    column="vin"    />
    </resultMap>

    <sql id="selectPwtBatteryVo">
        select battery_id, battery_name, battery_type, battery_factory, battery_serial, production_time, vin from pwt_battery
    </sql>

    <select id="selectPwtBatteryList" parameterType="PwtBattery" resultMap="PwtBatteryResult">
        <include refid="selectPwtBatteryVo"/>
        <where>  
            <if test="batteryName != null  and batteryName != ''"> and battery_name like concat('%', #{batteryName}, '%')</if>
            <if test="batteryFactory != null  and batteryFactory != ''"> and battery_factory like concat('%', #{batteryFactory}, '%')</if>
            <if test="batterySerial != null  and batterySerial != ''"> and battery_serial = #{batterySerial}</if>
        </where>
        order by battery_id desc
    </select>
    
    <select id="selectPwtBatteryById" parameterType="Long" resultMap="PwtBatteryResult">
        <include refid="selectPwtBatteryVo"/>
        where battery_id = #{batteryId}
    </select>

    <select id="checkVinUnique" parameterType="String" resultType="int">
        select count(1) from pwt_battery where vin = #{vin} limit 1
    </select>

    <select id="getByVinCode" parameterType="String" resultMap="PwtBatteryResult">
        <include refid="selectPwtBatteryVo"/>
        where vin = #{vin}
    </select>

    <insert id="insertPwtBattery" parameterType="PwtBattery" useGeneratedKeys="true" keyProperty="batteryId">
        insert into pwt_battery
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batteryName != null">battery_name,</if>
            <if test="batteryType != null">battery_type,</if>
            <if test="batteryFactory != null">battery_factory,</if>
            <if test="batterySerial != null">battery_serial,</if>
            <if test="productionTime != null">production_time,</if>
            <if test="vin != null">vin,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="batteryName != null">#{batteryName},</if>
            <if test="batteryType != null">#{batteryType},</if>
            <if test="batteryFactory != null">#{batteryFactory},</if>
            <if test="batterySerial != null">#{batterySerial},</if>
            <if test="productionTime != null">#{productionTime},</if>
            <if test="vin != null">#{vin},</if>
         </trim>
    </insert>

    <update id="updatePwtBattery" parameterType="PwtBattery">
        update pwt_battery
        <trim prefix="SET" suffixOverrides=",">
            <if test="batteryName != null">battery_name = #{batteryName},</if>
            <if test="batteryType != null">battery_type = #{batteryType},</if>
            <if test="batteryFactory != null">battery_factory = #{batteryFactory},</if>
            <if test="batterySerial != null">battery_serial = #{batterySerial},</if>
            <if test="productionTime != null">production_time = #{productionTime},</if>
            <if test="vin != null">vin = #{vin},</if>
        </trim>
        where battery_id = #{batteryId}
    </update>

    <delete id="deletePwtBatteryById" parameterType="Long">
        delete from pwt_battery where battery_id = #{batteryId}
    </delete>

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