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

    <resultMap type="CarPark" id="CarParkResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="spaceId"    column="space_id"    />
        <result property="carsNum"    column="cars_num"    />
        <result property="surplusNum"    column="surplus_num"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="spaceName"    column="space_name"    />
    </resultMap>

    <sql id="selectCarParkVo">
        select cp.id, cp.name, cp.space_id, cp.cars_num, cp.surplus_num, cp.remark, cp.create_by, cp.create_time,
               cp.update_by, cp.update_time,ss.name space_name
        from car_park cp
        left join sys_space ss on cp.space_id=ss.id

    </sql>

    <select id="selectCarParkList" parameterType="CarPark" resultMap="CarParkResult">
        <include refid="selectCarParkVo"/>
        <where>
            <if test="name != null  and name != ''"> and cp.name like concat('%', #{name}, '%')</if>
        </where>
    </select>

    <select id="selectCarParkById" parameterType="Long" resultMap="CarParkResult">
        <include refid="selectCarParkVo"/>
        where cp.id = #{id}
    </select>

    <insert id="insertCarPark" parameterType="CarPark" useGeneratedKeys="true" keyProperty="id">
        insert into car_park
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if test="spaceId != null">space_id,</if>
            <if test="carsNum != null">cars_num,</if>
            <if test="surplusNum != null">surplus_num,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="spaceId != null">#{spaceId},</if>
            <if test="carsNum != null">#{carsNum},</if>
            <if test="surplusNum != null">#{surplusNum},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateCarPark" parameterType="CarPark">
        update car_park
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="spaceId != null">space_id = #{spaceId},</if>
            <if test="carsNum != null">cars_num = #{carsNum},</if>
            <if test="surplusNum != null">surplus_num = #{surplusNum},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCarParkById" parameterType="Long">
        delete from car_park where id = #{id}
    </delete>

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

    <select id="selectItemTreeList" parameterType="Long" resultType="java.util.Map">
        select dict_value id,dict_label name from sys_dict_data
         where dict_type ='car_park_area'
        and dict_value in (select DISTINCT area from car_park_item where park_id=#{id})
    </select>

    <select id="carPlay" parameterType="Long" resultType="java.util.Map">
        select back_url url,id from sys_equipment where id in  (select equipment_id from car_park_item where area=#{id})
    </select>
    <select id="carPlayRecord" parameterType="Long" resultType="java.util.Map">
        select cpr.url,cpi.way,cpr.pass_time passTime 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
        where se.id in  (select equipment_id from car_park_item where area=#{id})
        ORDER BY cpr.pass_time desc limit 12
    </select>

    <delete id="deleteSelectCarInfo" parameterType="Long">
        delete from car_equipment_open where equipment_id = #{id}
    </delete>

    <insert id="selectCarInfo" >
        insert into car_equipment_open (equipment_id,io) values(#{id},#{flag})
    </insert>

    <select id="selectCarParkByName" parameterType="String" resultMap="CarParkResult">
        <include refid="selectCarParkVo"/>
        where cp.name = #{name}
    </select>

</mapper>
