<?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.tab.dao.TabDao">
    <!--查找六个月的采集金额数据  -->
    <select id="getInvoiceList" resultType="com.dxhy.core.tab.entity.CollectTabInvoice" databaseId="mysql">
        SELECT
        date_format(create_date, '%Y-%m') collectMonth,
        gf_name gfName,
        gf_tax_no gfTaxno,
        count(1) invoiceNum,
        truncate(sum(invoice_amount),2) invoiceAmount is null (invoiceAmount,0)
        FROM
        t_dx_record_invoice
        where
        gf_tax_no = #{taxno}
        AND
        date_format(create_date,'%Y-%m') in
        <foreach collection="startDate" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY date_format(create_date, '%Y-%m')
    </select>

    <!--查找六个月签收数据  -->
    <select id="getQsList" resultType="com.dxhy.core.tab.entity.InvoiceScan" databaseId="mysql">
        SELECT
        date_format(qs_date,'%Y-%m') qsMonth,
        gf_name gfName,
        gf_tax_no gfTaxno,
        count(1) invoiceNumber,
        truncate(sum(invoice_amount),2) invoiceAmount,
        truncate(sum(tax_amount),2) taxAmount
        FROM
        t_dx_invoice
        where
        gf_tax_no = #{taxno}
        AND
        qs_status = '1'
        AND
        date_format(qs_date,'%Y-%m') in
        <foreach collection="startDate" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY date_format(qs_date,'%Y-%m') order by qs_date desc
    </select>

    <!--查找六个属期认证数据  -->
    <select id="getRzList" resultType="com.dxhy.core.tab.entity.InvoiceRz" databaseId="mysql">
        SELECT
        a.rzh_belong_date skssq,
        b.taxname gfName,
        SUM(a.tax_amount) allTaxAmount,
        sum(if(a.invoice_type = '01', truncate(a.tax_amount,2), 0)) zengTaxAmount,
        sum(if(a.invoice_type = '03', truncate(a.tax_amount,2), 0)) jiTaxAmount,
        sum(if(a.invoice_type = '14', truncate(a.tax_amount,2), 0)) tongTaxAmount,
        sum(if(a.invoice_type = '08', truncate(a.tax_amount,2), 0)) dzzpTaxAmount,
        sum(if(a.invoice_type = '31', truncate(a.tax_amount,2), 0)) qdzTaxAmount,
        sum(if(a.invoice_type = '283', truncate(a.tax_amount,2), 0)) dzkpTaxAmount,
        sum(if(a.invoice_type = '161', truncate(a.tax_amount,2), 0)) xcdTaxAmount,
        sum(if(a.invoice_type = '183', truncate(a.tax_amount,2), 0)) jdcdzTaxAmount,
        sum(if(a.invoice_type = '185', truncate(a.tax_amount,2), 0)) qdzzTaxAmount,
        sum(if(a.invoice_type = '32', truncate(a.tax_amount,2), 0)) qdpTaxAmount
        FROM
        ${dxhyTertiary}.t_dx_record_invoice a INNER JOIN ${dxhyAdmin}.sys_dept b
        where
        a.gf_tax_no = b.taxno
        AND
        a.gf_tax_no = #{taxno}
        AND
        a.rzh_yesorno = '1'
        AND
        (a.invoice_type in('01','03','08','14','31','283','161','183','185') or (a.invoice_type ='32' and a.lq_tdyslxDm='08'))
        AND
        a.rzh_belong_date in
        <foreach collection="startDate" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY skssq order by a.rzh_belong_date desc
    </select>
    <!-- 查找三月逾期发票 -->
    <select id="getLaterInvoice" resultType="com.dxhy.core.tab.entity.LaterInvoice" databaseId="mysql">
        SELECT DATE_FORMAT(
                       DATE_ADD(
                               invoice_date,
                               INTERVAL 360 DAY
                           ),
                       '%Y%m'
                   )                       AS laterRzDate,
               t.gf_tax_no                    gfTaxno,
               t.gf_name                      gfName,
               t.invoice_code                 invoiceCode,
               t.invoice_no                   invoiceNo,
               date_format( t.invoice_date,'%Y-%m-%d')   invoiceDate,
               FORMAT(t.invoice_amount, 2) AS invoiceamount,
               FORMAT(t.tax_amount, 2)     AS taxAmount
        FROM t_dx_record_invoice AS t
                 LEFT JOIN t_dx_tax_current tdtc ON t.gf_tax_no = tdtc.taxno
        WHERE valid = 1
          AND (
                t.rzh_belong_date IS NULL
                OR t.rzh_belong_date = ''
            )
          AND t.gf_tax_no = #{taxno}
          AND t.invoice_amount > 0
          AND invoice_status = 0
          AND t.rzh_yesorno = 0
          AND ( t.invoice_type in('01','03','08','14','31','283','161','183','185') or ( t.invoice_type ='32' and t.lq_tdyslxDm='08'))
          AND t.confirm_date IS NULL
          AND t.rzh_date IS NULL
          AND t.tax_amount >= 0
          AND period_add(
                      tdtc.current_tax_period, '3') >= DATE_FORMAT(DATE_ADD(invoice_date, INTERVAL 360 DAY), '%Y%m')
        limit 8
    </select>
    <select id="getCount" resultType="int" databaseId="mysql">
        SELECT count(1)
        FROM t_dx_record_invoice AS t
                 LEFT JOIN t_dx_tax_current tdtc ON t.gf_tax_no = tdtc.taxno
        WHERE valid = 1
          AND (
                t.rzh_belong_date IS NULL
                OR t.rzh_belong_date = ''
            )
          AND t.gf_tax_no = #{taxno}
          AND t.invoice_amount > 0
          AND invoice_status = 0
          AND t.rzh_yesorno = 0
          AND ( t.invoice_type in('01','03','08','14','31','283','161','183','185') or ( t.invoice_type ='32' and t.lq_tdyslxDm='08'))
          AND t.confirm_date IS NULL
          AND t.rzh_date IS NULL
          AND t.tax_amount >= 0
          AND period_add(
                      tdtc.current_tax_period, '3') >= DATE_FORMAT(DATE_ADD(invoice_date, INTERVAL 360 DAY), '%Y%m')
    </select>
    <!-- 查询异常发票数量 -->
    <select id="getAbnormalInvoice" resultType="com.dxhy.core.tab.entity.AbnormalInvoice" databaseId="mysql">
        SELECT t.invoice_status               invoiceStatus,
               t.invoice_code                 invoiceCode,
               t.invoice_no                   invoiceNo,
               t.invoice_date                 invoiceDate,
               FORMAT(t.invoice_amount, 2) AS invoiceamount,
               FORMAT(t.tax_amount, 2)     AS taxAmount,
               t.invoice_amount               invoiceAmount,
               t.tax_amount                   taxAmount
        FROM t_dx_record_invoice t AND t.invoice_status != '0'
		AND t.gf_tax_no = #{taxno}
    </select>
    <!-- 查询跨年普票数量 -->
    <select id="getPuInvoice" resultType="com.dxhy.core.tab.entity.PuInvoice" databaseId="mysql">
        SELECT qs_date        qsDate,
               qs_status      qsStatus,
               invoice_code   invoiceCode,
               invoice_no     invoiceNo,
               invoice_date   invoiceDate,
               xf_name        xfName,
               gf_name        gfName,
               invoice_amount invoiceAmount,
               tax_amount     taxAmount
        FROM t_dx_record_invoice
        WHERE invoice_type IN ('04', '10', '11', '14')
          AND qs_status = '1'
          AND date_format(invoice_date, '%Y') = #{year}
          AND gf_tax_no = #{taxno}
    </select>
    <!-- 采集金额  -->
    <select id="getReInvoice" resultType="com.dxhy.core.tab.entity.CollectTabInvoice" databaseId="mysql">
        SELECT date_format(create_date, '%Y-%m')           collectMonth,
               gf_name                                     gfName,
               gf_tax_no                                   gfTaxno,
               count(1)                                    invoiceNum,
               IFNULL(TRUNCATE(sum(invoice_amount), 2), 0) invoiceAmount
        FROM t_dx_record_invoice
        where gf_tax_no = #{taxno}
          AND date_format(create_date, '%Y-%m') = #{month}
        GROUP BY date_format(create_date, '%Y-%m'), gf_tax_no
    </select>
    <!-- 采集税额 -->
    <select id="getReTaxInvoice" resultType="com.dxhy.core.tab.entity.CollecTaxTabInvoice" databaseId="mysql">
        SELECT date_format(create_date, '%Y-%m')       collectMonth,
               gf_name                                 gfName,
               gf_tax_no                               gfTaxno,
               count(1)                                invoiceNum,
               IFNULL(TRUNCATE(sum(tax_amount), 2), 0) taxAmount
        FROM t_dx_record_invoice
        where gf_tax_no = #{taxno}
          AND date_format(create_date, '%Y-%m') = #{month}
        GROUP BY date_format(create_date, '%Y-%m'), gf_tax_no
    </select>
    <!-- 查找该税号当前属期 -->
    <select id="getSq" resultType="String">
        SELECT current_tax_period
        FROM t_dx_tax_current
        WHERE taxno = #{taxno}
    </select>
    <!-- 查找当期认证 -->
    <select id="getRzDq" resultType="com.dxhy.core.tab.entity.RzInfo">
        SELECT invoice_type    name,
               sum(tax_amount) value
        FROM t_dx_record_invoice
        WHERE gf_tax_no = #{taxno}
          AND rzh_yesorno = '1'
          AND rzh_belong_date = #{month}
          AND (invoice_type in('01','03','08','14','31','283','161','183','185') or (invoice_type ='32' and lq_tdyslxDm='08'))
        GROUP BY invoice_type
    </select>
    <!-- 查出01、03、14总金额 -->
    <select id="getAllAmount" resultType="String">
        SELECT sum(tax_amount) allAmount
        FROM t_dx_record_invoice
        WHERE gf_tax_no = #{taxno}
          AND rzh_yesorno = '1'
          AND rzh_belong_date = #{month}
          AND (invoice_type in('01','03','08','14','31','283','161','183','185') or (invoice_type ='32' and lq_tdyslxDm='08'))
    </select>
    <!-- 查找税号名称 -->
    <select id="getName" resultType="String">
        select taxname
        from sys_dept
        where taxno = #{taxno}
          and org_type = '5';
    </select>


    <!--查找六个月的采集金额数据  -->
    <select id="getInvoiceList" resultType="com.dxhy.core.tab.entity.CollectTabInvoice" databaseId="oracle">
        SELECT
        to_char(t.create_date, 'yyyy-MM') collectMonth,
        a.taxname gfName,
        a.taxno gfTaxno,
        count(1) invoiceNum,
        TRUNC(sum(t.invoice_amount),2) invoiceAmount
        FROM
        ${dxhyTertiary}.t_dx_record_invoice
        left join ${dxhyAdmin}.sys_dept a on t.gf_tax_no = a.taxno
        where
        t.gf_tax_no = #{taxno}
        AND
        to_char(t.create_date,'yyyy-MM') in
        <foreach collection="startDate" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY to_char(t.create_date, 'yyyy-MM'),a.taxname,a.taxno
    </select>

    <!--查找六个月签收数据  -->
    <select id="getQsList" resultType="com.dxhy.core.tab.entity.InvoiceScan" databaseId="oracle">
        SELECT
        to_char(qs_date,'yyyy-MM') qsMonth,
        gf_name gfName,
        gf_tax_no gfTaxno,
        count(1) invoiceNumber,
        TRUNC(invoice_amount,2) invoiceAmount,
        TRUNC(tax_amount,2) taxAmount
        FROM
        t_dx_invoice
        where
        gf_tax_no = #{taxno}
        AND
        qs_status = '1'
        AND
        to_char(qs_date,'yyyy-MM') in
        <foreach collection="startDate" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <!--查找六个属期认证数据  -->
    <select id="getRzList" resultType="com.dxhy.core.tab.entity.InvoiceRz" databaseId="oracle">
        SELECT
        a.rzh_belong_date skssq,
        b.taxname gfName,
        SUM(a.tax_amount) allTaxAmount,
        sum(case when a.invoice_type = '01'then a.tax_amount else 0 end) zengTaxAmount,
        sum(case when a.invoice_type = '03'then a.tax_amount else 0 end) jiTaxAmount,
        sum(case when a.invoice_type = '14'then a.tax_amount else 0 end) tongTaxAmount,
        sum(case when a.invoice_type = '08'then a.tax_amount else 0 end) dzzpTaxAmount,
        sum(case when a.invoice_type = '31'then a.tax_amount else 0 end) qdzTaxAmount,
        sum(case when a.invoice_type = '283'then a.tax_amount else 0 end) dzkpTaxAmount,
        sum(case when a.invoice_type = '161'then a.tax_amount else 0 end) xcdTaxAmount,
        sum(case when a.invoice_type = '183'then a.tax_amount else 0 end) jdcdzTaxAmount,
        sum(case when a.invoice_type = '185'then a.tax_amount else 0 end) qdzzTaxAmount,
        sum(case when a.invoice_type = '32'then a.tax_amount else 0 end) qdpTaxAmount
        FROM
        ${dxhyTertiary}.T_DX_RECORD_INVOICE a INNER JOIN ${dxhyAdmin}.SYS_DEPT b
        on a.gf_tax_no = b.taxno
        where
        a.gf_tax_no = #{taxno}
        AND
        a.rzh_yesorno = '1'
        AND
         (a.invoice_type in('01','03','08','14','31','283','161','183','185') or (a.invoice_type ='32' and a.lq_tdyslxDm='08'))
        AND
        a.rzh_belong_date in
        <foreach collection="startDate" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY a.rzh_belong_date , b.taxname order by a.rzh_belong_date desc
    </select>
    <!-- 查找三月逾期发票 -->
    <select id="getLaterInvoice" resultType="com.dxhy.core.tab.entity.LaterInvoice" databaseId="oracle">
        SELECT TO_CHAR(invoice_date + 360, 'yyyy-mm') laterRzDate,
               t.gf_tax_no                            gfTaxno,
               t.gf_name                              gfName,
               t.invoice_code                         invoiceCode,
               t.invoice_no                           invoiceNo,
               t.invoice_date                         invoiceDate,
               TRUNC(T.invoice_amount, 2)             invoiceamount,
               TRUNC(T.tax_amount, 2)                 taxAmount

        FROM t_dx_record_invoice t
                 LEFT JOIN t_dx_tax_current tdtc ON t.gf_tax_no = tdtc.taxno
        WHERE valid = 1
          AND (
                T.rzh_belong_date IS NULL
                OR T.rzh_belong_date = ''
            )
          AND t.gf_tax_no = #{taxno}
          AND t.invoice_amount > 0
          AND invoice_status = 0
          AND t.rzh_yesorno = 0
          AND ( t.invoice_type in('01','03','08','14','31','283','161','183','185') or ( t.invoice_type ='32' and t.lq_tdyslxDm='08'))
          AND t.confirm_date IS NULL
          AND t.rzh_date IS NULL
          AND t.tax_amount >= 0
          AND ADD_MONTHS(
                      TO_DATE(
                              tdtc.current_tax_period,
                              'yyyymm'
                          ),
                      '3'
                  ) >= TO_DATE(
                      TO_CHAR(invoice_date + 360, 'yyyymm'),
                      'yyyymm'
                  )
          AND TO_CHAR(invoice_date, 'yyyymmdd') >= tdtc.select_start_date

    </select>
    <select id="getCount" resultType="int" databaseId="oracle">
        SELECT COUNT(1)
        FROM t_dx_record_invoice T
                 LEFT JOIN t_dx_tax_current tdtc ON T.gf_tax_no = tdtc.taxno
        WHERE valid = 1
          AND (
                T.rzh_belong_date IS NULL
                OR T.rzh_belong_date = ''
            )
          AND T.gf_tax_no = #{taxno}
          AND T.invoice_amount > 0
          AND T.invoice_status = 0
          AND T.rzh_yesorno = 0
          AND ( t.invoice_type in('01','03','08','14','31','283','161','183','185') or ( t.invoice_type ='32' and t.lq_tdyslxDm='08'))
          AND T.confirm_date IS NULL
          AND T.rzh_date IS NULL
          AND T.tax_amount >= 0
          AND ADD_MONTHS(
                      TO_DATE(
                              tdtc.current_tax_period,
                              'yyyymm'
                          ),
                      '3'
                  ) >= TO_DATE(
                      TO_CHAR(invoice_date + 360, 'yyyymm'),
                      'yyyymm'
                  )

    </select>
    <!-- 查询异常发票数量 -->
    <select id="getAbnormalInvoice" resultType="com.dxhy.core.tab.entity.AbnormalInvoice" databaseId="oracle">
        SELECT t.invoice_status           invoiceStatus,
               t.invoice_code             invoiceCode,
               t.invoice_no               invoiceNo,
               t.invoice_date             invoiceDate,
               trunc(t.invoice_amount, 2) invoiceamount,
               trunc(t.tax_amount, 2)     taxAmount,
               t.invoice_amount           invoiceAmount,
               t.tax_amount               taxAmount
        FROM t_dx_record_invoice t
        where t.invoice_status != '0'
          AND t.gf_tax_no = #{taxno}

    </select>
    <!-- 查询跨年普票数量 -->
    <select id="getPuInvoice" resultType="com.dxhy.core.tab.entity.PuInvoice" databaseId="oracle">
        SELECT qs_date        qsDate,
               qs_status      qsStatus,
               invoice_code   invoiceCode,
               invoice_no     invoiceNo,
               invoice_date   invoiceDate,
               xf_name        xfName,
               gf_name        gfName,
               invoice_amount invoiceAmount,
               tax_amount     taxAmount
        FROM t_dx_record_invoice
        WHERE invoice_type IN ('04', '10', '11', '14')
          AND qs_status = '1'
          AND to_char(invoice_date, 'yyyy') = #{year}
          AND gf_tax_no = #{taxno}

    </select>
    <!-- 采集金额  -->
    <select id="getReInvoice" resultType="com.dxhy.core.tab.entity.CollectTabInvoice" databaseId="oracle">
        SELECT TO_CHAR(create_date, 'yyyy-mm') collectMonth,
               gf_tax_no                       gfTaxno,
               COUNT(1)                        invoiceNum,
               nvl(
                       trunc(SUM(invoice_amount), 2),
                       0
                   )                           invoiceAmount
        FROM t_dx_record_invoice
        WHERE gf_tax_no = #{taxno}
          AND TO_CHAR(create_date, 'yyyy-mm') = #{month}
        GROUP BY TO_CHAR(create_date, 'yyyy-mm'), gf_tax_no

    </select>
    <!-- 采集税额 -->
    <select id="getReTaxInvoice" resultType="com.dxhy.core.tab.entity.CollecTaxTabInvoice" databaseId="oracle">
        SELECT TO_CHAR(create_date, 'yyyy-mm') collectMonth,
               gf_tax_no                       gfTaxno,
               COUNT(1)                        invoiceNum,
               nvl(
                       trunc(SUM(tax_amount), 2),
                       0
                   )                           taxAmount
        FROM t_dx_record_invoice
        WHERE gf_tax_no = #{taxno}
          AND TO_CHAR(create_date, 'yyyy-mm') = #{month}
        GROUP BY TO_CHAR(create_date, 'yyyy-mm'), gf_tax_no

    </select>
</mapper>
