<?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.ruoyi.oa.mapper.CmcCarMapper">

    <resultMap type="CmcCar" id="CmcCarResult">
        <result property="carId"    column="car_id"    />
        <result property="licensePlate"    column="license_plate"    />
        <result property="driver"    column="driver"    />
        <result property="acquisitionTime"    column="acquisition_time"    />
        <result property="cost"    column="cost"    />
        <result property="expectLife"    column="expect_life"    />
        <result property="expectKm"    column="expect_km"    />
        <result property="series"    column="series"    />
        <result property="brand"    column="brand"    />
        <result property="isRent"    column="is_rent"    />
        <result property="dayCost"    column="day_cost"    />
        <result property="remark"    column="remark"    />
        <result property="status"    column="status"    />
        <association property="driverUser"    javaType="SysUser"         resultMap="SysUserResult" />
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <sql id="selectCmcCarVo">
        select c.car_id, c.license_plate, c.driver, u.nick_name, c.acquisition_time, c.cost, c.expect_life, c.expect_km, c.series, c.brand, c.is_rent, c.day_cost, c.remark, c.status from cmc_car as c
        left join sys_user as u on u.user_id = c.driver
    </sql>

    <select id="selectCmcCarList" parameterType="CmcCar" resultMap="CmcCarResult">
        <include refid="selectCmcCarVo"/>
        <where>
            <if test="licensePlate != null  and licensePlate != ''"> and c.license_plate like concat('%', #{licensePlate}, '%')</if>
            <if test="driver != null "> and c.driver = #{driver}</if>
            <if test="acquisitionTime != null "> and c.acquisition_time = #{acquisitionTime}</if>
            <if test="cost != null "> and c.cost = #{cost}</if>
            <if test="expectLife != null "> and c.expect_life = #{expectLife}</if>
            <if test="expectKm != null "> and c.expect_km = #{expectKm}</if>
            <if test="series != null  and series != ''"> and c.series like concat('%', #{series}, '%')</if>
            <if test="brand != null  and brand != ''"> and c.brand like concat('%', #{brand}, '%')</if>
            <if test="isRent != null  and isRent != ''"> and c.isRent = #{isRent}</if>
            <if test="dayCost != null  and dayCost != ''"> and c.day_cost = #{dayCost}</if>
            <if test="remark != null  and remark != ''"> and c.remark = #{remark}</if>
            <if test="status != null  and status != ''"> and c.status = #{status}</if>
        </where>
    </select>

    <select id="selectCmcCarByCarId" parameterType="Integer" resultMap="CmcCarResult">
        <include refid="selectCmcCarVo"/>
        where car_id = #{carId}
    </select>

    <insert id="insertCmcCar" parameterType="CmcCar" useGeneratedKeys="true" keyProperty="carId">
        insert into cmc_car
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="licensePlate != null">license_plate,</if>
            <if test="driver != null">driver,</if>
            <if test="acquisitionTime != null">acquisition_time,</if>
            <if test="cost != null">cost,</if>
            <if test="expectLife != null">expect_life,</if>
            <if test="expectKm != null">expect_km,</if>
            <if test="series != null">series,</if>
            <if test="brand != null">brand,</if>
            <if test="isRent != null">is_rent,</if>
            <if test="dayCost != null">day_cost,</if>
            <if test="remark != null">remark,</if>
            <if test="status != null">status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="licensePlate != null">#{licensePlate},</if>
            <if test="driver != null">#{driver},</if>
            <if test="acquisitionTime != null">#{acquisitionTime},</if>
            <if test="cost != null">#{cost},</if>
            <if test="expectLife != null">#{expectLife},</if>
            <if test="expectKm != null">#{expectKm},</if>
            <if test="series != null">#{series},</if>
            <if test="brand != null">#{brand},</if>
            <if test="isRent != null">#{isRent},</if>
            <if test="dayCost != null">#{dayCost},</if>
            <if test="remark != null">#{remark},</if>
            <if test="status != null">#{status},</if>
        </trim>
    </insert>

    <update id="updateCmcCar" parameterType="CmcCar">
        update cmc_car
        <trim prefix="SET" suffixOverrides=",">
            <if test="licensePlate != null">license_plate = #{licensePlate},</if>
            <if test="driver != null">driver = #{driver},</if>
            <if test="acquisitionTime != null">acquisition_time = #{acquisitionTime},</if>
            <if test="cost != null">cost = #{cost},</if>
            <if test="expectLife != null">expect_life = #{expectLife},</if>
            <if test="expectKm != null">expect_km = #{expectKm},</if>
            <if test="series != null">series = #{series},</if>
            <if test="brand != null">brand = #{brand},</if>
            <if test="isRent != null">is_rent = #{isRent},</if>
            <if test="dayCost != null">day_cost = #{dayCost},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where car_id = #{carId}
    </update>

    <delete id="deleteCmcCarByCarId" parameterType="Long">
        delete from cmc_car where car_id = #{carId}
    </delete>

    <delete id="deleteCmcCarByCarIds" parameterType="String">
        delete from cmc_car where car_id in
        <foreach item="carId" collection="array" open="(" separator="," close=")">
            #{carId}
        </foreach>
    </delete>
</mapper>