<?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.dao.CheckTaskDao">
    <select id="getNoDetailInvoiceOld" resultType="com.dxhy.core.job.pojo.CheckNoDetailInvoice" databaseId="mysql">
        SELECT gf_tax_no buyerTaxNo,
               invoice_type,
               invoice_code,
               invoice_no,
               invoice_date,
               check_code,
               invoice_amount
        FROM t_dx_record_invoice
        where detail_yesorno = '0'
          and source_system = '0'
          AND check_date &lt;= date_sub(curdate(), INTERVAL 3 DAY)
          and invoice_date >= '2017-01-01'
    </select>
    <select id="getNoDetailInvoiceOld" resultType="com.dxhy.core.job.pojo.CheckNoDetailInvoice" databaseId="oracle">
        SELECT gf_tax_no buyerTaxNo,
               invoice_type,
               invoice_code,
               invoice_no,
               invoice_date,
               check_code,
               invoice_amount
        FROM t_dx_record_invoice
        where detail_yesorno = '0'
          and source_system = '0'
          AND check_date &lt;= trunc(sysdate - 3)
          and invoice_date >= to_date('2017-01-01', 'yyyy-mm-dd')
    </select>

    <!-- 查询需要补全明细的发票数据 -->
    <select id="getNoDetailInvoice" resultType="com.dxhy.core.job.pojo.CheckNoDetailInvoice" databaseId="mysql">
        SELECT gf_tax_no buyerTaxNo,
               invoice_type,
               invoice_code,
               invoice_no,
               invoice_date,
               check_code,
               invoice_amount,
               total_amount
        FROM t_dx_record_invoice
        where detail_yesorno = '0'
          and source_system = '0'
          AND check_date > date_sub(curdate(), INTERVAL 3 DAY)
          and invoice_date >= '2017-01-01'
    </select>

    <!-- 查询需要补全明细的发票数据 -->
    <select id="getNoDetailInvoice" resultType="com.dxhy.core.job.pojo.CheckNoDetailInvoice" databaseId="oracle">
        SELECT gf_tax_no buyerTaxNo,
               invoice_type,
               invoice_code,
               invoice_no,
               invoice_date,
               check_code,
               invoice_amount
        FROM t_dx_record_invoice
        where detail_yesorno = '0'
          and source_system = '0'
          AND check_date > trunc(sysdate - 3)
          and invoice_date >= to_date('2017-01-01', 'yyyy-mm-dd')
    </select>

    <delete id="deleteDetail">
        delete
        from t_dx_record_invoice_detail
        where uuid = #{invoiceDetailInfo.uuid}
    </delete>

    <insert id="insertNoDetailedInvoice">
        INSERT INTO t_dx_record_invoice_detail (
        uuid,
        invoice_code,
        invoice_no,
        detail_no,
        goods_name,
        model,
        unit,
        num,
        unit_price,
        detail_amount,
        tax_rate,
        tax_amount,
        cph,
        lx,
        txrqq,
        txrqz,
        goods_num
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.uuid},#{item.invoiceCode},#{item.invoiceNo},#{item.detailNo},#{item.goodsName},#{item.model},
            #{item.unit},#{item.num},#{item.unitPrice},#{item.detailAmount},#{item.taxRate},#{item.taxAmount},#{item.cph},#{item.lx},
            #{item.txrqq},#{item.txrqz},#{item.goodsNum})
        </foreach>
    </insert>


    <!-- 查验成功后更新抵账主表数据 -->
    <update id="updateRecordInvoice">

        UPDATE t_dx_record_invoice SET
        gf_tax_no=#{gfTaxNo},
        gf_name=#{gfName},
        invoice_type=#{invoiceType},
        invoice_code=#{invoiceCode},
        invoice_no=#{invoiceNo},
        total_amount=#{totalAmount},
        xf_tax_no=#{xfTaxNo},
        xf_name=#{xfName},
        xf_address_and_phone=#{xfAddressAndPhone},
        xf_bank_and_no=#{xfBankAndNo},
        gf_bank_and_no=#{gfBankAndNo},
        gf_address_and_phone=#{gfAddressAndPhone},
        invoice_date=#{invoiceDate},
        remark=#{remark},
        check_code=#{checkCode},
        detail_yesorno=#{detailYesorno},
        <if test="invoiceStatus!=null">
            invoice_status=#{invoiceStatus},
        </if>
        machinecode=#{machinecode}
        WHERE invoice_no=#{invoiceNo}
        <if test="invoiceCode!=null and invoiceCode!=''">
            AND  invoice_code=#{invoiceCode}
        </if>
    </update>
</mapper>
