<?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.dcsoft.system.vehicle.mapper.CarPassRecordMapper">

    <resultMap type="CarPassRecord" id="CarPassRecordResult">
        <result property="id"    column="id"    />
        <result property="parkId"    column="park_id"    />
        <result property="uniqueNo"    column="unique_no"    />
        <result property="direction"    column="direction"    />
        <result property="license"    column="license"    />
        <result property="passTime"    column="pass_time"    />
        <result property="triggerType"    column="trigger_type"    />
        <result property="colorType"    column="color_type"    />
        <result property="carColor"    column="car_color"    />
        <result property="url"    column="url"    />
        <result property="dataType"    column="data_type"    />
        <result property="sn"    column="sn"    />
        <result property="way"    column="way"    />
        <result property="parkName"    column="park_name"    />
        <result property="equipmentName"    column="equipment_name"    />
        <result property="unit"    column="unit"    />
        <result property="type"    column="type"    />
    </resultMap>

    <sql id="selectCarPassRecordVo">
        select cpr.id, cpr.park_id, cpr.unique_no, cpr.direction, cpr.license, cpr.pass_time, cpr.trigger_type, cpr.color_type,
               cpr.car_color, cpr.url, cpr.data_type, cpr.sn,cpi.way,cp.name park_name,se.name equipment_name,ci.unit,cpr.type
        from car_pass_record cpr
        left join sys_equipment se on cpr.sn=se.sequence
        left join car_park_item cpi on cpi.equipment_id=se.id
        left join car_park cp on cp.id=cpi.park_id
        left join car_info ci on cpr.license=ci.plate
    </sql>

    <select id="selectCarPassRecordList" parameterType="CarPassRecord" resultMap="CarPassRecordResult">
        <include refid="selectCarPassRecordVo"/>
        <where>
            <if test="direction != null  and direction != ''"> and direction = #{direction}</if>
            <if test="license != null  and license != ''"> and license = #{license}</if>
            <if test="passTime != null "> and date_format(pass_time, '%Y-%m-%d') = date_format(#{passTime}, '%Y-%m-%d')</if>
            <if test="triggerType != null  and triggerType != ''"> and trigger_type = #{triggerType}</if>
            <if test="colorType != null  and colorType != ''"> and color_type = #{colorType}</if>
            <if test="carColor != null  and carColor != ''"> and car_color = #{carColor}</if>
        </where>
        order by cpr.pass_time desc
    </select>

    <select id="selectCarPassRecordById" parameterType="Long" resultMap="CarPassRecordResult">
        <include refid="selectCarPassRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertCarPassRecord" parameterType="CarPassRecord">
        insert into car_pass_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="parkId != null and parkId != ''">park_id,</if>
            <if test="uniqueNo != null and uniqueNo != ''">unique_no,</if>
            <if test="direction != null">direction,</if>
            <if test="license != null and license != ''">license,</if>
            <if test="passTime != null">pass_time,</if>
            <if test="triggerType != null and triggerType != ''">trigger_type,</if>
            <if test="colorType != null and colorType != ''">color_type,</if>
            <if test="carColor != null">car_color,</if>
            <if test="url != null">url,</if>
            <if test="dataType != null">data_type,</if>
            <if test="sn != null and sn != ''">sn,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="parkId != null and parkId != ''">#{parkId},</if>
            <if test="uniqueNo != null and uniqueNo != ''">#{uniqueNo},</if>
            <if test="direction != null">#{direction},</if>
            <if test="license != null and license != ''">#{license},</if>
            <if test="passTime != null">#{passTime},</if>
            <if test="triggerType != null and triggerType != ''">#{triggerType},</if>
            <if test="colorType != null and colorType != ''">#{colorType},</if>
            <if test="carColor != null">#{carColor},</if>
            <if test="url != null">#{url},</if>
            <if test="dataType != null">#{dataType},</if>
            <if test="sn != null and sn != ''">#{sn},</if>
         </trim>
    </insert>

    <update id="updateCarPassRecord" parameterType="CarPassRecord">
        update car_pass_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="parkId != null and parkId != ''">park_id = #{parkId},</if>
            <if test="uniqueNo != null and uniqueNo != ''">unique_no = #{uniqueNo},</if>
            <if test="direction != null">direction = #{direction},</if>
            <if test="license != null and license != ''">license = #{license},</if>
            <if test="passTime != null">pass_time = #{passTime},</if>
            <if test="triggerType != null and triggerType != ''">trigger_type = #{triggerType},</if>
            <if test="colorType != null and colorType != ''">color_type = #{colorType},</if>
            <if test="carColor != null">car_color = #{carColor},</if>
            <if test="url != null">url = #{url},</if>
            <if test="dataType != null">data_type = #{dataType},</if>
            <if test="sn != null and sn != ''">sn = #{sn},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCarPassRecordById" parameterType="Long">
        delete from car_pass_record where id = #{id}
    </delete>

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