<?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="pwc.taxtech.atms.invoice.InputInvoiceMapper">

    <select id="getInputVATInvoiceCountByCondition" parameterType="pwc.taxtech.atms.dto.vatdto.InputInvoicePreviewQueryParam" resultType="java.lang.Long">
        SELECT
        COUNT (
        1
        )
        FROM
        INPUT_VAT_INVOICE
        WHERE
        1=1
        <if test="paras.periodStart">
            AND PERIOD_ID &gt;= #{paras.periodStart,jdbcType=VARCHAR}
        </if>
        <if test="paras.periodEnd">
            AND PERIOD_ID &lt;= #{paras.periodEnd,jdbcType=VARCHAR}
        </if>
        <if test="paras.certificationDateStart != null">
            AND CERTIFICATION_DATE &gt;= #{paras.certificationDateStart,jdbcType=VARCHAR}
        </if>
        <if test="paras.certificationDateEnd != null">
            AND CERTIFICATION_DATE &lt;= #{paras.certificationDateEnd,jdbcType=VARCHAR}
        </if>
        <if test="paras.invoiceCode != null and paras.invoiceCode!=''">
            AND INVOICE_CODE LIKE concat (
            '%',
            #{paras.invoiceCode,jdbcType=VARCHAR},
            '%'
            )
        </if>
        <if test="paras.invoiceNumber!=null and paras.invoiceNumber!=''">
            AND INVOICE_NUMBER LIKE concat (
            '%',
            #{paras.invoiceNumber,jdbcType=VARCHAR},
            '%'
            )
        </if>
        <if test="paras.sellerTaxNumber!=null and paras.sellerTaxNumber!=''">
            AND SELLER_TAX_NUMBER LIKE concat (
            '%',
            #{paras.sellerTaxNumber,jdbcType=VARCHAR},
            '%'
            )
        </if>
        <if test="paras.amountStart != null">
            AND AMOUNT &gt;= #{paras.amountStart,jdbcType=DECIMAL}
        </if>
        <if test="paras.amountEnd != null">
            AND AMOUNT &lt;= #{paras.amountEnd,jdbcType=DECIMAL}
        </if>
        <if test="paras.taxAmountStart != null">
            AND TAX_AMOUNT &gt;= #{paras.taxAmountStart,jdbcType=DECIMAL}
        </if>
        <if test="paras.taxAmountEnd != null">
            AND TAX_AMOUNT &lt;= #{paras.taxAmountEnd,jdbcType=DECIMAL}
        </if>
        <if test="paras.invoiceType!=null and paras.invoiceType!=999">
            AND INVOICE_TYPE_= #{param.invoiceType,jdbcType=INTEGER}
        </if>
        <if test="paras.certificationStatus==1">
            AND CERTIFICATION_RESULT LIKE '%1%'
        </if>
        <if test="paras.certificationStatus==2">
            AND CERTIFICATION_RESULT LIKE '%2%'
        </if>
    </select>

    <select id="getInputVATInvoiceCountByConditionWithPaging" resultMap="BaseResultMap">
        SELECT
        *
        FROM
        INPUT_VAT_INVOICE
        WHERE
        1=1
        <if test="paras.periodStart">
            AND PERIOD_ID &gt;= #{paras.periodStart,jdbcType=VARCHAR}
        </if>
        <if test="paras.periodEnd">
            AND PERIOD_ID &lt;= #{paras.periodEnd,jdbcType=VARCHAR}
        </if>
        <if test="paras.certificationDateStart != null">
            AND CERTIFICATION_DATE &gt;= #{paras.certificationDateStart,jdbcType=VARCHAR}
        </if>
        <if test="paras.certificationDateEnd != null">
            AND CERTIFICATION_DATE &lt;= #{paras.certificationDateEnd,jdbcType=VARCHAR}
        </if>
        <if test="paras.invoiceCode != null and paras.invoiceCode!=''">
            AND INVOICE_CODE LIKE concat (
            '%',
            #{paras.invoiceCode,jdbcType=VARCHAR},
            '%'
            )
        </if>
        <if test="paras.invoiceNumber!=null and paras.invoiceNumber!=''">
            AND INVOICE_NUMBER LIKE concat (
            '%',
            #{paras.invoiceNumber,jdbcType=VARCHAR},
            '%'
            )
        </if>
        <if test="paras.sellerTaxNumber!=null and paras.sellerTaxNumber!=''">
            AND SELLER_TAX_NUMBER LIKE concat (
            '%',
            #{paras.sellerTaxNumber,jdbcType=VARCHAR},
            '%'
            )
        </if>
        <if test="paras.amountStart != null">
            AND AMOUNT &gt;= #{paras.amountStart,jdbcType=DECIMAL}
        </if>
        <if test="paras.amountEnd != null">
            AND AMOUNT &lt;= #{paras.amountEnd,jdbcType=DECIMAL}
        </if>
        <if test="paras.taxAmountStart != null">
            AND TAX_AMOUNT &gt;= #{paras.taxAmountStart,jdbcType=DECIMAL}
        </if>
        <if test="paras.taxAmountEnd != null">
            AND TAX_AMOUNT &lt;= #{paras.taxAmountEnd,jdbcType=DECIMAL}
        </if>
        <if test="paras.invoiceType!=null and paras.invoiceType!=999">
            AND INVOICE_TYPE_= #{param.invoiceType,jdbcType=INTEGER}
        </if>
        <if test="paras.certificationStatus==1">
            AND CERTIFICATION_RESULT LIKE '%1%'
        </if>
        <if test="paras.certificationStatus==2">
            AND CERTIFICATION_RESULT LIKE '%2%'
        </if>
        ORDER BY
        InvoiceDate
        <if test="paras.pageInfo!=null and paras.pageInfo.pageSize!=null and limitFrom!=null">
            LIMIT #{limitFrom,jdbcType=INTEGER},
            #{paras.pageInfo.pageSize,jdbcType=INTEGER}
        </if>
    </select>

    <resultMap id="inputInvoiceResultDto" type="pwc.taxtech.atms.vat.dpo.InputInvoiceResultDto">
        <id property="invoiceID" column="invoice_id"/>
        <association property="inputVATInvoice" javaType="pwc.taxtech.atms.vat.entity.InputVatInvoice">
            <id column="ID" jdbcType="VARCHAR" property="id"/>
            <result column="PERIOD_ID" jdbcType="INTEGER" property="periodId"/>
            <result column="INVOICE_CODE" jdbcType="VARCHAR" property="invoiceCode"/>
            <result column="INVOICE_NUMBER" jdbcType="VARCHAR" property="invoiceNumber"/>
            <result column="INVOICE_DATE" jdbcType="TIMESTAMP" property="invoiceDate"/>
            <result column="SELLER_TAX_NUMBER" jdbcType="VARCHAR" property="sellerTaxNumber"/>
            <result column="AMOUNT" jdbcType="DECIMAL" property="amount"/>
            <result column="TAX_AMOUNT" jdbcType="DECIMAL" property="taxAmount"/>
            <result column="INVOICE_TYPE" jdbcType="INTEGER" property="invoiceType"/>
            <result column="CERTIFICATION_RESULT" jdbcType="VARCHAR" property="certificationResult"/>
            <result column="CERTIFICATION_DATE" jdbcType="TIMESTAMP" property="certificationDate"/>
            <result column="CREATOR_ID" jdbcType="VARCHAR" property="creatorId"/>
            <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="STATUS" jdbcType="INTEGER" property="status"/>
            <result column="PART_ACCNTED_PERIOD" jdbcType="INTEGER" property="partAccntedPeriod"/>
            <result column="ACCNTED_PERIOD" jdbcType="INTEGER" property="accntedPeriod"/>
        </association>
        <association property="inputVATInvoiceItem" javaType="pwc.taxtech.atms.vat.entity.InputVatInvoiceItem">
            <id column="ID" jdbcType="VARCHAR" property="id"/>
            <result column="INPUT_VAT_INVOICE_ID" jdbcType="VARCHAR" property="inputVATInvoiceId"/>
            <result column="PERIOD_ID" jdbcType="INTEGER" property="periodId"/>
            <result column="INVOICE_CODE" jdbcType="VARCHAR" property="invoiceCode"/>
            <result column="INVOICE_NUMBER" jdbcType="VARCHAR" property="invoiceNumber"/>
            <result column="AMOUNT" jdbcType="DECIMAL" property="amount"/>
            <result column="TAX_AMOUNT" jdbcType="DECIMAL" property="taxAmount"/>
            <result column="TAX_RATE" jdbcType="DECIMAL" property="taxRate"/>
            <result column="PRODUCTION_NAME" jdbcType="VARCHAR" property="productionName"/>
            <result column="CREATOR_ID" jdbcType="VARCHAR" property="creatorId"/>
            <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="PRODUCTION_CODE" jdbcType="VARCHAR" property="productionCode"/>
        </association>
        <association property="goodsMapping" javaType="pwc.taxtech.atms.vat.entity.GoodsMapping">
            <id column="ID" jdbcType="VARCHAR" property="id"/>
            <result column="CUST_GOODS_NAME" jdbcType="VARCHAR" property="custGoodsName"/>
            <result column="STD_GOODS_NAME" jdbcType="VARCHAR" property="stdGoodsName"/>
            <result column="CREATOR_ID" jdbcType="VARCHAR" property="creatorId"/>
            <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="UPDATE_TIME" jdbcType="TIMESTAMP" property="updateTime"/>
        </association>
    </resultMap>

    <select id="getInputInvoiceResultDto" resultMap="inputInvoiceResultDto">
        SELECT
        INPUT_INVOICE.ID AS INVOICE_ID,
        INPUTINVOICE.ID,
