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

    <resultMap id="BbycfpcxhjResultMap"
               type="com.dxhy.extend.model.ExtBbycfpbbRespVO">
        <result column="hjsl" property="hjsl"/>
        <result column="hjje" property="hjje"/>
        <result column="hjse" property="hjse"/>
        <result column="gfsh" property="gfsh"/>
        <result column="gfmc" property="gfmc"/>
        <result column="yczt" property="yczt"/>
    </resultMap>

    <resultMap id="hjxxMap"
               type="com.dxhy.extend.model.ExtBbycfpbbRespVO">
        <result column="hjsl" property="id"/>
        <result column="hjje" property="hjje"/>
        <result column="hjse" property="hjse"/>
    </resultMap>

    <select id="selectBbycfpbbHj" parameterType="java.util.Map"
            resultMap="BbycfpcxhjResultMap" databaseId="mysql">

        select t.invoice_status yczt,t.gf_tax_no gfsh,t.gf_name gfmc,count(1)
        hjsl,sum(t.invoice_amount)hjje,sum(t.tax_amount)hjse
        from t_dx_record_invoice t
        where t.invoice_status != '0'
        and (t.invoice_type in (${krzpz}) or (t.invoice_type = '32' and t.lq_tdyslxDm = '08'))
        and t.company = #{company}
        and DATE_FORMAT(t.invoice_date,'%Y-%m-%d') between #{kpksrq} and #{kpjsrq}
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <choose>
            <when test="yczt != null and yczt != '' and yczt != 'null'">
                and t.invoice_status = #{yczt}
            </when>
            <otherwise>
                and t.invoice_status <![CDATA[ <> ]]> 0
            </otherwise>
        </choose>

        group by t.invoice_status,t.gf_tax_no
    </select>
    <select id="selectBbycfpbbHj" parameterType="java.util.Map"
            resultMap="BbycfpcxhjResultMap" databaseId="oracle">

        select t.invoice_status yczt,o.nsrsbh gfsh,o.nsrmc gfmc,count(1)
        hjsl,sum(t.invoice_amount)hjje,sum(t.tax_amount)hjse
        from t_dx_record_invoice t left join sys_enterprise_config o on t.gf_tax_no =
        o.nsrsbh
        where t.invoice_status != '0'
        and (t.invoice_type in (${krzpz}) or (t.invoice_type = '32' and t.lq_tdyslxDm = '08'))
        and t.company = #{company}
        and to_char(t.invoice_date,'yyyy-mm-dd') between #{kpksrq} and #{kpjsrq}
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <choose>
            <when test="yczt != '0' and yczt != '' and yczt != null">
                and t.invoice_status = #{yczt}
            </when>
            <otherwise>
                and t.invoice_status <![CDATA[ <> ]]> 0
            </otherwise>
        </choose>

        group by t.invoice_status,o.nsrsbh,o.nsrmc
    </select>

    <select id="selectHjxx" parameterType="java.util.Map"
            resultMap="hjxxMap" databaseId="mysql">

        select count(1) hjsl,
        ifnull(sum(t.invoice_amount),0) hjje,
        ifnull(sum(t.tax_amount),0) hjse
        from t_dx_record_invoice t
        where t.invoice_status != '0'
        and (t.invoice_type in (${krzpz}) or (t.invoice_type = '32' and t.lq_tdyslxDm = '08'))
        and t.company = #{company}
        and DATE_FORMAT(t.invoice_date,'%Y-%m-%d') between #{kpksrq} and #{kpjsrq}
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <choose>
            <when test="yczt != null and yczt != '' and yczt != '0'">
                and t.invoice_status = #{yczt}
            </when>
            <otherwise>
                and t.invoice_status <![CDATA[ <> ]]> 0
            </otherwise>
        </choose>
    </select>
    <select id="selectHjxx" parameterType="java.util.Map"
            resultMap="hjxxMap" databaseId="oracle">

        select count(1) hjsl,
        COALESCE(sum(t.invoice_amount),0) hjje,
        COALESCE(sum(t.tax_amount),0) hjse
        from t_dx_record_invoice t left join sys_enterprise_config o on t.gf_tax_no =
        o.nsrmc
        where t.invoice_status != '0'
        and (t.invoice_type in (${krzpz}) or (t.invoice_type = '32' and t.lq_tdyslxDm = '08'))
        and t.company = #{company}
        and t.invoice_date between to_date(#{kpksrq},'yyyy-mm-dd hh24:mi:ss') and
        to_date(#{kpjsrq},'yyyy-mm-dd hh24:mi:ss')
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <choose>
            <when test="yczt != null and yczt != '' and yczt != '0'">
                and t.invoice_status = #{yczt}
            </when>
            <otherwise>
                and t.invoice_status <![CDATA[ <> ]]> 0
            </otherwise>
        </choose>
    </select>

</mapper>
