<?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.trade.service.salesorder.mapper.SalesOrderLogisticsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.trade.service.salesorder.domain.SalesOrderLogistics">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="order_id" property="orderId" />
        <result column="logistics_company" property="logisticsCompany" />
        <result column="deliver_time" property="deliverTime" />
        <result column="from_port" property="fromPort" />
        <result column="dest_port" property="destPort" />
        <result column="lading_bill_no" property="ladingBillNo" />
        <result column="lading_bill_file_id" property="ladingBillFileId" />
        <result column="estimated_arrival_time" property="estimatedArrivalTime" />
        <result column="deliver_remark" property="deliverRemark" />
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        order_id, logistics_company, deliver_time, from_port, dest_port, lading_bill_no, lading_bill_file_id, estimated_arrival_time, deliver_remark, gmt_create, gmt_modified
    </sql>
    <select id="queryListBySearch" resultType="com.trade.service.salesorder.vo.SalesOrderLogisticsVO">
        SELECT
        a.logistics_company,
        a.deliver_time,
        a.from_port,
        a.dest_port,
        a.lading_bill_no,
        b.order_no,
        c.entity_name as importer_name,
        e.platform_name as agency_enterprise_name,
        f.identifier_no AS customer_identifier_no,
        g.name AS enterprise_name
        FROM
        sales_order_logistics a
        join sales_order b on a.order_id = b.id
        left join importer_entity c on b.importer_id = c.id
        left join sales_contract d on b.sale_contract_id = d.id
        left join trading_platform e on d.agency_cert_no = e.cert_no
        left join customer_user f on b.customer_user_id = f.id
        left join enterprise g on b.customer_user_id = g.customer_user_id
        WHERE
        a.deleted = 0
        <if test="customerUserId!= null and customerUserId!= ''">
            AND b.customer_user_id = #{customerUserId}
        </if>
        <if test="orderNo!= null and orderNo!= ''">
            AND b.order_no = #{orderNo}
        </if>
        <if test="logisticsCompany!= null and logisticsCompany!= ''">
            AND a.logistics_company LIKE CONCAT('%', #{logisticsCompany}, '%')
        </if>
        <if test="agencyCertNo!= null and agencyCertNo!= ''">
            AND d.agency_cert_no = #{agencyCertNo}
        </if>
        <if test="destPort!= null and destPort!= ''">
            AND a.dest_port = #{destPort}
        </if>
        <if test="ladingBillNo!= null and ladingBillNo!= ''">
            AND a.lading_bill_no = #{ladingBillNo}
        </if>
        <if test="deliverTimeStart!= null">
            AND a.deliver_time &gt;= #{deliverTimeStart}
        </if>
        <if test="deliverTimeEnd!= null">
            AND a.deliver_time &lt;= #{deliverTimeEnd}
        </if>
        <if test="userName!= null and userName!= ''">
            AND f.identifier_no = #{userName} or g.name like CONCAT('%',#{userName},'%')
        </if>
        ORDER BY id DESC
    </select>
    <select id="getDetailByOrderNo" resultType="com.trade.service.salesorder.vo.SalesOrderLogisticsDtlVO">
        SELECT
            a.id,
            a.logistics_company,
            a.deliver_time,
            a.from_port,
            a.dest_port,
            a.estimated_arrival_time,
            a.lading_bill_no,
            a.lading_bill_file_id,
            a.deliver_remark,
            b.order_no,
            d.contract_no,
            e.platform_name as agency_enterprise_name
        FROM
            sales_order_logistics a
                join sales_order b on a.order_id = b.id
                left join sales_contract d on b.sale_contract_id = d.id
                left join trading_platform e on d.agency_cert_no = e.cert_no
        WHERE
            a.deleted = 0
          and b.order_no = #{orderNo}
    </select>

</mapper>