--         INPUTINVOICE.PERIOD_ID,
        INPUTINVOICE.FPDM,
        INPUTINVOICE.FPHM,
        INPUTINVOICE.KPRQ,
        INPUTINVOICE.XFSH,
        INPUTVATINVOICE.HJJE,
        INPUTVATINVOICE.HJSE,
        INPUTVATINVOICE.FPLX,
        INPUTVATINVOICE.RZJG,
        INPUTVATINVOICE.RJSJ,
--         INPUTVATINVOICE.CREATOR_ID,
        INPUTVATINVOICE.CJSJ,
--         INPUTVATINVOICE.`_S_T_A_T_U_S_`,
--         INPUTVATINVOICE.PART_ACCNTED_PERIOD,
--         INPUTVATINVOICE.ACCNTED_PERIOD,
        INPUTINVOICEDETAIL.ID,
        INPUTINVOICEDETAIL.FPID,
--         INPUTINVOICEDETAIL.PERIOD_ID,
        INPUTINVOICEDETAIL.FPDM,
        INPUTVATINVOICEITEM.FPHM,
        INPUTVATINVOICEITEM.JE,
        INPUTVATINVOICEITEM.SE,
        INPUTVATINVOICEITEM.SLV,
        INPUTVATINVOICEITEM.SPMC,
