<?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.jkcredit.invoice.hub.mapper.SearchInvoiceResultMapper">
    <resultMap id="baseResultMap"
               type="com.jkcredit.invoice.hub.model.po.searchInvoiceResult.SearchInvoiceResultPo">
        <id column="id" property="id"/>
        <result column="invoice_num" property="invoiceNum"/>
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_make_time" property="invoiceMakeTime"/>
        <result column="invoice_url" property="invoiceUrl"/>
        <result column="invoice_html_url" property="invoiceHtmlUrl"/>
        <result column="en_station" property="enStation"/>
        <result column="ex_station" property="exStation"/>
        <result column="ex_time" property="exTime"/>
        <result column="fee" property="fee"/>
        <result column="total_tax_amount" property="totalTaxAmount"/>
        <result column="plate_num" property="plateNum"/>
        <result column="vehicle_type" property="vehicleType"/>
        <result column="seller_name" property="sellerName"/>
        <result column="seller_taxpayer_code" property="sellerTaxpayerCode"/>
        <result column="waybill_num" property="waybillNum"/>
        <result column="waybill_status" property="waybillStatus"/>
        <result column="waybill_start_time" property="waybillStartTime"/>
        <result column="waybill_end_time" property="waybillEndTime"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="tax_rate" property="taxRate"/>
        <result column="invoice_type" property="invoiceType"/>
        <result column="amount" property="amount"/>
        <result column="transaction_id" property="transactionId"/>
        <result column="user_id" property="userId"/>
        <result column="create_time" property="createTime"/>
        <result column="trade_match_time" property="tradeMatchTime"/>
        <result column="buyer_name" property="buyerName"/>
        <result column="buyer_taxpayer_code" property="buyerTaxpayerCode"/>
        <result column="update_time" property="updateTime"/>
        <result column="activated" property="activated"/>
    </resultMap>

    <select id="getSearchInvoiceResult" resultMap="baseResultMap">
        select * from h_search_invoice_result where waybill_num = #{num}
    </select>

    <select id="getInvoiceRecords" resultType="com.jkcredit.invoice.hub.model.po.searchInvoiceResult.InvoiceRecordPo">
        select
        result.waybill_num as waybillNum, result.plate_num as plateNum, result.transaction_id as transactionId,
        result.fee as fee,
        result.total_amount as totalAmount, result.tax_rate as taxRate, result.amount as amount, result.create_time as
        createTime,
        user.app_key as appKey, user.company
        from h_search_invoice_result result
        left join h_user user on result.user_id = user.id
        <where>
            result.activated = 1 and user.activated = 1
            <if test="query.appKey != null and query.appKey != ''">
                and user.app_key = #{query.appKey}
            </if>
            <if test="query.plateNum != null and query.plateNum != ''">
                and result.plate_num = #{query.plateNum}
            </if>
            <if test="query.waybillNum != null and query.waybillNum != ''">
                and result.waybill_num = #{query.waybillNum}
            </if>
            <if test="query.transactionId != null and query.transactionId != ''">
                and result.transaction_id = #{query.transactionId}
            </if>
        </where>
    </select>
</mapper>
