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

    <select id="queryList" resultType="com.dxhy.manage.entity.SysDeptEntity">
        select *
        from sys_dept t,
             sys_user_company t1
        where t.company = t1.company
          and t.org_type = '1'
          and t1.user_id = #{userId}
    </select>


    <select id="queryTaxnoList" resultType="com.dxhy.manage.entity.SysDeptEntity">
        select *
        from sys_dept
        where org_type = '5'
          and parent_id = #{deptId}
    </select>

    <select id="queryAllTable" resultType="java.lang.String">
        select major_into_where_table
        from sys_dept
        where company = #{company}
          and org_type = '5'
        group by major_into_where_table
    </select>

    <select id="getTaxnoes" resultType="java.lang.String">
        SELECT taxno
        FROM sys_dept
        WHERE company = #{company}
          AND major_into_where_table = #{table}
          AND org_type = '5'
    </select>

    <select id="getDataBase" resultType="java.lang.String">
        select major_into_where_db
        from sys_dept
        where company = #{company}
        limit 1
    </select>

    <select id="getCollectNumByTaxno" resultType="com.dxhy.manage.model.CollectNumModel">
        select new_gf_tax_no taxno ,ifnull(sum(zs),0) num from dx_taxno_everyday where
        <![CDATA[create_date <= #{endDate}]]> and <![CDATA[create_date >= #{beginDate}]]>  and
        new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item.taxno}
        </foreach>
        GROUP BY taxno
    </select>

    <select id="getTaxName" resultType="java.lang.String">
        SELECT taxname
        FROM sys_dept
        WHERE taxno = #{taxno}
          and org_type = '5'
    </select>


    <select id="getTaxnoYearTotal" resultType="com.dxhy.manage.model.BlocNumModel">
        SELECT
        DATE_FORMAT( create_date, '%Y' ) yearDate,
        ifnull(sum(zs),0) count
        FROM
        dx_taxno_everyday
        WHERE
        new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND <![CDATA[create_date <= #{endDate}]]>
        AND <![CDATA[create_date >= #{beginDate}]]>
        AND DATE_FORMAT( create_date, '%Y' ) = #{year}
        GROUP BY
        yearDate
    </select>


    <select id="getTaxnoMonthTotal" resultType="com.dxhy.manage.model.BlocNumModel">
        SELECT
        DATE_FORMAT( create_date, '%Y-%m' ) yearDate,
        ifnull(sum(zs),0) count
        FROM
        dx_taxno_everyday
        WHERE
        new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND <![CDATA[create_date <= #{endDate}]]>
        AND <![CDATA[create_date >= #{beginDate}]]>
        AND DATE_FORMAT( create_date, '%Y-%m' ) = #{month}
        GROUP BY
        yearDate
    </select>


    <select id="getConfirmTotalByDate" resultType="com.dxhy.manage.model.CollectNumModel">
        select t.new_gf_tax_no taxno ,sum(t.zs) num from (
        select new_gf_tax_no ,ifnull(count(1),0) zs from dx_record_invoice_sq where
        <![CDATA[confirm_date <= #{endDate}]]>
        AND <![CDATA[confirm_date >= #{beginDate}]]>
        and new_gf_tax_no in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY new_gf_tax_no
        union all
        select new_gf_tax_no ,ifnull(sum(total),0) zs from dx_taxno_everyday_confirm where new_gf_tax_no in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND <![CDATA[confirm_date <= #{endDate}]]>
        AND <![CDATA[confirm_date >= #{beginDate}]]>
        GROUP BY
        new_gf_tax_no) t GROUP BY t.new_gf_tax_no
    </select>

    <select id="getConfirmTotalByBelongDate" resultType="com.dxhy.manage.model.CollectNumModel">
        select t.new_gf_tax_no taxno ,sum(t.zs) num from (
        select new_gf_tax_no ,ifnull(count(1),0) zs from dx_record_invoice_sq where
        <![CDATA[rzh_belong_date <= #{endDate}]]>
        AND <![CDATA[rzh_belong_date >= #{beginDate}]]>
        and new_gf_tax_no in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY new_gf_tax_no
        union all
        select new_gf_tax_no ,ifnull(sum(total),0) zs from dx_taxno_everyday_confirm where new_gf_tax_no in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND <![CDATA[rzh_belong_date <= #{endDate}]]>
        AND <![CDATA[rzh_belong_date >= #{beginDate}]]>
        GROUP BY
        new_gf_tax_no)t GROUP BY t.new_gf_tax_no
    </select>


    <select id="getTaxnoYearConfirmTotalByBelong" resultType="com.dxhy.manage.model.BlocNumModel">

        SELECT
        SUBSTR( rzh_belong_date, 1, 4 ) yearDate,
        ifnull(sum( total ),0) count
        FROM
        dx_taxno_everyday_confirm
        WHERE
        new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND <![CDATA[rzh_belong_date <= #{endDate}]]>
        AND <![CDATA[rzh_belong_date >= #{beginDate}]]>
        AND SUBSTR( rzh_belong_date, 1, 4 ) = #{year}
        group by yearDate
    </select>


    <select id="getTaxnoYearConfirmTotalByDate" resultType="com.dxhy.manage.model.BlocNumModel">
        SELECT
        t.yearDate yearDate,
        ifnull(sum( zs ),0) count
        FROM
        (
        SELECT
        DATE_FORMAT( confirm_date, '%Y' ) yearDate,
        ifnull( count( 1 ), 0 ) zs
        FROM
        dx_record_invoice_sq
        WHERE
        <![CDATA[confirm_date <= #{endDate}]]>
        AND <![CDATA[confirm_date >= #{beginDate}]]>
        AND new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>

        AND DATE_FORMAT( confirm_date, '%Y' ) = #{year}
        GROUP BY
        yearDate UNION ALL
        SELECT
        DATE_FORMAT( confirm_date, '%Y' ) yearDate,
        ifnull(sum( total ),0) zs
        FROM
        dx_taxno_everyday_confirm
        WHERE
        new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND <![CDATA[confirm_date <= #{endDate}]]>
        AND <![CDATA[confirm_date >= #{beginDate}]]>
        AND DATE_FORMAT( confirm_date, '%Y' ) = #{year}
        ) t where t.yearDate= #{year}
        GROUP BY
        t.yearDate
    </select>


    <select id="getTaxnoMonthConfirmTotalByBelong" resultType="com.dxhy.manage.model.BlocNumModel">

        SELECT
        rzh_belong_date yearDate,
        ifnull(sum( total ),0) count
        FROM
        dx_taxno_everyday_confirm
        WHERE
        new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND <![CDATA[rzh_belong_date <= #{endDate}]]>
        AND <![CDATA[rzh_belong_date >= #{beginDate}]]>
        AND rzh_belong_date= #{month}
        group by yearDate
    </select>

    <select id="getTaxnoMonthConfirmTotalBYDate" resultType="com.dxhy.manage.model.BlocNumModel">
        SELECT
        t.yearDate yearDate,
        ifnull(sum( zs ),0) count
        FROM
        (
        SELECT
        DATE_FORMAT( confirm_date, '%Y-%m' ) yearDate,
        ifnull( count( 1 ), 0 ) zs
        FROM
        dx_record_invoice_sq
        WHERE
        <![CDATA[confirm_date <= #{endDate}]]>
        AND <![CDATA[confirm_date >= #{beginDate}]]>
        AND new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>

        AND DATE_FORMAT( confirm_date, '%Y-%m' ) = #{month}
        GROUP BY
        yearDate UNION ALL
        SELECT
        DATE_FORMAT( confirm_date, '%Y-%m' ) yearDate,
        ifnull(sum( total ),0) zs
        FROM
        dx_taxno_everyday_confirm
        WHERE
        new_gf_tax_no IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND <![CDATA[confirm_date <= #{endDate}]]>
        AND <![CDATA[confirm_date >= #{beginDate}]]>
        AND DATE_FORMAT( confirm_date, '%Y-%m' ) = #{month}
        ) t where t.yearDate= #{month}
        GROUP BY
        t.yearDate

    </select>


    <select id="getAllOrgInfo" resultType="com.dxhy.manage.model.CollectNumModel">
        select taxno, taxname, '0' num
        from sys_dept
        where company = #{company}
          and org_type = '5'
    </select>

    <select id="getSysDept" resultType="com.dxhy.manage.entity.SysDeptEntity">
        SELECT taxno, taxname, create_date, major_into_where_table
        FROM sys_dept
        WHERE company = #{company}
          AND org_type = '5'
        order by create_date
    </select>

    <select id="getTaxNameByTaxno" resultType="com.dxhy.manage.entity.SysDeptEntity">
        SELECT taxno, taxname, create_date, major_into_where_table
        FROM sys_dept
        WHERE taxno = #{taxno}
          and org_type = '5'
    </select>


    <select id="getConfirmTotalByDateTaxno" resultType="com.dxhy.manage.model.CollectNumModel">
        select new_gf_tax_no taxno, ifnull(sum(total), 0) num
        from dx_taxno_everyday_confirm
        where new_gf_tax_no = #{entity.taxno}
          AND <![CDATA[rzh_belong_date <= #{endDate}]]>
          AND <![CDATA[rzh_belong_date >= #{beginDate}]]>
        GROUP BY new_gf_tax_no
    </select>


    <select id="getConfirmTotalByBelongDateTaxno" resultType="com.dxhy.manage.model.CollectNumModel">
        select t.new_gf_tax_no taxno, sum(t.zs) num
        from (
                 select new_gf_tax_no, ifnull(count(1), 0) zs
                 from ${entity.majorIntoWhereTable}
                 where
        <![CDATA[rzh_belong_date <= #{endDate}]]>
                   AND <![CDATA[rzh_belong_date >= #{beginDate}]]>
                   and new_gf_tax_no = #{entity.taxno}
                 GROUP BY new_gf_tax_no
                 union all
                 select new_gf_tax_no, ifnull(sum(total), 0) zs
                 from dx_taxno_everyday_confirm
                 where new_gf_tax_no = #{entity.taxno}
                   AND <![CDATA[rzh_belong_date <= #{endDate}]]>
                   AND <![CDATA[rzh_belong_date >= #{beginDate}]]>
                 GROUP BY new_gf_tax_no) t
        GROUP BY t.new_gf_tax_no
    </select>


    <select id="getTaxnoesCount" resultType="int">
        select count(1)
        from sys_dept
        where company = #{company}
          and org_type != 1
    </select>
</mapper>
