<?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.extend.dao.InvoiceCertificationReportDao">

    <resultMap id="reportStatisticsMap" type="com.dxhy.extend.model.ExtReportStatisticsModel">
        <result column="totalCount" property="totalCount"/>
        <result column="totalAmount" property="totalAmount"/>
        <result column="totalTax" property="totalTax"/>
    </resultMap>

    <select id="queryInvoiceList" resultType="com.dxhy.extend.model.ExtTtRzhzRespVo">
        select
        t.gf_tax_no gfTaxNo,
        t.gf_name gfName,
        t.rzh_belong_date,
        count(1) count,
        sum(invoice_amount) amount,
        sum(tax_amount) tax,
        sum(total_amount) totalAmount
        from t_dx_record_invoice t
        WHERE t.valid = 1 AND t.rzh_yesorno='1'
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

        <if test="rzhBelongDate != null and rzhBelongDate != ''">
            AND t.rzh_belong_date =#{rzhBelongDate}

        </if>
        group by t.gf_tax_no, t.gf_name
        union all
        select

        t.taxno gfTaxNo,
        t.taxname gfName,
        ${rzhBelongDate} rzhBelongDate,
        0 count,
        0 amount,
        0 tax,
        0 totalAmount

        FROM t_dx_tax_current t
        WHERE t.taxno in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND NOT EXISTS ( SELECT 1 FROM t_dx_record_invoice d WHERE d.rzh_belong_date = #{rzhBelongDate} AND
        d.rzh_yesorno = 1 AND d.gf_tax_no = t.taxno)
        order by count desc

    </select>

    <select id="queryInvoiceListCount" resultMap="reportStatisticsMap">
        select count(1) totalCount, sum(a.amount) totalAmount, sum(a. tax) totalTax from(
        select t.gf_tax_no gfTaxNo,
        t.gf_name gfName,count(1) count, ifnull(sum(invoice_amount), 0) amount, ifnull(sum(tax_amount), 0) tax
        from t_dx_record_invoice t
        WHERE t.valid = 1 AND t.rzh_yesorno='1'

        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

        <if test="rzhBelongDate != null and rzhBelongDate != ''">
            AND t.rzh_belong_date =#{rzhBelongDate}

        </if>
        group by t.gf_tax_no, t.gf_name
        union all
        select t.taxno gfTaxNo,
        t.taxname gfName,count(1) count,
        0 amount,
        0 tax
        FROM t_dx_tax_current t
        WHERE t.taxno in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND NOT EXISTS ( SELECT 1 FROM t_dx_record_invoice d WHERE d.rzh_belong_date = #{rzhBelongDate} AND
        d.rzh_yesorno = 1 AND d.gf_tax_no = t.taxno )
        <if test="gfName != null and gfName != ''  and gfName != '-1'">
            AND t.taxno =#{gfName}
        </if>
        group by t.taxno, t.taxname) a

    </select>

</mapper>
