<?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.xxfc.platform.vehicle.mapper.VehicleUpkeepLogMapper">
    <resultMap id="BaseResultMap" type="com.xxfc.platform.vehicle.entity.VehicleUpkeepLog">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="vehicle_id" property="vehicleId" jdbcType="VARCHAR"/>
        <result column="operator" property="operator" jdbcType="VARCHAR"/>
        <result column="date" property="date" jdbcType="TIMESTAMP"/>
        <result column="mileage" property="mileage" jdbcType="INTEGER"/>
        <result column="amount" property="amount" jdbcType="DECIMAL"/>
        <result column="approvers" property="approvers" jdbcType="VARCHAR"/>
        <result column="branch_company_id" property="branchCompanyId" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    <insert id="addItems">
        insert into vehicle_upkeep_log_item(log_id, item_id)
        values
        <trim suffixOverrides=",">
            <foreach collection="upkeepItems" item="item">
                (#{id}, #{item}),
            </foreach>
        </trim>
    </insert>
    <delete id="deleteItemsByLogId">
        delete from vehicle_upkeep_log_item
        where log_id = #{id}
    </delete>
    <select id="selectVoAll" resultType="com.xxfc.platform.vehicle.pojo.VehicleUpkeepLogVo">
        select a.*, b.number_plate, c.name as branch_company_name
        from vehicle_upkeep_log a
        left join vehicle b on a.vehicle_id = b.id
        left join branch_company c on a.branch_company_id = c.id
        <trim prefix="where">
            1=1
            <if test="numberPlate != null and numberPlate != ''">
                and b.number_plate = #{numberPlate}
            </if>
            <if test="companyList != null and companyList.size() > 0">
                and ( a.branch_company_id in (
                    <trim  suffixOverrides=",">
                        <foreach collection="companyList" item="companyId">
                            #{companyId},
                        </foreach>
                    </trim>
                    )
                )
            </if>
            <if test="code !=null">
               and b.code=#{code}
            </if>
        </trim>
        order by a.create_time desc
    </select>
    <select id="selectVoById" resultType="com.xxfc.platform.vehicle.pojo.VehicleUpkeepLogVo">
        select a.*, b.number_plate, c.name as branch_company_name
        from vehicle_upkeep_log a
        left join vehicle b on a.vehicle_id = b.id
        left join branch_company c on a.branch_company_id = c.id
        where a.id = #{id}
    </select>
    <select id="getItemsByLogId" resultType="com.xxfc.platform.vehicle.pojo.VehicleUpkeepLogItemVo">
        select a.*, b.name as item_name
        from vehicle_upkeep_log_item a
        left join vehicle_upkeep_item b on a.item_id = b.id
        where a.log_id = #{id}
    </select>
    <select id="selectLastByVehicleId" resultType="com.xxfc.platform.vehicle.entity.VehicleUpkeepLog">
        select *
        from vehicle_upkeep_log
        where vehicle_id = #{vehicleId}
        order by create_time desc
        limit 1
    </select>

    <insert id="insertUpkeepLogItmes" parameterType="com.xxfc.platform.vehicle.pojo.VehicleUpkeepLogVo">
        insert into vehicle_upkeep_log_item(log_id, item_id)
        values
        <trim suffixOverrides=",">
            <foreach collection="upkeepItems" item="item">
                (#{id}, #{item.itemId}),
            </foreach>
        </trim>
    </insert>
</mapper>