<?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="org.springblade.transport.mapper.MileageRecordMapper">

    <resultMap id="mileageRecordResultMap" type="org.springblade.transport.pojo.vo.MileageRecordVO">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="vehicle_type" property="vehicleType"/>
        <result column="vehicle_no" property="vehicleNo"/>
        <result column="previous_month_mileage" property="previousMonthMileage"/>
        <result column="current_month_mileage" property="currentMonthMileage"/>
        <result column="monthly_mileage" property="monthlyMileage"/>
        <result column="total_mileage" property="totalMileage"/>
        <result column="mileage_unit" property="mileageUnit"/>
        <result column="attachments" property="attachments"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <select id="selectMileageRecordPage" resultMap="mileageRecordResultMap">
        SELECT
            id,
            tenant_id,
            create_user,
            create_dept,
            create_time,
            update_user,
            update_time,
            status,
            is_deleted,
            vehicle_type,
            vehicle_no,
            CASE WHEN previous_month_mileage &lt; 0 THEN 0 ELSE previous_month_mileage END AS previous_month_mileage,
            CASE WHEN current_month_mileage &lt; 0 THEN 0 ELSE current_month_mileage END AS current_month_mileage,
            CASE WHEN monthly_mileage &lt; 0 THEN 0 ELSE monthly_mileage END AS monthly_mileage,
            CASE WHEN total_mileage &lt; 0 THEN 0 ELSE total_mileage END AS total_mileage,
            mileage_unit,
            attachments,
            remark
        FROM
            blade_mileage_record
        WHERE
            is_deleted = 0
        <if test="mileageRecord.createDept != null">
            AND create_dept = #{mileageRecord.createDept}
        </if>
        <if test="mileageRecord.vehicleType != null and mileageRecord.vehicleType != ''">
            AND vehicle_type = #{mileageRecord.vehicleType}
        </if>
        <if test="mileageRecord.vehicleNo != null and mileageRecord.vehicleNo != ''">
            <bind name="vehicleNoLike" value="'%' + mileageRecord.vehicleNo + '%'"/>
            AND vehicle_no LIKE #{vehicleNoLike}
        </if>
        <if test="mileageRecord.totalMileageStart != null">
            AND total_mileage &gt;= #{mileageRecord.totalMileageStart}
        </if>
        <if test="mileageRecord.totalMileageEnd != null">
            AND total_mileage &lt;= #{mileageRecord.totalMileageEnd}
        </if>
        <if test="mileageRecord.createTimeStart != null and mileageRecord.createTimeStart != ''">
            AND create_time &gt;= #{mileageRecord.createTimeStart}
        </if>
        <if test="mileageRecord.createTimeEnd != null and mileageRecord.createTimeEnd != ''">
            AND create_time &lt;= #{mileageRecord.createTimeEnd}
        </if>
        ORDER BY create_time DESC
    </select>

</mapper>
