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

    <!--  根据发票代码和发票号码查询发票底账信息   -->
    <select id="findInvoiceByCodeAndNo" parameterType="java.lang.String"
            resultType="com.dxhy.core.job.entity.TDxRecordInvoiceJobEntity">
        select uuid, export_mark
        from t_dx_record_invoice
        where invoice_code = #{invoiceCode}
          and invoice_no = #{invoiceNo}
    </select>

    <select id="findInvoiceByUUID" parameterType="java.lang.String"
            resultType="com.dxhy.core.job.entity.TDxRecordInvoiceJobEntity">
        select uuid, invoice_type
        from t_dx_record_invoice
        where uuid = #{uuid}
    </select>

    <!--  根据出口转内销证明编号查询发票底账信息   -->
    <select id="findInvoice" parameterType="java.lang.String" resultType="com.dxhy.core.job.entity.TDxExportInvoiceJob">
        select *
        from t_dx_export_invoice
        where export_no = #{exportNo}
          and uuid = #{uuid}
          and gf_tax_no = #{gfTaxNo}
    </select>

    <!-- 获取底账表中该税号下最后的海出口转内销发票采集时间 -->
    <select id="selectLastExportInvoiceDate" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="mysql">
        SELECT DATE_FORMAT(create_date, "%Y%m%d%H%i%s")
        FROM t_dx_export_invoice
        where gf_tax_no = #{taxno}
        order by create_date desc
        limit 1
    </select>
    <!-- 获取底账表中该税号下最后的出口转内销发票采集时间 -->
    <select id="selectLastExportInvoiceDate" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="oracle">
        SELECT TO_CHAR(create_date, 'yyyyMMddHH24mmss')
        FROM t_dx_export_invoice
        where gf_tax_no = #{taxno}
          and rownum = 1
        order by create_date desc
    </select>

    <update id="updateExportInvoice" databaseId="mysql">
        update t_dx_export_invoice set last_update_date=now()
        <if test="invoiceStatus!=null">
            , invoice_status = #{invoiceStatus},status_update_date= #{statusUpdateDate}
        </if>
        <if test='rzhYesorno!=null'>
            , rzh_yesorno = #{rzhYesorno}, rzh_belong_date=#{rzhBelongDate},auth_status=#{authStatus},rzlx=#{rzlx},
            rzh_date=#{rzhDate},bdk_status = #{bdkStatus}
        </if>
        <if test="bdkMessage!=null and bdkMessage!=''">
            ,bdk_message = #{bdkMessage}
        </if>
        where export_no = #{exportNo}
    </update>
    <update id="updateExportInvoice" databaseId="oracle">
        update t_dx_export_invoice set last_update_date=sysdate
        <if test="invoiceStatus!=null">
            , invoice_status = #{invoiceStatus},status_update_date= #{statusUpdateDate}
        </if>
        <if test='rzhYesorno!=null'>
            , rzh_yesorno = #{rzhYesorno}, rzh_belong_date=#{rzhBelongDate},auth_status=#{authStatus},rzlx=#{rzlx},
            rzh_date=#{rzhDate},bdk_status = #{bdkStatus}
        </if>
        where export_no = #{exportNo}
    </update>

    <update id="updateExportMark" databaseId="mysql">
        update t_dx_record_invoice
        set export_mark = #{exportMark}
        where uuid = #{uuid}
    </update>
    <update id="updateExportMark" databaseId="oracle">
        update t_dx_record_invoice
        set export_mark = #{exportMark}
        where uuid = #{uuid}
    </update>


    <!-- 汇总需要发送税号认证的税号 -->
    <select id="getNeedSendTaxNo" resultType="java.lang.String">
        select distinct gf_tax_no
        from t_dx_export_invoice
        where auth_status = '2'
           or cxrz_status = '1'
           or cxbdk_status = '1'
        group by gf_tax_no
    </select>

    <select id="getApplyInvoiceCount" resultType="java.lang.Integer">
        select count(*)
        from t_dx_export_invoice
        where (auth_status = '2' or cxrz_status = '1' or cxbdk_status = '1')
          and gf_tax_no = #{taxno}
    </select>

    <!--查询要申请认证的出口转内销发票-->
    <select id="getApplyExportInvoice" resultType="com.dxhy.core.job.entity.TDxExportInvoiceJob" databaseId="mysql">
        select export_no,
               invoice_code,
               invoice_no,
               rzh_belong_date,
               gf_tax_no,
               rzlx,
               export_yxse,
               export_tax_amount,
               invoice_type
        from t_dx_export_invoice
        where (auth_status = '2' or cxrz_status = '1' or cxbdk_status = '1')
          and gf_tax_no = #{taxno}
        limit 200
    </select>
    <!--查询要申请认证的出口转内销发票-->
    <select id="getApplyExportInvoice" resultType="com.dxhy.core.job.entity.TDxExportInvoiceJob" databaseId="oracle">
        select export_no,
               invoice_code,
               invoice_no,
               rzh_belong_date,
               gf_tax_no,
               rzlx,
               export_yxse,
               export_tax_amount
        from t_dx_export_invoice
        where (auth_status = '2' or cxrz_status = '1' or cxbdk_status = '1')
          and gf_tax_no = #{taxno} <![CDATA[ and rownum <= 200
        ]]>
    </select>

    <!--查询需要获取结果的批次号-->
    <select id="getBatchNo" resultType="com.dxhy.core.job.entity.TDxCustomsApplyRecord">
        select g.batch_no, g.ace_id, g.ace_key, g.gf_tax_no
        from t_dx_customs_apply_record g
        where g.result_status = 'N'
          and g.record_type = #{type}
        group by g.batch_no, g.ace_id, g.ace_key, g.gf_tax_no
    </select>

    <!--申请认证成功，更新底账表-->
    <update id="updateExportInvoiceApply" databaseId="mysql">
        update t_dx_export_invoice SET
        <if test='rzlx=="1" or rzlx=="4"'>
            auth_status='3',send_date=now()
        </if>
        <if test='rzlx=="5"'>
            cxrz_status='2'
        </if>
        <if test='rzlx=="6"'>
            cxbdk_status='2'
        </if>
        where export_no = #{exportNo} and uuid = #{uuid} and gf_tax_no = #{gfTaxNo}
    </update>
    <update id="updateExportInvoiceApply" databaseId="oracle">
        update t_dx_export_invoice SET
        <if test='rzlx=="1" or rzlx=="4"'>
            auth_status='3',send_date=sysdate
        </if>
        <if test='rzlx=="5"'>
            cxrz_status='2'
        </if>
        <if test='rzlx=="6"'>
            cxbdk_status='2'
        </if>
        where export_no = #{exportNo} and uuid = #{uuid} and gf_tax_no = #{gfTaxNo}
    </update>


    <update id="updateApply">
        update t_dx_customs_apply_record
        SET result_status='Y'
        where export_no = #{exportNo}
        <if test="invoiceCode!=null and invoiceCode!=''">
            and invoice_code = #{invoiceCode}
        </if>
          and invoice_no = #{invoiceNo}
          and gf_tax_no = #{gfTaxNo}and record_type = '4'
    </update>

    <!--认证结果返回，更新底账表-->
    <update id="updateRecord">
        update t_dx_export_invoice SET
        rzh_yesorno= #{result.legalizeState},
        rzh_date=#{rzhDate},
        auth_status=#{result.authState},
        rzh_belong_date=#{result.legalizeBelongDate},
        rzlx= #{result.rzlx},
        rzh_back_msg=#{result.failureReason}
        <if test="result.yxse!=null and result.yxse!=''">
            ,export_yxse=#{result.yxse}
        </if>
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test='result.authState=="4"'>
            ,cxrz_status='0'
            ,cxrz_date=null
        </if>
        where export_no=#{result.toDivertCode}
        <if test="result.invoiceCode!=null and result.invoiceCode!=''">
            and invoice_code = #{result.invoiceCode}
        </if>
         and invoice_no = #{result.invoiceNo} and gf_tax_no = #{gfTaxNo}
    </update>

    <!--撤销不抵扣认证结果返回，更新底账表-->
    <update id="updateRecordForRebackNoDeduction" databaseId="mysql">
        update t_dx_export_invoice SET
        cxbdk_status=#{result.authState},
        cxbdk_date=now(),
        bdk_current_period=#{result.legalizeBelongDate}
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test='result.authState=="4"'>
            ,auth_status='6'
            ,rzh_yesorno='0'
            ,rzh_date = null
            ,rzh_belong_date = null
            ,confirm_user =null
            ,export_yxse=export_tax_amount
        </if>
        where export_no=#{result.toDivertCode}
        <if test="result.invoiceCode!=null and result.invoiceCode!=''">
            and invoice_code = #{result.invoiceCode}
        </if>
        and invoice_no = #{result.invoiceNo} and gf_tax_no = #{gfTaxNo}
    </update>
    <update id="updateRecordForRebackNoDeduction" databaseId="oracle">
        update t_dx_export_invoice SET
        cxbdk_status=#{result.authState},
        cxbdk_date=sysdate,
        bdk_current_period=#{result.legalizeBelongDate}
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test='result.authState=="4"'>
            ,auth_status='6'
            ,rzh_yesorno='0'
            ,rzh_date = null
            ,rzh_belong_date = null
            ,confirm_user =null
            ,export_yxse=export_tax_amount
        </if>
        where export_no=#{result.toDivertCode}
        <if test="result.invoiceCode!=null and result.invoiceCode!=''">
            and invoice_code = #{result.invoiceCode}
        </if>
        and invoice_no =#{result.invoiceNo} and gf_tax_no = #{gfTaxNo}
    </update>

    <!--撤销抵扣认证结果返回，更新底账表-->
    <update id="updateRecordForRebackRz" databaseId="mysql">
        update t_dx_export_invoice SET
        cxrz_status=#{result.authState},
        cxrz_date=now(),
        cxrz_current_period=#{result.legalizeBelongDate}
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test='result.authState=="4"'>
            ,auth_status='6'
            ,rzh_yesorno='0'
            ,rzh_date = null
            ,rzh_belong_date = null
            ,send_date = null
            ,confirm_user =null
            ,export_yxse=export_tax_amount
        </if>
        where export_no=#{result.toDivertCode}
        <if test="result.invoiceCode!=null and result.invoiceCode!=''">
            and invoice_code = #{result.invoiceCode}
        </if>
        and invoice_no =#{result.invoiceNo} and gf_tax_no = #{gfTaxNo}
    </update>
    <update id="updateRecordForRebackRz" databaseId="oracle">
        update t_dx_export_invoice SET
        cxrz_status=#{result.authState},
        cxrz_date=sysdate,
        cxrz_current_period=#{result.legalizeBelongDate}
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test='result.authState=="4"'>
            ,auth_status='6'
            ,rzh_yesorno='0'
            ,rzh_date = null
            ,rzh_belong_date = null
            ,send_date = null
            ,confirm_user =null
            ,export_yxse=export_tax_amount
        </if>
        where export_no=#{result.toDivertCode}
        <if test="result.invoiceCode!=null and result.invoiceCode!=''">
            and invoice_code = #{result.invoiceCode}
        </if>
        and invoice_no = #{result.invoiceNo} and gf_tax_no = #{gfTaxNo}
    </update>

    <!--不抵扣认证结果返回，更新底账表-->
    <update id="updateRecordForNoDeduction">
        update t_dx_export_invoice SET
        rzh_yesorno= #{result.legalizeState},
        rzh_date=#{rzhDate},
        auth_status=#{result.authState},
        rzh_belong_date=#{result.legalizeBelongDate},
        rzlx= #{result.rzlx},
        rzh_back_msg=#{result.failureReason}
        <if test="result.yxse!=null and result.yxse!=''">
            ,export_yxse=#{result.yxse}
        </if>
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test='result.authState=="4"'>
            ,cxbdk_status='0'
            ,cxbdk_date=null
        </if>
        <if test="result.bdkMessage!='' and result.bdkMessage!=null">
            ,bdk_message=#{result.bdkMessage}
        </if>
        where export_no=#{result.toDivertCode}
        <if test="result.invoiceCode!=null and result.invoiceCode!=''">
            and invoice_code = #{result.invoiceCode}
        </if>
        and invoice_no =#{result.invoiceNo} and gf_tax_no = #{gfTaxNo}
    </update>

</mapper>
