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

    <resultMap id="BaseResultMap"
               type="com.dxhy.core.model.BbfpzhmxcxRespVO">
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_no" property="invoiceNo"/>
        <result column="kprq" property="invoiceDate"/>
        <result column="invoice_type" property="invoiceType"/>
        <result column="gfsh" property="gfsh"/>
        <result column="gfmc" property="gfmc"/>
        <result column="gfdzdh" property="gfdzdh"/>
        <result column="gfkhhzh" property="gfkhhzh"/>
        <result column="xfsh" property="xfsh"/>
        <result column="xfmc" property="xfmc"/>
        <result column="xfdzdh" property="xfdzdh"/>
        <result column="xfkhhzh" property="xfkhhzh"/>
        <result column="invoice_amount" property="invoiceAmount"/>
        <result column="tax_amount" property="taxAmount"/>
        <result column="jshj" property="jshj"/>
        <result column="hlmc" property="hlmc"/>
        <result column="ggxh" property="ggxh"/>
        <result column="dw" property="dw"/>
        <result column="num" property="num"/>
        <result column="dj" property="dj"/>
        <result column="je" property="je"/>
        <result column="se" property="se"/>
        <result column="sl" property="sl"/>
        <result column="bz" property="bz"/>
        <result column="uuid" property="uuid"/>
        <result column="in_account_status" property="inAccountStatus"/>
        <result column="cph" property="cph"/>
        <result column="lx" property="lx"/>
        <result column="txrqq" property="txrqq"/>
        <result column="txrqz" property="txrqz"/>
    </resultMap>

    <select id="selectByPramsMap" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="mysql">
        select
        t.invoice_code,t.invoice_no,DATE_FORMAT(t.invoice_date,'%Y-%m-%d')
        kprq,t.gf_tax_no gfsh,t.gf_name gfmc,t.gf_address_and_phone gfdzdh,t.gf_bank_and_no gfkhhzh,
        t.invoice_type,
        t.xf_tax_no xfsh,t.xf_name xfmc,t.xf_address_and_phone xfdzdh,t.xf_bank_and_no
        xfkhhzh,t.tax_amount,t.invoice_amount,t.rzh_belong_date skssq,
        t.total_amount jshj,t.remark bz,t.uuid,b.goods_name hlmc,b.model ggxh,b.unit dw,b.num num,b.unit_price
        dj,b.detail_amount je,b.tax_amount se,b.tax_rate sl,
        t.in_account_status,b.cph,b.lx,b.txrqq,b.txrqz
        from
        ${dxhyTertiary}.t_dx_record_invoice t
        left JOIN ${dxhyDetail}.t_dx_record_invoice_detail b ON t.uuid = b.uuid
        WHERE t.invoice_date between #{kpksrq} and #{kpjsrq}
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="invoiceStatus != null and invoiceStatus != '' and invoiceStatus != 'null' and invoiceStatus != '99' ">
            and t.invoice_status = #{invoiceStatus}
        </if>
        <if test="qszt != null and qszt != '' and qszt != 'null' and qszt != '99'">
            and t.qs_status = #{qszt}
        </if>
        <if test="qsfs != null and qsfs != '' and qsfs != 'null' and qsfs != '99'">
            and t.qs_type = #{qsfs}
        </if>
        <if test="qsksrq != null and qsksrq != '' and qsksrq != 'null' and qsjsrq != null and qsjsrq != '' and qsjsrq != 'null'">
            and t.qs_date between #{qsksrq} and #{qsjsrq}
        </if>
        <if test="rzksrq != null and rzksrq != '' and rzksrq != 'null' and rzjsrq != null and rzjsrq != '' and rzjsrq != 'null'">
            and t.rzh_date between #{rzksrq} and #{rzjsrq}
        </if>
        <if test="rzzt != null and rzzt != '' and rzzt != 'null'  and rzzt != '99'">
            and t.rzh_yesorno = #{rzzt}
        </if>
        <if test="rzclzt != null and rzclzt != '' and rzclzt != 'null'  and rzclzt != '99'">
            and t.auth_status = #{rzclzt}
        </if>
        <if test="rzlx != null and rzlx != '' and rzlx != 'null' and rzlx != '99'">
            and t.bdk_status = #{rzlx}
        </if>
        <if
                test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '99' ">
            and t.invoice_type = #{invoiceType}
        </if>
        <if test="invoiceSource != null and invoiceSource != '' and invoiceSource != 'null' and invoiceSource != '99' ">
            and t.invoice_source = #{invoiceSource}
        </if>
        <if
                test="qsfs != null and qsfs != '' and qsfs != 'null' and qsfs != '99' ">
            and t.qs_type = #{qsfs}
        </if>
        <if test="xfmc != null and xfmc != '' and xfmc != 'null' ">
            and t.xf_name like CONCAT(#{xfmc},'%')
        </if>
        <if test="xfsh != null and xfsh != '' and xfsh != 'null' ">
            and SUBSTR(t.xf_tax_no , -6) = #{xfsh}
        </if>
        <if test="amountMin!=null and amountMin!=''">
            and t.invoice_amount &gt;= #{amountMin}
        </if>

        <if test="amountMax!=null and amountMax!=''">
            and t.invoice_amount &lt;= #{amountMax}
        </if>
        <if test="taxAmountMin!=null and taxAmountMin!=''">
            and t.tax_amount &gt;= #{taxAmountMin}
        </if>

        <if test="taxAmountMax!=null and taxAmountMax!=''">
            and t.tax_amount &lt;= #{taxAmountMax}
        </if>
        <if
                test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null'  ">
            and t.invoice_no = #{invoiceNo}
        </if>
        <if test="skssq != null and skssq != '' and skssq != 'null' ">
            and t.rzh_belong_date = #{skssq}
        </if>
        <if test="rzksrq != null and rzjsrq != null ">
            and t.rzh_date between #{rzksrq} and #{rzjsrq}
        </if>
        <if test="qsjsrq != null and qsjsrq != null ">
            and t.qs_date between #{qsksrq} and #{qsjsrq}
        </if>
        <if test="qsfs != null and qsfs != '' and qsfs != 'null' ">
            and t.qs_type = #{qsfs}
        </if>
        <if test="glzt != null and glzt != '' and glzt != 'null' and glzt != '99'">
            and t.glzt = #{glzt}
        </if>
        <if test="rzhr != null and rzhr != '' and rzhr != 'null' ">
            and t.confirm_user like CONCAT(#{rzhr},'%')
        </if>
        <if test="qsr != null and qsr != '' and qsr != 'null' ">
            and t.qs_name = #{qsr}
        </if>
        <if test="businessCode != '99' and businessCode != null and businessCode != '' and businessCode != 'null' ">
            and t.dept_id = #{businessCode}
        </if>
        <if test="businessCode==''">
            and (t.dept_id is null or t.dept_id='' or t.dept_id='99')
        </if>
        <if test='businessCode == "99" and business!=null and business.size()>0'>
            and (t.dept_id in
            <foreach collection="business" index="index" item="item" open="(" separator="," close="">
                #{item.businessCode}
            </foreach>
            ) or (t.dept_id is null or t.dept_id='' or t.dept_id='99'))
        </if>
        <if test='inAccountStatus !=null and inAccountStatus!="" and inAccountStatus!="99"'>
            and t.in_account_status = #{inAccountStatus}
        </if>
        <if test="company !=null and company !=''">
            and t.company = #{company}
        </if>
        <if test="voucherNumber!=null and voucherNumber!='' and voucherNumber!='null'">
            and t.voucher_number like concat('%',concat(#{voucherNumber},'%'))
        </if>
        order by t.invoice_date desc
    </select>
    <select id="selectByPramsMap" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="oracle">
        select
        t.invoice_code,t.invoice_no,to_char(t.invoice_date,'yyyy-mm-dd')
        kprq,t.gf_tax_no gfsh,t.gf_name gfmc,t.gf_address_and_phone gfdzdh,t.gf_bank_and_no gfkhhzh,
        t.invoice_type,
        t.xf_tax_no xfsh,t.xf_name xfmc,t.xf_address_and_phone xfdzdh,t.xf_bank_and_no
        xfkhhzh,t.tax_amount,t.invoice_amount,t.rzh_belong_date skssq,
        t.total_amount jshj,t.remark bz,t.uuid,b.goods_name hlmc,b.model ggxh,b.unit dw,b.num num,b.unit_price
        dj,b.detail_amount je,b.tax_amount se,b.tax_rate sl,
        t.in_account_status,b.cph,b.lx,b.txrqq,b.txrqz
        from
        ${dxhyTertiary}.t_dx_record_invoice t
        left JOIN ${dxhyDetail}.t_dx_record_invoice_detail b ON t.uuid = b.uuid
        where
        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>
        <if test="invoiceStatus != null and invoiceStatus != '' and invoiceStatus != 'null' and invoiceStatus != '99' ">
            and t.invoice_status = #{invoiceStatus}
        </if>
        <if test="qszt != null and qszt != '' and qszt != 'null' and qszt != '99' ">
            and t.qs_status = #{qszt}
        </if>
        <if test="rzzt != null and rzzt != '' and rzzt != 'null' and rzzt != '99' ">
            and t.rzh_yesorno = #{rzzt}
        </if>
        <if test="qsksrq != null and qsksrq != '' and qsksrq != 'null' and qsjsrq != null and qsjsrq != '' and qsjsrq != 'null'">
            and t.qs_date between to_date(#{qsksrq},'yyyy-mm-dd hh24:mi:ss') and to_date(#{qsjsrq},'yyyy-mm-dd
            hh24:mi:ss')
        </if>
        <if test="rzksrq != null and rzksrq != '' and rzksrq != 'null' and rzjsrq != null and rzjsrq != '' and rzjsrq != 'null'">
            and t.rzh_date between to_date(#{rzksrq},'yyyy-mm-dd hh24:mi:ss') and to_date(#{rzjsrq},'yyyy-mm-dd
            hh24:mi:ss')
        </if>
        <if test="rzclzt != null and rzclzt != '' and rzclzt != 'null' and rzclzt != '99' ">
            and t.auth_status = #{rzclzt}
        </if>
        <if test="rzlx != null and rzlx != '' and rzlx != 'null' and rzlx != '99'">
            and t.bdk_status = #{rzlx}
        </if>
        <if
                test="invoiceType != null and invoiceType != '' and invoiceType != 'null' and invoiceType != '99' ">
            and t.invoice_type = #{invoiceType}
        </if>
        <if test="invoiceSource != null and invoiceSource != '' and invoiceSource != 'null' and invoiceSource != '99' ">
            and t.invoice_source = #{invoiceSource}
        </if>
        <if
                test="qsfs != null and qsfs != '' and qsfs != 'null'  and qsfs != '99'">
            and t.qs_type = #{qsfs}
        </if>
        <if test="xfmc != null and xfmc != '' and xfmc != 'null' ">
            and t.xf_name like CONCAT(#{xfmc},'%')
        </if>
        <if test="xfsh != null and xfsh != '' and xfsh != 'null' ">
            and SUBSTR(t.xf_tax_no , -6) = #{xfsh}
        </if>
        <if test="amountMin!=null and amountMin!=''">
            and t.invoice_amount &gt;= #{amountMin}
        </if>

        <if test="amountMax!=null and amountMax!=''">
            and t.invoice_amount &lt;= #{amountMax}
        </if>
        <if test="taxAmountMin!=null and taxAmountMin!=''">
            and t.tax_amount &gt;= #{taxAmountMin}
        </if>

        <if test="taxAmountMax!=null and taxAmountMax!=''">
            and t.tax_amount &lt;= #{taxAmountMax}
        </if>
        <if
                test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        <if test="skssq != null and skssq != '' and skssq != 'null' ">
            and t.rzh_belong_date = #{skssq}
        </if>
        <if test="rzksrq != null and rzjsrq != null">
            and t.rzh_date between to_date(#{rzksrq},'yyyy-mm-dd hh24:mi:ss') and to_date(#{rzjsrq},'yyyy-mm-dd
            hh24:mi:ss')
        </if>
        <if test="qsfs != null and qsfs != '' and qsfs != 'null' ">
            and t.qs_type = #{qsfs}
        </if>
        <if test="qsksrq != null and qsjsrq != null">
            and t.qs_date between to_date(#{qsksrq},'yyyy-mm-dd hh24:mi:ss') and to_date(#{qsjsrq},'yyyy-mm-dd
            hh24:mi:ss')
        </if>
        <if test="glzt != null and glzt != '' and glzt != 'null' and glzt != '99'">
            and t.glzt = #{glzt}
        </if>
        <if test="rzhr != null and rzhr != '' and rzhr != 'null' ">
            and t.confirm_user like CONCAT(#{rzhr},'%')
        </if>
        <if test="qsr != null and qsr != '' and qsr != 'null' ">
            and t.qs_name = #{qsr}
        </if>
        <if test="businessCode != '99' and businessCode != null and businessCode != '' and businessCode != 'null' ">
            and t.dept_id = #{businessCode}
        </if>
        <if test="businessCode==''">
            and (t.dept_id is null or t.dept_id='' or t.dept_id='99')
        </if>
        <if test='businessCode == "99" and business!=null and business.size()>0'>
            and (t.dept_id in
            <foreach collection="business" index="index" item="item" open="(" separator="," close="">
                #{item.businessCode}
            </foreach>
            ) or (t.dept_id is null or t.dept_id='' or t.dept_id='99'))
        </if>
        <if test='inAccountStatus !=null and inAccountStatus!="" and inAccountStatus!="99"'>
            and t.in_account_status = #{inAccountStatus}
        </if>
        <if test="company !=null and company !=''">
            and t.company = #{company}
        </if>
        order by t.invoice_date desc
    </select>

</mapper>
