<?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.exportrebate.mapper.TaxRebatePurchaseDetailsMapper">

    <resultMap type="com.trade.service.trade.exportrebate.dto.TaxRebatePurchaseAbstractInfo"
               id="TaxRebatePurchaseAbstractInfoMap">
        <result property="id" column="id"/>
        <result property="rebateExportDetailsId" column="rebate_export_details_id"/>
        <result property="customerUserId" column="customer_user_id"/>
        <result property="agencyCertNo" column="agency_cert_no"/>
        <result property="agencyName" column="agency_name"/>
        <result property="enterpriseCertNo" column="enterprise_cert_no"/>
        <result property="enterpriseName" column="enterprise_name"/>
        <result property="purchaseOrderNo" column="purchase_order_no"/>
        <result property="declarationMonthYear" column="declaration_month_year"/>
        <result property="declarationBatch" column="declaration_batch"/>
        <result property="declarationSerialNo" column="declaration_serial_no"/>
        <result property="correlationNo" column="correlation_no"/>
        <result property="voucherType" column="voucher_type"/>
        <result property="voucherTypeName" column="voucher_type_name"/>
        <result property="purchaseVoucherNo" column="purchase_voucher_no"/>
        <result property="invoiceDate" column="invoice_date"/>
        <result property="vendorTaxNo" column="vendor_tax_no"/>
        <result property="hsCode" column="hs_code"/>
        <result property="exportCommodityCode" column="export_commodity_code"/>
        <result property="exportCommodityName" column="export_commodity_name"/>
        <result property="measurementUnit" column="measurement_unit"/>
        <result property="exportQuantity" column="export_quantity"/>
        <result property="taxBaseAmount" column="tax_base_amount"/>
        <result property="addedTaxRate" column="added_tax_rate"/>
        <result property="exportTaxRebateRate" column="export_tax_rebate_rate"/>
        <result property="taxRefundableAmount" column="tax_refundable_amount"/>
        <result property="taxRefundAmount" column="tax_refund_amount"/>
        <result property="status" column="status"/>
    </resultMap>

    <select id="getTaxRebatePurchaseAbstractInfoPagesBySearch"
            parameterType="com.trade.service.trade.exportrebate.dto.search.TaxRebatePurchaseAbstractInfoSearchDto"
            resultMap="TaxRebatePurchaseAbstractInfoMap">
        SELECT
        d.`id`,
        d.`rebate_export_details_id`,
        d.`customer_user_id`,
        d.`agency_cert_no`,
        p.`platform_name` AS `agency_name`,
        e.`cert_no` AS `enterprise_cert_no`,
        e.`name` AS `enterprise_name`,
        o.`order_no` AS `purchase_order_no`,
        d.`declaration_month_year`,
        d.`declaration_batch`,
        d.`declaration_serial_no`,
        d.`correlation_no`,
        d.`voucher_type`,
        d.`voucher_type_name`,
        d.`purchase_voucher_no`,
        d.`invoice_date`,
        d.`vendor_tax_no`,
        d.`hs_code`,
        d.`export_commodity_code`,
        d.`export_commodity_name`,
        d.`measurement_unit`,
        d.`export_quantity`,
        d.`tax_base_amount`,
        d.`added_tax_rate`,
        d.`export_tax_rebate_rate`,
        d.`tax_refundable_amount`,
        d.`tax_refund_amount`,
        d.`status`
        FROM `tax_rebate_purchase_details` d
        LEFT JOIN trading_platform p ON p.cert_no = d.agency_cert_no
        LEFT JOIN enterprise e ON e.customer_id = d.customer_user_id
        LEFT JOIN purchase_order_invoice i on i.purchase_voucher_no = d.purchase_voucher_no
        LEFT JOIN purchase_order o on o.id = i.order_id
        WHERE d.deleted = 0 AND d.agency_cert_no = #{agencyCertNo}
        AND d.declaration_month_year = #{declarationMonthYear}
        AND d.declaration_batch =#{declarationBatch}
        AND d.status = #{status}
        <if test="purchaseOrderNo != null and purchaseOrderNo != ''">
            AND o.order_no LIKE CONCAT ('%',#{purchaseOrderNo},'%')
        </if>
        <if test="purchaseVoucherNo != null and purchaseVoucherNo != ''">
            AND d.purchase_voucher_no LIKE CONCAT ('%',#{purchaseVoucherNo},'%')
        </if>
        <if test="vendorTaxNo != null and vendorTaxNo != ''">
            AND d.vendor_tax_no LIKE CONCAT ('%',#{vendorTaxNo},'%')
        </if>
        <if test="agencyName != null and agencyName != ''">
            AND p.platform_name LIKE CONCAT ('%',#{agencyName},'%')
        </if>
        <if test="enterpriseName != null and enterpriseName != ''">
            AND e.name LIKE CONCAT ('%',#{enterpriseName},'%')
        </if>

    </select>
    <select id="getByExportDetailIds"
            resultMap="TaxRebatePurchaseAbstractInfoMap" >
        SELECT
        d.`id`,
        d.`rebate_export_details_id`,
        d.`customer_user_id`,
        d.`agency_cert_no`,
        o.`order_no` AS `purchase_order_no`,
        d.`declaration_month_year`,
        d.`declaration_batch`,
        d.`declaration_serial_no`,
        d.`correlation_no`,
        d.`voucher_type`,
        d.`voucher_type_name`,
        d.`purchase_voucher_no`,
        d.`invoice_date`,
        d.`vendor_tax_no`,
        d.`hs_code`,
        d.`export_commodity_code`,
        d.`export_commodity_name`,
        d.`measurement_unit`,
        d.`export_quantity`,
        d.`tax_base_amount`,
        d.`added_tax_rate`,
        d.`export_tax_rebate_rate`,
        d.`tax_refundable_amount`,
        d.`tax_refund_amount`,
        d.`status`
        FROM `tax_rebate_purchase_details` d
        LEFT JOIN purchase_order_invoice i on i.purchase_voucher_no = d.purchase_voucher_no
        LEFT JOIN purchase_order o on o.id = i.order_id
        WHERE d.deleted = 0
        AND d.rebate_export_details_id IN
        <foreach collection="exportDetailIds" item="detailId" open="(" separator="," close=")">
            #{detailId}
        </foreach>
    </select>

</mapper>

