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

    <resultMap id="transportVehicleResultMap" type="org.springblade.transport.pojo.vo.TransportVehicleVO">
        <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="organization_name" property="organizationName"/>
        <result column="plate_no" property="plateNo"/>
        <result column="plate_color" property="plateColor"/>
        <result column="vehicle_type" property="vehicleType"/>
        <result column="outer_length" property="outerLength"/>
        <result column="outer_width" property="outerWidth"/>
        <result column="outer_height" property="outerHeight"/>
        <result column="approved_load_kg" property="approvedLoadKg"/>
        <result column="traction_mass_kg" property="tractionMassKg"/>
        <result column="business_relation" property="businessRelation"/>
        <result column="energy_type" property="energyType"/>
        <result column="compulsory_scrap_date" property="compulsoryScrapDate"/>
        <result column="compulsory_scrap_long_term" property="compulsoryScrapLongTerm"/>
        <result column="customs_record_no" property="customsRecordNo"/>
        <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_image" property="drivingLicenseImage"/>
        <result column="driving_license_main_back" property="drivingLicenseMainBack"/>
        <result column="driving_license_vice_front" property="drivingLicenseViceFront"/>
        <result column="driving_license_vice_back" property="drivingLicenseViceBack"/>
        <result column="road_transport_cert_no" property="roadTransportCertNo"/>
        <result column="road_transport_cert_start_date" property="roadTransportCertStartDate"/>
        <result column="road_transport_cert_end_date" property="roadTransportCertEndDate"/>
        <result column="road_transport_cert_long_term" property="roadTransportCertLongTerm"/>
        <result column="road_transport_cert_image" property="roadTransportCertImage"/>
        <result column="annual_review_end_date" property="annualReviewEndDate"/>
        <result column="annual_review_long_term" property="annualReviewLongTerm"/>
        <result column="registration_no" property="registrationNo"/>
        <result column="registration_date" property="registrationDate"/>
        <result column="registration_image" property="registrationImage"/>
        <result column="remark" property="remark"/>
        <result column="certification_status" property="certificationStatus"/>
        <result column="certification_reject_reason" property="certificationRejectReason"/>
    </resultMap>

    <sql id="BaseColumn">
        id,
        tenant_id,
        create_user,
        create_dept,
        create_time,
        update_user,
        update_time,
        status,
        is_deleted,
        organization_name,
        plate_no,
        plate_color,
        vehicle_type,
        outer_length,
        outer_width,
        outer_height,
        approved_load_kg,
        traction_mass_kg,
        business_relation,
        energy_type,
        compulsory_scrap_date,
        compulsory_scrap_long_term,
        customs_record_no,
        driving_license_no,
        driving_license_start_date,
        driving_license_end_date,
        driving_license_long_term,
        driving_license_image,
        driving_license_main_back,
        driving_license_vice_front,
        driving_license_vice_back,
        road_transport_cert_no,
        road_transport_cert_start_date,
        road_transport_cert_end_date,
        road_transport_cert_long_term,
        road_transport_cert_image,
        annual_review_end_date,
        annual_review_long_term,
        registration_no,
        registration_date,
        registration_image,
        remark
        , certification_status
        , certification_reject_reason
    </sql>

    <sql id="ExpiryWithin30Condition">
        (
            (driving_license_long_term != 1 AND driving_license_end_date BETWEEN #{vehicle.today} AND #{vehicle.warningDate})
            OR (road_transport_cert_long_term != 1 AND road_transport_cert_end_date BETWEEN #{vehicle.today} AND #{vehicle.warningDate})
        )
    </sql>

    <sql id="ExpiryExpiredCondition">
        (
            (driving_license_long_term != 1 AND driving_license_end_date &lt; #{vehicle.today})
            OR (road_transport_cert_long_term != 1 AND road_transport_cert_end_date &lt; #{vehicle.today})
        )
    </sql>

    <sql id="QueryCondition">
        is_deleted = 0
        <if test="vehicle.organizationName != null and vehicle.organizationName != ''">
            <bind name="organizationNameLike" value="'%' + vehicle.organizationName + '%'"/>
            AND organization_name LIKE #{organizationNameLike}
        </if>
        <if test="vehicle.plateNo != null and vehicle.plateNo != ''">
            <bind name="plateNoLike" value="'%' + vehicle.plateNo + '%'"/>
            AND plate_no LIKE #{plateNoLike}
        </if>
        <if test="vehicle.vehicleType != null and vehicle.vehicleType != ''">
            AND vehicle_type = #{vehicle.vehicleType}
        </if>
        <if test="vehicle.businessRelation != null and vehicle.businessRelation != ''">
            AND business_relation = #{vehicle.businessRelation}
        </if>
        <if test="vehicle.energyType != null and vehicle.energyType != ''">
            AND energy_type = #{vehicle.energyType}
        </if>
        <if test="vehicle.status != null">
            AND status = #{vehicle.status}
        </if>
        <if test="vehicle.certificationStatus != null">
            AND certification_status = #{vehicle.certificationStatus}
        </if>
        <if test="vehicle.expireStatus != null and vehicle.expireStatus == 'within30'">
            AND <include refid="ExpiryWithin30Condition"/>
        </if>
        <if test="vehicle.expireStatus != null and vehicle.expireStatus == 'expired'">
            AND <include refid="ExpiryExpiredCondition"/>
        </if>
    </sql>

    <select id="selectTransportVehiclePage" resultMap="transportVehicleResultMap">
        SELECT
            <include refid="BaseColumn"/>
        FROM
            blade_transport_vehicle
        WHERE
            <include refid="QueryCondition"/>
        ORDER BY create_time DESC
    </select>

    <select id="selectExpiryStat" resultType="org.springblade.transport.pojo.vo.TransportVehicleExpiryStatVO">
        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_vehicle
        WHERE
            <include refid="QueryCondition"/>
    </select>

</mapper>
