<?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.TicketDao">
    <resultMap id="InvoicePoolHjResultMap"
               type="com.dxhy.core.entity.TicketInvoiceModel">
        <result column="hjsl" property="id"/>
        <result column="hjje" property="invoiceAmount"/>
        <result column="hjse" property="taxAmount"/>
    </resultMap>
    <select id="queryTotal" parameterType="java.util.Map"
            resultMap="InvoicePoolHjResultMap" databaseId="mysql">
        select count(1) hjsl,sum(t.invoice_amount)hjje,sum(t.tax_amount)hjse
        from t_dx_invoice_reimburse t
        where 1=1
        <if test="kpksrq != null and kpksrq != '' and kpksrq != 'null' and kpjsrq != null and kpjsrq != '' and kpjsrq != 'null'">
            and t.invoice_date between #{kpksrq} and #{kpjsrq}
        </if>
        <if test="cjksyf != null and cjksyf != '' and cjksyf != 'null' and cjjsyf != null and cjjsyf != '' and cjjsyf != 'null'">
            and t.create_date between #{cjksyf} and #{cjjsyf}
        </if>
        <if test="keyWord != null and keyWord != '' and keyWord != 'null' ">
            and CONCAT(IFNULL(t.passenger,''),IFNULL(t.passenger_s_f_z_h,'')) like CONCAT('%',#{keyWord},'%')
        </if>
        and t.gf_tax_no in
        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and t.invoice_type in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="qszt != null and qszt != '' and qszt != 'null' and qszt != '99'">
            and t.qs_status = #{qszt}
        </if>
        <if test="qsyf != null and qsyf != null and qsyf != '' and qsyf != ''">
            and DATE_FORMAT(t.qs_date,'%Y-%m') = #{qsyf}
        </if>
        <if test="qsr != null and qsr != '' and qsr != 'null' ">
            and t.qs_name like CONCAT(#{qsr},'%')
        </if>
        <if test="qsfs != null and qsfs != '' and qsfs != 'null' and qsfs != '99'">
            and t.qs_type = #{qsfs}
        </if>
        <if test="isDeduction != null and isDeduction != ''">
            and t.is_deduction = #{isDeduction}
        </if>

    </select>


    <select id="queryTickets" parameterType="java.util.Map"
            resultType="com.dxhy.core.entity.TicketInvoiceModel" databaseId="mysql">
        select *
        from t_dx_invoice_reimburse t
        where 1=1
        <if test="kpksrq != null and kpksrq != '' and kpksrq != 'null' and kpjsrq != null and kpjsrq != '' and kpjsrq != 'null'">
            and t.invoice_date between #{kpksrq} and #{kpjsrq}
        </if>
        <if test="cjksyf != null and cjksyf != '' and cjksyf != 'null' and cjjsyf != null and cjjsyf != '' and cjjsyf != 'null'">
            and t.create_date between #{cjksyf} and #{cjjsyf}
        </if>
        <if test="keyWord != null and keyWord != '' and keyWord != 'null' ">
            and CONCAT(IFNULL(t.passenger,''),IFNULL(t.passenger_s_f_z_h,'')) like CONCAT('%',#{keyWord},'%')
        </if>
<!--        and t.gf_tax_no in-->
<!--        <foreach collection="gfsh" index="index" item="item" open="(" separator="," close=")">-->
<!--            #{item}-->
<!--        </foreach>-->
        and t.invoice_type in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="qszt != null and qszt != '' and qszt != 'null' and qszt != '99'">
            and t.qs_status = #{qszt}
        </if>
        <if test="qsyf != null and qsyf != null and qsyf != '' and qsyf != ''">
            and DATE_FORMAT(t.qs_date,'%Y-%m') = #{qsyf}
        </if>
        <if test="qsr != null and qsr != '' and qsr != 'null' ">
            and t.qs_name like CONCAT(#{qsr},'%')
        </if>
        <if test="qsfs != null and qsfs != '' and qsfs != 'null' and qsfs != '99'">
            and t.qs_type = #{qsfs}
        </if>
        <if test="isDeduction != null and isDeduction != ''">
            and t.is_deduction = #{isDeduction}
        </if>
        <if test="company !=null and company !=''">
            and t.company = #{company}
        </if>
        <if test="postingTimeEnd != null and postingTimeEnd != '' and postingTimeEnd != 'null' and postingTimeEnd != '99' and postingTimeStart != null and postingTimeStart != '' and postingTimeStart != 'null' and postingTimeStart != '99'">
            and t.posting_time between #{postingTimeStart} and #{postingTimeEnd}
        </if>
        <if test="imageNumber != null and imageNumber != '' and imageNumber != 'null' and imageNumber != '99'">
            and t.image_id = #{imageNumber}
        </if>
        <if test="accountPeriod != null and accountPeriod != '' and accountPeriod != 'null' and accountPeriod != '99'">
            and t.account_period = #{accountPeriod}
        </if>
        <if test="inAccountStatus != null and inAccountStatus != '' and inAccountStatus != 'null' and inAccountStatus != '99' and inAccountStatus == '1'.toString()">
            and t.inaccount_status = #{inAccountStatus}
        </if>
        <if test="inAccountStatus != null and inAccountStatus != '' and inAccountStatus != 'null' and inAccountStatus != '99' and inAccountStatus == '0'.toString()">
            and (t.inaccount_status = '0' or t.inaccount_status = '' or t.inaccount_status is null)
        </if>
        <if test="bzdh != null and bzdh != '' and bzdh != 'null' and badh != '99'">
            and t.bzdh = #{bzdh}
        </if>
        <if test="bzr != null and bzr != '' and bzr != 'null'">
            and t.bzr like concat('%',#{bzr},'%')
        </if>
        <if test="snVoucherNumber != null and snVoucherNumber != '' and snVoucherNumber != 'null' and snVoucherNumber != '99'">
            and t.sn_voucher_number = #{snVoucherNumber}
        </if>
        <if test="companyCode != null and companyCode != '' and companyCode != 'null' and companyCode == '99'">
            and (t.comp_code in
            <foreach collection="companyCodes" index="index" item="companyCode" separator="," open="(" close=")">
                #{companyCode}
            </foreach>
            or t.comp_code is null or t.comp_code = ''
            )
        </if>
        <if test="companyCode != null and companyCode != '' and companyCode != 'null' and companyCode != '99' and companyCode != '88'">
            and t.comp_code = #{companyCode}
        </if>
        <if test="companyCode != null and companyCode != '' and companyCode != 'null' and companyCode != '99' and companyCode == '88'">
            and (t.comp_code is null or t.comp_code = '')
        </if>
    </select>


    <select id="selectPassengerTheme"
            resultType="com.dxhy.core.model.InvoiceMainBodyVO">
        SELECT invoice_code,
               invoice_no,
               invoice_date,
               invoice_type,
               gf_name,
               gf_address_and_phone,
               gf_bank_and_no,
               gf_tax_no,
               xf_tax_no,
               xf_name,
               xf_address_and_phone,
               xf_bank_and_no,
               invoice_status,
               create_date,
               '' AS                            rzlx,
               source_system,
               date_format(qs_date, '%Y-%m-%d') qsrq,
               qs_name,
               qs_type,
               qs_status                        qszt,
               '' AS                            rzh_date,
               '' AS                            rzh_type,
               '' AS                            gx_user_name,
               '' AS                            dqskssq,
               related_business_documents,
               business_document_maker,
               business_document_number,
               reimbursement_time,
               reimbursement_status,
               ticket_number,
               passenger,
               is_deduction,
               uuid,
               departure_city,
               reaching_city,
               invoice_amount,
               tax_amount,
               total_amount,
               tax_rate,
               tax_code,
               train_number,
               date_format(ticket_departure_time,'%Y-%m-%d %H:%i:%s') ticket_departure_time,
               date_format(ticket_arrival_time,'%Y-%m-%d %H:%i:%s') ticket_arrival_time,
               passenger_s_f_z_h,
               fuel_surcharge,
               scan_id,
               fare,
               caac_development_fund,
               seat_level as seatLevel
        FROM t_dx_invoice_reimburse
        where select_id = #{selectId}

    </select>


    <select id="selectInvoiceMainBody"
            resultType="com.dxhy.core.model.InvoiceMainBodyVO"
            databaseId="mysql">
        select d.*
        from (
                 SELECT a.*
                 FROM (
                          SELECT invoice_code,
                                 invoice_no,
                                 invoice_date,
                                 invoice_type,
                                 invoice_amount,
                                 tax_amount,
                                 total_amount,
                                 gf_name,
                                 gf_address_and_phone,
                                 gf_bank_and_no,
                                 gf_tax_no,
                                 xf_tax_no,
                                 xf_name,
                                 xf_address_and_phone,
                                 xf_bank_and_no,
                                 invoice_status,
                                 create_date,
                                 source_system,
                                 qs_date,
                                 qs_name,
                                 qs_type,
                                 rzh_date,
                                 rzh_type,
                                 confirm_user as gx_user_name,
                                 rzh_belong_date dqskssq,
                                 ''           AS related_business_documents,
                                 ''           AS business_document_maker,
                                 ''           AS business_document_number,
                                 ''           AS reimbursement_time,
                                 ''           AS reimbursement_status,
                                 ''           AS ticket_number,
                                 ''           AS passenger,
                                 ''           AS is_deduction,
                                 uuid,
                                 ''           AS departure_city,
                                 ''           AS reaching_city,
                                 ''           AS tax_rate,
                                 remark,
                                 scan_id
                          FROM t_dx_record_invoice
                          WHERE invoice_code = #{invoiceCode}
                            and invoice_no = #{invoiceNo}
                          UNION ALL
                          SELECT invoice_code,
                                 invoice_no,
                                 invoice_date,
                                 invoice_type,
                                 invoice_amount,
                                 tax_amount,
                                 total_amount,
                                 gf_name,
                                 gf_address_and_phone,
                                 gf_bank_and_no,
                                 gf_tax_no,
                                 xf_tax_no,
                                 xf_name,
                                 xf_address_and_phone,
                                 xf_bank_and_no,
                                 invoice_status,
                                 create_date,
                                 source_system,
                                 qs_date,
                                 qs_name,
                                 qs_type,
                                 '' AS rzh_date,
                                 '' AS rzh_type,
                                 '' AS gx_user_name,
                                 '' AS dqskssq,
                                 related_business_documents,
                                 business_document_maker,
                                 business_document_number,
                                 reimbursement_time,
                                 reimbursement_status,
                                 ticket_number,
                                 passenger,
                                 is_deduction,
                                 uuid,
                                 departure_city,
                                 reaching_city,
                                 tax_rate,
                                 '' as remark,
                                 scan_id
                          FROM t_dx_invoice_reimburse
                          where invoice_code = #{invoiceCode}
                            and invoice_no = #{invoiceNo}
                      ) AS a
                 ORDER BY a.create_date
                 limit 10
             ) AS d
        GROUP BY d.uuid
        having min(d.create_date)
    </select>

    <select id="getGfName" resultType="java.lang.String">
        select taxname
        from sys_dept
        where taxno = #{gfsh}
          and org_type = '5'
    </select>

    <select id="getBlocName" resultType="java.lang.String">
        select taxname
        from sys_dept
        where company = (select company from sys_dept where taxno = #{gfsh} and org_type = '5')
          and org_type = '1'
    </select>
</mapper>
