<?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.ExtPaperDao">
    <resultMap id="BuyPaperResultMap" type="com.dxhy.extend.entity.ExtBuyInvoicePaper">
        <result column="gf_tax_no" property="gfTaxNo"/>
        <result column="create_date" property="createDate"/>
        <result column="tax_amount" property="sourceSystem"/>
        <result column="invoice_amount" property="invoiceAmount"/>
    </resultMap>

    <select id="listBuyInvoicePaper" parameterType="java.util.Map"
            resultType="com.dxhy.extend.entity.ExtBuyInvoicePaper" databaseId="mysql">
        SELECT
        DATE_FORMAT(collect_date,'%Y-%m-%d') collectDate,
        collect_from collectFrom,
        count(1) allCount,
        SUM(invoice_amount) invoiceAmount,
        SUM(tax_amount) taxAmount

        <if test="dataType == 1">
            FROM
            t_dx_record_invoice t
            WHERE t.detail_yesorno = '1' and t.collect_status='1'
            AND t.gf_tax_no IN
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dataType == 2">
            FROM
            t_dx_sale_record_invoice t
            WHERE t.detail_yesorno = '1' and t.collect_status='1'
            AND t.xf_tax_no IN
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="cjksrq != null and cjksrq!= '' and cjjsrq != null and cjjsrq != ''">
            AND t.collect_date BETWEEN #{cjksrq} and #{cjjsrq}
        </if>
        <if test="company != null and company!= '' ">
            AND t.company = #{company}
        </if>
        GROUP BY DATE_FORMAT(t.collect_date,'%Y-%m-%d'),t.collect_from
        ORDER BY t.collect_date DESC
    </select>

    <select id="selectInvoicePaper" parameterType="java.util.Map"
            resultType="com.dxhy.extend.entity.ExtSelInvoicePaper" databaseId="mysql">
        SELECT
        invoice_type invoiceType,
        invoice_code invoiceCode,
        invoice_no invoiceNo,
        DATE_FORMAT(invoice_date,'%Y-%m-%d') invoiceDate,
        gf_name buyerName,
        xf_name saleName,
        invoice_amount invoiceAmount,
        tax_amount taxAmount,
        total_amount totalAmount,
        in_pool_reason inPoolReason,
        pool_status poolStatus
        FROM
        <if test="dataType == 1">
            t_dx_record_invoice t
            WHERE t.detail_yesorno = '1' and t.collect_status='1'
            AND t.gf_tax_no IN
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dataType == 2">
            t_dx_sale_record_invoice t
            WHERE t.detail_yesorno = '1' and t.collect_status='1'
            AND t.xf_tax_no IN
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="cjrq != null and cjrq!= ''">
            AND DATE_FORMAT(t.collect_date,'%Y-%m-%d') = #{cjrq}
        </if>
        <if test="SourceSystemId != null and SourceSystemId != ''">
            AND t.collect_from = #{SourceSystemId}
        </if>
        ORDER BY t.collect_date DESC
    </select>


    <select id="getSuccCount" parameterType="java.util.Map"
            resultType="int" databaseId="mysql">
        SELECT
        count(1)
        FROM
        <if test="dataType == 1">
            t_dx_record_invoice t
            WHERE t.detail_yesorno = '1' and t.pool_status='0'
            AND t.gf_tax_no IN
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dataType == 2">
            t_dx_sale_record_invoice t
            WHERE t.detail_yesorno = '1' and t.pool_status='0'
            AND t.xf_tax_no IN
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="cjrq != null and cjrq!= ''">
            AND DATE_FORMAT(t.collect_date,'%Y-%m-%d') = #{cjrq}
        </if>
        <if test="SourceSystemId != null and SourceSystemId != ''">
            AND t.collect_from = #{SourceSystemId}
        </if>
        ORDER BY t.collect_date DESC
    </select>


    <select id="getFailCount" parameterType="java.util.Map"
            resultType="int" databaseId="mysql">
        SELECT
        count(1)
        FROM
        <if test="dataType == 1">
            t_dx_record_invoice t
            WHERE t.detail_yesorno = '1' and t.pool_status='1'
            AND t.gf_tax_no IN
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dataType == 2">
            t_dx_sale_record_invoice t
            WHERE t.detail_yesorno = '1' and t.pool_status='1'
            AND t.xf_tax_no IN
            <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="cjrq != null and cjrq!= ''">
            AND DATE_FORMAT(t.collect_date,'%Y-%m-%d') = #{cjrq}
        </if>
        <if test="SourceSystemId != null and SourceSystemId != ''">
            AND t.collect_from = #{SourceSystemId}
        </if>
        ORDER BY t.collect_date DESC
    </select>
</mapper>
