<?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.xinxincaravan.caravan.vehicle.mapper.VehicleDepartureLogMapper" >


    <select id="selectLastByVehicleId" resultType="com.xinxincaravan.caravan.vehicle.entity.VehicleDepartureLog">
        select * from vehicle_departure_log
        where vehicle_id = #{vehicleId}
        order by create_time desc
        limit 1
    </select>
    <select id="selectByVehicleId">
        select vehicle_departure_log.*,vehicle.number_plate
        from vehicle_departure_log
        left join vehicle on vehicle_departure_log.vehicle_id = vehicle.id
        where vehicle_id = #{vehicleId}
        order by create_time desc
    </select>
    <select id="selectVoAll" resultType="com.xinxincaravan.caravan.vehicle.vo.VehicleDepartureLogVo">
        select vehicle_departure_log.*,vehicle.number_plate
        from vehicle_departure_log
        left join vehicle on vehicle_departure_log.vehicle_id = vehicle.id
        <trim prefix="where">
            <if test="numberPlate != null and numberPlate != ''">
                vehicle.number_plate = #{numberPlate}
            </if>
        </trim>
        order by create_time desc
    </select>
    <select id="selectVoAllNotAllData" resultType="com.xinxincaravan.caravan.vehicle.vo.VehicleDepartureLogVo">
        select vehicle_departure_log.*,vehicle.number_plate
        from vehicle_departure_log
        left join vehicle on vehicle_departure_log.vehicle_id = vehicle.id
        <trim prefix="where" suffixOverrides="and">
            <if test="numberPlate != null and numberPlate != ''">
                vehicle.number_plate = #{numberPlate} and
            </if>
            <if test="companyList != null">
                ( vehicle.park_branch_company_id in (
                <foreach collection="companyList" item="companyId">
                    <trim suffixOverrides=",">
                        #{companyId},
                    </trim>
                </foreach>
                )
                or vehicle.expect_destination_branch_company_id in (
                <foreach collection="companyList" item="companyId">
                    <trim suffixOverrides=",">
                        #{companyId},
                    </trim>
                </foreach>
                )
                )
            </if>
        </trim>
        order by create_time desc
    </select>
    <select id="selectMileageByVehicleId" resultType="int">
        select sum(mileage_end - mileage_start)
        from vehicle_departure_log
        where vehicle_id = #{vehicleId};
    </select>
    <select id="selectDayByVehicleId" resultType="int">
        select sum(to_days(arrival_time) - to_days(departure_time))
        from vehicle_departure_log
        where vehicle_id = #{vehicleId};
    </select>

</mapper>