<?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.ExtCompanyIndexDao">

    <select id="getJxTotalByTaxno" resultType="com.dxhy.extend.model.CompanyPoolIndexVO">
        select ifnull(count(1), 0) invoiceJxSize, ifnull(sum(invoice_amount), 0) invoiceJxAmount
        from t_dx_record_invoice
        where gf_tax_no = #{taxno}
          and date_format(create_date, '%Y-%m') = #{currMonth}
        group by gf_tax_no
    </select>

    <select id="getXxTotalByTaxno" resultType="com.dxhy.extend.model.CompanyPoolIndexVO">
        select ifnull(count(1), 0) invoiceXxSize, ifnull(sum(invoice_amount), 0) invoiceXxAmount
        from t_dx_sale_record_invoice
        where xf_tax_no = #{taxno}
          and date_format(create_date, '%Y-%m') = #{currMonth}
        group by xf_tax_no
    </select>

    <select id="getJxYearStat" resultType="com.dxhy.extend.model.YearStatVO">
        SELECT t1.date_str monthDate , COALESCE(t2.addCount, 0) as invoiceXxAmount
        FROM(
        SELECT DATE_FORMAT(@cdate:= date_add(@cdate, interval - 1 month),'%Y-%m') as date_str
        FROM(SELECT @cdate:= date_add(CURDATE(), interval + 1 month) from t_dx_record_invoice) tmp1
        where
        <![CDATA[ date_add(@cdate, interval - 1 month) >=#{startTime}]]>
        ) t1
        LEFT JOIN(

        select DATE_FORMAT(create_date,'%Y-%m') date,ifnull(sum(invoice_amount),0) addCount from
        t_dx_record_invoice
        where
        gf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        AND  <![CDATA[DATE_FORMAT(create_date,'%Y-%m') >=#{startTime}]]>
        AND  <![CDATA[DATE_FORMAT(create_date,'%Y-%m') <=#{endTime}]]>
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ) t2
        on t1.date_str = t2.date where  <![CDATA[t1.date_str >=#{startTime}]]> and
        <![CDATA[t1.date_str <=#{endTime}]]> order by t1.date_str ASC
    </select>

    <select id="getXxYearStat" resultType="com.dxhy.extend.model.YearStatVO">
        SELECT t1.date_str monthDate , COALESCE(t2.addCount, 0) as invoiceXxAmount
        FROM(
        SELECT DATE_FORMAT(@cdate:= date_add(@cdate, interval - 1 month),'%Y-%m') as date_str
        FROM(SELECT @cdate:= date_add(CURDATE(), interval + 1 month) from t_dx_sale_record_invoice) tmp1
        where
        <![CDATA[ date_add(@cdate, interval - 1 month) >=#{startTime}]]>
        ) t1
        LEFT JOIN(

        select DATE_FORMAT(create_date,'%Y-%m') date,ifnull(sum(invoice_amount),0) addCount from
        t_dx_sale_record_invoice
        where
        xf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        AND  <![CDATA[DATE_FORMAT(create_date,'%Y-%m') >=#{startTime}]]>
        AND  <![CDATA[DATE_FORMAT(create_date,'%Y-%m') <=#{endTime}]]>
        GROUP BY DATE_FORMAT(create_date,'%Y-%m')
        ) t2
        on t1.date_str = t2.date where <![CDATA[t1.date_str >=#{startTime}]]> and
        <![CDATA[t1.date_str <=#{endTime}]]> order by t1.date_str ASC
    </select>

    <select id="getJxInvoiceType" resultType="com.dxhy.extend.model.PieDataVO">
        select t.name,sum(t.value) value from (
        select invoice_type name,count(1) value from t_dx_record_invoice where
        gf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        and date_format(create_date,'%Y-%m') = #{currMonth}
        GROUP BY invoice_type
        union all
        select invoice_type name,count(1) value from t_dx_invoice_reimburse where
        gf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        and date_format(create_date,'%Y-%m') = #{currMonth}
        GROUP BY invoice_type
        ) t GROUP BY t.name
    </select>
    <!--进项风险概览-->
    <select id="getJxRiskCollect" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        a.invoiceStatusTotalAmount,
        a.glztTotalAmout,
        a.illegalSupplierTotalAmout,
        ( a.invoiceStatusTotalAmount + a.glztTotalAmout + a.illegalSupplierTotalAmout ) totalAmount
        FROM
        (
        SELECT
        SUM( CASE WHEN invoice_status != '0' THEN TRUNCATE ( total_amount, '2' ) ELSE 0 END ) AS
        invoiceStatusTotalAmount,
        SUM( CASE WHEN glzt != '0' THEN TRUNCATE ( total_amount, '2' ) ELSE 0 END ) AS glztTotalAmout,
        SUM( CASE WHEN illegal_supplier = '1' THEN TRUNCATE ( total_amount, '2' ) ELSE 0 END ) AS
        illegalSupplierTotalAmout
        FROM
        t_dx_record_invoice
        WHERE
        gf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        AND date_format(invoice_date,'%Y-%m') = #{currMonth}
        ) a
    </select>
    <!--销项风险概览-->
    <select id="getXxRiskCollect" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        a.invoiceStatusTotalAmount,
        a.glztTotalAmout,
        a.illegalSupplierTotalAmout,
        ( a.invoiceStatusTotalAmount + a.glztTotalAmout + a.illegalSupplierTotalAmout ) totalAmount
        FROM
        (
        SELECT
        SUM( CASE WHEN invoice_status != '0' THEN TRUNCATE ( total_amount, '2' ) ELSE 0 END ) AS
        invoiceStatusTotalAmount,
        SUM( CASE WHEN glzt != '0' THEN TRUNCATE ( total_amount, '2' ) ELSE 0 END ) AS glztTotalAmout,
        SUM( CASE WHEN illegal_supplier = '1' THEN TRUNCATE ( total_amount, '2' ) ELSE 0 END ) AS
        illegalSupplierTotalAmout
        FROM
        t_dx_sale_record_invoice
        WHERE
        xf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        AND date_format(invoice_date,'%Y-%m') = #{currMonth}
        ) a
    </select>
    <!--进项票池对账信息-->
    <select id="getJxAccount" resultType="com.dxhy.extend.model.AccountInfoVO">
        SELECT
        a.dictname invoiceType,
        IFNULL( taxBureauCount, 0 ) taxBureauCount,
        IFNULL( qsCount, 0 ) qsCount
        FROM
        (
        SELECT
        dictcode,
        dictname
        FROM
        dxhy_admin.sys_dictdeta
        WHERE
        dicttypecode = 'XXPCLX_ALL'
        AND dictcode !=99
        order by dictcode asc
        ) a
        LEFT JOIN
        ( SELECT
        invoice_type,
        count( 1 ) taxBureauCount
        FROM
        t_dx_record_invoice
        WHERE collect_status = '1'
        AND gf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        AND date_format(invoice_date,'%Y-%m') = #{currMonth} GROUP BY invoice_type) b ON a.dictcode = b.invoice_type
        LEFT JOIN
        ( SELECT
        invoice_type,
        count( 1 ) qsCount
        FROM t_dx_record_invoice
        WHERE qs_status = '1'
        AND gf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        AND date_format(invoice_date,'%Y-%m') = #{currMonth} GROUP BY invoice_type ) c ON a.dictcode = c.invoice_type
        order by a.dictcode asc
    </select>
    <!--销项票池对账信息-->
    <select id="getXxAccount" resultType="com.dxhy.extend.model.AccountInfoVO">
        SELECT
        a.dictname invoiceType,
        IFNULL( taxBureauCount, 0 ) taxBureauCount,
        IFNULL( qsCount, 0 ) qsCount
        FROM
        (
        SELECT
        dictcode,
        dictname
        FROM
        dxhy_admin.sys_dictdeta
        WHERE
        dicttypecode = 'XXPCLX_ALL'
        AND dictcode !=99
        ) a
        LEFT JOIN
        ( SELECT
        invoice_type,
        count( 1 ) taxBureauCount
        FROM
        t_dx_sale_record_invoice
        WHERE collect_status = '1'
        AND xf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        AND date_format(invoice_date,'%Y-%m') = #{currMonth} GROUP BY invoice_type) b ON a.dictcode = b.invoice_type
        LEFT JOIN
        ( SELECT
        invoice_type,
        count( 1 ) qsCount
        FROM t_dx_sale_record_invoice
        WHERE qs_status = '1'
        AND xf_tax_no in
        <foreach item="taxno" collection="taxnos" open="(" separator="," close=")">
            #{taxno}
        </foreach>
        AND date_format(invoice_date,'%Y-%m') = #{currMonth} GROUP BY invoice_type ) c ON a.dictcode = c.invoice_type
        order by a.dictcode asc
    </select>
</mapper>
