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

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

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

    <!-- 插入底账表  -->
    <insert id="insertRecordInvoice" parameterType="com.dxhy.core.job.entity.TDxRecordInvoiceJobEntity">
        insert into t_dx_record_invoice(
        invoice_code,
        invoice_no,
        dqskssq,
        gxjzr,
        gxfwq,
        gxfwz,
        invoice_type,
        gf_name,
        gf_tax_no,
        gf_address_and_phone,
        gf_bank_and_no,
        xf_name,
        xf_tax_no,
        xf_address_and_phone,
        xf_bank_and_no,
        invoice_date,
        invoice_amount,
        tax_amount,
        total_amount,
        remark,
        invoice_status,
        rzh_yesorno,
        rzh_date,
        rzh_belong_date,
        sfdbts,
        rzlx,
        rzh_type,
        check_code,
        txfbz,
        lslbz,
        xxly,
        dk_tax_amount,
        yqkgxbz,
        yclx,
        glzt,
        sfygx,/**是否已勾选*/
        auth_status,/**认证处理状态*/
        create_date,/**创建时间*/
        uuid,/**uuid*/
        detail_yesorno,/**是否存入明细 0 无明细 1 有明细 */
        source_system,/**底账来源 0-采集 1-查验*/
        valid,/**是否有效 1-有效 0-无效*/
        out_status,/**转出状态 0-未转出 1-全部转出 2-部分转出*/
        qs_status,/**签收结果*/
        status_update_date/**发票状态修改时间*/
        )values(
        #{invoiceCode},
        #{invoiceNo},
        #{dqskssq},
        #{gxjzr},
        #{gxfwq},
        #{gxfwz},
        #{invoiceType},
        #{gfName},
        #{gfTaxNo},
        #{gfAddressAndPhone},
        #{gfBankAndNo},
        #{xfName},
        #{xfTaxNo},
        #{xfAddressAndPhone},
        #{xfBankAndNo},
        #{invoiceDate},
        #{invoiceAmount},
        #{taxAmount},
        #{totalAmount},
        #{remark},
        #{invoiceStatus},
        #{rzhYesorno},
        #{rzhDate},
        #{rzhBelongDate},
        #{sfdbts},
        #{rzlx},
        #{rzhType},
        #{checkCode},
        #{txfbz},
        #{lslbz},
        #{xxly},
        #{dkTaxAmount},
        #{yqkgxbz},
        #{yclx},
        #{glbz},
        #{sfygx},
        #{authStatus},
        now(),
        #{uuid},
        <choose>
            <when test="detailYesorno != null and detailYesorno !=''">
                #{detailYesorno},
            </when>
            <otherwise>
                "0",
            </otherwise>
        </choose>
        /**是否存入明细 0 无明细 1 有明细 */
        "0",/**底账来源 0-采集 1-查验*/
        "1",/**是否有效 1-有效 0-无效*/
        "0",/**转出状态 0-未转出 1-全部转出 2-部分转出*/
        #{qsStatus},
        now()
        )
    </insert>

    <update id="updateRecordInvoice" parameterType="com.dxhy.core.job.entity.TDxRecordInvoiceJobEntity">
        update t_dx_record_invoice
        <set>
            <if test="invoiceCode != null and invoiceCode != ''">invoice_code=#{invoiceCode},</if>
            <if test="invoiceNo != null and invoiceNo != ''">invoice_no=#{invoiceNo},</if>
            <if test="dqskssq != null and dqskssq != ''">dqskssq=#{dqskssq},</if>
            <if test="gxjzr != null and gxjzr != ''">gxjzr=#{gxjzr},</if>
            <if test="gxfwq != null and gxfwq != ''">gxfwq=#{gxfwq},</if>
            <if test="gxfwz != null and gxfwz != ''">gxfwz=#{gxfwz},</if>
            <if test="invoiceType != null and invoiceType != ''">invoice_type=#{invoiceType},</if>
            <if test="gfName != null and gfName != ''">gf_name=#{gfName},</if>
            <if test="gfTaxNo != null and gfTaxNo != ''">gf_tax_no=#{gfTaxNo},</if>
            <if test="gfAddressAndPhone != null and gfAddressAndPhone != ''">
                gf_address_and_phone=#{gfAddressAndPhone},
            </if>
            <if test="gfBankAndNo != null and gfBankAndNo != ''">gf_bank_and_no=#{gfBankAndNo},</if>
            <if test="xfName != null and xfName != ''">xf_name=#{xfName},</if>
            <if test="xfTaxNo != null and xfTaxNo != ''">xf_tax_no=#{xfTaxNo},</if>
            <if test="xfAddressAndPhone != null and xfAddressAndPhone != ''">
                xf_address_and_phone=#{xfAddressAndPhone},
            </if>
            <if test="xfBankAndNo != null and xfBankAndNo != ''">xf_bank_and_no=#{xfBankAndNo},</if>
            <if test="invoiceDate != null">invoice_date=#{invoiceDate,jdbcType=TIMESTAMP},</if>
            <if test="invoiceAmount != null and invoiceAmount != ''">invoice_amount=#{invoiceAmount},</if>
            <if test="taxAmount != null and taxAmount != ''">tax_amount=#{taxAmount},</if>
            <if test="totalAmount != null and totalAmount != ''">total_amount=#{totalAmount},</if>
            <if test="remark != null and remark != ''">remark=#{remark},</if>
            <if test="invoiceStatus != null and invoiceStatus != ''">invoice_status=#{invoiceStatus},</if>
            <if test="rzhYesorno != null and rzhYesorno != ''">rzh_yesorno=#{rzhYesorno},</if>
            <if test="rzhDate != null">rzh_date=#{rzhDate,jdbcType=TIMESTAMP},</if>
            <if test="rzhBelongDate != null">rzh_belong_date=#{rzhBelongDate,jdbcType=TIMESTAMP},</if>
            <if test="sfdbts != null and sfdbts != ''">sfdbts=#{sfdbts},</if>
            <if test="rzlx != null and rzlx != ''">rzlx=#{rzlx},</if>
            <if test="rzhType != null and rzhType != ''">rzh_type=#{rzhType},</if>
            <if test="checkCode != null and checkCode != ''">check_code=#{checkCode},</if>
            <if test="txfbz != null and txfbz != ''">txfbz=#{txfbz},</if>
            <if test="lslbz != null and lslbz != ''">lslbz=#{lslbz},</if>
            <if test="sfygx != null and sfygx != ''">sfygx=#{sfygx},</if>
            <if test="authStatus != null and authStatus != ''">auth_status=#{authStatus},</if>
            <if test="bdkStatus != null and bdkStatus != ''">bdk_status=#{bdkStatus},</if>
            <if test="bdkDate != null">bdk_date=#{bdkDate,jdbcType=TIMESTAMP},</if>
            <if test="bdkCurrentPeriod != null">bdk_current_period=#{bdkCurrentPeriod,jdbcType=TIMESTAMP},</if>
            <if test="uuid != null and uuid != ''">uuid=#{uuid},</if>
            <if test="detailYesorno != null and detailYesorno != ''">detail_yesorno=#{detailYesorno},</if>
            <if test="statusUpdateDate != null">status_update_date=#{statusUpdateDate,jdbcType=TIMESTAMP},</if>
            <if test="collectStatus !=null and collectStatus!=''">
                collect_status=#{collectStatus},
                collect_from=#{collectFrom},
                collect_date=#{collectDate},
                pool_status=#{poolStatus},
                in_pool_reason=#{inPoolReason},
            </if>
        </set>
        where invoice_code = #{invoiceCode} and invoice_no = #{invoiceNo}
    </update>


    <!--  发票状态更新  -->
    <update id="updateInvoiceByState" parameterType="com.dxhy.core.job.entity.State" databaseId="mysql">
        update t_dx_record_invoice set invoice_status =#{state.invoiceStatus},last_update_date=now()
        <if test='state.invoiceStatus!="0"'>
            ,status_update_date=now()
        </if>
        <if test="rzFlag!=null">
            ,rzh_yesorno=#{state.legalizeState} ,sfygx=#{state.legalizeState}
            ,auth_status=#{state.authStatus},rzh_date=#{state.legalizeDate ,jdbcType=TIMESTAMP}
            ,rzh_belong_date=#{state.legalizeBelongDate},rzh_type=#{state.legalizeType}
        </if>

        <if test="state.xxly!=null">
            ,xxly=#{state.xxly}
        </if>
        <if test="state.glzt!=null">
            ,glzt=#{state.glzt}
        </if>
        <if test="state.yxse!=null">
            ,dk_tax_amount=#{state.yxse}
        </if>
        <if test="state.yqkgxbz!=null">
            ,yqkgxbz=#{state.yqkgxbz}
        </if>
        <if test="state.bdkStatus != null">
            ,bdk_status = #{state.bdkStatus}
        </if>
        <if test="state.bdkMessage!=null and state.bdkMessage!=''">
            ,bdk_message = #{state.bdkMessage}
        </if>

        where invoice_code =#{state.invoiceCode} and invoice_no = #{state.invoiceNo}
    </update>
    <!--  发票状态更新  -->
    <update id="updateInvoiceByState" parameterType="com.dxhy.core.job.entity.State" databaseId="oracle">
        update t_dx_record_invoice set invoice_status =#{state.invoiceStatus},last_update_date=sysdate
        <if test='state.invoiceStatus!="0"'>
            ,status_update_date=sysdate
        </if>
        <if test="rzFlag!=null">
            ,rzh_yesorno=#{state.legalizeState} ,sfygx=#{state.legalizeState}
            ,auth_status=#{state.authStatus},rzh_date=#{state.legalizeDate ,jdbcType=TIMESTAMP}
            ,rzh_belong_date=#{state.legalizeBelongDate},rzh_type=#{state.legalizeType}
        </if>

        <if test="state.xxly!=null">
            ,xxly=#{state.xxly}
        </if>
        <if test="state.glzt!=null">
            ,glzt=#{state.glzt}
        </if>
        <if test="state.yxse!=null">
            ,dk_tax_amount=#{state.yxse}
        </if>
        <if test="state.yqkgxbz!=null">
            ,yqkgxbz=#{state.yqkgxbz}
        </if>
        <if test="state.bdkStatus != null">
            ,bdk_status = #{state.bdkStatus}
        </if>
        <if test="state.bdkMessage!=null and state.bdkMessage!=''">
            ,bdk_message = #{state.bdkMessage}
        </if>
        where invoice_code =#{state.invoiceCode} and invoice_no = #{state.invoiceNo}
    </update>

    <!-- 插入获取勾选发票  -->
    <insert id="insertBySelect" parameterType="com.dxhy.core.job.entity.InvoiceSelectInfo" databaseId="mysql">
        insert into t_dx_record_invoice(
        invoice_type,
        invoice_code,
        invoice_no,
        invoice_date,
        invoice_status,
        invoice_amount,
        tax_amount,
        gf_tax_no,
        gf_name,
        xf_tax_no,
        xf_name,
        gxjzr,
        gxfwq,
        gxfwz,
        sfygx,
        rzh_belong_date,
        sfdbts,
        rzlx,
        create_date,
        uuid,
        detail_yesorno,
        source_system,
        valid,
        out_status,
        rzh_yesorno,
        dqskssq,
        rzh_type,
        rzh_date,
        xxly,
        glzt,
        dk_tax_amount,
        yqkgxbz,
        yclx,
        qs_status
        <if test="invoiceSelectInfo.qsDate!=null">
            ,qs_date
        </if>
        <if test="invoiceSelectInfo.qsType!=null">
            ,qs_type
        </if>
        <if test="invoiceSelectInfo.authStatus!=null">
            ,auth_status
        </if>
        <if test='invoiceSelectInfo.invoiceStatus!="0"'>
            ,status_update_date
        </if>
        )values(
        #{invoiceSelectInfo.invoiceType},
        #{invoiceSelectInfo.invoiceCode},
        #{invoiceSelectInfo.invoiceNo},
        #{invoiceSelectInfo.invoiceDate},
        #{invoiceSelectInfo.invoiceStatus},
        #{invoiceSelectInfo.invoiceAmount},
        #{invoiceSelectInfo.taxAmount},
        #{invoiceSelectInfo.buyerTaxNo},
        #{invoiceSelectInfo.buyerName},
        #{invoiceSelectInfo.salerTaxNo},
        #{invoiceSelectInfo.salerName},
        #{invoiceSelectInfo.legalizeEndDate},
        #{invoiceSelectInfo.legalizeInvoiceDateBegin},
        #{invoiceSelectInfo.legalizeInvoiceDateEnd},
        #{invoiceSelectInfo.legalizeState},
        #{invoiceSelectInfo.legalizeBlongDate},
        #{invoiceSelectInfo.sfdbts},
        #{invoiceSelectInfo.rzlx},
        now(),
        #{invoiceSelectInfo.uuid},
        ${'0'},
        ${'0'},
        ${'1'},
        ${'0'},
        #{invoiceSelectInfo.legalizeState},
        #{invoiceSelectInfo.currentTaxPeriod},
        #{invoiceSelectInfo.legalizeType},
        #{invoiceSelectInfo.legalizeDate ,jdbcType=TIMESTAMP},
        #{invoiceSelectInfo.xxly},
        #{invoiceSelectInfo.glzt},
        #{invoiceSelectInfo.dkTaxAmount},
        #{invoiceSelectInfo.yqkgxbz},
        #{invoiceSelectInfo.yclx},
        #{invoiceSelectInfo.qsStatus}
        <if test="invoiceSelectInfo.qsDate!=null">
            ,#{invoiceSelectInfo.qsDate}
        </if>
        <if test="invoiceSelectInfo.qsType!=null">
            ,#{invoiceSelectInfo.qsType}
        </if>
        <if test="invoiceSelectInfo.authStatus!=null">
            ,#{invoiceSelectInfo.authStatus}
        </if>
        <if test='invoiceSelectInfo.invoiceStatus!="0"'>
            ,now()
        </if>
        )
    </insert>

    <!-- 插入获取勾选发票  -->
    <insert id="insertBySelect" parameterType="com.dxhy.core.job.entity.InvoiceSelectInfo" databaseId="oracle">
        insert into t_dx_record_invoice(
        invoice_type,
        invoice_code,
        invoice_no,
        invoice_date,
        invoice_status,
        invoice_amount,
        tax_amount,
        gf_tax_no,
        gf_name,
        xf_tax_no,
        xf_name,
        gxjzr,
        gxfwq,
        gxfwz,
        sfygx,
        rzh_belong_date,
        sfdbts,
        rzlx,
        create_date,
        uuid,
        detail_yesorno,
        source_system,
        valid,
        out_status,
        rzh_yesorno,
        dqskssq,
        rzh_type,
        rzh_date,
        qs_status
        <if test="invoiceSelectInfo.qsDate!=null">
            ,qs_date
        </if>
        <if test="invoiceSelectInfo.qsType!=null">
            ,qs_type
        </if>
        <if test="invoiceSelectInfo.authStatus!=null">
            ,auth_status
        </if>
        <if test='invoiceSelectInfo.invoiceStatus!="0"'>
            ,status_update_date
        </if>
        )values(
        #{invoiceSelectInfo.invoiceType},
        #{invoiceSelectInfo.invoiceCode},
        #{invoiceSelectInfo.invoiceNo},
        #{invoiceSelectInfo.invoiceDate},
        #{invoiceSelectInfo.invoiceStatus},
        #{invoiceSelectInfo.invoiceAmount},
        #{invoiceSelectInfo.taxAmount},
        #{invoiceSelectInfo.buyerTaxNo},
        #{invoiceSelectInfo.buyerName},
        #{invoiceSelectInfo.salerTaxNo},
        #{invoiceSelectInfo.salerName},
        #{invoiceSelectInfo.legalizeEndDate},
        #{invoiceSelectInfo.legalizeInvoiceDateBegin},
        #{invoiceSelectInfo.legalizeInvoiceDateEnd},
        #{invoiceSelectInfo.legalizeState},
        #{invoiceSelectInfo.legalizeBlongDate},
        #{invoiceSelectInfo.sfdbts},
        #{invoiceSelectInfo.rzlx},
        now(),
        #{invoiceSelectInfo.uuid},
        ${'0'},
        ${'0'},
        ${'1'},
        ${'0'},
        #{invoiceSelectInfo.legalizeState},
        #{invoiceSelectInfo.currentTaxPeriod},
        #{invoiceSelectInfo.legalizeType},
        #{invoiceSelectInfo.legalizeDate ,jdbcType=TIMESTAMP},
        #{invoiceSelectInfo.qsStatus}
        <if test="invoiceSelectInfo.qsDate!=null">
            ,#{invoiceSelectInfo.qsDate}
        </if>
        <if test="invoiceSelectInfo.qsType!=null">
            ,#{invoiceSelectInfo.qsType}
        </if>
        <if test="invoiceSelectInfo.authStatus!=null">
            ,#{invoiceSelectInfo.authStatus}
        </if>
        <if test='invoiceSelectInfo.invoiceStatus!="0"'>
            ,sysdate
        </if>
        )
    </insert>

    <!--  通过发票代码和发票号码查询发票   -->
    <select id="findCountInvoiceByCodeAndNo" parameterType="java.lang.String" resultType="java.lang.Integer">
        select count(*)
        from t_dx_record_invoice
        where invoice_code = #{invoice_code}
          and invoice_no = #{invoice_no}
    </select>

    <!--  通过发票代码和发票号码查询发票   -->
    <select id="findInvoiceByUUid" parameterType="java.lang.String"
            resultType="com.dxhy.core.job.entity.TDxRecordInvoiceJobEntity">
        select *
        from t_dx_record_invoice
        where uuid = #{uuid}
    </select>


    <!-- 更新发票获取接口接收的数据 -->
    <update id="updateInvoiceByCodeAndNo" parameterType="com.dxhy.core.entity.TDxRecordInvoice" databaseId="mysql">
        update t_dx_record_invoice set
        gf_tax_no = #{invoiceInfo.gfTaxNo},
        gf_name=#{invoiceInfo.gfName},
        gf_address_and_phone = #{invoiceInfo.gfAddressAndPhone},
        gf_bank_and_no =#{invoiceInfo.gfBankAndNo},
        xf_tax_no = #{invoiceInfo.xfTaxNo},
        xf_name =#{invoiceInfo.xfName},
        xf_address_and_phone = #{invoiceInfo.xfAddressAndPhone},
        xf_bank_and_no = #{invoiceInfo.xfBankAndNo},
        remark = #{invoiceInfo.remark},

        invoice_date = #{invoiceInfo.invoiceDate},
        last_update_date=now()
        <if test='invoiceInfo.sourceSystem!="2"'>
            ,invoice_amount = #{invoiceInfo.invoiceAmount}
            ,tax_amount = #{invoiceInfo.taxAmount}
            ,total_amount = #{invoiceInfo.totalAmount}
            ,source_system=#{invoiceInfo.sourceSystem}
        </if>
        <if test="invoiceInfo.invoiceStatus!=null">
            , invoice_status = #{invoiceInfo.invoiceStatus},status_update_date= #{invoiceInfo.statusUpdateDate}
        </if>
        <if test='invoiceInfo.rzhYesorno!=null'>
            , rzh_yesorno = #{invoiceInfo.rzhYesorno},
            rzh_belong_date=#{invoiceInfo.rzhBelongDate},rzh_type=#{invoiceInfo.rzhType},auth_status=#{invoiceInfo.authStatus},
            rzlx=#{invoiceInfo.rzlx},sfygx='1',rzh_date=#{invoiceInfo.rzhDate},bdk_status = #{invoiceInfo.bdkStatus}
        </if>
        where uuid=#{invoiceInfo.uuid}

    </update>
    <!-- 更新发票获取接口接收的数据 -->
    <update id="updateInvoiceByCodeAndNo" parameterType="com.dxhy.core.entity.TDxRecordInvoice" databaseId="oracle">
        update t_dx_record_invoice set
        gf_tax_no = #{invoiceInfo.gfTaxNo},
        gf_name=#{invoiceInfo.gfName},
        gf_address_and_phone = #{invoiceInfo.gfAddressAndPhone},
        gf_bank_and_no =#{invoiceInfo.gfBankAndNo},
        xf_tax_no = #{invoiceInfo.xfTaxNo},
        xf_name =#{invoiceInfo.xfName},
        xf_address_and_phone = #{invoiceInfo.xfAddressAndPhone},
        xf_bank_and_no = #{invoiceInfo.xfBankAndNo},
        remark = #{invoiceInfo.remark},
        invoice_date = #{invoiceInfo.invoiceDate},
        last_update_date=sysdate
        <if test='invoiceInfo.sourceSystem!="2"'>
            ,invoice_amount = #{invoiceInfo.invoiceAmount}
            ,tax_amount = #{invoiceInfo.taxAmount}
            ,total_amount = #{invoiceInfo.totalAmount}
            ,source_system=#{invoiceInfo.sourceSystem}
        </if>
        <if test="invoiceInfo.invoiceStatus!=null">
            , invoice_status = #{invoiceInfo.invoiceStatus},status_update_date= #{invoiceInfo.statusUpdateDate}
        </if>
        <if test="invoiceInfo.rzhYesorno!=null">
            , rzh_yesorno = #{invoiceInfo.rzhYesorno},
            rzh_belong_date=#{invoiceInfo.rzhBelongDate},rzh_type=#{invoiceInfo.rzhType},auth_status=#{invoiceInfo.authStatus},
            rzlx=#{invoiceInfo.rzlx},sfygx='1',rzh_date=#{invoiceInfo.rzhDate},bdk_status = #{invoiceInfo.bdkStatus}
        </if>

        where uuid=#{invoiceInfo.uuid}

    </update>

    <!-- 发票获取，去更新签收表数据   -->
    <update id="updateSignData" parameterType="com.dxhy.core.entity.TDxRecordInvoice">
        update t_dx_invoice
        set qs_date   = now(),
            qs_status = ${'1'},
            gf_tax_no = #{invoiceInfo.gfTaxNo},
            gf_name=#{invoiceInfo.gfName},
            xf_tax_no = #{invoiceInfo.xfTaxNo},
            xf_name   =#{invoiceInfo.xfName},
            notes     = '签收成功'
        where invoice_code = #{invoiceInfo.invoiceCode}
          and invoice_no = #{invoiceInfo.invoiceNo}
          and qs_status = '0'
    </update>

    <!-- 查询扫描签收表   -->
    <select id="selectSign" parameterType="java.lang.String" resultType="com.dxhy.core.job.entity.InvoiceScanEntity">
        select invoice_code invoiceCode, invoice_no invoiceNo, qs_type qsType, qs_status qsStatus, qs_date qsDate
        from t_dx_invoice
        where invoice_code = #{code}
          and invoice_no = #{no}
    </select>

    <!-- 查询扫描签收表可推送艺龙结算平台的数据   -->
    <select id="selectByScan" parameterType="java.lang.String" resultType="com.dxhy.core.job.entity.InvoiceScanEntity">
        select invoice_code invoiceCode, invoice_no invoiceNo, qs_type qsType, qs_status qsStatus, qs_date qsDate
        from t_dx_invoice d
                 LEFT JOIN ${dxhyAdmin}.sys_user u on d.user_account = u.user_id
        where u.push_yesorno = 1
          and d.uuid = #{uuid}
    </select>

    <!-- 获取底账表中该税号下最后的开票日期 -->
    <select id="selectLastInvoiceDate" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="mysql">
        SELECT DATE_FORMAT(req_date, "%Y%m%d%H%i%s")
        FROM t_dx_http_log
        where taxno = #{taxno}
          and type = #{type}order by req_date desc
        limit 1
    </select>
    <select id="selectOrgCollectDate" resultType="com.dxhy.core.job.entity.TAcOrg">
        select distinct g.taxno, g.first_collect_date
        from sys_dept g
        where g.org_type = '5'
          and g.taxno = #{taxno}
          and cj_status = '1'
          and g.ace_id is not null
          and g.ace_key is not null
    </select>
    <!-- 获取底账表中该税号下最后的开票日期 -->
    <select id="selectLastInvoiceDate" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="oracle">
        SELECT TO_CHAR(req_date, 'yyyyMMddHH24mmss')
        FROM t_dx_http_log
        where taxno = #{taxno}
          and type = #{type}
          and rownum = 1
        order by req_date desc
    </select>

    <!-- 获取底账表中该税号下最后的开票日期 全票面 -->
    <select id="selectLastInvoiceDateQPM" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="mysql">
        SELECT DATE_FORMAT(create_date, "%Y%m%d%H%i%s")
        FROM t_dx_record_invoice
        where gf_tax_no = #{taxno}
          and invoice_type = #{type}
          and detail_yesorno = "1"
        order by create_date desc
        limit 1
    </select>
    <!-- 获取底账表中该税号下最后的开票日期 全票面 -->
    <select id="selectLastInvoiceDateQPM" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="oracle">
        SELECT TO_CHAR(create_date, 'yyyyMMddHH24mmss')
        FROM t_dx_record_invoice
        where gf_tax_no = #{taxno}
          and invoice_type = #{type}
          and detail_yesorno = '1'
          and rownum = 1
        order by create_date desc
    </select>

    <update id="updateInvoice" databaseId="mysql">
        update t_dx_record_invoice set invoice_no = #{invoiceInfo.invoiceNo},last_update_date=now()
        <if test="invoiceInfo.invoiceStatus!=null">
            , invoice_status = #{invoiceInfo.invoiceStatus},status_update_date= #{invoiceInfo.statusUpdateDate}
        </if>
        <if test='invoiceInfo.rzhYesorno!=null'>
            , rzh_yesorno = #{invoiceInfo.rzhYesorno},
            rzh_belong_date=#{invoiceInfo.rzhBelongDate},rzh_type=#{invoiceInfo.rzhType},auth_status=#{invoiceInfo.authStatus},rzlx=#{invoiceInfo.rzlx},
            sfygx=#{invoiceInfo.sfygx},rzh_date=#{invoiceInfo.rzhDate},bdk_status = #{invoiceInfo.bdkStatus}
        </if>
        <if test="invoiceInfo.checkCode !=null and invoiceInfo.checkCode!=''">
            ,check_code = #{invoiceInfo.checkCode}
            ,check_date = #{invoiceInfo.checkDate}
        </if>
        <if test="invoiceInfo.collectStatus !=null and invoiceInfo.collectStatus!=''">
            ,collect_status=#{invoiceInfo.collectStatus}
            ,collect_from=#{invoiceInfo.collectFrom}
            ,collect_date=#{invoiceInfo.collectDate}
            ,pool_status=#{invoiceInfo.poolStatus}
            ,in_pool_reason=#{invoiceInfo.inPoolReason}
        </if>
        where   invoice_no = #{invoiceInfo.invoiceNo}
        <if test="invoiceInfo.invoiceCode !=null and invoiceInfo.invoiceCode!='' and invoiceInfo.invoiceCode!='null'">
            and invoice_code = #{invoiceInfo.invoiceCode}
        </if>
    </update>
    <update id="updateInvoice" databaseId="oracle">
        update t_dx_record_invoice set invoice_no = #{invoiceInfo.invoiceNo},last_update_date=sysdate
        <if test="invoiceInfo.invoiceStatus!=null">
            , invoice_status = #{invoiceInfo.invoiceStatus},status_update_date= #{invoiceInfo.statusUpdateDate}
        </if>
        <if test='invoiceInfo.rzhYesorno!=null'>
            , rzh_yesorno = #{invoiceInfo.rzhYesorno},
            rzh_belong_date=#{invoiceInfo.rzhBelongDate},rzh_type=#{invoiceInfo.rzhType},auth_status=#{invoiceInfo.authStatus},
            rzlx=#{invoiceInfo.rzlx},sfygx=#{invoiceInfo.sfygx},rzh_date=#{invoiceInfo.rzhDate},bdk_status =
            #{invoiceInfo.bdkStatus}
        </if>
        <if test="invoiceInfo.checkCode !=null and invoiceInfo.checkCode!=''">
            ,check_code = #{invoiceInfo.checkCode}
            ,check_date = #{invoiceInfo.checkDate}
        </if>
        where   invoice_no = #{invoiceInfo.invoiceNo}
        <if test="invoiceInfo.invoiceCode !=null and invoiceInfo.invoiceCode!='' and invoiceInfo.invoiceCode!='null'">
            and invoice_code = #{invoiceInfo.invoiceCode}
        </if>
    </update>


    <!--数据平台 销项发票入库使用 -->
    <update id="updateSaleInvoice" databaseId="mysql">
        update t_dx_sale_record_invoice set invoice_no = #{invoiceInfo.invoiceNo},last_update_date=now()
        <if test="invoiceInfo.invoiceStatus!=null">
            , invoice_status = #{invoiceInfo.invoiceStatus},status_update_date= now()
        </if>
        <if test='invoiceInfo.rzhYesorno!=null'>
            , rzh_yesorno = #{invoiceInfo.rzhYesorno},
            rzh_belong_date=#{invoiceInfo.rzhBelongDate},rzh_type=#{invoiceInfo.rzhType},auth_status=#{invoiceInfo.authStatus},rzlx=#{invoiceInfo.rzlx},
            sfygx=#{invoiceInfo.sfygx},rzh_date=#{invoiceInfo.rzhDate},bdk_status = #{invoiceInfo.bdkStatus}
        </if>
        <if test="invoiceInfo.checkCode !=null and invoiceInfo.checkCode!=''">
            ,check_code = #{invoiceInfo.checkCode}
            ,check_date = #{invoiceInfo.checkDate}
        </if>
        <if test="invoiceInfo.collectStatus !=null and invoiceInfo.collectStatus!=''">
            ,collect_status=#{invoiceInfo.collectStatus}
            ,collect_from=#{invoiceInfo.collectFrom}
            ,collect_date=#{invoiceInfo.collectDate}
            ,pool_status=#{invoiceInfo.poolStatus}
            ,in_pool_reason=#{invoiceInfo.inPoolReason}
        </if>
        where invoice_code = #{invoiceInfo.invoiceCode} and invoice_no = #{invoiceInfo.invoiceNo}
    </update>

    <update id="updateInvoiceDetail">
        update t_dx_record_invoice
        set detail_yesorno = #{invoiceInfo.detailYesorno}
        where uuid = #{invoiceInfo.uuid}
    </update>

    <!-- 插入发票日志关联表   -->
    <insert id="insertInvoiceLog" parameterType="com.dxhy.core.job.entity.TDxInvoiceLogJobEntity">
        insert into t_dx_invoice_log (invoice_code,
                                      invoice_no,
                                      log_id)
        values (#{tDxInvoiceLog.invoiceCode},
                #{tDxInvoiceLog.invoiceNo},
                #{tDxInvoiceLog.logId})
    </insert>

    <!-- 保存税率数据-->
    <insert id="saveStatistics" databaseId="mysql">
        INSERT IGNORE INTO t_dx_record_invoice_statistics
        (invoice_code,
         invoice_no,
         detail_amount,
         tax_rate,
         tax_amount,
         total_amount,
         create_date)
        VALUES (#{rs.invoiceCode},
                #{rs.invoiceNo},
                #{rs.detailAmount},
                #{rs.taxRate},
                #{rs.taxAmount},
                #{rs.totalAmount},
                NOW());
    </insert>
    <!-- 保存税率数据-->
    <insert id="saveStatistics" databaseId="oracle">
        INSERT IGNORE INTO t_dx_record_invoice_statistics
        (invoice_code,
         invoice_no,
         detail_amount,
         tax_rate,
         tax_amount,
         total_amount,
         create_date)
        VALUES (#{rs.invoiceCode},
                #{rs.invoiceNo},
                #{rs.detailAmount},
                #{rs.taxRate},
                #{rs.taxAmount},
                #{rs.totalAmount},
                sysdate);
    </insert>

    <!-- 查找部门表中的company值存放在底账发票信息中 -->
    <select id="findCompany" resultType="java.lang.String" databaseId="mysql">
        select company
        from sys_dept
        where taxno = #{gfTaxNo}
          and org_type = '5'
        limit 1
    </select>
    <select id="findCompany" resultType="java.lang.String" databaseId="oracle">
        select company
        from sys_dept
        where taxno = #{gfTaxNo}
          and org_type = '5'
          and ROWNUM = 1
    </select>

    <select id="getInvoiceLog" resultType="java.lang.Integer">
        select count(1)
        from sys_invoice_log
        where invoice_code = #{invoiceCode}
          and invoice_no = #{invoiceNo}
          and type = #{type}
    </select>
    <update id="updateInvoiceByxfTaxNo" parameterType="java.lang.String">
        update  t_dx_record_invoice set illegal_supplier='1'
        where xf_tax_no = #{xfsh}
    </update>
</mapper>
