<?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.InvoicePoolDao">
    <sql id="sql_fields_added">
        d.invoice_code,
		d.invoice_no,
		d.invoice_type,
		d.invoice_date,
		d.gf_tax_no,
		d.gf_name,
		d.gf_address_and_phone,
		d.gf_bank_and_no,
		d.xf_tax_no,
		d.xf_name,
		d.xf_address_and_phone,
		d.xf_bank_and_no,
		d.invoice_amount,
		d.tax_amount,
		d.total_amount,
		d.remark,
		d.invoice_status,
		d.rzh_yesorno,
		d.confirm_date,
		d.dqskssq,
		d.gxjzr,
		d.gxfwq,
		d.gxfwz,
		d.rzh_type,
		d.rzh_belong_date,
		d.sfygx,
		d.sfdbts,
		d.rzlx,
		d.check_code,
		d.txfbz,
		d.lslbz,
		d.rzh_type,
		d.xxly,
		d.dk_tax_amount,
		d.yqkgxbz,
		d.glzt,
		d.scan_id,
		d.printor
    </sql>
    <!--判断批次号是否申请过-->
    <select id="countBatChno" resultType="int">
        select count(1)
        from t_dx_account_batch
        where batch_no = #{batchNo}
    </select>
    <!--查找抵帐表by UUID-->
    <select id="getRecordJson" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM t_dx_record_invoice
        WHERE uuid = #{uuid}
    </select>
    <!--修改入账信息-->
    <update id="updateRecAccount" parameterType="com.dxhy.erp.entity.InAccountData">
        UPDATE t_dx_record_invoice
        SET payment_status  = #{item.inAccountStatus},
            payment_date    = #{item.inAccountDate},
            voucher_number  = #{item.voucherNumber},
            document_number = #{item.documentNumber}
        WHERE uuid = #{item.uuid}
    </update>
    <!--入账信息插入入账批次表-->
    <insert id="saveAccountBat">
        insert into t_dx_account_batch
        (
        batch_no,
        invoice_code,
        invoice_no,
        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.invoiceCode},
            #{item.invoiceNo},
            #{item.uuid},
            now(),
            #{item.inAccountStatus},
            #{item.inAccountDate},
            #{item.voucherNumber},
            #{item.documentNumber},
            #{item.status},
            #{item.failureReason}
            )
        </foreach>
    </insert>
    <!--判断摊销批次号是否重复-->
    <select id="countAmortizeBatChno" resultType="int">
        select count(1)
        from t_dx_amortize_batch
        where batch_no = #{batchNo}
    </select>
    <!--判断批次表业务单据号是否使用-->
    <select id="countDocumentNo" resultType="int">
        select count(1)
        from t_dx_amortize_batch
        where amortize_document_number = #{documentNumber}
          AND uuid = #{uuid}
    </select>
    <!--修改摊销信息-->
    <update id="updateRecAmortize" parameterType="com.dxhy.erp.entity.AmortizeData">
        UPDATE t_dx_record_invoice
        SET
        amortize_user = #{item.amortizeUser},
        amortize_amount = #{item.amortizeAmount},
        amortize_document_number = #{item.documentNumber},
        remain_amortize_amount = #{item.remainAmortizeAmount}
        <choose>
            <when test="item.overStatus != null and item.overStatus != ''">
                ,amortize_status = #{item.overStatus}
            </when>
            <otherwise>
                ,amortize_status = #{item.amortizeStatus}
            </otherwise>
        </choose>
        WHERE uuid = #{item.uuid}
    </update>
    <!--摊销批次表-->
    <insert id="saveAmortizeBat">
        insert into t_dx_amortize_batch
        (
        batch_no,
        invoice_code,
        invoice_no,
        uuid,
        create_date,
        amortize_status,
        amortize_amount,
        remain_amortize_amount,
        status,
        failure_reason,
        amortize_document_number,
        amortize_user
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{batchNo},
            #{item.invoiceCode},
            #{item.invoiceNo},
            #{item.uuid},
            now(),
            #{item.amortizeStatus},
            #{item.amortizeAmount},
            #{item.remainAmortizeAmount},
            #{item.status},
            #{item.failureReason},
            #{item.documentNumber},
            #{item.amortizeUser}
            )
        </foreach>
    </insert>
    <!-- 进项获取底账总条数-->
    <select id="countBuyerInvoice" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        <if test="data.getDataType == 1">
            t_dx_record_invoice d
        </if>
        <if test="data.getDataType == 2">
            t_dx_sale_record_invoice d
        </if>
        WHERE
        d.detail_yesorno = '1'
        <if test="data.getDataType == 1">
            AND d.gf_tax_no =#{data.taxNo}
        </if>
        <if test="data.getDataType == 2">
            AND d.xf_tax_no =#{data.taxNo}
        </if>
        AND  <![CDATA[d.create_date >=#{data.acquisitionDateBegin}]]>
        AND  <![CDATA[d.create_date < #{data.acquisitionDateEnd}]]>
        AND d.invoice_type = #{data.invoiceType}
    </select>
    <!--进项数据获取-->
    <select id="listBuyerInvoice" resultType="com.dxhy.erp.entity.TdxRecordInvoice">
        SELECT
        <include refid="sql_fields_added"/>
        FROM
        <if test="data.getDataType == 1">
            t_dx_record_invoice d
        </if>
        <if test="data.getDataType == 2">
            t_dx_sale_record_invoice d
        </if>
        WHERE
        d.detail_yesorno = '1'
        <if test="data.getDataType == 1">
            AND d.gf_tax_no =#{data.taxNo}
        </if>
        <if test="data.getDataType == 2">
            AND d.xf_tax_no =#{data.taxNo}
        </if>
        AND  <![CDATA[d.create_date >=#{data.acquisitionDateBegin}]]>
        AND  <![CDATA[d.create_date < #{data.acquisitionDateEnd}]]>
        AND d.invoice_type = #{data.invoiceType}
        order by d.id LIMIT #{startRow},#{num}
    </select>
    <!--机动车明细数据获取-->
    <select id="selectVehicle" resultType="com.dxhy.erp.model.VehicleSaleInvoices">
        select * from
        <if test="dataType == 1">
            t_dx_vehicle_sale_invoice
        </if>
        <if test="dataType == 2">
            t_dx_vehicle_sale_invoice_sales
        </if>
        where uuid=#{uuid}
    </select>
    <!--专、普、电、通行费明细数据获取-->
    <select id="listInvoiceDetail" resultType="com.dxhy.erp.model.InvoiceDetailInfo">
        select * from
        <if test="dataType == 1">
            t_dx_record_invoice_detail
        </if>
        <if test="dataType == 2">
            t_dx_record_invoice_detail_sales
        </if>
        where uuid=#{uuid}
    </select>
    <!-- 进项获取底账状态变更总条数-->
    <select id="countState" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        <if test="data.getDataType == 1">
            t_dx_record_invoice d
        </if>
        <if test="data.getDataType == 2">
            t_dx_sale_record_invoice d
        </if>

        WHERE
        d.detail_yesorno = '1'
        <if test="data.getDataType == 1">
            AND d.gf_tax_no =#{data.taxNo}
        </if>
        <if test="data.getDataType == 2">
            AND d.xf_tax_no =#{data.taxNo}
        </if>
        AND(( <![CDATA[status_update_date >=#{data.statusChangeDateBegin}]]>
        AND  <![CDATA[status_update_date < #{data.statusChangeDateEnd}]]>) or
        (<![CDATA[rzh_date >=#{data.statusChangeDateBegin}]]>
        AND  <![CDATA[rzh_date < #{data.statusChangeDateEnd}]]>))
        AND d.invoice_type = #{data.invoiceType}
    </select>
    <!--获取状态变更发票-->
    <select id="listInvoiceState" resultType="com.dxhy.erp.model.State">
        select
        invoice_code invoiceCode,
        invoice_no invoiceNo,
        invoice_status invoiceStatus,
        qs_status qsStatus,
        qs_name qsUser,
        date_format(qs_date,'%Y%m%d%H%i%S') qsDate,
        rzh_yesorno legalizeState,
        date_format(rzh_date,'%Y%m%d') legalizeDate,
        rzh_belong_date legalizeBelongDate,
        rzlx,
        glzt managementStatus,
        xxly informationSources,
        dk_tax_amount taxCredit,
        yqkgxbz overdueCheckMark
        from
        <if test="data.getDataType == 1">
            t_dx_record_invoice
        </if>
        <if test="data.getDataType == 2">
            t_dx_sale_record_invoice
        </if>

        where
        detail_yesorno='1'
        <if test="data.getDataType == 1">
            AND gf_tax_no =#{data.taxNo}
        </if>
        <if test="data.getDataType == 2">
            AND xf_tax_no =#{data.taxNo}
        </if>
        AND(( <![CDATA[status_update_date >=#{data.statusChangeDateBegin}]]>
        AND  <![CDATA[status_update_date < #{data.statusChangeDateEnd}]]>) or
        (<![CDATA[rzh_date >=#{data.statusChangeDateBegin}]]>
        AND  <![CDATA[rzh_date < #{data.statusChangeDateEnd}]]>))
        AND invoice_type = #{data.invoiceType}
        order by invoice_date limit #{startRow},#{num}
    </select>
</mapper>
