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

    <select id="getTaxno" resultType="com.dxhy.core.job.entity.TAcOrg">
        SELECT
        DISTINCT qy.nsrsbh taxno,
        qy.nsrmc taxname,
        config.db_name dbName,
        qy.company,
        config.ace_id aceId,
        config.ace_key aceKey,
        config.data_ace_id dataAceId,
        config.data_ace_key dataAceKey
          FROM
        sys_group_config config
        JOIN sys_enterprise_config qy ON config.company = qy.company
        where qy.sfcj = '1'
    </select>

    <!--<select id="getTaxno" resultType="com.dxhy.core.job.entity.TAcOrg">
        select *
        from sys_dept
        where org_type = '5'
          and cj_status = '1'
          and product_type = '1'
    </select>-->

    <select id="selectLastInvoiceDate" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="mysql">
        SELECT DATE_FORMAT(req_date, '%Y-%m-%d')
        FROM t_dx_http_log_data_platform
        where taxno = #{taxno}
          and invoice_type = #{type}
          and subject = #{reqType}
          and type = '01'
        order by req_date desc limit 1
    </select>

    <select id="selectLastInvoiceDate" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="oracle">
        select c.* from (
        SELECT to_char(req_date, 'YYYY-MM-DD')
        FROM t_dx_http_log_data_platform
        where taxno = #{taxno}
          and invoice_type = #{type}
          and subject = #{reqType}
          and type = '01'
        order by req_date desc) c where  rownum = 1
    </select>

    <select id="selectLastInvoiceDateState" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="mysql">
        SELECT DATE_FORMAT(req_date, '%Y-%m-%d')
        FROM t_dx_http_log_data_platform
        where taxno = #{taxno}
          and subject = #{reqType}
          and type = '02'
        order by req_date desc limit 1
    </select>

    <select id="selectLastInvoiceDateState" parameterType="java.lang.String" resultType="java.lang.String"
            databaseId="oracle">
        select c.* from (
        SELECT to_char(req_date, 'YYYY-MM-DD')
        FROM t_dx_http_log_data_platform
        where taxno = #{taxno}
          and subject = #{reqType}
          and type = '02'
        order by req_date desc ) c where  rownum = 1
    </select>

    <select id="getNotParseHttpLogs" resultType="com.dxhy.core.job.entity.TDxHttpLogDataPlatform">
        select log_id,
               taxno,
               type,
               req_date,
               req_text,
               res_date,
               res_text,
               status,
               company,
               invoice_type,
               subject
        from t_dx_http_log_data_platform
        where type = #{type}
          and status = '0'
    </select>

    <update id="updateHttpLogById" parameterType="com.dxhy.core.job.entity.TDxHttpLogDataPlatform">
        update t_dx_http_log_data_platform
        set status = #{status},
            total  =#{total}
        where log_id = #{logId}
    </update>

    <delete id="delDetailIncome" parameterType="java.lang.String">
        delete
        from t_dx_record_invoice_detail
        where uuid = #{uuid}
    </delete>
    <delete id="delDetailSales" parameterType="java.lang.String">
        delete
        from t_dx_record_invoice_detail_sales
        where uuid = #{uuid}
    </delete>

    <insert id="insertDetailIncome" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceDetailInfo">
        insert into t_dx_record_invoice_detail (invoice_code,
                                                invoice_no,
                                                goods_name,
                                                model,
                                                unit,
                                                num,
                                                unit_price,
                                                detail_amount,
                                                tax_rate,
                                                tax_amount,
                                                goods_num,
                                                uuid,
                                                is_discount,
                                                cph,
                                                lx,
                                                txrqq,
                                                txrqz)
        values (#{entity.invoiceCode},
                #{entity.invoiceNo},
                #{entity.ivwName},
                #{entity.ivspec},
                #{entity.unit},
                #{entity.wnumb},
                #{entity.wprice},
                #{entity.amount},
                #{entity.taxRate},
                #{entity.taxAmount},
                #{entity.itemCode},
                #{entity.uuid},
                #{entity.isDiscount},
                #{entity.platNo},
                #{entity.vehicleType},
                #{entity.transitStartTime},
                #{entity.transitEndTime})
    </insert>
    <insert id="insertDetailSales" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceDetailInfo">
        insert into t_dx_record_invoice_detail_sales (invoice_code,
                                                      invoice_no,
                                                      goods_name,
                                                      model,
                                                      unit,
                                                      num,
                                                      unit_price,
                                                      detail_amount,
                                                      tax_rate,
                                                      tax_amount,
                                                      goods_num,
                                                      uuid,
                                                      is_discount,
                                                      cph,
                                                      lx,
                                                      txrqq,
                                                      txrqz)
        values (#{entity.invoiceCode},
                #{entity.invoiceNo},
                #{entity.ivwName},
                #{entity.ivspec},
                #{entity.unit},
                #{entity.wnumb},
                #{entity.wprice},
                #{entity.amount},
                #{entity.taxRate},
                #{entity.taxAmount},
                #{entity.itemCode},
                #{entity.uuid},
                #{entity.isDiscount},
                #{entity.platNo},
                #{entity.vehicleType},
                #{entity.transitStartTime},
                #{entity.transitEndTime})
    </insert>


    <select id="getDetailCount" resultType="int">
        select count(1)
        from t_dx_record_invoice_detail
        where uuid = #{uuid}
    </select>


    <update id="updateRecordInvoice" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceInfo" databaseId="mysql">
        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="invoiceType != null and invoiceType != ''">invoice_type=#{invoiceType},</if>
            <if test="buyerName != null and buyerName != ''">gf_name=#{buyerName},</if>
            <if test="buyerTaxNo != null and buyerTaxNo != ''">gf_tax_no=#{buyerTaxNo},</if>
            <if test="buyerAddressPhone != null and buyerAddressPhone != ''">
                gf_address_and_phone=#{buyerAddressPhone},
            </if>
            <if test="buyerBankAccount != null and buyerBankAccount != ''">gf_bank_and_no=#{buyerBankAccount},</if>
            <if test="sellerName != null and sellerName != ''">xf_name=#{sellerName},</if>
            <if test="sellerTaxNo != null and sellerTaxNo != ''">xf_tax_no=#{sellerTaxNo},</if>
            <if test="sellerAddressPhone != null and sellerAddressPhone != ''">
                xf_address_and_phone=#{sellerAddressPhone},
            </if>
            <if test="sellerBankAccount != null and sellerBankAccount != ''">xf_bank_and_no=#{sellerBankAccount},</if>
            <if test="invoiceDate != null">invoice_date=#{invoiceDate,jdbcType=TIMESTAMP},</if>
            <if test="totalAmount != null and totalAmount != ''">invoice_amount=#{totalAmount},</if>
            <if test="totalTaxAmount != null and totalTaxAmount != ''">tax_amount=#{totalTaxAmount},</if>
            <if test="allAmount != null and allAmount != ''">total_amount=#{allAmount},</if>
            <if test="remark != null and remark != ''">remark=#{remark},</if>
            <if test="invoiceState != null and invoiceState != ''">invoice_status=#{invoiceState},</if>
            <if test="legalizeState != null and legalizeState != ''">rzh_yesorno=#{legalizeState},</if>
            <if test="legalizeDate != null">rzh_date=#{legalizeDate,jdbcType=TIMESTAMP},</if>
            <if test="legalizeBelongDate != null">rzh_belong_date=#{legalizeBelongDate,jdbcType=TIMESTAMP},</if>
            <if test="legalizeType != null and legalizeType != ''">rzh_type=#{legalizeType},</if>
            <if test="checkCode != null and checkCode != ''">check_code=#{checkCode},</if>

            <if test="uuid != null and uuid != ''">uuid=#{uuid},</if>
            <if test="statusUpdateDate != null">status_update_date=#{statusUpdateDate},</if>
            <if test="detailYesorno != null and detailYesorno != ''">detail_yesorno=#{detailYesorno},</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="updateRecordInvoice" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceInfo" databaseId="oracle">
        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="invoiceType != null and invoiceType != ''">invoice_type=#{invoiceType},</if>
            <if test="buyerName != null and buyerName != ''">gf_name=#{buyerName},</if>
            <if test="buyerTaxNo != null and buyerTaxNo != ''">gf_tax_no=#{buyerTaxNo},</if>
            <if test="buyerAddressPhone != null and buyerAddressPhone != ''">
                gf_address_and_phone=#{buyerAddressPhone},
            </if>
            <if test="buyerBankAccount != null and buyerBankAccount != ''">gf_bank_and_no=#{buyerBankAccount},</if>
            <if test="sellerName != null and sellerName != ''">xf_name=#{sellerName},</if>
            <if test="sellerTaxNo != null and sellerTaxNo != ''">xf_tax_no=#{sellerTaxNo},</if>
            <if test="sellerAddressPhone != null and sellerAddressPhone != ''">
                xf_address_and_phone=#{sellerAddressPhone},
            </if>
            <if test="sellerBankAccount != null and sellerBankAccount != ''">xf_bank_and_no=#{sellerBankAccount},</if>
            <if test="invoiceDate != null">invoice_date=to_date(#{invoiceDate,jdbcType=TIMESTAMP},'yyyy-mm-dd hh24:mi:ss'),</if>
            <if test="totalAmount != null and totalAmount != ''">invoice_amount=#{totalAmount},</if>
            <if test="totalTaxAmount != null and totalTaxAmount != ''">tax_amount=#{totalTaxAmount},</if>
            <if test="allAmount != null and allAmount != ''">total_amount=#{allAmount},</if>
            <if test="remark != null and remark != ''">remark=#{remark},</if>
            <if test="invoiceState != null and invoiceState != ''">invoice_status=#{invoiceState},</if>
            <if test="legalizeState != null and legalizeState != ''">rzh_yesorno=#{legalizeState},</if>
            <if test="legalizeDate != null">rzh_date=to_date(#{legalizeDate,jdbcType=TIMESTAMP},'yyyy-mm-dd hh24:mi:ss'),</if>
            <if test="legalizeBelongDate != null">rzh_belong_date=#{legalizeBelongDate,jdbcType=TIMESTAMP},</if>
            <if test="legalizeType != null and legalizeType != ''">rzh_type=#{legalizeType},</if>
            <if test="checkCode != null and checkCode != ''">check_code=#{checkCode},</if>

            <if test="uuid != null and uuid != ''">uuid=#{uuid},</if>
            <if test="statusUpdateDate != null">status_update_date=#{statusUpdateDate},</if>
            <if test="detailYesorno != null and detailYesorno != ''">detail_yesorno=#{detailYesorno},</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="updateState" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceInfo" databaseId="mysql">
        update t_dx_record_invoice
        set invoice_status=#{entity.invoiceState},
            status_update_date=now()
        where uuid = #{entity.uuid}
    </update>

    <update id="updateState" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceInfo" databaseId="oracle">
        update t_dx_record_invoice
        set invoice_status=#{entity.invoiceState},
            status_update_date=sysdate
        where uuid = #{entity.uuid}
    </update>


    <update id="updateSaleState" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceInfo" databaseId="mysql">
        update t_dx_sale_record_invoice
        set invoice_status=#{entity.invoiceState},
            status_update_date=now()
        where uuid = #{entity.uuid}
    </update>

    <update id="updateSaleState" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceInfo" databaseId="oracle">
        update t_dx_sale_record_invoice
        set invoice_status=#{entity.invoiceState},
            status_update_date=sysdate
        where uuid = #{entity.uuid}
    </update>


    <insert id="insertFailLog" databaseId="mysql">
        insert into t_dx_data_platform_invoice_log (invoice_code, invoice_no, system_source, message, create_date)
        values (#{entity.invoiceCode}, #{entity.invoiceNo}, '3', #{message}, now())
    </insert>

    <insert id="insertFailLog" databaseId="oracle">
        insert into t_dx_data_platform_invoice_log (invoice_code, invoice_no, system_source, message, create_date)
        values (#{entity.invoiceCode}, #{entity.invoiceNo}, '3', #{message}, sysdate)
    </insert>


    <select id="getDeptByTaxno" resultType="com.dxhy.core.job.entity.TAcOrg">
        SELECT config.db_name dbName
          FROM
        sys_group_config config
        JOIN sys_enterprise_config qy ON config.company = qy.company
        where qy.nsrsbh = #{taxno}
    </select>
<!--
    <select id="getDeptByTaxno" resultType="com.dxhy.core.job.entity.TAcOrg">
        select *
        from sys_dept
        where taxno = #{taxno}
          and org_type = '5'
          and product_type = '1'
    </select>
-->


    <select id="getNeedParseTaxno" resultType="java.lang.String">
        select taxno
        from t_dx_http_log_data_platform
        where type = '01'
          and status in ('0', '2')
        group by taxno
    </select>


    <select id="getHttpLogsByTaxno" resultType="com.dxhy.core.job.entity.TDxHttpLogDataPlatform">
        select log_id,
               taxno,
               type,
               req_date,
               req_text,
               res_date,
               res_text,
               status,
               company,
               invoice_type,
               subject
        from t_dx_http_log_data_platform
        where type = '01'
          and status in ('0', '2')
          and taxno = #{taxno}
    </select>

    <select id="getFirstCollectDate" resultType="java.lang.String" databaseId="mysql">
        SELECT date_format(first_collect_date, '%Y-%m-%d') firstCollectDate
          FROM
        sys_group_config config
        JOIN sys_enterprise_config qy ON config.company = qy.company
        where qy.nsrsbh = #{taxno} limit 1
    </select>
<!--
    <select id="getFirstCollectDate" resultType="java.lang.String" databaseId="mysql">
        select date_format(first_collect_date, '%Y-%m-%d') firstCollectDate
        from sys_dept
        where taxno = #{taxno}
          and org_type = '5' limit 1
    </select>
-->

    <select id="getFirstCollectDate" resultType="java.lang.String" databaseId="oracle">
        select to_char(first_collect_date, 'YYYY-MM-DD') firstCollectDate
        sys_group_config config
        JOIN sys_enterprise_config qy ON config.company = qy.company
        where qy.nsrsbh = #{taxno} and rownum = 1
    </select>
<!--
    <select id="getFirstCollectDate" resultType="java.lang.String" databaseId="oracle">
        select to_char(first_collect_date, 'YYYY-MM-DD') firstCollectDate
        from sys_dept
        where taxno = #{taxno}
          and org_type = '5' and rownum = 1
    </select>
-->


    <update id="updateSaleRecordInvoice" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceInfo" databaseId="mysql">
        update t_dx_sale_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="invoiceType != null and invoiceType != ''">invoice_type=#{invoiceType},</if>
            <if test="buyerName != null and buyerName != ''">gf_name=#{buyerName},</if>
            <if test="buyerTaxNo != null and buyerTaxNo != ''">gf_tax_no=#{buyerTaxNo},</if>
            <if test="buyerAddressPhone != null and buyerAddressPhone != ''">
                gf_address_and_phone=#{buyerAddressPhone},
            </if>
            <if test="buyerBankAccount != null and buyerBankAccount != ''">gf_bank_and_no=#{buyerBankAccount},</if>
            <if test="sellerName != null and sellerName != ''">xf_name=#{sellerName},</if>
            <if test="sellerTaxNo != null and sellerTaxNo != ''">xf_tax_no=#{sellerTaxNo},</if>
            <if test="sellerAddressPhone != null and sellerAddressPhone != ''">
                xf_address_and_phone=#{sellerAddressPhone},
            </if>
            <if test="sellerBankAccount != null and sellerBankAccount != ''">xf_bank_and_no=#{sellerBankAccount},</if>
            <if test="invoiceDate != null">invoice_date=#{invoiceDate,jdbcType=TIMESTAMP},</if>
            <if test="totalAmount != null and totalAmount != ''">invoice_amount=#{totalAmount},</if>
            <if test="totalTaxAmount != null and totalTaxAmount != ''">tax_amount=#{totalTaxAmount},</if>
            <if test="allAmount != null and allAmount != ''">total_amount=#{allAmount},</if>
            <if test="remark != null and remark != ''">remark=#{remark},</if>
            <if test="invoiceState != null and invoiceState != ''">invoice_status=#{invoiceState},</if>
            <if test="legalizeState != null and legalizeState != ''">rzh_yesorno=#{legalizeState},</if>
            <if test="legalizeDate != null">rzh_date=#{legalizeDate,jdbcType=TIMESTAMP},</if>
            <if test="legalizeBelongDate != null">rzh_belong_date=#{legalizeBelongDate,jdbcType=TIMESTAMP},</if>
            <!--             <if test="rzlx != null and rzlx != ''" >rzlx=#{rzlx},</if>   -->
            <if test="legalizeType != null and legalizeType != ''">rzh_type=#{legalizeType},</if>
            <if test="checkCode != null and checkCode != ''">check_code=#{checkCode},</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="statusUpdateDate != null">status_update_date=#{statusUpdateDate},</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="updateSaleRecordInvoice" parameterType="com.dxhy.core.job.pojo.DataPlatformInvoiceInfo" databaseId="oracle">
        update t_dx_sale_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="invoiceType != null and invoiceType != ''">invoice_type=#{invoiceType},</if>
            <if test="buyerName != null and buyerName != ''">gf_name=#{buyerName},</if>
            <if test="buyerTaxNo != null and buyerTaxNo != ''">gf_tax_no=#{buyerTaxNo},</if>
            <if test="buyerAddressPhone != null and buyerAddressPhone != ''">
                gf_address_and_phone=#{buyerAddressPhone},
            </if>
            <if test="buyerBankAccount != null and buyerBankAccount != ''">gf_bank_and_no=#{buyerBankAccount},</if>
            <if test="sellerName != null and sellerName != ''">xf_name=#{sellerName},</if>
            <if test="sellerTaxNo != null and sellerTaxNo != ''">xf_tax_no=#{sellerTaxNo},</if>
            <if test="sellerAddressPhone != null and sellerAddressPhone != ''">
                xf_address_and_phone=#{sellerAddressPhone},
            </if>
            <if test="sellerBankAccount != null and sellerBankAccount != ''">xf_bank_and_no=#{sellerBankAccount},</if>
            <if test="invoiceDate != null">invoice_date=to_date(#{invoiceDate,jdbcType=TIMESTAMP},'yyyy-mm-dd hh24:mi:ss'),</if>
            <if test="totalAmount != null and totalAmount != ''">invoice_amount=#{totalAmount},</if>
            <if test="totalTaxAmount != null and totalTaxAmount != ''">tax_amount=#{totalTaxAmount},</if>
            <if test="allAmount != null and allAmount != ''">total_amount=#{allAmount},</if>
            <if test="remark != null and remark != ''">remark=#{remark},</if>
            <if test="invoiceState != null and invoiceState != ''">invoice_status=#{invoiceState},</if>
            <if test="legalizeState != null and legalizeState != ''">rzh_yesorno=#{legalizeState},</if>
            <if test="legalizeDate != null">rzh_date=to_date(#{legalizeDate,jdbcType=TIMESTAMP},'yyyy-mm-dd hh24:mi:ss'),</if>
            <if test="legalizeBelongDate != null">rzh_belong_date=#{legalizeBelongDate,jdbcType=TIMESTAMP},</if>
            <!--             <if test="rzlx != null and rzlx != ''" >rzlx=#{rzlx},</if>   -->
            <if test="legalizeType != null and legalizeType != ''">rzh_type=#{legalizeType},</if>
            <if test="checkCode != null and checkCode != ''">check_code=#{checkCode},</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="statusUpdateDate != null">status_update_date=#{statusUpdateDate},</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>

</mapper>
