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

    <resultMap type="CarInfo" id="CarInfoResult">
        <result property="customerId"    column="customer_id"    />
        <result property="enableTime"    column="enable_time"    />
        <result property="overdueTime"    column="overdue_time"    />
        <result property="enable"    column="enable"    />
        <result property="plate"    column="plate"    />
        <result property="timeSegEnable"    column="time_seg_enable"    />
        <result property="segTime"    column="seg_time"    />
        <result property="needAlarm"    column="need_alarm"    />
        <result property="vehicleCode"    column="vehicle_code"    />
        <result property="vehicleComment"    column="vehicle_comment"    />
        <result property="peopleId"    column="people_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="sync"    column="sync"    />
        <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="parkId"    column="park_id"    />
        <result property="peopleName"    column="people_name"    />
        <result property="parkName"    column="park_name"    />
        <result property="num"    column="num"    />
        <result property="carType"    column="car_type"    />
        <result property="unit"    column="unit"    />
        <result property="phone"    column="phone"    />
    </resultMap>

    <sql id="selectCarInfoVo">
        select ci.customer_id, ci.enable_time, ci.overdue_time, ci.enable, ci.plate, ci.time_seg_enable, ci.seg_time, ci.need_alarm,
               ci.vehicle_code, ci.vehicle_comment, ci.people_id, ci.del_flag, ci.sync, ci.remark,
               ci.create_by, ci.create_time, ci.update_by, ci.update_time,ci.park_id, cp.name park_name,p.name people_name,
               ci.num,ci.car_type,ci.unit,ci.phone
        from car_info ci
        left join car_park cp on ci.park_id=cp.id
        left join sys_people p on ci.people_id=p.id

    </sql>

    <select id="selectCarInfoList" parameterType="CarInfo" resultMap="CarInfoResult">
        <include refid="selectCarInfoVo"/>
        <where>
            1=1 and ci.del_flag=0
            <if test="enableTime != null "> and ci.enable_time = #{enableTime}</if>
            <if test="overdueTime != null "> and ci.overdue_time = #{overdueTime}</if>
            <if test="enable != null  and enable != ''"> and ci.enable = #{enable}</if>
            <if test="plate != null  and plate != ''"> and ci.plate like concat('%', #{plate}, '%') </if>
            <if test="peopleId != null "> and ci.people_id = #{peopleId}</if>
            <if test="peopleName != null "> and p.name = #{peopleName}</if>
            <if test="unit != null "> and ci.unit = #{unit}</if>
        </where>
    </select>

    <select id="selectCarInfoByCustomerId" parameterType="Long" resultMap="CarInfoResult">
        <include refid="selectCarInfoVo"/>
        where ci.customer_id = #{customerId}
    </select>

    <insert id="insertCarInfo" parameterType="CarInfo" useGeneratedKeys="true" keyProperty="customerId">
        insert into car_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerId != null">customer_id,</if>
            <if test="enableTime != null">enable_time,</if>
            <if test="overdueTime != null">overdue_time,</if>
            <if test="enable != null">enable,</if>
            <if test="plate != null and plate != ''">plate,</if>
            <if test="timeSegEnable != null and timeSegEnable != ''">time_seg_enable,</if>
            <if test="segTime != null">seg_time,</if>
            <if test="needAlarm != null">need_alarm,</if>
            <if test="vehicleCode != null">vehicle_code,</if>
            <if test="vehicleComment != null">vehicle_comment,</if>
            <if test="peopleId != null">people_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="sync != null">sync,</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>
            <if test="parkId != null">park_id,</if>
            <if test="num != null">num,</if>
            <if test="carType != null">car_type,</if>
            <if test="unit != null">unit,</if>
            <if test="phone != null">phone,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerId != null">#{customerId},</if>
            <if test="enableTime != null">#{enableTime},</if>
            <if test="overdueTime != null">#{overdueTime},</if>
            <if test="enable != null">#{enable},</if>
            <if test="plate != null and plate != ''">#{plate},</if>
            <if test="timeSegEnable != null and timeSegEnable != ''">#{timeSegEnable},</if>
            <if test="segTime != null">#{segTime},</if>
            <if test="needAlarm != null">#{needAlarm},</if>
            <if test="vehicleCode != null">#{vehicleCode},</if>
            <if test="vehicleComment != null">#{vehicleComment},</if>
            <if test="peopleId != null">#{peopleId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="sync != null">#{sync},</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>
            <if test="parkId != null">#{parkId},</if>
            <if test="num != null">#{num},</if>
            <if test="carType != null">#{carType},</if>
            <if test="unit != null">#{unit},</if>
            <if test="phone != null">#{phone},</if>
        </trim>
    </insert>

    <update id="updateCarInfo" parameterType="CarInfo">
        update car_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="enableTime != null">enable_time = #{enableTime},</if>
            <if test="overdueTime != null">overdue_time = #{overdueTime},</if>
            <if test="enable != null">enable = #{enable},</if>
            <if test="plate != null and plate != ''">plate = #{plate},</if>
            <if test="timeSegEnable != null and timeSegEnable != ''">time_seg_enable = #{timeSegEnable},</if>
            <if test="segTime != null">seg_time = #{segTime},</if>
            <if test="needAlarm != null">need_alarm = #{needAlarm},</if>
            <if test="vehicleCode != null">vehicle_code = #{vehicleCode},</if>
            <if test="vehicleComment != null">vehicle_comment = #{vehicleComment},</if>
            <if test="peopleId != null">people_id = #{peopleId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="sync != null">sync = #{sync},</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>
            <if test="parkId != null">park_id = #{parkId},</if>
            <if test="num != null">num = #{num},</if>
            <if test="carType != null">car_type = #{carType},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="phone != null">phone = #{phone},</if>

        </trim>
        where customer_id = #{customerId}
    </update>

    <update id="deleteCarInfoByCustomerId" parameterType="Long">
        update  car_info set del_flag=2 where customer_id = #{customerId}
    </update>

    <update id="deleteCarInfoByCustomerIds" parameterType="String">
        update  car_info set del_flag=2 where customer_id in
        <foreach item="customerId" collection="array" open="(" separator="," close=")">
            #{customerId}
        </foreach>
    </update>

    <select id="selectCarInfoByCarNo" parameterType="String" resultMap="CarInfoResult">
        <include refid="selectCarInfoVo"/>
        where ci.plate = #{plate} and ci.del_flag=0
        limit 1
    </select>
    <select id="selectCarInfoByPlate" resultMap="CarInfoResult">
        select
            *
        from car_info
        where plate = #{plate}
    </select>

</mapper>
