<?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.core.dao.TtFprzhzDao">

    <select id="queryInvoiceList" resultType="com.dxhy.core.model.TtFprzhzRespVo">
        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="query.gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

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

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

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

        FROM t_dx_tax_current t
        WHERE t.taxno in
        <foreach collection="query.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 = #{query.rzhBelongDate} AND
        d.rzh_yesorno = 1 AND d.gf_tax_no = t.taxno)
        order by count desc
    </select>

</mapper>
