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

    <!-- 保存扫描发票数据-->
    <insert id="saveInvoice" parameterType="com.dxhy.erp.entity.InvoiceSavePo"
            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,
         xf_name)
        VALUES (#{invoiceType, jdbcType=TINYINT},
                #{invoiceCode, jdbcType=VARCHAR},
                #{invoiceSerialNo, jdbcType=VARCHAR},
                #{invoiceNo, jdbcType=VARCHAR},
                #{gfTaxNo, jdbcType=VARCHAR},
                #{gfName, jdbcType=VARCHAR},
                #{invoiceAmount, jdbcType=DECIMAL},
                #{taxAmount, jdbcType=DECIMAL},
                #{totalAmount, jdbcType=DECIMAL},
                #{xfTaxNo, jdbcType=VARCHAR},
                #{invoiceDate, jdbcType=DATE},
                #{userAccount, jdbcType=VARCHAR},
                #{userName, jdbcType=VARCHAR},
                #{invoiceStatus.value, jdbcType=TINYINT},
                1,
                #{uuid, jdbcType=VARCHAR},
                #{createDate, jdbcType=TIMESTAMP},
                #{qsDate},
                #{scanId},
                #{notes},
                #{checkCode},
                #{xfName, jdbcType=VARCHAR})
    </insert>
    <select id="selectByUuid" resultType="com.dxhy.erp.entity.InvoiceSavePo">
        select invoice_code,
               invoice_no,
               scan_id,
               invoice_status,
               invoice_type,
               user_name,
               gf_Name
        from t_dx_invoice
        where uuid = #{uuid,jdbcType=VARCHAR}
    </select>
    <!-- 获取库中最大流水号 -->
    <select id="getMaxSerialNo" resultType="java.lang.String">
        SELECT MAX(invoice_serial_no + 0)
        FROM t_dx_invoice
    </select>

    <!-- 物理删除数据 -->
    <update id="deleteInvoice" parameterType="java.lang.Long">
        DELETE
        FROM t_dx_invoice
        WHERE id = #{id}
    </update>

    <!-- 查询扫描表未认证的发票信息 -->
    <select id="automaticSign" resultType="com.dxhy.erp.entity.AutomaticSign">
        SELECT SUBSTRING(r.invoice_date, 1, 10) invoice_date,
               r.invoice_code,
               r.invoice_no,
               r.gf_tax_no,
               r.xf_tax_no,
               r.invoice_amount,
               r.tax_amount,
               r.total_amount,
               r.uuid
        FROM t_dx_invoice r,
             sys_dept o
        WHERE r.invoice_status = 0
          AND r.valid = 1
          AND r.gf_tax_no = o.taxno
          AND r.invoice_status = 0
        ORDER BY r.gf_tax_no
    </select>

    <!-- 自动签收更新底扫描的scanPathId和qsh_status -->
    <update id="timingSing">
        UPDATE t_dx_invoice
        SET invoice_status = #{invoiceStatus},
            qs_date        = now(),
            update_date    = now()

        WHERE uuid = #{uuId}
    </update>


    <!-- 获取有无权限推送艺龙结算平台 -->
    <select id="queryByUuid" resultType="com.dxhy.erp.entity.InvoiceSavePo">
        SELECT *
        FROM t_dx_invoice d
                 LEFT JOIN sys_user u on d.user_account = u.username

        where u.push_yesorno = 1
          AND d.invoice_status = 1
          and d.uuid = #{uuId}
    </select>


    <select id="selectXfName" resultType="java.lang.String">
        select xf_name
        from t_dx_record_invoice
        where xf_tax_no = #{xfTaxNo,jdbcType=VARCHAR}
          and xf_name is not null
    </select>

    <insert id="saveInvoices" parameterType="com.dxhy.erp.entity.InvoiceSavePo"
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO t_dx_record_invoice
        (invoice_type,
         invoice_code,
         invoice_no,
         gf_tax_no,
         gf_name,
         invoice_amount,
         tax_amount,
         total_amount,
         xf_tax_no,
         invoice_date,
         invoice_status,
         valid,
         uuid,
         create_date,
         qs_date,
         xf_name)
        VALUES (#{invoiceType, jdbcType=TINYINT},
                #{invoiceCode, jdbcType=VARCHAR},
                #{invoiceNo, jdbcType=VARCHAR},
                #{gfTaxNo, jdbcType=VARCHAR},
                #{gfName, jdbcType=VARCHAR},
                #{invoiceAmount, jdbcType=DECIMAL},
                #{taxAmount, jdbcType=DECIMAL},
                #{totalAmount, jdbcType=DECIMAL},
                #{xfTaxNo, jdbcType=VARCHAR},
                #{invoiceDate, jdbcType=DATE},
                0,
                1,
                #{uuid, jdbcType=VARCHAR},
                #{createDate, jdbcType=TIMESTAMP},
                #{qsDate},
                #{xfName, jdbcType=VARCHAR})
    </insert>

    <update id="updateRecode" parameterType="com.dxhy.erp.entity.SignedInvoiceVo">
        UPDATE t_dx_record_invoice d
        SET d.xf_name=#{xfName, jdbcType=VARCHAR},
            gf_tax_no=#{gfTaxNo, jdbcType=VARCHAR},
            gf_name=#{gfName, jdbcType=VARCHAR},
            invoice_amount=#{invoiceAmount, jdbcType=DECIMAL},
            tax_amount=#{taxAmount, jdbcType=DECIMAL},
            total_amount=#{totalAmount, jdbcType=DECIMAL},
            xf_tax_no=#{xfTaxNo, jdbcType=VARCHAR},
            invoice_date=#{invoiceDate, jdbcType=DATE},
            qs_date= now()
        WHERE d.invoice_code = #{invoiceCode}
          AND d.invoice_no = #{invoiceNo}
          and xf_address_and_phone is null
          and xf_bank_and_no is null
    </update>
</mapper>
