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

    <resultMap id="BaseResultMap"
               type="com.dxhy.core.model.FpRzRespVO">
        <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="xf_tax_no" property="xfsh"/>
        <result column="xf_name" property="xfmc"/>
        <result column="invoice_amount" property="invoiceAmount"/>
        <result column="tax_amount" property="taxAmount"/>
        <result column="dk_tax_amount" property="dkTaxAmount"/>
        <result column="invoice_status" property="invoiceStatus"/>
        <result column="rzh_date" property="rzsj"/>
        <result column="qs_date" property="qsrq"/>
        <result column="rzh_type" property="rzhType"/>
        <result column="dqskssq" property="skssq"/>
        <result column="auth_status" property="rzclzt"/>
        <result column="qs_type" property="qsfs"/>
        <result column="qs_status" property="qszt"/>
        <result column="confirm_user" property="rzr"/>
        <result column="qs_name" property="qsr"/>
        <result column="rzh_yesorno" property="rzzt"/>
        <result column="deptId" property="deptId"/>
        <result column="deptName" property="businessName"/>
        <result column="cxrz_status" property="cxrzStatus"/>
        <result column="bdk_status" property="bdkStatus"/>
        <result column="export_mark" property="exportMark"/>
        <result column="in_account_status" property="inAccountStatus"/>
        <result column="voucher_number" property="voucherNumber"/>
        <result column="ele_invoice_no" property="eleInvoiceNo"/>

    </resultMap>

    <resultMap id="RzcxhjResultMap"
               type="com.dxhy.core.model.FpRzRespVO">
        <result column="hjsl" property="id"/>
        <result column="hjje" property="invoiceAmount"/>
        <result column="hjse" property="taxAmount"/>
    </resultMap>

    <resultMap id="authenticationListMap"
               type="com.dxhy.core.model.AuthenticationCheckListVO">
        <result column="id" jdbcType="BIGINT" property="id"/>
        <result column="invoice_code" jdbcType="VARCHAR" property="invoiceCode"/>
        <result column="invoice_no" jdbcType="VARCHAR" property="invoiceNo"/>
        <result column="invoice_type" jdbcType="VARCHAR" property="invoiceType"/>
        <result column="invoice_status" jdbcType="VARCHAR" property="invoiceStatus"/>
        <result column="invoice_date" jdbcType="TIMESTAMP" property="invoiceDate"/>
        <result column="gf_tax_no" jdbcType="VARCHAR" property="buyerTax"/>
        <result column="gf_name" jdbcType="VARCHAR" property="buyerName"/>
        <result column="xf_tax_no" jdbcType="VARCHAR" property="sellerTax"/>
        <result column="xf_name" jdbcType="VARCHAR" property="sellerName"/>
        <result column="invoice_amount" jdbcType="VARCHAR" property="invoiceAmount"/>
        <result column="tax_amount" jdbcType="VARCHAR" property="taxAmount"/>
        <result column="qs_status" jdbcType="VARCHAR" property="signInStatus"/>
        <result column="qs_date" jdbcType="TIMESTAMP" property="signInDate"/>
        <result column="qs_type" jdbcType="VARCHAR" property="signInType"/>
        <result column="qs_name" jdbcType="VARCHAR" property="signInPeople"/>
        <result column="rzh_date" jdbcType="TIMESTAMP" property="authenticationDate"/>
        <result column="rzh_yesorno" jdbcType="VARCHAR" property="authenticationStatus"/>
        <result column="auth_status" jdbcType="VARCHAR" property="authenticationDetailStatus"/>
        <result column="rzlx" jdbcType="VARCHAR" property="authenticationType"/>
        <result column="gx_user_name" jdbcType="VARCHAR" property="authenticationPerson"/>
        <result column="rzh_belong_date" jdbcType="VARCHAR" property="taxPeriod"/>
        <result column="dept_id" jdbcType="VARCHAR" property="deptId"/>
    </resultMap>

    <sql id="Authentication_Check_Column_List">
        t.id,invoice_code,invoice_no,invoice_type,invoice_status,DATE_FORMAT(invoice_date,'%Y-%m-%d') invoice_date,gf_tax_no,gf_name,xf_tax_no,xf_name,invoice_amount,tax_amount,qs_status,date_format(qs_date,'%Y-%m-%d') qs_date,qs_type,qs_name,date_format(rzh_date,'%Y-%m-%d') rzh_date,rzh_yesorno,auth_status,rzlx,gx_user_name,rzh_belong_date,b.business_name dept_id
    </sql>

    <select id="selectByPramsMap" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="mysql">
        select
        t.invoice_code,t.invoice_no,t.invoice_type,DATE_FORMAT(t.invoice_date,'%Y-%m-%d')kprq,t.gf_tax_no
        gfsh,t.gf_name
        gfmc,t.dept_id deptId,t.dept_name deptName,
        t.xf_tax_no,t.xf_name,t.tax_amount,t.dk_tax_amount,t.invoice_status,t.invoice_amount,
        date_format(t.rzh_date,'%Y-%m-%d') rzh_date,date_format(t.qs_date, '%Y-%m-%d')
        qs_date,t.rzh_type,
        t.gx_user_name,t.rzh_belong_date
        dqskssq,t.auth_status,t.qs_type,t.qs_status,t.confirm_user,t.qs_name,t.rzh_yesorno,t.cxrz_status,t.bdk_status, t.ele_invoice_no,
        t.export_mark,t.in_account_status,t.payment_status,DATE_FORMAT(t.payment_date,'%Y-%m-%d')payment_date,t.voucher_number
        from t_dx_record_invoice t
        WHERE date_format(t.invoice_date,'%Y-%m-%d') between #{kpksrq} and #{kpjsrq}
        and (t.invoice_type in('01','03','08','14','31','283','161','183','185') or (t.invoice_type ='32' and t.lq_tdyslxDm='08'))
        and t.bdk_status in ('1','2')
        and t.export_mark = '0'
        and t.auth_status != '0'
        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="voucherNumber != null and voucherNumber != ''">
            and t.voucher_number like concat('%',concat(#{voucherNumber},'%'))
        </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 date_format(t.qs_date,'%Y-%m-%d') between #{qsksrq} and #{qsjsrq}
        </if>
        <if
                test="rzksrq != null and rzksrq != '' and rzksrq != 'null' and rzjsrq != null and rzjsrq != '' and rzjsrq != 'null'">
            and date_format(t.rzh_date,'%Y-%m-%d') 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="paymentDateStart != null and paymentDateStart != '' and paymentDateStart != 'null' ">
            <![CDATA[ AND date_format(t.payment_date,'%Y-%m-%d') >= #{paymentDateStart} ]]>
        </if>
        <if test="paymentDateEnd != null and paymentDateEnd != '' and paymentDateEnd != 'null' ">
            <![CDATA[ AND date_format(t.payment_date,'%Y-%m-%d')  <= #{paymentDateEnd} ]]>
        </if>
        <if test="paymentStatus != null and paymentStatus != '' and paymentStatus != '99'">
            and t.payment_status = #{paymentStatus}
        </if>
        <if test="rzlx !=null and rzlx != ''">
            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="eleInvoiceNo!=null and eleInvoiceNo!='' and eleInvoiceNo!='null'">
            and t.ele_invoice_no = #{eleInvoiceNo}
        </if>
        <if test="xfmc != null and xfmc != '' and xfmc != 'null' ">
            and t.xf_name like CONCAT(#{xfmc},'%')
        </if>
        <if test="rzhr != null and rzhr != '' and rzhr != 'null' ">
            and t.confirm_user in
            <foreach collection="rzhr" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </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="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="qsr != null and qsr != '' and qsr != 'null' ">
            and t.qs_name = #{qsr}
        </if>
        order by t.rzh_date desc,t.invoice_date desc
    </select>
    <select id="selectByPramsMap" parameterType="java.util.Map"
            resultMap="BaseResultMap" databaseId="oracle">
        select
        t.invoice_code,t.invoice_no,t.invoice_type,to_char(t.invoice_date,'yyyy-mm-dd') kprq,
        t.gf_tax_no gfsh,t.gf_name gfmc,t.dept_id deptId,t.dept_name deptName,
        t.xf_tax_no,t.xf_name,t.tax_amount,t.dk_tax_amount,t.invoice_status,t.invoice_amount,
        to_char(t.rzh_date,'yyyy-mm-dd') rzh_date,to_char(t.qs_date,'yyyy-mm-dd')
        qs_date,t.rzh_type,t.id
        id,t.ele_invoice_no,
        t.gx_user_name,t.rzh_belong_date
        dqskssq,t.auth_status,t.qs_type,t.qs_status,t.confirm_user,t.qs_name,t.rzh_yesorno,t.cxrz_status,t.bdk_status,
        t.export_mark,t.in_account_status,t.payment_status,to_date(t.payment_date,'yyyy-mm-dd')payment_date
        from t_dx_record_invoice t
        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.auth_status != '0'
        and (t.invoice_type in('01','03','08','14','31','283','161','183','185') or (t.invoice_type ='32' and t.lq_tdyslxDm='08'))
        and t.bdk_status in ('1','2')
        and t.export_mark = '0'
        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 t.bdk_status = #{rzlx}
        </if>
        <if test="paymentDateStart != null and paymentDateStart != '' and paymentDateStart != 'null' ">
            <![CDATA[ AND date_format(t.payment_date,'%Y-%m-%d') >= #{paymentDateStart} ]]>
        </if>
        <if test="paymentDateEnd != null and paymentDateEnd != '' and paymentDateEnd != 'null' ">
            <![CDATA[ AND date_format(t.payment_date,'%Y-%m-%d')  <= #{paymentDateEnd} ]]>
        </if>
        <if test="paymentStatus != null and paymentStatus != '' and map.paymentStatus != '99'">
            and t.payment_status = #{paymentStatus}
        </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="eleInvoiceNo!=null and eleInvoiceNo!='' and eleInvoiceNo!='null'">
            and t.ele_invoice_no = #{eleInvoiceNo}
        </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="rzhr != null and rzhr != '' and rzhr != 'null' ">
            and t.confirm_user in
            <foreach collection="rzhr" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </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="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="qsr != null and qsr != '' and qsr != 'null' ">
            and t.qs_name = #{qsr}
        </if>
        order by t.rzh_date desc,t.invoice_date desc
    </select>


    <select id="selectTaxRebateByPramsMap"
            parameterType="java.util.Map"
            resultMap="authenticationListMap" databaseId="mysql">
        select
        <include refid="Authentication_Check_Column_List"/>
        from t_dx_record_invoice t
        left join ${dxhyAdmin}.sys_business b on t.dept_id = b.business_code
        WHERE date_format(t.invoice_date,'%Y-%m-%d') between #{invoiceStartTime} and #{invoiceEndTime}
        and (t.rzlx = '2' or t.rzlx = '3')
        and t.export_mark = '0'
        and t.gf_tax_no in
        <foreach collection="taxNoList" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        <if test="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="invoiceStatus != '99'">
            and t.invoice_status = #{invoiceStatus}
        </if>
        <if test="authenticationStatus != '99'">
            and t.rzh_yesorno = #{authenticationStatus}
        </if>
        <if test='authenticationStatus == "1"'>
            and date_format(t.rzh_date,'%Y-%m-%d') between #{authenticationStartTime} and
            #{authenticationEndTime}
        </if>
        <if test="authenticationDetailStatus != '99'">
            and t.auth_status = #{authenticationDetailStatus}
        </if>
        <if
                test="authenticationPeople != null and authenticationPeople != '' and authenticationPeople != 'null'">
            and t.gx_user_name like concat(#{authenticationPeople},'%')
        </if>
        <if test="deptId != null and deptId != '' and deptId != 'null' and deptId != '99' ">
            and t.dept_id = #{deptId}
        </if>
        <if test="deptId == ''">
            and t.dept_id is null
        </if>
        <if test="currentPeriod != null and currentPeriod != '' and currentPeriod != 'null'">
            and t.rzh_belong_date = #{currentPeriod}
        </if>
        <if test="sellerName != null and sellerName != '' and sellerName != 'null' ">
            and t.xf_name like concat(#{sellerName},'%')
        </if>
        <if test="signInStatus != '99'">
            and t.qs_status = #{signInStatus}
        </if>
        <if test='signInStatus == "1"'>
            and date_format(t.qs_date,'%Y-%m-%d') between #{signInStartTime} and #{signInEndTime}
        </if>
        <if test="signInType != '99'">
            and t.qs_type = #{signInType}
        </if>
        <if test="signInPerson != '' and signInPerson != null and signInPerson != 'null'">
            and t.qs_name =#{signInPerson}
        </if>
        <if test="company !=null and company!=''">
            and t.company = #{company}
        </if>
        order by t.qs_date desc,t.invoice_date desc
    </select>
    <select id="selectTaxRebateByPramsMap"
            parameterType="java.util.Map"
            resultMap="authenticationListMap" databaseId="oracle">
        select
        t.id,invoice_code,invoice_no,invoice_type,invoice_status,to_char(invoice_date,'yyyy-mm-dd')
        invoice_date,
        gf_tax_no,gf_name,xf_tax_no,xf_name,invoice_amount,tax_amount,qs_status,to_char(qs_date,'yyyy-mm-dd')
        qs_date,
        qs_type,qs_name,to_char(rzh_date,'yyyy-mm-dd')
        rzh_date,rzh_yesorno,auth_status,rzlx,gx_user_name,
        rzh_belong_date,b.business_name dept_id
        from t_dx_record_invoice t
        left join ${dxhyAdmin}.sys_business b on t.dept_id = b.business_code
        WHERE to_char(t.invoice_date,'yyyy-mm-dd') between #{invoiceStartTime} and #{invoiceEndTime}
        and (t.rzlx = '2' or t.rzlx = '3')
        and t.export_mark = '0'
        and t.gf_tax_no in
        <foreach collection="taxNoList" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="invoiceNo != null and invoiceNo != '' and invoiceNo != 'null' ">
            and t.invoice_no = #{invoiceNo}
        </if>
        <if test="invoiceType != '99' and invoiceType!='' and invoiceType!='null' and invoiceType!=null">
            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="invoiceStatus != '99'">
            and t.invoice_status = #{invoiceStatus}
        </if>
        <if test="authenticationStatus != '99'">
            and t.rzh_yesorno = #{authenticationStatus}
        </if>
        <if test='authenticationStatus == "1"'>
            and to_char(t.rzh_date,'yyyy-mm-dd') between #{rzksrq} and #{rzjsrq}
        </if>
        <if test="authenticationDetailStatus != '99'">
            and t.auth_status = #{authenticationDetailStatus}
        </if>
        <if
                test="authenticationPeople != null and authenticationPeople != '' and authenticationPeople != 'null'">
            and t.gx_user_name like concat(#{authenticationPeople},'%')
        </if>
        <if test="deptId != null and deptId != '' and deptId != 'null' and deptId != '99' ">
            and t.dept_id = #{deptId}
        </if>
        <if test="deptId == ''">
            and t.dept_id is null
        </if>
        <if test="currentPeriod != null and currentPeriod != '' and currentPeriod != 'null'">
            and t.rzh_belong_date = #{currentPeriod}
        </if>
        <if test="sellerName != null and sellerName != '' and sellerName != 'null' ">
            and t.xf_name like concat(#{sellerName},'%')
        </if>
        <if test="signInStatus != '99'">
            and t.qs_status = #{signInStatus}
        </if>
        <if test='signInStatus == "1"'>
            and to_char(t.qs_date,'yyyy-mm-dd') between #{signInStartTime} and #{signInEndTime}
        </if>
        <if test="signInType != '99'">
            and t.qs_type = #{signInType}
        </if>
        <if test="signInPerson != '' and signInPerson != null and signInPerson != 'null'">
            and t.qs_name =#{signInPerson}
        </if>
        <if test="company !=null and company!=''">
            and t.company = #{company}
        </if>
        order by t.qs_date desc,t.invoice_date desc
    </select>
</mapper>
