<?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.InvoiceElectronDao">
    <insert id="saveInvoice" parameterType="com.dxhy.erp.entity.InvoiceElectron"
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO t_dx_invoice
        (invoice_type,
         invoice_code,
         invoice_serial_no,
         invoice_no,
         gf_tax_no,
         gf_name,
         invoice_amount,
         tax_amount,
         total_amount,
         xf_tax_no,
         invoice_date,
         user_account,
         user_name,
         invoice_status,
         valid,
         uuid,
         create_date,
         qs_date,
         scan_id,
         notes,
         check_code)
        VALUES (#{invoiceType},
                #{invoiceCode},
                #{invoiceSerialNo},
                #{invoiceNo},
                #{gfTaxNo},
                #{gfName},
                #{invoiceAmount},
                #{taxAmount},
                #{totalAmount},
                #{xfTaxNo},
                #{invoiceDate},
                #{userAccount},
                #{userName},
                #{qsStatus},
                1,
                #{uuid},
                #{createDate},
                #{qsDate},
                #{scanId},
                #{notes},
                #{checkCode})
    </insert>
    <select id="selectByInvoice" resultType="com.dxhy.erp.entity.InvoiceElectron">
        select id,
               invoice_no,
               invoice_code,
               invoice_type,
               gf_tax_no,
               gf_name,
               xf_tax_no,
               invoice_amount,
               tax_amount,
               total_amount,
               invoice_date,
               invoice_status,
               qs_date,
               scan_id,
               create_date,
               notes,
               pdf_name
        from t_dx_invoice
        where create_date = #{createDate}
          and invoice_type = #{invoiceType}
          and valid = 1
    </select>
    <update id="updateByInvoice" databaseId="mysql">
        update t_dx_invoice
        set update_date=now(),
            qs_date = now(),
            qs_status=#{invoice.qsStatus},
            notes = #{invoice.notes},
            vendor_id=#{invoice.vendorId},
            request_id=#{invoice.requestId},
            check_return_code=#{invoice.checkReturnCode}
        where uuid = #{invoice.uuid}
    </update>

    <update id="updateByInvoice" databaseId="oracle">
        update t_dx_invoice
        set update_date=sysdate,
            qs_date = sysdate,
            qs_status=#{invoice.qsStatus},
            notes = #{invoice.notes},
            vendor_id=#{invoice.vendorId},
            request_id=#{invoice.requestId},
            check_return_code=#{invoice.checkReturnCode}
        where uuid = #{invoice.uuid}
    </update>

    <update id="updateByInvoiceStatus" databaseId="mysql">
        update t_dx_invoice
        set update_date=now(),
            invoice_status=#{invoiceStatus}
        where uuid = #{uuid}
    </update>

    <update id="updateByInvoiceStatus" databaseId="oracle">
        update t_dx_invoice
        set update_date=sysdate,
            invoice_status=#{invoiceStatus}
        where uuid = #{uuid}
    </update>

    <insert id="insertRecordInvoice" parameterType="com.dxhy.erp.entity.RecordInvoice" databaseId="mysql">
        insert into t_dx_record_invoice(invoice_type,
                                        invoice_code,
                                        invoice_no,
                                        invoice_date,
                                        gf_tax_no,
                                        gf_name,
                                        gf_address_and_phone,
                                        gf_bank_and_no,
                                        xf_tax_no,
                                        xf_name,
                                        xf_address_and_phone,
                                        xf_bank_and_no,
                                        invoice_amount,
                                        tax_amount,
                                        total_amount,
                                        remark,
                                        last_update_date,
                                        invoice_status,
                                        handle_state,
                                        qs_date,
                                        rzh_yesorno,
                                        source_SYSDATE,
                                        valid,
                                        uuid,
                                        create_date,
                                        the_only_tax,
                                        auth_status,
                                        scan_status)
        values (#{invoiceType},
                #{invoiceCode},
                #{invoiceNo},
                #{invoiceDate},
                #{gfTaxNo},
                #{gfName},
                #{gfAddressAndPhone},
                #{gfBankAndNo},
                #{xfTaxNo},
                #{xfName},
                #{xfAddressAndPhone},
                #{xfBankAndNo},
                #{invoiceAmount},
                #{taxAmount},
                #{totalAmount},
                #{remark},
                #{lastUpdateDate},
                #{invoiceStatus},
                0,
                #{qsDate},
                0,
                1,
                1,
                #{uuid},
                now(),
                #{theOnlyTax},
                0,
                #{scanStatus})
    </insert>


    <insert id="insertRecordInvoice" parameterType="com.dxhy.erp.entity.RecordInvoice" databaseId="oracle">
        insert into t_dx_record_invoice(invoice_type,
                                        invoice_code,
                                        invoice_no,
                                        invoice_date,
                                        gf_tax_no,
                                        gf_name,
                                        gf_address_and_phone,
                                        gf_bank_and_no,
                                        xf_tax_no,
                                        xf_name,
                                        xf_address_and_phone,
                                        xf_bank_and_no,
                                        invoice_amount,
                                        tax_amount,
                                        total_amount,
                                        remark,
                                        last_update_date,
                                        invoice_status,
                                        handle_state,
                                        qs_date,
                                        rzh_yesorno,
                                        source_SYSDATE,
                                        valid,
                                        uuid,
                                        create_date,
                                        the_only_tax,
                                        auth_status,
                                        scan_status)
        values (#{invoiceType},
                #{invoiceCode},
                #{invoiceNo},
                #{invoiceDate},
                #{gfTaxNo},
                #{gfName},
                #{gfAddressAndPhone},
                #{gfBankAndNo},
                #{xfTaxNo},
                #{xfName},
                #{xfAddressAndPhone},
                #{xfBankAndNo},
                #{invoiceAmount},
                #{taxAmount},
                #{totalAmount},
                #{remark},
                #{lastUpdateDate},
                #{invoiceStatus},
                0,
                #{qsDate},
                0,
                1,
                1,
                #{uuid},
                SYSDATE,
                #{theOnlyTax},
                0,
                #{scanStatus})
    </insert>

    <update id="updateByRecordInvoice" databaseId="mysql">
        update t_dx_record_invoice
        set update_date=now(),
            qs_date=now(),
            last_update_date=now(),
            scan_status=#{scanStatus}
        where uuid = #{uuid}
    </update>

    <update id="updateByRecordInvoice" databaseId="oracle">
        update t_dx_record_invoice
        set update_date=SYSDATE,
            qs_date=SYSDATE,
            last_update_date=SYSDATE,
            scan_status=#{scanStatus}
        where uuid = #{uuid}
    </update>


    <update id="updateByRecordInvoiceRequestId" databaseId="mysql">
        update t_dx_record_invoice
        set last_update_date=now(),
            vendor_id=#{vendorId},
            request_id=#{requestId}
        where uuid = #{uuid}
    </update>

    <update id="updateByRecordInvoiceRequestId" databaseId="oracle">
        update t_dx_record_invoice
        set last_update_date=SYSDATE,
            vendor_id=#{vendorId},
            request_id=#{requestId}
        where uuid = #{uuid}
    </update>

    <update id="updateByRequestId" databaseId="mysql">
        update t_dx_invoice
        set update_date=now(),
            request_id=#{requestId},
            vendor_id=#{vendorId},
            old_request_id=#{oldRequestId}
        where uuid = #{uuid}
    </update>

    <update id="updateByRequestId" databaseId="oracle">
        update t_dx_invoice
        set update_date=SYSDATE,
            request_id=#{requestId},
            vendor_id=#{vendorId},
            old_request_id=#{oldRequestId}
        where uuid = #{uuid}
    </update>


    <update id="updateByRequestId" databaseId="mysql">
        update t_dx_invoice
        set update_date=now(),
            request_id=#{requestId},
            vendor_id=#{vendorId},
            old_request_id=#{oldRequestId}
        where uuid = #{uuid}
    </update>


    <update id="updateByRequestId" databaseId="oracle">
        update t_dx_invoice
        set update_date=SYSDATE,
            request_id=#{requestId},
            vendor_id=#{vendorId},
            old_request_id=#{oldRequestId}
        where uuid = #{uuid}
    </update>

    <insert id="insertImg" parameterType="com.dxhy.erp.entity.InvoiceImgSavePo" databaseId="mysql">
        insert into t_dx_invoice_img(image_path,
                                     uuid,
                                     valid,
                                     scan_id,
                                     create_date,
                                     update_date)
        values (#{imagePath},
                #{uuid},
                1,
                #{scanId},
                now(),
                now())
    </insert>
    <insert id="insertImg" parameterType="com.dxhy.erp.entity.InvoiceImgSavePo" databaseId="oracle">
        insert into t_dx_invoice_img(image_path,
                                     uuid,
                                     valid,
                                     scan_id,
                                     create_date,
                                     update_date)
        values (#{imagePath},
                #{uuid},
                1,
                #{scanId},
                SYSDATE,
                SYSDATE)
    </insert>
    <insert id="insertImgLog" parameterType="com.dxhy.erp.entity.ElectronLog" databaseId="mysql">
        insert into t_dx_electron_log(invoice_no,
                                      invoice_code,
                                      file_path,
                                      create_date,
                                      file_name)
        values (#{invoiceNo},
                #{invoiceCode},
                #{filePath},
                now(),
                #{fileName})
    </insert>

    <insert id="insertImgLog" parameterType="com.dxhy.erp.entity.ElectronLog" databaseId="oracle">
        insert into t_dx_electron_log(invoice_no,
                                      invoice_code,
                                      file_path,
                                      create_date,
                                      file_name)
        values (#{invoiceNo},
                #{invoiceCode},
                #{filePath},
                SYSDATE,
                #{fileName})
    </insert>


    <sql id="column_sql">
        id
        ,
		scan_path,
		create_date,
		update_date,
		valid
    </sql>

    <update id="updateByNote">
        update t_dx_invoice
        set notes=#{note}
        where uuid = #{uuid}
    </update>
    <update id="updateByCheckReturnCode">
        update t_dx_invoice
        set notes=#{note},
            check_return_code=#{returnCode},
            request_id=#{requestId},
            vendor_id=#{vendorId},
            old_request_id=#{requestId}
        where uuid = #{uuid}
    </update>


    <update id="updateByTimes">
        update t_dx_invoice
        set create_date=#{createDate},
            notes=#{note},
            scan_id=#{scanId}
        where uuid = #{uuid}
    </update>
    <update id="updateByQsDate">
        update t_dx_invoice
        set qs_date=null
        where uuid = #{uuid}
    </update>
    <update id="updateByInfo">
        update t_dx_invoice
        set gf_tax_no=#{gfTaxNo},
            xf_tax_no=#{xfTaxNo},
            total_amount=#{totalAmount},
            invoice_amount=#{invoiceAmount},
            tax_amount=#{taxAmount},
            invoice_date=#{invoiceDate},
            gf_name=#{gfName},
            xf_name=#{xfName}
        where invoice_code = #{invoiceCode}
          and invoice_no = #{invoiceNo}
    </update>
    <select id="selectByTaxNo" resultType="java.lang.String">
        select tax_code
        from t_dx_org_taxno
        where tax_code = #{taxCode}
    </select>

    <select id="selectInvoiceByIds" resultType="com.dxhy.erp.entity.InvoiceElectron">
        select
        id,
        invoice_no,
        invoice_code,
        invoice_type,
        gf_tax_no,
        gf_name,
        xf_tax_no,
        invoice_amount,
        tax_amount,
        total_amount,
        invoice_date,
        invoice_status,
        qs_date,
        scan_id,
        create_date,
        notes,
        pdf_name,
        check_code
        from t_dx_invoice
        where valid=1
        <if test="isAll == 0 and isAll != ''">
            AND invoice_status != 1
        </if>
        AND id IN
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        ORDER BY create_date
    </select>

    <update id="updateElectron">
        UPDATE t_dx_invoice
        SET invoice_code   = #{invoiceCode},
            invoice_no     = #{invoiceNo},
            gf_tax_no      = #{gfTaxNo},
            gf_name        = #{gfName},
            xf_tax_no      = #{xfTaxNo},
            invoice_amount = #{invoiceAmount},
            tax_amount     = #{taxAmount},
            total_amount   = #{totalAmount},
            invoice_date   = #{invoiceDate},
            check_code     = #{checkCode},
            notes          = #{notes},
            uuid           = #{uuid}
        WHERE id = #{id}
    </update>

    <select id="selectInvoiceElectron" resultType="com.dxhy.erp.entity.InvoiceElectron">
        select *
        from t_dx_invoice
        where invoice_status != 1
          and invoice_type = '3'
          and user_account = #{userAccount}
    </select>
    <select id="selectElectron" resultType="com.dxhy.erp.entity.InvoiceElectron">
        select *
        from t_dx_invoice
        where invoice_code = #{invoiceCode}
          and invoice_no = #{invoiceNo}
    </select>
    <select id="selectByScanId" resultType="com.dxhy.erp.entity.InvoiceImgSavePo">
        select *
        from t_dx_invoice_img
        where scan_id = #{scanId}
    </select>
    <select id="selectByRequestId" resultType="com.dxhy.erp.entity.InvoiceElectron">
        select *
        from t_dx_invoice
        where request_id = #{requestId}
           or old_request_id = #{requestId}
    </select>

    <select id="selectElectronToCheck" resultType="com.dxhy.erp.entity.InvoiceElectron">
        SELECT *
        FROM t_dx_invoice
        WHERE qs_type = '5'
          AND company = 'YL'
          AND user_name = 'e-invoice'
          AND qs_status = '0'
          AND ((
                   check_return_code != '1007'
			AND check_return_code != '1026'
			AND check_return_code != '1022'
			AND check_return_code != '0001'
			AND check_return_code != '2022'
                   )
            OR check_return_code IS NULL)
    </select>
</mapper>
