<?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.erp.dao.TdxCustomsRecordDao">
    <select id="selectCustomsCount" resultType="Integer">
        select count(1)
        from t_dx_customs_record t
        where t.gf_tax_no = #{pramsMap.buyerTaxNo}
          and date_format(t.create_date, '%Y%m%d%H%i%s') between #{pramsMap.createDateBegin} and
            #{pramsMap.createDateEnd}

    </select>

    <select id="selectCustoms" resultType="com.dxhy.erp.entity.TdxCustomsRecord" databaseId="mysql">
        select *
        from t_dx_customs_record t
        where t.gf_tax_no = #{pramsMap.buyerTaxNo}
          and date_format(t.create_date, '%Y%m%d%H%i%s') between #{pramsMap.startDate} and
            #{pramsMap.endDate}
        order by t.create_date
        limit #{pramsMap.start},#{pramsMap.num}
    </select>

    <select id="selectCustoms" resultType="com.dxhy.erp.entity.TdxCustomsRecord" databaseId="oracle">
        select *
        from (
                 SELECT ROWNUM AS rowno, r.*
                 FROM (
                          select *
                          from T_DX_CUSTOMS_RECORD t
                          where t.GF_TAX_NO = #{pramsMap.buyerTaxNo}
                            and TO_CHAR(t.CREATE_DATE, 'yyyyMMddHH24mmss') between #{pramsMap.startDate} and
                              #{pramsMap.endDate}
                          order by t.CREATE_DATE) r <![CDATA[ where ROWNUM <= #{pramsMap.start} + #{pramsMap.num} ]]>) ) table_alias
      <![CDATA[  WHERE table_alias.rowno > #{pramsMap.start}
        ]]>
    </select>
    <select id="selectByStateAllCount" resultType="Integer">
        select
        count(1)
        from t_dx_record_invoice t
        where
        detail_yesorno='1'
        and date_format(last_update_date,'%Y%m%d%H%i%s') between #{pramsMap.startDate} and
        #{pramsMap.endDate}
        <if test="pramsMap.buyerTaxNo !=null and pramsMap.buyerTaxNo != ''">
            and gf_tax_no = #{pramsMap.buyerTaxNo}
        </if>
    </select>
    <select id="selectByState" resultType="com.dxhy.erp.entity.CustomsResInfo" databaseId="mysql">
        select customs_code                        customsNo,
               gf_tax_no                           buyerTaxNo,
               date_format(fill_in_date, '%Y%m%d') fillInDate,
               rzh_yesorno                         legalizeState,
               date_format(rzh_date, '%Y%m%d')     legalizeDate,
               rzh_belong_date                     legalizeBelongDate,
               rzlx,
               glzt,
               xxly,
               truncate(yx_tax_amount, 2)          yxse,
               truncate(tax_amount, 2)             amount,
               entry_discrepancy                   isDiscrepancy
        from t_dx_customs_record t
        where t.gf_tax_no = #{pramsMap.buyerTaxNo}
          and date_format(t.update_date, '%Y%m%d%H%i%s') between #{pramsMap.startDate} and
            #{pramsMap.endDate}
        order by t.update_date
        limit #{pramsMap.start},#{pramsMap.num}
    </select>

    <select id="selectByState" resultType="com.dxhy.erp.entity.CustomsResInfo" databaseId="oracle">
        select *
        from (
                 SELECT ROWNUM AS rowno, r.*
                 FROM (
                          select CUSTOMS_CODE                      customsNo,
                                 GF_TAX_NO                         buyerTaxNo,
                                 TO_CHAR(FILL_IN_DATE, 'yyyyMMdd') fillInDate,
                                 RZH_YESORNO                       legalizeState,
                                 TO_CHAR(RZH_DATE, 'yyyyMMdd')     legalizeDate,
                                 RZH_BELONG_DATE                   legalizeBelongDate,
                                 RZLX,
                                 GLZT,
                                 XXLY,
                                 trunc(YX_TAX_AMOUNT, 2)           yxse,
                                 trunc(TAX_AMOUNT, 2)              amount,
                                 ENTRY_DISCREPANCY                 isDiscrepancy
                          from T_DX_CUSTOMS_RECORD t
                          where t.GF_TAX_NO = #{pramsMap.buyerTaxNo}
                            and TO_CHAR(t.UPDATE_DATE, 'yyyyMMddHH24mmss') between #{pramsMap.startDate} and
                              #{pramsMap.endDate}
                          order by t.UPDATE_DATE) r <![CDATA[ where ROWNUM <= #{pramsMap.start} + #{pramsMap.num} ]]>) ) table_alias
    </select>

    <select id="getCustomsRecord" resultType="com.dxhy.erp.entity.TdxCustomsRecord">
        select *
        from t_dx_customs_record
        where gf_tax_no = #{buyerTaxNo}
          and customs_code = #{customsCode}
    </select>

    <select id="getCustomsRecordByCode" resultType="com.dxhy.erp.entity.TdxCustomsRecord">
        select *
        from t_dx_customs_record
        where customs_code = #{customsCode} limit 1
    </select>
    <select id="selectAuth" resultType="com.dxhy.erp.entity.TdxRecordInvoice">
        select
        t.invoice_code,
        t.invoice_no,
        t.is_auth
        from t_dx_record_invoice t
        where t.invoice_type in('01','03','14')
        and t.rzh_yesorno = '0'
        and date_format(t.invoice_date,'%Y-%m-%d') between #{pramsMap.invoiceDateBegin} and
        #{pramsMap.invoiceDateEnd}
        <if test="pramsMap.buyerTaxNo !=null and pramsMap.buyerTaxNo != ''">
            and t.gf_tax_no = #{pramsMap.buyerTaxNo}
        </if>

    </select>
    <update id="updateCxrz" databaseId="mysql">
        update t_dx_customs_record
        set rzlx='5',
            cxrz_status='1',
            cxrz_current_period=#{taxPeriod},
            cxrz_date=now()
        where customs_code = #{customsCode}
    </update>

    <update id="updateCxrz" databaseId="oracle">
        update t_dx_customs_record
        set rzlx='5',
            cxrz_status='1',
            cxrz_current_period=#{taxPeriod},
            cxrz_date=sysdate
        where customs_code = #{customsCode}
    </update>
    <update id="updateCxbdk" databaseId="mysql">
        update t_dx_customs_record
        set rzlx='6',
            cxbdk_status='1',
            bdk_current_period=#{taxPeriod},
            cxbdk_date=now()
        where customs_code = #{customsCode}
    </update>
    <update id="updateCxbdk" databaseId="oracle">
        update t_dx_customs_record
        set rzlx='6',
            cxbdk_status='1',
            bdk_current_period=#{taxPeriod},
            cxbdk_date=sysdate
        where customs_code = #{customsCode}
    </update>

    <update id="updateRz" databaseId="mysql">
        update t_dx_customs_record
        set rzlx           = #{params.applyRzlx},
            auth_status='2',
            bdk_status=#{bdkStatus},
            yx_tax_amount=#{params.yxse},
            confirm_date=now(),
            confirm_user=#{params.rzhUser},
            rzh_belong_date=#{params.applyTaxPeriod},
            cxrz_date=null,
            cxrz_status='0',
            cxbdk_date=null,
            cxbdk_status='0'
        where customs_code = #{params.customsNo}
    </update>

    <update id="updateRz" databaseId="oracle">
        update t_dx_customs_record
        set rzlx           = #{params.applyRzlx},
            auth_status='2',
            bdk_status=#{bdkStatus},
            yx_tax_amount=#{params.yxse},
            confirm_date=sysdate,
            confirm_user=#{params.rzhUser},
            rzh_belong_date=#{params.applyTaxPeriod},
            cxrz_date=null,
            cxrz_status='0',
            cxbdk_date=null,
            cxbdk_status='0'
        where customs_code = #{params.customsNo}
    </update>
    <select id="selectAuthResult" resultType="Integer">
        select count(1)
        from t_dx_customs_record t,
             t_dx_auth_record d
        where t.customs_code = d.customs_code
          and d.batch_no = #{batchNo}
          and t.auth_status in ('1', '2', '3')
    </select>
    <select id="selectAuthResultBack" resultType="com.dxhy.erp.entity.CustomsResInfo" databaseId="mysql">
        select d.customs_code                        customsNo,
               t.rzh_yesorno                         legalizeState,
               date_format(t.rzh_date, '%Y%m%d')     legalizeDate,
               t.rzh_back_msg                        failureReason,
               date_format(t.fill_in_date, '%Y%m%d') fillInDate,
               t.auth_status                         authState,
               d.apply_tax_period                    legalizeBelongDate,
               d.rzlx,
               t.yx_tax_amount                       yxse,
               t.glzt,
               t.tax_amount                          amount,
               d.batch_no                            batchNo
        from t_dx_auth_record d
                 left join t_dx_customs_record t on t.customs_code = d.customs_code
        where d.batch_no = #{batchNo}
          and t.auth_status in ('0', '4', '5', '6')
    </select>

    <select id="selectAuthResultBack" resultType="com.dxhy.erp.entity.CustomsResInfo" databaseId="oracle">
        select d.customs_code                      customsNo,
               t.rzh_yesorno                       legalizeState,
               to_char(t.rzh_date, 'YYYYMMDD')     legalizeDate,
               t.rzh_back_msg                      failureReason,
               to_char(t.fill_in_date, 'YYYYMMDD') fillInDate,
               t.auth_status                       authState,
               d.apply_tax_period                  legalizeBelongDate,
               d.rzlx,
               t.yx_tax_amount                     yxse,
               t.glzt,
               t.tax_amount                        amount,
               d.batch_no                          batchNo
        from t_dx_auth_record d
                 left join t_dx_customs_record t on t.customs_code = d.customs_code
        where d.batch_no = #{batchNo}
          and t.auth_status in ('0', '4', '5', '6')
    </select>

    <select id="selectUploadResult" resultType="Integer">
        select count(1)
        from t_dx_customs_record t,
             t_dx_upload_record d
        where t.customs_code = d.customs_code
          and d.batch_no = #{batchNo}
          and t.collect_upload_status in ('0', '1')
    </select>
    <select id="selectUploadResultBack" resultType="com.dxhy.erp.entity.CustomsUploadBackInfo" databaseId="mysql">
        select t.customs_code                          customsCode,
               t.tax_amount                            taxAmount,
               date_format(t.fill_in_date, '%Y-%m-%d') fillInDate,
               date_format(t.entry_date, '%Y-%m-%d')   entryDate,
               t.fail_reason                           failureReason,
               t.audit_status                          auditResult,
               t.apply_check                           applyCheckYesorno,
               t.check_status                          examineResult,
               t.collect_upload_status                 authState,
               d.batch_no                              batchNo
        from t_dx_upload_record d
                 left join t_dx_customs_record t on t.customs_code = d.customs_code
        where d.batch_no = #{batchNo}
          and t.collect_upload_status in ('2', '3')
    </select>

    <select id="selectUploadResultBack" resultType="com.dxhy.erp.entity.CustomsUploadBackInfo" databaseId="oracle">
        select t.customs_code                        customsCode,
               t.tax_amount                          taxAmount,
               to_char(t.fill_in_date, 'YYYY-MM-DD') fillInDate,
               to_char(t.entry_date, 'YYYY-MM-DD')   entryDate,
               t.fail_reason                         failureReason,
               t.audit_status                        auditResult,
               t.apply_check                         applyCheckYesorno,
               t.check_status                        examineResult,
               t.collect_upload_status               authState,
               d.batch_no                            batchNo
        from t_dx_upload_record d
                 left join t_dx_customs_record t on t.customs_code = d.customs_code
        where d.batch_no = #{batchNo}
          and t.collect_upload_status in ('2', '3')
    </select>

    <select id="selectAuthResultCount" resultType="Integer">
        select
        count(1)
        from t_dx_record_invoice t
        where 1=1
        <if test="pramsMap.buyerTaxNo !=null and pramsMap.buyerTaxNo != ''">
            and t.gf_tax_no = #{pramsMap.buyerTaxNo}
        </if>
        <if test="pramsMap.rzhStatus != null and pramsMap.rzhStatus != ''">
            and rzh_yesorno = #{pramsMap.rzhStatus}
        </if>
        <if test="pramsMap.taxPeriod != null and pramsMap.taxPeriod != ''">
            and rzh_belong_date = #{pramsMap.taxPeriod}
        </if>
        <if test="pramsMap.rzhUser != null and pramsMap.rzhUser != ''">
            and confirm_user = #{pramsMap.rzhUser}
        </if>

    </select>
    <select id="selectAllAuthResult" resultType="com.dxhy.erp.model.AuthStatus">
        select
        invoice_code invoiceCode,
        invoice_no invoiceNo,
        invoice_amount invoiceAmount,
        tax_amount taxAmount,
        date_format(invoice_date,'%Y%m%d') invoiceDate,
        rzh_back_msg failureReason,
        auth_status authState,
        rzh_belong_date legalizeBelongDate,
        rzlx ,
        glzt managementStatus,
        xxly informationSources,
        dk_tax_amount taxCredit,
        yqkgxbz overdueCheckMark,
        date_format(rzh_date,'%Y%m%d') legalizeDate
        from t_dx_record_invoice t
        where 1=1
        <if test="pramsMap.buyerTaxNo !=null and pramsMap.buyerTaxNo != ''">
            and t.gf_tax_no = #{pramsMap.buyerTaxNo}
        </if>
        <if test="pramsMap.rzhStatus != null and pramsMap.rzhStatus != ''">
            and rzh_yesorno = #{pramsMap.rzhStatus}
        </if>
        <if test="pramsMap.taxPeriod != null and pramsMap.taxPeriod != ''">
            and rzh_belong_date = #{pramsMap.taxPeriod}
        </if>
        <if test="pramsMap.rzhUser != null and pramsMap.rzhUser != ''">
            and confirm_user = #{pramsMap.rzhUser}
        </if>
        order by t.create_date limit #{pramsMap.start},#{pramsMap.num}
    </select>

    <select id="selectRzhStatus" resultType="com.dxhy.erp.entity.TdxRecordInvoice">
        select t.invoice_code,
               t.invoice_no,
               t.rzh_yesorno,
               t.rzh_belong_date,
               t.rzh_date,
               t.rzlx,
               t.is_auth,
               t.dk_tax_amount
        from t_dx_record_invoice t
        where uuid = #{uuid}

    </select>

    <!--判断批次号是否申请过-->
    <select id="countBatChno" resultType="int">
        select count(1)
        from t_dx_account_batch_customs
        where batch_no = #{batchNo}
    </select>

    <!--海关入账信息插入入账批次表-->
    <insert id="saveAccountBat">
        insert into t_dx_account_batch_customs
        (
        batch_no,
        customs_code,
        uuid,
        create_date,
        in_account_status,
        in_account_date,
        voucher_no,
        document_number,
        status,
        failure_reason
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{batchNo},
            #{item.customsCode},
            #{item.uuid},
            now(),
            #{item.inAccountStatus},
            #{item.inAccountDate},
            #{item.voucherNumber},
            #{item.documentNumber},
            #{item.status},
            #{item.failureReason}
            )
        </foreach>
    </insert>

    <!--修改入账信息-->
    <update id="setAccount" parameterType="com.dxhy.erp.entity.CustomsInAccountData">
        UPDATE t_dx_customs_record
        SET in_account_status  = #{inAccountStatus},
            in_account_date    = #{inAccountDate},
            voucher_number  = #{voucherNumber},
            document_number = #{documentNumber}
        WHERE customs_code = #{customsCode}
    </update>
</mapper>
