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

    <resultMap id="BaseResultMap" type="com.trade.service.trade.purchaseorder.domain.PurchaseOrderInvoice">
        <id column="amount" property="amount"/>
        <id column="deleted" property="deleted"/>
        <id column="digital_invoice_number" property="digitalInvoiceNumber"/>
        <id column="file_id" property="fileId"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="gmt_modified" property="gmtModified"/>
        <id column="id" property="id"/>
        <id column="invoice_code" property="invoiceCode"/>
        <id column="invoice_number" property="invoiceNumber"/>
        <id column="invoice_type" property="invoiceType"/>
        <id column="invoicing_date" property="invoicingDate"/>
        <id column="invoice_status" property="invoiceStatus"/>
        <id column="order_id" property="orderId"/>
        <id column="tax_amount" property="taxAmount"/>
        <id column="certification_date" property="certificationDate"/>
        <id column="bz" property="bz"/>
        <id column="purchase_voucher_no" property="purchaseVoucherNo"/>
        <id column="source" property="source"/>
        <id column="pull_state" property="pullState"/>
    </resultMap>
    <sql id="Base_Column_List">
        amount
        , deleted, digital_invoice_number, file_id, gmt_create, gmt_modified, id, invoice_code, invoice_number, invoice_type, invoicing_date, certification_date, invoice_status, order_id, tax_amount, bz, purchase_voucher_no, source, pull_state
    </sql>

    <resultMap id="VoResultMap" type="com.trade.service.trade.purchaseorder.vo.PurchaseOrderInvoiceVo">
        <id column="amount" property="amount"/>
        <id column="digital_invoice_number" property="digitalInvoiceNumber"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
        <id column="enterprise_name" property="enterpriseName"/>
        <id column="currency" property="currency"/>
        <id column="id" property="id"/>
        <id column="invoice_code" property="invoiceCode"/>
        <id column="invoice_number" property="invoiceNumber"/>
        <id column="invoice_type" property="invoiceType"/>
        <id column="invoicing_date" property="invoicingDate"/>
        <id column="platform_name" property="agencyEnterpriseName"/>
        <id column="order_no" property="orderNo"/>
        <id column="tax_amount" property="taxAmount"/>
        <id column="purchase_voucher_no" property="purchaseVoucherNo"/>
        <id column="entity_name" property="vendorEntityName"/>
    </resultMap>
    <select id="selectByCondition"
            parameterType="com.trade.service.trade.purchaseorder.dto.PurchaseOrderInvoiceSearchDto"
            resultMap="VoResultMap">
        select
        t.amount, po.currency, po.customer_user_id, po.order_no, t.invoicing_date, t.digital_invoice_number,
        t.id, t.invoice_type, t.invoice_code, t.invoice_number, t.tax_amount, t.bz, t.certification_date,
        t.purchase_voucher_no,
        e.name AS enterprise_name, e.customer_identifier_no, e.tax_no, p.platform_name, v.entity_name
        from purchase_order_invoice t left join purchase_order po on t.order_id = po.id
        left join enterprise e on po.customer_user_id = e.customer_id
        left join trading_platform p on po.agency_cert_no = p.cert_no
        left join vendor_entity v on v.entity_no = po.entity_no
        <where>
            t.deleted = 0
            <if test="customerUserId != null">
                AND po.customer_user_id = #{customerUserId}
            </if>
            <if test="orderNo != null and orderNo != ''">
                AND po.order_no = #{orderNo}
            </if>
            <if test="invoiceInfo != null and invoiceInfo != ''">
                AND t.invoice_number LIKE CONCAT ('%',#{invoiceInfo},'%') OR t.invoice_code = #{invoiceInfo}
            </if>
            <if test="customerUserInfo != null and customerUserInfo != ''">
                AND e.name LIKE CONCAT ('%',#{customerUserInfo},'%') OR e.customer_identifier_no = #{customerUserInfo}
            </if>
            <if test="agencyCertNo != null and agencyCertNo != ''">
                AND po.agency_cert_no = #{agencyCertNo}
            </if>
            <if test="entityNo != null and entityNo != ''">
                AND po.entity_no = #{entityNo}
            </if>
            <if test="timeStart != null">
                AND t.invoicing_date <![CDATA[ >= ]]> #{timeStart}
            </if>
            <if test="timeEnd != null">
                AND t.invoicing_date <![CDATA[ <= ]]> #{timeEnd}
            </if>
            <if test="source != null">
                AND t.source = #{source}
            </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>
            <if test="null != invoiceTypeList and invoiceTypeList.size()>0">
                AND t.invoice_type IN
                <foreach collection="invoiceTypeList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY t.invoicing_date DESC
    </select>

    <resultMap id="VoDetailResultMap" type="com.trade.service.trade.purchaseorder.vo.PurchaseOrderInvoiceDetailsVo">
        <id column="amount" property="amount"/>
        <id column="digital_invoice_number" property="digitalInvoiceNumber"/>
        <id column="currency" property="currency"/>
        <id column="invoice_code" property="invoiceCode"/>
        <id column="invoice_number" property="invoiceNumber"/>
        <id column="invoice_type" property="invoiceType"/>
        <id column="invoicing_date" property="invoicingDate"/>
        <id column="platform_name" property="agencyEnterpriseName"/>
        <id column="order_no" property="orderNo"/>
        <id column="tax_amount" property="taxAmount"/>
        <id column="certification_date" property="certificationDate"/>
        <id column="bz" property="bz"/>
        <id column="tax_no" property="taxNo"/>
        <id column="order_id" property="orderId"/>
        <id column="enterprise_name" property="enterpriseName"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
        <id column="invoice_status" property="certificationState"/>
    </resultMap>
    <select id="selectByInvoiceId" resultMap="VoDetailResultMap">
        select t.amount,
               t.digital_invoice_number,
               po.currency,
               po.order_no,
               po.id  as order_id,
               t.invoicing_date,
               t.invoice_type,
               t.invoice_code,
               t.invoice_number,
               t.invoice_status,
               t.tax_amount,
               t.bz,
               t.certification_date,
               e.tax_no,
               p.platform_name,
               e.name AS enterprise_name,
               e.customer_identifier_no
        from purchase_order_invoice t
                 left join purchase_order po on t.order_id = po.id
                 left join enterprise e on po.customer_user_id = e.customer_id
                 left join trading_platform p on po.agency_cert_no = p.cert_no
        where t.id = #{id}
    </select>
    <select id="selectByInvoiceStatus" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from purchase_order_invoice
        where invoice_status = #{invoiceStatus}
    </select>


    <resultMap id="DocumentResultMap" type="com.trade.service.trade.purchaseorder.vo.PurchaseOrderInvoiceDocumentVo">
        <id column="amount" property="amount"/>
        <id column="currency" property="currency"/>
        <id column="id" property="id"/>
        <id column="invoice_number" property="invoiceNumber"/>
        <id column="invoice_type" property="invoiceType"/>
        <id column="invoicing_date" property="invoicingDate"/>
        <id column="purchase_order_no" property="purchaseOrderNo"/>
        <id column="sales_order_no" property="salesOrderNo"/>
    </resultMap>

    <select id="selectPurchaseOrderInvoiceDocument"
            parameterType="com.trade.service.trade.purchaseorder.dto.PurchaseOrderInvoiceDocumentSearchDto"
            resultMap="DocumentResultMap">
        select
        t.amount, po.currency, po.order_no as purchase_order_no, t.invoicing_date,
        t.id, t.invoice_type, t.invoice_number, so.order_no as sales_order_no
        from purchase_order_invoice t left join purchase_order po on t.order_id = po.id
        left join purchase_contract pc on pc.contract_no = po.purchase_contract_no
        left join sales_order so on so.id = po.sales_order_id
        <where>
            t.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="invoiceNumber != null and invoiceNumber != ''">
                AND t.invoice_number = #{invoiceNumber}
            </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.invoicing_date <![CDATA[ >= ]]> #{timeStart}
            </if>
            <if test="timeEnd != null">
                AND t.invoicing_date <![CDATA[ <= ]]> #{timeEnd}
            </if>
            <if test="null != invoiceTypeList and invoiceTypeList.size()>0">
                AND t.invoice_type IN
                <foreach collection="invoiceTypeList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY t.invoicing_date DESC
    </select>

</mapper>
