<?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.passengerticket.dao.PassengerReimbursementSystemDao">
    <resultMap id="BaseResultMap" type="com.dxhy.passengerticket.entity.TdxInvoiceReimburse">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="invoice_type" property="invoiceType" jdbcType="VARCHAR"/>
        <result column="invoice_code" property="invoiceCode" jdbcType="VARCHAR"/>
        <result column="invoice_no" property="invoiceNo" jdbcType="VARCHAR"/>
        <result column="invoice_date" property="invoiceDate" jdbcType="DATE"/>
        <result column="ticket_number" property="ticketNumber" jdbcType="VARCHAR"/>
        <result column="passenger" property="passenger" jdbcType="VARCHAR"/>
        <result column="invoice_amount" property="invoiceAmount" jdbcType="VARCHAR"/>
        <result column="tax_amount" property="taxAmount" jdbcType="VARCHAR"/>
        <result column="total_amount" property="totalAmount" jdbcType="VARCHAR"/>
        <result column="invoice_status" property="invoiceStatus" jdbcType="VARCHAR"/>
        <result column="dqskssq" property="dqskssq" jdbcType="VARCHAR"/>
        <result column="source_system" property="sourceSystem" jdbcType="VARCHAR"/>
        <result column="select_id" property="selectId" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="selectpassengerTicketList" parameterType="java.util.Map"
            resultType="com.dxhy.passengerticket.vo.InvoiceMainBody" databaseId="mysql">
        select d.* from (
        select a.* from (
        select b.* from(
        SELECT
        id,
        invoice_type,
        invoice_code,
        invoice_no,
        invoice_date,
        '' as ticket_number,
        '' as passenger,
        invoice_amount,
        tax_amount,
        total_amount,
        invoice_status,
        dqskssq,
        source_system,
        uuid,
        create_date,
        '' as reimbursement_status,
        qs_date,
        '' as is_deduction,
        gf_tax_no,
        '' as select_id
        FROM t_dx_record_invoice
        ) as b
        WHERE
        invoice_type IN ('92','90','89','88','87')
        <if test="gfTaxNo!='' ">
            and gf_tax_no =#{gfTaxNo}
        </if>
        <if test="invoiceMaxDate!=null">
            and invoice_date &lt;= #{invoiceMaxDate}
        </if>
        <if test="invoiceMinDate!=null">
            and invoice_date &gt;= #{invoiceMinDate}
        </if>
        <if test="invoiceType!='' and invoiceType!=null and invoiceType !='99' ">
            and invoice_type = #{invoiceType}
        </if>
        <if test="invoiceStatus!='' and invoiceStatus!=null and invoiceStatus!='99' ">
            and invoice_status = #{invoiceStatus}
        </if>
        <if test="qsDateMin!=null">
            and qs_date &lt;= #{qsDateMax}
        </if>
        <if test="qsDateMin!=null">
            and qs_date &gt;= #{qsDateMin}
        </if>
        UNION ALL
        select c.* from (
        SELECT
        id,
        invoice_type,
        invoice_code,
        invoice_no,
        invoice_date,
        ticket_number,
        passenger,
        invoice_amount,
        tax_amount,
        total_amount,
        invoice_status,
        dqskssq,
        source_system,
        uuid,
        create_date,
        reimbursement_status,
        qs_date,
        is_deduction,
        gf_tax_no,
        select_id
        FROM
        t_dx_invoice_reimburse
        ) as c
        WHERE
        invoice_type IN ('92','90','89','88','87','10')
        and is_deduction = '0'
        <if test="gfTaxNo!='' ">
            and gf_tax_no =#{gfTaxNo}
        </if>
        <if test=" invoiceMaxDate!=null ">
            and invoice_date &lt;= #{invoiceMaxDate}
        </if>
        <if test="invoiceMinDate!=null">
            and invoice_date &gt;= #{invoiceMinDate}
        </if>
        <if test="invoiceType!='' and invoiceType!=null and invoiceType!='99' ">
            and invoice_type = #{invoiceType}
        </if>
        <if test="invoiceStatus!='' and invoiceStatus!=null and invoiceStatus!='99'  ">
            and invoice_status = #{invoiceStatus}
        </if>
        <if test="ticketNumber!='' and ticketNumber!=null  ">
            and ticket_number = #{ticketNumber}
        </if>
        <if test="passenger!='' and passenger!=null">
            and passenger = #{passenger}
        </if>
        <if
                test="reimbursementStatus!='' and reimbursementStatus!=null and reimbursementStatus != '99'  ">
            and reimbursement_status = #{reimbursementStatus}
        </if>
        <if test=" qsDateMin!=null  ">
            and qs_date &lt;= #{qsDateMax}
        </if>
        <if test="qsDateMin!=null  ">
            and qs_date &gt;= #{qsDateMin}
        </if>
        ) AS a ORDER BY
        a.create_date limit 100000
        ) AS d
        GROUP BY
        d.uuid having min(d.create_date)
    </select>


    <select id="selectpassengerTicketList" parameterType="java.util.Map"
            resultType="com.dxhy.passengerticket.vo.InvoiceMainBody" databaseId="oracle">
        <!-- select d.* from ( -->
        select * from (

        SELECT
        id,
        invoice_type,
        invoice_code,
        invoice_no,
        invoice_date,
        cast('' as NVARCHAR2(32)) ticket_number,
        cast('' as NVARCHAR2(32)) passenger,
        invoice_amount,
        tax_amount,
        total_amount,
        invoice_status,
        dqskssq,
        source_system,
        uuid,
        create_date,
        cast('' as NVARCHAR2(32)) reimbursement_status,
        qs_date,
        cast('' as NVARCHAR2(32)) is_deduction,
        gf_tax_no,
        cast('' as NVARCHAR2(32)) select_id
        FROM t_dx_record_invoice

        WHERE
        invoice_type IN ('92','90','89','88','87')
        <if test="gfTaxNo!='' ">
            and gf_tax_no =#{gfTaxNo}
        </if>
        <if test="invoiceMaxDate!=null">
            and invoice_date &lt;= to_date(#{invoiceMaxDate},'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="invoiceMinDate!=null">
            and invoice_date &gt;= to_date(#{invoiceMinDate},'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="invoiceType!='' and invoiceType!=null and invoiceType !='99' ">
            and invoice_type = #{invoiceType}
        </if>
        <if test="invoiceStatus!='' and invoiceStatus!=null and invoiceStatus!='99' ">
            and invoice_status = #{invoiceStatus}
        </if>
        <if test="qsDateMin!=null">
            and qs_date &lt;= to_date(#{qsDateMax},'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="qsDateMin!=null">
            and qs_date &gt;= to_date(#{qsDateMin},'yyyy-MM-dd hh24:mi:ss')
        </if>
        UNION ALL

        SELECT
        id,
        invoice_type,
        invoice_code,
        invoice_no,
        invoice_date,
        ticket_number,
        passenger,
        invoice_amount,
        tax_amount,
        total_amount,
        invoice_status,
        dqskssq,
        source_system,
        uuid,
        create_date,
        reimbursement_status,
        qs_date,
        is_deduction,
        gf_tax_no,
        select_id
        FROM
        t_dx_invoice_reimburse

        WHERE
        invoice_type IN ('92','90','89','88','87','10')
        and is_deduction = '0'
        <if test="gfTaxNo!='' ">
            and gf_tax_no =#{gfTaxNo}
        </if>
        <if test=" invoiceMaxDate!=null ">
            and invoice_date &lt;= to_date(#{invoiceMaxDate},'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="invoiceMinDate!=null">
            and invoice_date &gt;= to_date(#{invoiceMinDate},'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="invoiceType!='' and invoiceType!=null and invoiceType!='99' ">
            and invoice_type = #{invoiceType}
        </if>
        <if test="invoiceStatus!='' and invoiceStatus!=null and invoiceStatus!='99'  ">
            and invoice_status = #{invoiceStatus}
        </if>
        <if test="ticketNumber!='' and ticketNumber!=null  ">
            and ticket_number = #{ticketNumber}
        </if>
        <if test="passenger!='' and passenger!=null">
            and passenger = #{passenger}
        </if>
        <if
                test="reimbursementStatus!='' and reimbursementStatus!=null and reimbursementStatus != '99'  ">
            and reimbursement_status = #{reimbursementStatus}
        </if>
        <if test=" qsDateMin!=null  ">
            and qs_date &lt;= to_date(#{qsDateMax},'yyyy-MM-dd hh24:mi:ss')
        </if>
        <if test="qsDateMin!=null  ">
            and qs_date &gt;= to_date(#{qsDateMin},'yyyy-MM-dd hh24:mi:ss')
        </if>
        )
        ORDER BY create_date

        <!-- limit 100000
        ) AS d
        GROUP BY
        d.uuid having min(d.create_date) -->
    </select>


    <select id="selectUuid" parameterType="java.lang.String"
            resultType="java.lang.String" databaseId="mysql">
        select uuid
        from t_dx_record_invoice
        where uuid = #{uuid}
          and qs_status = '1'
    </select>
    <select id="selectPassengerTicketDetails"
            resultType="com.dxhy.passengerticket.entity.TdxInvoiceReimburse" databaseId="mysql">
        SELECT invoice_type,
               invoice_code,
               invoice_no,
               invoice_date,
               ticket_number,
               passenger,
               invoice_amount,
               tax_amount,
               total_amount,
               invoice_status,
               dqskssq,
               source_system,
               departure_city,
               reaching_city,
               related_business_documents,
               business_document_maker,
               business_document_number,
               reimbursement_time,
               reimbursement_status,
               train_number,
               ticket_departure_time,
               ticket_arrival_time,
               xf_name,
               gf_tax_no,
               gf_name,
               xf_tax_no,
               select_id,
               uuid
        FROM t_dx_invoice_reimburse
        where select_id = #{selectId}
    </select>

    <insert id="updateiInsertReimbursement" databaseId="mysql">
        update t_dx_invoice_reimburse
        set reimbursement_status = #{reimbursementStatus}
        where select_id = #{selectId}
    </insert>

    <select id="selectOrgType" resultType="java.lang.String" databaseId="mysql">
        select db_name
        from sys_dept
        where taxno = #{gfTaxNo}
          and org_type = '5'
    </select>

    <delete id="delectReimbursementSystem" databaseId="mysql">
        delete
        from t_dx_invoice_reimburse
        where select_id = #{selectId}
    </delete>
    <delete id="deleteVehicleDetail" databaseId="mysql">
        delete
        from t_dx_vehicle_sale_invoice
        where uuid = #{uuid}
    </delete>
    <delete id="deleteInvoiceDetails" databaseId="mysql">
        delete
        from t_dx_record_invoice_detail
        where uuid = #{uuid}
    </delete>
</mapper>
