<?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.CarParkRecordMapper">

    <resultMap type="CarParkRecord" id="CarParkRecordResult">
        <result property="id"    column="id"    />
        <result property="equipmentId"    column="equipment_id"    />
        <result property="parkId"    column="park_id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="sync"    column="sync"    />
        <result property="equipmentName"    column="equipment_name"    />
        <result property="parkName"    column="park_name"    />
        <result property="customerName"    column="customer_name"    />
    </resultMap>

    <sql id="selectCarParkRecordVo">
        select cpr.id, cpr.equipment_id, cpr.park_id, cpr.customer_id, cpr.sync,
        se.name equipment_name,cp.name park_name,ci.plate customer_name
        from car_park_record cpr
        left join sys_equipment se on cpr.equipment_id=se.id
        left join car_park cp on cpr.park_id=cp.id
        left join car_info ci on cpr.customer_id=ci.customer_id
    </sql>

    <select id="selectCarParkRecordList" parameterType="CarParkRecord" resultMap="CarParkRecordResult">
        <include refid="selectCarParkRecordVo"/>
        <where>
            <if test="equipmentId != null "> and cpr.equipment_id = #{equipmentId}</if>
            <if test="parkId != null "> and cpr.park_id = #{parkId}</if>
            <if test="customerId != null "> and cpr.customer_id = #{customerId}</if>
            <if test="customerName != null ">and ci.plate like concat('%', #{customerName}, '%') </if>
            <if test="sync != null  and sync != ''"> and cpr.sync = #{sync}</if>
        </where>
    </select>

    <select id="selectCarParkRecordById" parameterType="Long" resultMap="CarParkRecordResult">
        <include refid="selectCarParkRecordVo"/>
        where cpr.id = #{id}
    </select>

    <insert id="insertCarParkRecord" parameterType="CarParkRecord" useGeneratedKeys="true" keyProperty="id">
        insert into car_park_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id,</if>
            <if test="parkId != null">park_id,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="sync != null">sync,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">#{equipmentId},</if>
            <if test="parkId != null">#{parkId},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="sync != null">#{sync},</if>
         </trim>
    </insert>

    <update id="updateCarParkRecord" parameterType="CarParkRecord">
        update car_park_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id = #{equipmentId},</if>
            <if test="parkId != null">park_id = #{parkId},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="sync != null">sync = #{sync},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCarParkRecordById" parameterType="Long">
        delete from car_park_record where id = #{id}
    </delete>

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

    <delete id="deleteCarParkRecordByParkId" parameterType="Long">
        delete from car_park_record where park_id = #{parkId}
    </delete>


    <update id="deleteCarParkRecordByCustomerIds" parameterType="String">
        update  car_park_record set sync=0  where customer_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <delete id="deleteCarParkRecordByCustomerId" parameterType="Long">
        delete from  car_park_record where customer_id = #{customerId}
    </delete>

</mapper>
