<?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.manage.dao.CheckStatDao">

    <select id="queryCheckTaxno" resultType="com.dxhy.manage.entity.SysDeptEntity">
        select *
        from sys_dept t
        where t.org_type = '5'
        and t.company in (select company from sys_dept where parent_id='94330' and org_type='1')
    </select>

    <select id="getTaxNameByTaxno" resultType="com.dxhy.manage.entity.SysDeptEntity">
        select *
        from sys_dept
        where taxno = #{taxno}
    </select>

    <select id="getCheckCount" resultType="com.dxhy.manage.model.CheckNumModel">
        SELECT t.YEAR,
               IFNULL(t.checkCount, 0)      checkCount,
               IFNULL(t1.successCount, 0)   successCount,
               ifnull(t2.account, 0)        account,
               ifnull(t3.exceptionCount, 0) exceptionCount,
               ifnull(t4.failCount, 0)      failCount
        FROM (
                 SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                        count(1)                       checkCount
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y')
             ) t
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                   count(1)                       successCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '1'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y')
        ) t1 ON t.YEAR = t1.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                   count(1)                       account
            FROM dx_invoice_check_statistics
            WHERE return_out_code IN ('0001', '0002', '0005', '0006', '0007')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y')
        ) t2 ON t.YEAR = t2.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                   count(1)                       exceptionCount
            FROM dx_invoice_check_statistics
            WHERE return_message IN ('查验异常', '调用失败', '处理失败')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y')
        ) t3 ON t.YEAR = t3.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                   count(1)                       failCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '2'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y')
        ) t4 ON t.YEAR = t4.YEAR
        limit #{start},#{limit}
    </select>

    <select id="getCheckCountMonth" resultType="com.dxhy.manage.model.CheckNumModel">
        SELECT t.YEAR,
               IFNULL(t.checkCount, 0)      checkCount,
               IFNULL(t1.successCount, 0)   successCount,
               ifnull(t2.account, 0)        account,
               ifnull(t3.exceptionCount, 0) exceptionCount,
               ifnull(t4.failCount, 0)      failCount
        FROM (
                 SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                        count(1)                          checkCount
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y-%m')
             ) t
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                   count(1)                          successCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '1'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m')
        ) t1 ON t.YEAR = t1.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                   count(1)                          account
            FROM dx_invoice_check_statistics
            WHERE return_out_code IN ('0001', '0002', '0005', '0006', '0007')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m')
        ) t2 ON t.YEAR = t2.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                   count(1)                          exceptionCount
            FROM dx_invoice_check_statistics
            WHERE return_message IN ('查验异常', '调用失败', '处理失败')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m')
        ) t3 ON t.YEAR = t3.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                   count(1)                          failCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '2'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m')
        ) t4 ON t.YEAR = t4.YEAR
        limit #{start},#{limit}
    </select>

    <select id="getCheckCountMonthCount" resultType="int">
        select count(1)
        from (
                 SELECT count(1)
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y-%m')) a
    </select>

    <select id="getCheckCountYearCount" resultType="int">
        select count(1)
        from (
                 SELECT count(1)
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y')) a
    </select>


    <select id="getCheckCountByExport" resultType="com.dxhy.manage.model.CheckNumModel">
        SELECT t.YEAR,
               IFNULL(t.checkCount, 0)      checkCount,
               IFNULL(t1.successCount, 0)   successCount,
               ifnull(t2.account, 0)        account,
               ifnull(t3.exceptionCount, 0) exceptionCount,
               ifnull(t4.failCount, 0)      failCount
        FROM (
                 SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                        count(1)                       checkCount
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y')
             ) t
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                   count(1)                       successCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '1'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y')
        ) t1 ON t.YEAR = t1.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                   count(1)                       account
            FROM dx_invoice_check_statistics
            WHERE return_out_code IN ('0001', '0002', '0005', '0006', '0007')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y')
        ) t2 ON t.YEAR = t2.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                   count(1)                       exceptionCount
            FROM dx_invoice_check_statistics
            WHERE return_message IN ('查验异常', '调用失败', '处理失败')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y')
        ) t3 ON t.YEAR = t3.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y') YEAR,
                   count(1)                       failCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '2'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y')
        ) t4 ON t.YEAR = t4.YEAR
    </select>


    <select id="getCheckCountMonthByExport" resultType="com.dxhy.manage.model.CheckNumModel">
        SELECT t.YEAR,
               IFNULL(t.checkCount, 0)      checkCount,
               IFNULL(t1.successCount, 0)   successCount,
               ifnull(t2.account, 0)        account,
               ifnull(t3.exceptionCount, 0) exceptionCount,
               ifnull(t4.failCount, 0)      failCount
        FROM (
                 SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                        count(1)                          checkCount
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y-%m')
             ) t
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                   count(1)                          successCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '1'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m')
        ) t1 ON t.YEAR = t1.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                   count(1)                          account
            FROM dx_invoice_check_statistics
            WHERE return_out_code IN ('0001', '0002', '0005', '0006', '0007')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m')
        ) t2 ON t.YEAR = t2.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                   count(1)                          exceptionCount
            FROM dx_invoice_check_statistics
            WHERE return_message IN ('查验异常', '调用失败', '处理失败')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m')
        ) t3 ON t.YEAR = t3.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m') YEAR,
                   count(1)                          failCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '2'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m')
        ) t4 ON t.YEAR = t4.YEAR
    </select>


    <select id="getDayCheckCount" resultType="com.dxhy.manage.model.CheckNumModel">
        SELECT t.YEAR,
               IFNULL(t.checkCount, 0)      checkCount,
               IFNULL(t1.successCount, 0)   successCount,
               ifnull(t2.account, 0)        account,
               ifnull(t3.exceptionCount, 0) exceptionCount,
               ifnull(t4.failCount, 0)      failCount
        FROM (
                 SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                        count(1)                             checkCount
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
             ) t
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                   count(1)                             successCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '1'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
        ) t1 ON t.YEAR = t1.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                   count(1)                             account
            FROM dx_invoice_check_statistics
            WHERE return_out_code IN ('0001', '0002', '0005', '0006', '0007')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
        ) t2 ON t.YEAR = t2.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                   count(1)                             exceptionCount
            FROM dx_invoice_check_statistics
            WHERE return_message IN ('查验异常', '调用失败', '处理失败')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
        ) t3 ON t.YEAR = t3.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                   count(1)                             failCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '2'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
        ) t4 ON t.YEAR = t4.YEAR
        limit #{start},#{limit}
    </select>

    <select id="getDayCheckCountCount" resultType="int">
        select count(1)
        from (
                 SELECT count(1)
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')) a
    </select>

    <select id="getDayCheckCountByExport" resultType="com.dxhy.manage.model.CheckNumModel">
        SELECT t.YEAR,
               IFNULL(t.checkCount, 0)      checkCount,
               IFNULL(t1.successCount, 0)   successCount,
               ifnull(t2.account, 0)        account,
               ifnull(t3.exceptionCount, 0) exceptionCount,
               ifnull(t4.failCount, 0)      failCount
        FROM (
                 SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                        count(1)                             checkCount
                 FROM dx_invoice_check_statistics
                 WHERE taxno = #{taxno}
                   AND <![CDATA[create_date >= #{beginDate}]]>
                   and <![CDATA[create_date <= #{endDate}]]>
                 GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
             ) t
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                   count(1)                             successCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '1'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
        ) t1 ON t.YEAR = t1.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                   count(1)                             account
            FROM dx_invoice_check_statistics
            WHERE return_out_code IN ('0001', '0002', '0005', '0006', '0007')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
        ) t2 ON t.YEAR = t2.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                   count(1)                             exceptionCount
            FROM dx_invoice_check_statistics
            WHERE return_message IN ('查验异常', '调用失败', '处理失败')
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
        ) t3 ON t.YEAR = t3.
            YEAR
                 LEFT JOIN (
            SELECT DATE_FORMAT(create_date, '%Y-%m-%d') YEAR,
                   count(1)                             failCount
            FROM dx_invoice_check_statistics
            WHERE check_status = '2'
              and taxno = #{taxno}
              AND <![CDATA[create_date >= #{beginDate}]]>
              and <![CDATA[create_date <= #{endDate}]]>
            GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d')
        ) t4 ON t.YEAR = t4.YEAR

    </select>
</mapper>
