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

    <!--  缴款书状态更新  -->
    <update id="updateCustomsState" databaseId="mysql">
        update t_dx_customs_record set update_date = now()
        <if test='glzt!=null and glzt != "0"'>
            ,status_update_date=now()
        </if>
        <if test="rzhYesorno!=null">
            ,rzh_yesorno=#{rzhYesorno}
            ,auth_status=#{authStatus},rzh_date=#{rzhDate}
            ,rzh_belong_date=#{rzhBelongDate},rzlx=#{rzlx}
        </if>

        <if test="xxly!=null">
            ,xxly=#{xxly}
        </if>
        <if test="glzt!=null">
            ,glzt=#{glzt}
        </if>
        <if test="yxTaxAmount!=null">
            ,yx_tax_amount=#{yxTaxAmount}
        </if>

        <if test="bdkStatus != null ">
            ,bdk_status = #{bdkStatus}
        </if>

        <if test="entryDiscrepancy != null">
            ,entry_discrepancy = #{entryDiscrepancy}
        </if>
        <if test="bdkMessage!=null and bdkMessage!=''">
            ,bdk_message=#{bdkMessage}
        </if>
        where customs_code =#{customsCode}
    </update>
    <!--  缴款书状态更新  -->
    <update id="updateCustomsState" databaseId="oracle">
        update t_dx_customs_record set
        <if test='glzt!=null and glzt != "0"'>
            ,status_update_date=sysdate
        </if>
        <if test="rzhYesorno!=null">
            ,rzh_yesorno=#{rzhYesorno}
            ,auth_status=#{authStatus},rzh_date=#{rzhDate}
            ,rzh_belong_date=#{rzhBelongDate},rzlx=#{rzlx}
        </if>

        <if test="xxly!=null">
            ,xxly=#{xxly}
        </if>
        <if test="glzt!=null">
            ,glzt=#{glzt}
        </if>
        <if test="yxTaxAmount!=null">
            ,yx_tax_amount=#{yxTaxAmount}
        </if>

        <if test="bdkStatus != null ">
            ,bdk_status = #{bdkStatus}
        </if>

        <if test="entryDiscrepancy != null">
            ,entry_discrepancy = #{entryDiscrepancy}
        </if>
        <if test="bdkMessage!=null and bdkMessage!=''">
            ,bdk_message=#{bdkMessage}
        </if>
        where customs_code =#{customsCode}
    </update>


    <!--  通过缴款书号码查询缴款书   -->
    <select id="getExportByCustomsCode" parameterType="java.lang.String" resultType="java.lang.Integer">
        select count(1)
        from t_dx_export_customs
        where customs_code = #{customsCode}
    </select>

    <!--  通过缴款书号码查询缴款书   -->
    <select id="getByCustomsCode" parameterType="java.lang.String"
            resultType="com.dxhy.core.job.entity.TDxCustomsRecordJob">
        select *
        from t_dx_customs_record
        where customs_code = #{customsCode}
    </select>

    <select id="selectLastReqDate" 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="selectLastReqDate" 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="selectLastCustomsDate" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="mysql">
        SELECT DATE_FORMAT(create_date, "%Y%m%d%H%i%s")
        FROM t_dx_customs_record
        where gf_tax_no = #{taxno}
        order by create_date desc
        limit 1
    </select>
    <!-- 获取底账表中该税号下最后的海关缴款书采集时间 -->
    <select id="selectLastCustomsDate" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="oracle">
        SELECT TO_CHAR(create_date, 'yyyyMMddHH24mmss')
        FROM t_dx_customs_record
        where gf_tax_no = #{taxno}
          and rownum = 1
        order by create_date desc
    </select>

    <update id="updateCustoms" databaseId="mysql">
        update t_dx_customs_record set update_date=now()
        <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='glzt!=null and glzt != "0"'>
            ,status_update_date=now()
        </if>

        <if test='dataSources!=null and dataSources != "0"'>
            ,xxly=#{xxly},glzt=#{glzt},yx_tax_amount=#{yxTaxAmount} ,tax_amount = #{taxAmount},entry_discrepancy =
            #{entryDiscrepancy}
            ,gf_tax_no=#{gfTaxNo},fill_in_date=#{fillInDate},dqskssq=#{dqskssq},gxjzr=#{gxjzr},gxfwq=#{gxfwq},gxfwz=#{gxfwz},data_sources='0'
        </if>
        <if test="bdkMessage!=null and bdkMessage!=''">
            ,bdk_message=#{bdkMessage}
        </if>
        where customs_code = #{customsCode}
    </update>
    <update id="updateCustoms" databaseId="oracle">
        update t_dx_customs_record set update_date=sysdate
        <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='glzt!=null and glzt != "0"'>
            ,status_update_date=sysdate
        </if>

        <if test='dataSources!=null and dataSources != "0"'>
            ,xxly=#{xxly},glzt=#{glzt},yx_tax_amount=#{yxTaxAmount} ,tax_amount = #{taxAmount},entry_discrepancy =
            #{entryDiscrepancy}
            ,gf_tax_no=#{gfTaxNo},fill_in_date=#{fillInDate},dqskssq=#{dqskssq},gxjzr=#{gxjzr},gxfwq=#{gxfwq},gxfwz=#{gxfwz},data_sources='0'
        </if>
        <if test="bdkMessage!=null and bdkMessage!=''">
            ,bdk_message=#{bdkMessage}
        </if>
        where customs_code = #{customsCode}
    </update>

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

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

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

    <!--申请认证成功，更新底账表-->
    <update id="updateCustomsRecord" databaseId="mysql">
        update t_dx_customs_record SET
        <if test='rzlx=="1" or rzlx=="2" 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 customs_code=#{customsCode}
    </update>
    <update id="updateCustomsRecord" databaseId="oracle">
        update t_dx_customs_record SET
        <if test='rzlx=="1" or rzlx=="2" 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 customs_code=#{customsCode}
    </update>

    <!--申请录入成功，更新底账表-->
    <update id="updateCustomsUploadInfo" databaseId="mysql">
        update t_dx_customs_record
        SET collect_upload_status='1',
            collect_upload_date=now()
        where customs_code = #{customsCode}
    </update>
    <update id="updateCustomsUploadInfo" databaseId="oracle">
        update t_dx_customs_record
        SET collect_upload_status='1',
            collect_upload_date=sysdate
        where customs_code = #{customsCode}
    </update>

    <!--查询要申请认证的缴款书-->
    <select id="getApplyCustoms" resultType="com.dxhy.core.job.entity.TDxCustomsRecordJob" databaseId="mysql">
        select customs_code, rzh_belong_date, fill_in_date, gf_tax_no, rzlx, yx_tax_amount, tax_amount,bdk_message
        from t_dx_customs_record
        where (auth_status = '2' or cxrz_status = '1' or cxbdk_status = '1')
          and gf_tax_no = #{taxno}
        limit 200
    </select>
    <!--查询要申请认证的缴款书-->
    <select id="getApplyCustoms" resultType="com.dxhy.core.job.entity.TDxCustomsRecordJob" databaseId="oracle">
        select customs_code, rzh_belong_date, fill_in_date, gf_tax_no, rzlx, yx_tax_amount, tax_amount,bdk_message
        from t_dx_customs_record
        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.gf_tax_no, g.ace_id, g.ace_key
        from t_dx_customs_apply_record g
        where g.result_status = 'N'
          and g.record_type = #{type}
        group by g.batch_no, g.gf_tax_no, g.ace_id, g.ace_key
    </select>

    <update id="updateApply">
        update t_dx_customs_apply_record
        SET result_status='Y'
        where customs_code = #{customsCode}
          and record_type = #{recordType}
    </update>

    <!--撤销不抵扣认证结果返回，更新底账表-->
    <update id="updateRecordForRebackNoDeduction" databaseId="mysql">
        update t_dx_customs_record SET
        cxbdk_status=#{result.authState},
        cxbdk_date=now()
        <if test='result.legalizeBelongDate!=null and result.legalizeBelongDate != ""'>
            ,bdk_current_period=#{result.legalizeBelongDate}
        </if>
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test="result.bdkMessage!=null and result.bdkMessage!=''">
            ,bdk_message=#{result.bdkMessage}
        </if>
        <if test='result.authState=="4"'>
            ,auth_status='6'
            ,rzh_yesorno='0'
            ,rzh_date = null
            ,rzh_belong_date = null
            ,confirm_user =null
            ,yx_tax_amount=tax_amount
            ,bdk_yy = ''
            ,bdk_message = ''
        </if>
        where customs_code=#{result.customsNo}
    </update>
    <update id="updateRecordForRebackNoDeduction" databaseId="oracle">
        update t_dx_customs_record SET
        cxbdk_status=#{result.authState},
        cxbdk_date=sysdate
        <if test='result.legalizeBelongDate!=null and result.legalizeBelongDate != ""'>
            ,bdk_current_period=#{result.legalizeBelongDate}
        </if>
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test="result.bdkMessage!=null and result.bdkMessage!=''">
            ,bdk_message=#{result.bdkMessage}
        </if>
        <if test='result.authState=="4"'>
            ,auth_status='6'
            ,rzh_yesorno='0'
            ,rzh_date = null
            ,rzh_belong_date = null
            ,confirm_user =null
            ,yx_tax_amount=tax_amount
            ,bdk_yy = ''
            ,bdk_message = ''
        </if>
        where customs_code=#{result.customsNo}
    </update>
    <!--撤销抵扣认证结果返回，更新底账表-->
    <update id="updateRecordForRebackRz" databaseId="mysql">
        update t_dx_customs_record SET
        cxrz_status=#{result.authState},
        cxrz_date=now()
        <if test='result.legalizeBelongDate!=null and result.legalizeBelongDate != ""'>
            ,cxrz_current_period=#{result.legalizeBelongDate}
        </if>
        <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
            ,yx_tax_amount=tax_amount
        </if>
        where customs_code=#{result.customsNo}
    </update>
    <update id="updateRecordForRebackRz" databaseId="oracle">
        update t_dx_customs_record SET
        cxrz_status=#{result.authState},
        cxrz_date=sysdate
        <if test='result.legalizeBelongDate!=null and result.legalizeBelongDate != ""'>
            ,cxrz_current_period=#{result.legalizeBelongDate}
        </if>
        <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
            ,yx_tax_amount=tax_amount
        </if>
        where customs_code=#{result.customsNo}
    </update>
    <!--抵扣认证结果返回，更新底账表-->
    <update id="updateRecord">
        update t_dx_customs_record SET
        rzh_yesorno= #{result.legalizeState},
        rzh_date=#{rzhDate},
        auth_status=#{result.authState},
        rzlx= #{result.rzlx},
        rzh_back_msg=#{result.failureReason}
        <if test='result.legalizeBelongDate!=null and result.legalizeBelongDate != ""'>
            ,rzh_belong_date=#{result.legalizeBelongDate}
        </if>
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test="result.yxse!=null and result.yxse!=''">
            ,yx_tax_amount=#{result.yxse}
        </if>

        <if test='result.authState=="4"'>
            ,cxrz_status='0'
            ,cxrz_date=null
        </if>
        where customs_code=#{result.customsNo}
    </update>

    <!--不抵扣认证结果返回，更新底账表-->
    <update id="updateRecordForNoDeduction">
        update t_dx_customs_record SET
        rzh_yesorno= #{result.legalizeState},
        rzh_date=#{rzhDate},
        auth_status=#{result.authState},
        rzlx= #{result.rzlx},
        rzh_back_msg=#{result.failureReason}
        <if test='result.legalizeBelongDate!=null and result.legalizeBelongDate != ""'>
            ,rzh_belong_date=#{result.legalizeBelongDate}
        </if>
        <if test='result.glzt!=null and result.glzt != ""'>
            ,glzt=#{result.glzt}
        </if>
        <if test="result.yxse!=null and result.yxse!=''">
            ,yx_tax_amount=#{result.yxse}
        </if>
        <if test='result.authState=="4"'>
            ,cxbdk_status='0'
            ,cxbdk_date=null
        </if>
        where customs_code=#{result.customsNo}
    </update>

    <!-- 退税认证结果返回 -->
    <update id="updateRecordForDrawback">
        update t_dx_customs_record SET
        rzh_yesorno= #{result.legalizeState},
        rzh_date=#{rzhDate},
        auth_status=#{result.authState},
        rzlx= #{result.rzlx},
        rzh_back_msg=#{result.failureReason}
        <if test='result.legalizeBelongDate!=null and result.legalizeBelongDate != ""'>
            , rzh_belong_date=#{result.legalizeBelongDate}
        </if>
        <if test="result.yxse!=null and result.yxse!=''">
            ,yx_tax_amount=#{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>
        <if test="result.bdkMessage!=null and result.bdkMessage!=''">
            ,bdk_message=#{result.bdkMessage}
        </if>
        where customs_code=#{result.customsNo}
    </update>

    <!-- 汇总需要发送录入的税号 -->
    <select id="getNeedUploadTaxNo" resultType="java.lang.String">
        select distinct gf_tax_no
        from t_dx_customs_record
        where data_sources in ('1', '2', '3', '4', '5')
          and collect_upload_status = '0'
        group by gf_tax_no
    </select>

    <select id="getUploadCustomsCount" resultType="java.lang.Integer">
        select count(*)
        from t_dx_customs_record
        where data_sources in ('1', '2', '3', '4', '5')
          and collect_upload_status = '0'
          and gf_tax_no = #{taxno}
    </select>

    <!--查询要申请录入的缴款书-->
    <select id="getUploadCustoms" resultType="com.dxhy.core.job.entity.TDxCustomsRecordJob" databaseId="mysql">
        select customs_code, tax_amount, fill_in_date
        from t_dx_customs_record
        where data_sources in ('1', '2', '3', '4', '5')
          and collect_upload_status = '0'
          and gf_tax_no = #{taxno}
        limit 200
    </select>
    <!--查询要申请录入的缴款书-->
    <select id="getUploadCustoms" resultType="com.dxhy.core.job.entity.TDxCustomsRecordJob" databaseId="oracle">
        select customs_code, tax_amount, fill_in_date
        from t_dx_customs_record
        where data_sources in ('1', '2', '3', '4', '5')
          and collect_upload_status = '0'
          and gf_tax_no = #{taxno} <![CDATA[ and rownum <= 200
        ]]>
    </select>

    <!-- 更新录入上传结果-->
    <update id="updateUploadResult">
        update t_dx_customs_record SET

        <if test="result.fillInDate!=null">
            fill_in_date = #{result.fillInDate},
        </if>
        <if test="result.taxAmount!=null and result.taxAmount!=''">
            tax_amount = #{result.taxAmount},
        </if>
        collect_upload_status=#{result.collectUploadStatus},
        <if test="result.auditStatus!=null and result.auditStatus!=''">
            audit_status=#{result.auditStatus},
        </if>
        <if test="result.applyCheck!=null and result.applyCheck!=''">
            apply_check= #{result.applyCheck},
        </if>
        fail_reason=#{result.failReason},
        <if test="result.checkStatus!=null and result.checkStatus!=''">
            check_status=#{result.checkStatus},
        </if>
        upload_date=#{result.uploadDate}

        where customs_code=#{result.customsCode}
    </update>

</mapper>
