<?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.trade.purchaseorder.mapper.PurchaseOrderLogisticsMapper">

    <resultMap id="BaseResultMap" type="com.trade.service.trade.purchaseorder.domain.PurchaseOrderLogistics">
        <id column="arrive_time" property="arriveTime"/>
        <id column="company_name" property="companyName"/>
        <id column="deleted" property="deleted"/>
        <id column="delivery_time" property="deliveryTime"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="gmt_modified" property="gmtModified"/>
        <id column="id" property="id"/>
        <id column="logistics_order_number" property="logisticsOrderNumber"/>
        <id column="transport_invoice_number" property="transportInvoiceNumber"/>
        <id column="kprq" property="kprq"/>
        <id column="purchase_order_id" property="purchaseOrderId"/>
        <id column="remark" property="remark"/>
        <!--<id column="transport_certificate_file_id" property="transportCertificateFileId" /> -->
    </resultMap>
    <resultMap id="VoResultMap" type="com.trade.service.trade.purchaseorder.vo.PurchaseOrderLogisticsVo">
        <id column="id" property="id"/>
        <id column="arrive_time" property="arriveTime"/>
        <id column="company_name" property="companyName"/>
        <id column="delivery_time" property="deliveryTime"/>
        <id column="logistics_order_number" property="logisticsOrderNumber"/>
        <id column="transport_invoice_number" property="transportInvoiceNumber"/>
        <id column="kprq" property="kprq"/>
        <id column="remark" property="remark"/>
        <!--<id column="transport_certificate_file_id" property="transportCertificateFileId" /> -->
    </resultMap>

    <resultMap id="VoPageResultMap" type="com.trade.service.trade.purchaseorder.vo.PurchaseOrderLogisticsPageVo">
        <id column="id" property="id"/>
        <id column="arrive_time" property="arriveTime"/>
        <id column="company_name" property="companyName"/>
        <id column="delivery_time" property="deliveryTime"/>
        <id column="logistics_order_number" property="logisticsOrderNumber"/>
        <id column="remark" property="remark"/>
        <id column="purchase_order_no" property="purchaseOrderNo"/>
        <id column="entity_name" property="vendorEntityName"/>
        <id column="platform_name" property="agencyName"/>
        <id column="enterprise_name" property="enterpriseName"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
        <id column="purchase_contract_no" property="purchaseContractNo"/>
        <id column="contract_name" property="purchaseContractName"/>
        <id column="transport_invoice_number" property="transportInvoiceNumber"/>
        <id column="kprq" property="kprq"/>
    </resultMap>

    <sql id="Base_Column_List">
        arrive_time
        , company_name, deleted, delivery_time, gmt_create, gmt_modified, id, logistics_order_number, purchase_order_id, remark, transport_invoice_number, kprq
    </sql>

    <select id="selectByPurchaseOrderId" resultMap="VoResultMap">
        select id,
               arrive_time,
               company_name,
               delivery_time,
               logistics_order_number,
               transport_invoice_number,
               remark,
               kprq
        from purchase_order_logistics
        where purchase_order_id = #{purchaseOrderId}
          and deleted = 0 limit 1
    </select>

    <select id="selectOrderLogisticsByPage" resultMap="VoPageResultMap">
        select t.id,
        t.arrive_time,
        t.company_name,
        t.delivery_time,
        t.logistics_order_number,
        t.transport_invoice_number,
        t.remark,
        t.kprq,
        po.order_no AS purchase_order_no,
        v.entity_name,
        p.platform_name,
        e.name AS enterprise_name,
        e.customer_identifier_no,
        po.purchase_contract_no,
        pc.contract_name
        from purchase_order po
        left join enterprise e on po.customer_user_id = e.customer_id
        left join purchase_order_logistics t on po.id = t.purchase_order_id
        left join vendor_entity v on po.entity_no = v.entity_no
        left join trading_platform p on po.agency_cert_no = p.cert_no
        left join purchase_contract pc on po.purchase_contract_no = pc.contract_no
        where po.deleted = 0
        <if test="customerUserId != null">
            AND po.customer_user_id = #{customerUserId}
        </if>
        <if test="logisticsOrderNumber != null and logisticsOrderNumber != ''">
            AND t.logistics_order_number = #{logisticsOrderNumber}
        </if>
        <if test="companyName != null and companyName != ''">
            AND t.company_name = #{companyName}
        </if>
        <if test="transportInvoiceNumber != null and transportInvoiceNumber != ''">
            AND t.transport_invoice_number = #{transportInvoiceNumber}
        </if>
        <if test="purchaseOrderNo != null and purchaseOrderNo != ''">
            AND po.order_no = #{purchaseOrderNo}
        </if>
        <if test="customerUserInfo != null and customerUserInfo != ''">
            AND e.name LIKE CONCAT ('%',#{customerUserInfo},'%') OR e.customer_identifier_no = #{customerUserInfo}
        </if>
        <if test="vendorEntityNo != null and vendorEntityNo != ''">
            AND po.entity_no = #{vendorEntityNo}
        </if>
        <if test="agencyCertNo != null and agencyCertNo != ''">
            AND po.agency_cert_no = #{agencyCertNo}
        </if>
        <if test="point == 0 ">
            AND (po.agency_cert_no is not null and po.agency_cert_no != '')
        </if>
        <if test="purchaseContractInfo != null and purchaseContractInfo != ''">
            AND pc.contract_name LIKE CONCAT ('%',#{purchaseContractInfo},'%') OR pc.contract_no =
            #{purchaseContractInfo}
        </if>
        <if test="timeStart != null">
            AND t.delivery_time <![CDATA[ >= ]]> #{timeStart}
        </if>
        <if test="timeEnd != null">
            AND t.delivery_time <![CDATA[ <= ]]> #{timeEnd}
        </if>
        <if test="null != customerUserIdList and customerUserIdList.size()>0">
            AND po.customer_user_id IN
            <foreach collection="customerUserIdList" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY t.delivery_time DESC

    </select>


    <resultMap id="DocumenPageResultMap"
               type="com.trade.service.trade.purchaseorder.vo.PurchaseOrderLogisticsDocumentVo">
        <id column="id" property="id"/>
        <id column="company_name" property="companyName"/>
        <id column="delivery_time" property="deliveryTime"/>
        <id column="logistics_order_number" property="logisticsOrderNumber"/>
        <id column="purchase_order_no" property="purchaseOrderNo"/>
        <id column="transport_invoice_number" property="transportInvoiceNumber"/>
        <id column="sales_order_no" property="salesOrderNo"/>
    </resultMap>

    <select id="selectPurchaseOrderLogisticsDocument"
            parameterType="com.trade.service.trade.purchaseorder.dto.PurchaseOrderLogisticsDocumentSearchDto"
            resultMap="DocumenPageResultMap">
        select t.id,
        t.company_name,
        t.delivery_time,
        t.logistics_order_number,
        t.transport_invoice_number,
        po.order_no AS purchase_order_no,
        so.order_no as sales_order_no
        from purchase_order_logistics t
        left join purchase_order po on po.id = t.purchase_order_id
        left join purchase_contract pc on po.purchase_contract_no = pc.contract_no
        left join sales_order so on so.id = po.sales_order_id
        where po.deleted = 0
        <if test="customerUserId != null">
            AND po.customer_user_id = #{customerUserId}
        </if>
        <if test="purchaseContractNo != null and purchaseContractNo != ''">
            AND pc.contract_no = #{purchaseContractNo}
        </if>
        <if test="logisticsOrderNumber != null and logisticsOrderNumber != ''">
            AND t.logistics_order_number = #{logisticsOrderNumber}
        </if>
        <if test="companyName != null and companyName != ''">
            AND t.company_name = #{companyName}
        </if>
        <if test="transportInvoiceNumber != null and transportInvoiceNumber != ''">
            AND t.transport_invoice_number = #{transportInvoiceNumber}
        </if>
        <if test="purchaseOrderNo != null and purchaseOrderNo != ''">
            AND po.order_no = #{purchaseOrderNo}
        </if>
        <if test="salesOrderNo != null and salesOrderNo != ''">
            AND so.order_no = #{salesOrderNo}
        </if>
        <if test="timeStart != null">
            AND t.delivery_time <![CDATA[ >= ]]> #{timeStart}
        </if>
        <if test="timeEnd != null">
            AND t.delivery_time <![CDATA[ <= ]]> #{timeEnd}
        </if>
        ORDER BY t.delivery_time DESC

    </select>
    <select id="selectByPurchaseContractNo"
            resultMap="VoResultMap">
        select t.id,
               t.arrive_time,
               t.company_name,
               t.delivery_time,
               t.logistics_order_number,
               t.transport_invoice_number,
               t.remark,
               t.kprq
        from purchase_order_logistics t
                 left join purchase_order po on po.id = t.purchase_order_id
                 left join purchase_contract pc on po.purchase_contract_no = pc.contract_no
        where t.deleted = 0
          and pc.purchase_contract_no = #{purchaseContractNo}
    </select>
</mapper>
