<?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.DriverMapper">

    <resultMap id="driverResultMap" type="org.springblade.transport.pojo.vo.DriverVO">
        <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="driver_name" property="driverName"/>
        <result column="id_card_no" property="idCardNo"/>
        <result column="birthday" property="birthday"/>
        <result column="gender" property="gender"/>
        <result column="nation" property="nation"/>
        <result column="education" property="education"/>
        <result column="address_region" property="addressRegion"/>
        <result column="address" property="address"/>
        <result column="posts" property="posts"/>
        <result column="id_card_front" property="idCardFront"/>
        <result column="id_card_back" property="idCardBack"/>
        <result column="head_photo" property="headPhoto"/>
        <result column="driving_type" property="drivingType"/>
        <result column="driving_license_no" property="drivingLicenseNo"/>
        <result column="driving_license_start_date" property="drivingLicenseStartDate"/>
        <result column="driving_license_end_date" property="drivingLicenseEndDate"/>
        <result column="driving_license_long_term" property="drivingLicenseLongTerm"/>
        <result column="driving_license_front" property="drivingLicenseFront"/>
        <result column="driving_license_back" property="drivingLicenseBack"/>
        <result column="qualification_type" property="qualificationType"/>
        <result column="qualification_no" property="qualificationNo"/>
        <result column="qualification_end_date" property="qualificationEndDate"/>
        <result column="qualification_long_term" property="qualificationLongTerm"/>
        <result column="qualification_front" property="qualificationFront"/>
        <result column="qualification_back" property="qualificationBack"/>
        <result column="driver_type" property="driverType"/>
        <result column="mobile" property="mobile"/>
        <result column="contact_relation" property="contactRelation"/>
        <result column="organization_name" property="organizationName"/>
        <result column="emergency_contact_name" property="emergencyContactName"/>
        <result column="emergency_contact_mobile" property="emergencyContactMobile"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <sql id="BaseColumn">
        id,
        tenant_id,
        create_user,
        create_dept,
        create_time,
        update_user,
        update_time,
        status,
        is_deleted,
        driver_name,
        id_card_no,
        birthday,
        gender,
        nation,
        education,
        address_region,
        address,
        posts,
        id_card_front,
        id_card_back,
        head_photo,
        driving_type,
        driving_license_no,
        driving_license_start_date,
        driving_license_end_date,
        driving_license_long_term,
        driving_license_front,
        driving_license_back,
        qualification_type,
        qualification_no,
        qualification_end_date,
        qualification_long_term,
        qualification_front,
        qualification_back,
        driver_type,
        mobile,
        contact_relation,
        organization_name,
        emergency_contact_name,
        emergency_contact_mobile,
        remark
    </sql>

    <sql id="ExpiryExpiredCondition">
        (
            ((driving_license_long_term IS NULL OR driving_license_long_term != 1) AND driving_license_end_date &lt; #{driver.today})
            OR ((qualification_long_term IS NULL OR qualification_long_term != 1) AND qualification_end_date &lt; #{driver.today})
        )
    </sql>

    <sql id="ExpiryWithin30Condition">
        (
            NOT <include refid="ExpiryExpiredCondition"/>
            AND (
                ((driving_license_long_term IS NULL OR driving_license_long_term != 1) AND driving_license_end_date BETWEEN #{driver.today} AND #{driver.warningDate})
                OR ((qualification_long_term IS NULL OR qualification_long_term != 1) AND qualification_end_date BETWEEN #{driver.today} AND #{driver.warningDate})
            )
        )
    </sql>

    <sql id="QueryCondition">
        is_deleted = 0
        <if test="driver.driverName != null and driver.driverName != ''">
            <bind name="driverNameLike" value="'%' + driver.driverName + '%'"/>
            AND driver_name LIKE #{driverNameLike}
        </if>
        <if test="driver.mobile != null and driver.mobile != ''">
            <bind name="mobileLike" value="'%' + driver.mobile + '%'"/>
            AND mobile LIKE #{mobileLike}
        </if>
        <if test="driver.idCardNo != null and driver.idCardNo != ''">
            <bind name="idCardNoLike" value="'%' + driver.idCardNo + '%'"/>
            AND id_card_no LIKE #{idCardNoLike}
        </if>
        <if test="driver.drivingType != null and driver.drivingType != ''">
            AND driving_type = #{driver.drivingType}
        </if>
        <if test="driver.driverType != null and driver.driverType != ''">
            AND driver_type = #{driver.driverType}
        </if>
        <if test="driver.organizationName != null and driver.organizationName != ''">
            <bind name="organizationNameLike" value="'%' + driver.organizationName + '%'"/>
            AND organization_name LIKE #{organizationNameLike}
        </if>
        <if test="driver.status != null">
            AND status = #{driver.status}
        </if>
        <if test="driver.expireStatus != null and driver.expireStatus == 'within30'">
            AND <include refid="ExpiryWithin30Condition"/>
        </if>
        <if test="driver.expireStatus != null and driver.expireStatus == 'expired'">
            AND <include refid="ExpiryExpiredCondition"/>
        </if>
    </sql>

    <select id="selectDriverPage" resultMap="driverResultMap">
        SELECT
            <include refid="BaseColumn"/>
        FROM
            blade_transport_driver
        WHERE
            <include refid="QueryCondition"/>
        ORDER BY create_time DESC
    </select>

    <select id="selectExpiryStat" resultType="org.springblade.transport.pojo.vo.DriverExpiryStatVO">
        SELECT
            COUNT(1) AS total,
            SUM(CASE WHEN <include refid="ExpiryWithin30Condition"/> THEN 1 ELSE 0 END) AS within30,
            SUM(CASE WHEN <include refid="ExpiryExpiredCondition"/> THEN 1 ELSE 0 END) AS expired
        FROM
            blade_transport_driver
        WHERE
            <include refid="QueryCondition"/>
    </select>

</mapper>
