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

    <resultMap type="CarPassGather" id="CarPassGatherResult">
        <result property="id"    column="id"    />
        <result property="parkId"    column="park_id"    />
        <result property="parkName"    column="park_name"    />
        <result property="area"    column="area"    />
        <result property="license"    column="license"    />
        <result property="joinTime"    column="join_time"    />
        <result property="sn"    column="sn"    />
        <result property="equipmentName"    column="equipment_name"    />
        <result property="timeLong"    column="time_long"    />
    </resultMap>

    <sql id="selectCarPassGatherVo">
        select cpg.id, cpg.park_id, cpg.area, cpg.license, cpg.join_time, cpg.sn,cp.name park_name,se.name equipment_name,HOUR(TIMEDIFF(NOW(),cpg.join_time)) AS time_long
        from car_pass_gather cpg
        left join car_park cp on cp.id=cpg.park_id
        left join sys_equipment se on se.sequence=cpg.sn
    </sql>

    <select id="selectCarPassGatherList" parameterType="CarPassGather" resultMap="CarPassGatherResult">
        <include refid="selectCarPassGatherVo"/>
        <where>
            <if test="parkId != null  and parkId != ''"> and cpg.park_id = #{parkId}</if>
            <if test="area != null  and area != ''"> and cpg.area = #{area}</if>
            <if test="license != null  and license != ''"> and cpg.license like concat('%', #{license}, '%') </if>
            <if test="joinTime != null "> and date_format(cpg.join_time, '%Y-%m-%d') = date_format(#{joinTime}, '%Y-%m-%d') </if>
        </where>
    </select>

    <select id="selectCarPassGatherById" parameterType="Long" resultMap="CarPassGatherResult">
        <include refid="selectCarPassGatherVo"/>
        where cpg.id = #{id}
    </select>

    <insert id="insertCarPassGather" parameterType="CarPassGather" useGeneratedKeys="true" keyProperty="id">
        insert into car_pass_gather
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parkId != null">park_id,</if>
            <if test="area != null">area,</if>
            <if test="license != null">license,</if>
            <if test="joinTime != null">join_time,</if>
            <if test="sn != null">sn,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parkId != null">#{parkId},</if>
            <if test="area != null">#{area},</if>
            <if test="license != null">#{license},</if>
            <if test="joinTime != null">#{joinTime},</if>
            <if test="sn != null">#{sn},</if>
         </trim>
    </insert>

    <update id="updateCarPassGather" parameterType="CarPassGather">
        update car_pass_gather
        <trim prefix="SET" suffixOverrides=",">
            <if test="parkId != null">park_id = #{parkId},</if>
            <if test="area != null">area = #{area},</if>
            <if test="license != null">license = #{license},</if>
            <if test="joinTime != null">join_time = #{joinTime},</if>
            <if test="sn != null">sn = #{sn},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCarPassGatherById" parameterType="Long">
        delete from car_pass_gather where id = #{id}
    </delete>

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

    <select id="parkTop" parameterType="Long" resultType="java.util.Map">
        select cars_num num from car_park
       where id = #{parkId}
        UNION ALL
        select count(1) num from car_pass_gather
        where park_id=#{parkId}
    </select>

</mapper>
