<?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.hotwj.platform.resourceManagement.driverInfo.mapper.DriverPageMapper">

    <select id="selectDriverPage" resultType="com.hotwj.platform.resourceManagement.driverInfo.domain.vo.DriverPageVo">
        SELECT
        d.id AS driverId,
        c.company_name AS companyName,
        d.name AS driverName,
        d.phone AS phone,
        d.id_card_no AS idCard,
        (CASE WHEN d.status = 1 THEN 1 ELSE 0 END) AS isActive,
        d.entry_date AS hireDate,
        d.current_address AS currentAddress,
        IFNULL(v.plate_number, d.plate_number) AS plateNumber,

        d.driver_license_no AS licenseNumber,
        d.driver_license_archive_no AS licenseFileNo,
        d.driver_license_vehicle_type AS allowedVehicleType,
        d.driver_license_first_issue_date AS licenseFirstIssueDate,
        d.driver_license_expire_date AS licenseExpireDate,
        d.driver_license_long_term as driverLicenseLongTerm,

        d.qualification_no AS qualificationNo,
        d.qualification_type AS qualificationType,
        d.qualification_first_issue_date AS qualificationFirstIssueDate,
        d.qualification_valid_end_date AS qualificationExpireDate
        FROM hot_driver d
        LEFT JOIN sys_company c ON d.company_id = c.id
        LEFT JOIN hot_vehicle v ON d.vehicle_id = v.id
        <where>
            d.is_deleted = 0
            AND c.region_code = #{districtCode}
            <if test="bo.companyName != null and bo.companyName != ''">
                AND c.company_name LIKE CONCAT('%', #{bo.companyName}, '%')
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND d.name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND (v.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%') OR d.plate_number LIKE CONCAT('%',
                #{bo.plateNumber}, '%'))
            </if>
            <if test="bo.driverPhone != null and bo.driverPhone != ''">
                AND d.phone LIKE CONCAT('%', #{bo.driverPhone}, '%')
            </if>
            <if test="bo.idCard != null and bo.idCard != ''">
                AND d.id_card_no LIKE CONCAT('%', #{bo.idCard}, '%')
            </if>
            <if test="bo.isActive != null">
                <if test="bo.isActive == true">
                    AND d.status = 1
                </if>
                <if test="bo.isActive == false">
                    AND d.status != 1
                </if>
            </if>
            <if test="bo.auditStatus != null">
                AND d.audit_status = #{bo.auditStatus}
            </if>
        </where>
        ORDER BY d.create_time DESC
    </select>
</mapper>
