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

    <resultMap id="transportShipResultMap" type="org.springblade.transport.pojo.vo.TransportShipVO">
        <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="ship_name" property="shipName"/>
        <result column="ship_identifier_no" property="shipIdentifierNo"/>
        <result column="organization_name" property="organizationName"/>
        <result column="ship_inspection_no" property="shipInspectionNo"/>
        <result column="ship_type" property="shipType"/>
        <result column="nationality_cert_end_date" property="nationalityCertEndDate"/>
        <result column="nationality_cert_long_term" property="nationalityCertLongTerm"/>
        <result column="nationality_cert_image" property="nationalityCertImage"/>
        <result column="safe_manning_cert_end_date" property="safeManningCertEndDate"/>
        <result column="safe_manning_cert_long_term" property="safeManningCertLongTerm"/>
        <result column="safe_manning_cert_image" property="safeManningCertImage"/>
        <result column="business_transport_cert_end_date" property="businessTransportCertEndDate"/>
        <result column="business_transport_cert_long_term" property="businessTransportCertLongTerm"/>
        <result column="business_transport_cert_image" property="businessTransportCertImage"/>
        <result column="lease_end_date" property="leaseEndDate"/>
        <result column="lease_long_term" property="leaseLongTerm"/>
        <result column="lease_contract_image" property="leaseContractImage"/>
        <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,
        ship_name,
        ship_identifier_no,
        organization_name,
        ship_inspection_no,
        ship_type,
        nationality_cert_end_date,
        nationality_cert_long_term,
        nationality_cert_image,
        safe_manning_cert_end_date,
        safe_manning_cert_long_term,
        safe_manning_cert_image,
        business_transport_cert_end_date,
        business_transport_cert_long_term,
        business_transport_cert_image,
        lease_end_date,
        lease_long_term,
        lease_contract_image,
        remark
    </sql>

    <sql id="ExpiryWithin30Condition">
        (
            (nationality_cert_long_term != 1 AND nationality_cert_end_date BETWEEN #{ship.today} AND #{ship.warningDate})
            OR (safe_manning_cert_long_term != 1 AND safe_manning_cert_end_date BETWEEN #{ship.today} AND #{ship.warningDate})
            OR (business_transport_cert_long_term != 1 AND business_transport_cert_end_date BETWEEN #{ship.today} AND #{ship.warningDate})
            OR (lease_long_term != 1 AND lease_end_date BETWEEN #{ship.today} AND #{ship.warningDate})
        )
    </sql>

    <sql id="ExpiryExpiredCondition">
        (
            (nationality_cert_long_term != 1 AND nationality_cert_end_date &lt; #{ship.today})
            OR (safe_manning_cert_long_term != 1 AND safe_manning_cert_end_date &lt; #{ship.today})
            OR (business_transport_cert_long_term != 1 AND business_transport_cert_end_date &lt; #{ship.today})
            OR (lease_long_term != 1 AND lease_end_date &lt; #{ship.today})
        )
    </sql>

    <sql id="QueryCondition">
        is_deleted = 0
        <if test="ship.shipName != null and ship.shipName != ''">
            <bind name="shipNameLike" value="'%' + ship.shipName + '%'"/>
            AND ship_name LIKE #{shipNameLike}
        </if>
        <if test="ship.shipIdentifierNo != null and ship.shipIdentifierNo != ''">
            <bind name="shipIdentifierNoLike" value="'%' + ship.shipIdentifierNo + '%'"/>
            AND ship_identifier_no LIKE #{shipIdentifierNoLike}
        </if>
        <if test="ship.organizationName != null and ship.organizationName != ''">
            <bind name="organizationNameLike" value="'%' + ship.organizationName + '%'"/>
            AND organization_name LIKE #{organizationNameLike}
        </if>
        <if test="ship.shipInspectionNo != null and ship.shipInspectionNo != ''">
            <bind name="shipInspectionNoLike" value="'%' + ship.shipInspectionNo + '%'"/>
            AND ship_inspection_no LIKE #{shipInspectionNoLike}
        </if>
        <if test="ship.shipType != null and ship.shipType != ''">
            AND ship_type = #{ship.shipType}
        </if>
        <if test="ship.status != null">
            AND status = #{ship.status}
        </if>
        <if test="ship.expireStatus != null and ship.expireStatus == 'within30'">
            AND <include refid="ExpiryWithin30Condition"/>
        </if>
        <if test="ship.expireStatus != null and ship.expireStatus == 'expired'">
            AND <include refid="ExpiryExpiredCondition"/>
        </if>
    </sql>

    <select id="selectTransportShipPage" resultMap="transportShipResultMap">
        SELECT
            <include refid="BaseColumn"/>
        FROM
            blade_transport_ship
        WHERE
            <include refid="QueryCondition"/>
        ORDER BY create_time DESC
    </select>

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

</mapper>
