<?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.dxhy.erp.dao.DeclareDao">

    <select id="getTransferStatistics" resultType="com.dxhy.erp.model.TransferStatisticsModel" databaseId="mysql">
        SELECT record.invoice_type invoiceType,
        reason.out_reason transferReason,
        ifnull(sum(tdio.out_invoice_amout), 0) outInvoiceAmoutSum,
        ifnull(sum(tdio.out_tax_amount), 0) outTaxAmountSum,
        COUNT(*) outInvoiceCount
        FROM t_dx_record_invoice_out tdio
        LEFT JOIN t_dx_record_invoice record ON tdio.uuid = record.uuid
        LEFT JOIN t_dx_out_reason reason ON reason.reason_code = tdio.out_reason
        where tdio.is_cancel ='0' and record.invoice_status='0'
        and record.invoice_type in ('01','08','14','03')
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and record.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and tdio.out_belong_date = #{belongDate}
        </if>
        GROUP BY 	transferReason,invoiceType
    </select>

    <select id="getTransferStatistics" resultType="com.dxhy.erp.model.TransferStatisticsModel" databaseId="oracle">
        SELECT record.invoice_type invoiceType,
        reason.out_reason transferReason,
        nvl(sum(tdio.out_invoice_amout), 0) outInvoiceAmoutSum,
        nvl(sum(tdio.out_tax_amount), 0) outTaxAmountSum,
        COUNT(*) outInvoiceCount
        FROM t_dx_record_invoice_out tdio
        LEFT JOIN t_dx_record_invoice record ON tdio.uuid = record.uuid
        LEFT JOIN t_dx_out_reason reason ON reason.reason_code = tdio.out_reason
        where tdio.is_cancel ='0' and record.invoice_status='0'
        and record.invoice_type in ('01','08','14','03')
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and record.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and tdio.out_belong_date = #{belongDate}
        </if>
        GROUP BY 	transferReason,invoiceType
    </select>



    <select id="getVatStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="mysql">
        SELECT recode.invoice_type invoiceType,
        ifnull(sum(recode.dk_tax_amount),0) dkTaxAmount,
        ifnull(sum(recode.invoice_amount),0) invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_record_invoice recode
        where recode.rzh_yesorno ='1' and recode.invoice_type in ('01','08','14','03')
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and recode.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and recode.rzh_belong_date = #{belongDate}
        </if>
        GROUP BY recode.invoice_type
    </select>

    <select id="getVatStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="oracle">
        SELECT recode.invoice_type invoiceType,
        nvl(sum(recode.dk_tax_amount),0) dkTaxAmount,
        nvl(sum(recode.invoice_amount),0) invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_record_invoice recode
        where recode.rzh_yesorno ='1' and recode.invoice_type in ('01','08','14','03')
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and recode.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and recode.rzh_belong_date = #{belongDate}
        </if>
        GROUP BY recode.invoice_type
    </select>


    <select id="getTicketStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="mysql">
        SELECT reimburse.invoice_type invoiceType,
        ifnull(sum(reimburse.tax_amount),0) dkTaxAmount,
        ifnull(sum(reimburse.invoice_amount),0) invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_invoice_reimburse reimburse
        where reimburse.invoice_type in ('92','90','87','88','89','14') and reimburse.qs_status = '1'
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and reimburse.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and date_format(reimburse.qs_date, '%Y%m') = #{belongDate}
        </if>
        GROUP BY reimburse.invoice_type
    </select>
    <select id="getTicketStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="oracle">
        SELECT reimburse.invoice_type invoiceType,
        nvl(sum(reimburse.tax_amount),0) dkTaxAmount,
        nvl(sum(reimburse.invoice_amount),0) invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_invoice_reimburse reimburse
        where  reimburse.invoice_type in ('92','90','87','88','89','14') and reimburse.qs_status = '1'
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and reimburse.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and to_char(reimburse.qs_date,'yyyyMM') = #{belongDate}
        </if>
        GROUP BY reimburse.invoice_type
    </select>


    <select id="getWithholdingStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="mysql">
        SELECT reimburse.invoice_type invoiceType,
        ifnull(sum(reimburse.tax_amount),0) dkTaxAmount,
        '0' as invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_invoice_reimburse reimburse
        where  reimburse.invoice_type in ('102','103') and reimburse.qs_status = '1'
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and reimburse.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and date_format(reimburse.qs_date, '%Y%m') = #{belongDate}
        </if>
        GROUP BY reimburse.invoice_type
    </select>
    <select id="getWithholdingStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="oracle">
        SELECT reimburse.invoice_type invoiceType,
        nvl(sum(reimburse.tax_amount),0) dkTaxAmount,
        '0' as invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_invoice_reimburse reimburse
        where  reimburse.invoice_type in ('102','103') and reimburse.qs_status = '1'
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and reimburse.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and to_char(reimburse.qs_date,'yyyyMM') = #{belongDate}
        </if>
        GROUP BY reimburse.invoice_type
    </select>


    <select id="getBridgeStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="mysql">
        SELECT '101' as  invoiceType,
        ifnull(sum(reimburse.tax_amount),0) dkTaxAmount,
        ifnull(sum(reimburse.invoice_amount),0) invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_invoice_reimburse reimburse
        where  reimburse.invoice_type = '101' and reimburse.qs_status = '1'
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and reimburse.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and date_format(reimburse.qs_date, '%Y%m') = #{belongDate}
        </if>
    </select>
    <select id="getBridgeStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="oracle">
        SELECT '101' as  invoiceType,
        nvl(sum(reimburse.tax_amount),0) dkTaxAmount,
        nvl(sum(reimburse.invoice_amount),0) invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_invoice_reimburse reimburse
        where  reimburse.invoice_type = '101' and reimburse.qs_status = '1'
        <if test="buyerTaxNo != null and buyerTaxNo != ''">
            and reimburse.gf_tax_no = #{buyerTaxNo}
        </if>
        <if test="belongDate != null and belongDate !=''">
            and to_char(reimburse.qs_date,'yyyyMM') = #{belongDate}
        </if>
    </select>


    <select id="getCustomStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="mysql">
        SELECT '17' as  invoiceType,
        ifnull(sum(recode.yx_tax_amount),0) dkTaxAmount,
        '0' as invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_customs_record recode
        <where>
            recode.rzh_yesorno ='1'
            <if test="buyerTaxNo != null and buyerTaxNo != ''">
                and recode.gf_tax_no = #{buyerTaxNo}
            </if>
            <if test="belongDate != null and belongDate !=''">
                and recode.rzh_belong_date = #{belongDate}
            </if>
        </where>
    </select>

    <select id="getCustomStatistics" resultType="com.dxhy.erp.model.ReportInvoiceStatisticsModel" databaseId="oracle">
        SELECT '17' as invoiceType,
        nvl(sum(recode.yx_tax_amount),0) dkTaxAmount,
        '0' as invoiceAmoutSum,
        COUNT(*) dkInvoiceCount
        from t_dx_customs_record recode
        <where>
            recode.rzh_yesorno ='1'
            <if test="buyerTaxNo != null and buyerTaxNo != ''">
                and recode.gf_tax_no = #{buyerTaxNo}
            </if>
            <if test="belongDate != null and belongDate !=''">
                and recode.rzh_belong_date = #{belongDate}
            </if>
        </where>
    </select>
</mapper>
