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

    <resultMap id="RelateInvoiceResultMap" type="com.dxhy.core.model.ReceiptInvoiceView">
        <result column="id" property="id"/>
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_no" property="invoiceNo"/>
        <result column="uuid" property="uuid"/>
        <result column="out_status" property="outStatus"/>
        <result column="out_invoice_amout" property="outInvoiceAmout"/>
        <result column="out_tax_amount" property="outTaxAmount"/>
        <result column="out_reason" property="outReason"/>
        <result column="out_remark" property="outRemark"/>
        <result column="out_date" property="outDate"/>
        <result column="out_by" property="outBy"/>
        <result column="is_cancel" property="isCancel"/>
        <!--以下直接copy 发票抵账表的字段，跟pojo同样对应-->
        <result column="gf_name" property="gfName"/>
        <result column="xf_name" property="xfName"/>
        <result column="gf_tax_no" property="gfTaxNo"/>
        <result column="invoice_date" property="invoiceDate"/>
        <result column="invoice_status" property="invoiceStatus"/>
        <result column="invoice_amount" property="invoiceAmount"/>
        <result column="tax_amount" property="taxAmount"/>
        <result column="rzh_date" property="certDate"/>
        <result column="invoice_type" property="invoiceType"/>

    </resultMap>


    <sql id="Base_Column_List">
        a.id,
        a.invoice_code,
        a.invoice_no,
        a.uuid,
        a.out_status,
        a.out_invoice_amout,
        a.out_tax_amount,
        a.out_reason,
        a.out_remark,
        a.out_date,
        a.out_by,
        a.is_cancel,
        a.out_belong_date
    </sql>

    <sql id="common_query_condition">
        <if test="gfsh != null">
            and b.gf_tax_no = #{gfsh}
        </if>
        <if test="gfshList != null and gfshList.size()>0">
            and b.gf_tax_no in
            <foreach collection="gfshList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="sellerName != null">
            and b.xf_name LIKE CONCAT(#{sellerName}, '%')
        </if>
        <if test="invoiceNo != null">
            and a.invoice_no = #{invoiceNo}
        </if>
        <if test="certStatus != null">
            and b.rzh_yesorno = #{certStatus}
        </if>
        <if test="invoiceOutStatus != null and invoiceOutStatus.size()>0">
            and b.out_status in
            <foreach collection="invoiceOutStatus" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="reasonId != null">
            and a.out_reason = #{reasonId}
        </if>

        <if test="invoiceStatus != null">
            and b.invoice_status = #{invoiceStatus}
        </if>
        <if test="cancelStatus != null">
            and a.is_cancel = #{cancelStatus}
        </if>
        <if test="businessCode != null">
            and b.dept_id = #{businessCode}
        </if>
        <if test="company !=null and company !=''">
            and b.company = #{company}
        </if>
    </sql>

    <select id="listPageByInvoice" resultMap="RelateInvoiceResultMap" parameterType="map" databaseId="mysql">
        SELECT
        <include refid="Base_Column_List"/>
        , b.gf_tax_no,b.gf_name,b.xf_name,b.invoice_date,b.invoice_status,b.invoice_amount,b.tax_amount
        , b.rzh_date,b.invoice_type
        FROM t_dx_record_invoice_out a
        LEFT JOIN t_dx_record_invoice b ON a.uuid = b.uuid
        <where>
            1=1
            <include refid="common_query_condition"/>
            <if test="outStartDate != null">
                <![CDATA[  and DATE_FORMAT(a.out_date,'%Y-%m-%d') >= DATE_FORMAT(#{outStartDate},'%Y-%m-%d') ]]>
            </if>
            <if test="outEndDate != null">
                <![CDATA[  and DATE_FORMAT(a.out_date,'%Y-%m-%d') <= DATE_FORMAT(#{outEndDate},'%Y-%m-%d')  ]]>
            </if>
        </where>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
    <select id="listPageByInvoice" resultMap="RelateInvoiceResultMap" parameterType="map" databaseId="oracle">
        SELECT
        <include refid="Base_Column_List"/>
        , b.gf_tax_no,b.gf_name,b.xf_name,b.invoice_date,b.invoice_status,b.invoice_amount,b.tax_amount
        , b.rzh_date,b.invoice_type
        FROM t_dx_record_invoice_out a
        LEFT JOIN t_dx_record_invoice b ON a.uuid = b.uuid
        <where>
            1=1
            <include refid="common_query_condition"/>
            <if test="outStartDate != null and outEndDate != null">
                and a.out_date between to_date(#{outStartDate}||' 00:00:00','yyyy-mm-dd hh24:mi:ss') and
                to_date(#{outEndDate}||' 23:59:59','yyyy-mm-dd hh24:mi:ss')
            </if>
        </where>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>


</mapper>