--         INPUTVATINVOICEITEM.CREATOR_ID,
--         INPUTVATINVOICEITEM.CREATE_TIME,
        INPUTVATINVOICEITEM.GGXH
        FROM
          INPUT_INVOICE INPUTINVOICE
        JOIN
          INPUT_INVOICE_DETAIL INPUTINVOICEDETAIL
        ON
        INPUTINVOICE.ID = INPUTINVOICEDETAIL.FPID
        AND INPUT_INVOICE.PERIOD_ID = INPUTVATINVOICEITEM.PERIOD_ID
--         LEFT JOIN
--           ${dbName}.INPUT_MATERIAL_ITEM MATERIALITEM
--         ON
--         INPUTVATINVOICEITEM.PRODUCTION_NAME = MATERIALITEM.CUST_GOODS_NAME
    </select>

    <resultMap id="cellInvoiceDto" type="pwc.taxtech.atms.vat.dpo.CellInvoiceDto">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="INVOICE_DATE" jdbcType="VARCHAR" property="invoiceDate"/>
        <result column="INVOICE_NUMBER" jdbcType="VARCHAR" property="invoiceNumber"/>
        <result column="INVOICE_CODE" jdbcType="VARCHAR" property="invoiceCode"/>
        <result column="AMOUNT" jdbcType="VARCHAR" property="amount"/>
        <result column="TAX_AMOUNT" jdbcType="VARCHAR" property="taxAmount"/>
        <result column="RATE" jdbcType="VARCHAR" property="rate"/>
        <result column="PRODUCTION_NAME" jdbcType="VARCHAR" property="productionName"/>
        <result column="DATA_SOURCE_ID" jdbcType="VARCHAR" property="dataSourceId"/>
        <result column="DATA_SOURCE_NAME" jdbcType="VARCHAR" property="dataSourceName"/>
        <result column="OPERATION_TYPE" jdbcType="VARCHAR" property="operationType"/>
        <result column="INVOICE_TYPE" jdbcType="VARCHAR" property="invoiceType"/>
    </resultMap>

    <select id="selectInputInvoiceWithItem" resultMap="cellInvoiceDto">
        SELECT
        III.ID AS ID,
        ii.FPRQ AS INVOICE_DATE,
        iii.FPHM AS INVOICE_NUMBER,
        iii.FPDM AS INVOICE_CODE,
        iii.JE AS AMOUNT,
        iii.SE AS TAX_AMOUNT,
        iii.SLV AS RATE,
        iii.SPMC AS PRODUCTION_NAME,
        ii.FPLX AS INVOICE_TYPE
        FROM
        INPUT_INVOICE_DETAIL iii
        JOIN
        INPUT_INVOICE ii
        ON
        iii.FPID = ii.ID
    </select>

    <resultMap id="inputVATInvoiceItemExtendDto" type="pwc.taxtech.atms.dto.vatdto.InputVATInvoiceItemExtendDto">
        <id column="invoiceId" jdbcType="VARCHAR" property="invoiceId"/>
        <result column="INVOICE_ITEM_ID" jdbcType="VARCHAR" property="invoiceItemId"/>
        <result column="PERIOD" jdbcType="INTEGER" property="period"/>
        <result column="CERTIFICATION_DATE" jdbcType="TIMESTAMP" property="certificationDate"/>
        <result column="SELLER_TAX_NUMBER" jdbcType="VARCHAR" property="sellerTaxNumber"/>
        <result column="INVOICE_CODE" jdbcType="VARCHAR" property="invoiceCode"/>
        <result column="INVOICE_NUMBER" jdbcType="VARCHAR" property="invoiceNumber"/>
        <result column="AMOUNT" jdbcType="DECIMAL" property="amount"/>
        <result column="INVOICE_TYPE" jdbcType="INTEGER" property="invoiceType"/>
        <result column="TAX_AMOUNT" jdbcType="DECIMAL" property="taxAmount"/>
    </resultMap>

    <select id="selectInvoiceExtendDto" resultMap="inputVATInvoiceItemExtendDto">
        SELECT
        B.ID AS INVOICE_ID,
        A.ID AS INVOICE_ITEM_ID,
        A.PERIOD_ID AS PERIOD,
        B.CERTIFICATION_DATE AS CERTIFICATION_DATE,
        B.SELLER_TAX_NUMBER AS SELLER_TAX_NUMBER,
        B.INVOICE_CODE AS INVOICE_CODE,
        B.INVOICE_NUMBER AS INVOICE_NUMBER,
        B.AMOUNT AS AMOUNT,
        B.INVOICE_TYPE AS INVOICE_TYPE,
        A.TAX_AMOUNT AS TAX_AMOUNT
        FROM
        INPUT_INVOICE_DETAIL A
        JOIN
        INPUT_INVOICE B
        ON
        A.FPID = B.ID
        WHERE
        A.ID = #{invoiceItemId,jdbcType=VARCHAR}
    </select>

</mapper>