<?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.PwtVehicleMapper">
    
    <resultMap type="PwtVehicle" id="PwtVehicleResult">
        <result property="vehicleId"    column="vehicle_id"    />
        <result property="vehicleName"    column="vehicle_name"    />
        <result property="vehicleNo"    column="vehicle_no"    />
        <result property="vin"    column="vin"    />
        <result property="rfid"    column="rfid"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <select id="getCount" resultType="int">
        select COUNT(*) FROM pwt_vehicle
    </select>
    <sql id="selectPwtVehicleVo">
        select vehicle_id, vehicle_name, vehicle_no, vin, rfid, create_time from pwt_vehicle
    </sql>

    <select id="selectPwtVehicleList" parameterType="PwtVehicle" resultMap="PwtVehicleResult">
        <include refid="selectPwtVehicleVo"/>
        <where>  
            <if test="vehicleNo != null  and vehicleNo != ''"> and vehicle_no like concat('%', #{vehicleNo}, '%')</if>
            <if test="vin != null  and vin != ''"> and vin = #{vin}</if>
            <if test="rfid != null  and rfid != ''"> and rfid = #{rfid}</if>
        </where>
        order by vehicle_id desc
    </select>
    
    <select id="selectPwtVehicleById" parameterType="Long" resultMap="PwtVehicleResult">
        <include refid="selectPwtVehicleVo"/>
        where vehicle_id = #{vehicleId}
    </select>

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

    <select id="checkVehicleNoUnique" parameterType="String" resultType="int">
         select count(1) from pwt_vehicle where vehicle_no = #{vehicleNo} limit 1
    </select>

    <select id="checkVehicleRfidUnique" parameterType="String" resultType="int">
         select count(1) from pwt_vehicle where rfid = #{rfid} limit 1
    </select>

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

    <select id="getByVehicleNo" parameterType="String" resultMap="PwtVehicleResult">
        <include refid="selectPwtVehicleVo"/>
         where vehicle_no = #{vehicleNo}
    </select>

    <select id="getByRfid" parameterType="String" resultMap="PwtVehicleResult">
        <include refid="selectPwtVehicleVo"/>
         where rfid = #{rfid}
    </select>

    <insert id="insertPwtVehicle" parameterType="PwtVehicle" useGeneratedKeys="true" keyProperty="vehicleId">
        insert into pwt_vehicle
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="vehicleName != null">vehicle_name,</if>
            <if test="vehicleNo != null">vehicle_no,</if>
            <if test="vin != null">vin,</if>
            <if test="rfid != null">rfid,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="vehicleName != null">#{vehicleName},</if>
            <if test="vehicleNo != null">#{vehicleNo},</if>
            <if test="vin != null">#{vin},</if>
            <if test="rfid != null">#{rfid},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updatePwtVehicle" parameterType="PwtVehicle">
        update pwt_vehicle
        <trim prefix="SET" suffixOverrides=",">
            <if test="vehicleName != null">vehicle_name = #{vehicleName},</if>
            <if test="vehicleNo != null">vehicle_no = #{vehicleNo},</if>
            <if test="vin != null">vin = #{vin},</if>
            <if test="rfid != null">rfid = #{rfid},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where vehicle_id = #{vehicleId}
    </update>

    <delete id="deletePwtVehicleById" parameterType="Long">
        delete from pwt_vehicle where vehicle_id = #{vehicleId}
    </delete>

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