<?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.bonus.core.pay.common.mapper.TradeRecordMapper">

    <!--根据设备订单号查询支付结果-->
    <select id="queryTradeRecordInfo" resultType="com.bonus.core.pay.common.model.TradeRecord">
        SELECT tr.id,
        tr.related_trade_id,
        tr.trade_state tradeState,
        tr.amount,
        tr.cust_id custId,
        tr.pay_type payType,
        tr.pay_channel payChannel,
        tr.source_type sourceType,
        tr.out_trade_no outTradeNo,
        tr.trade_result tradeResult,
        tr.param
        FROM trade_record tr
        INNER JOIN trade_order_recharge ort ON tr.id = ort.trade_id
        <where>
            <if test="macOrderId != null and macOrderId != ''">
                AND ort.mac_order_id = #{macOrderId}
            </if>
            <if test="orderRechargeId != null">
                AND ort.order_recharge_id = #{orderRechargeId}
            </if>
            <if test="tradeState != null">
                AND tr.trade_state = #{tradeState}
            </if>
            <if test="tradeType != null">
                AND tr.trade_type = #{tradeType}
            </if>
            <if test="startTime != null">
                AND tr.create_time <![CDATA[ >= ]]> #{startTime}
                AND ort.create_time <![CDATA[ >= ]]> #{startTime}
            </if>
            <if test="endTime != null">
                AND tr.create_time <![CDATA[ <= ]]> #{endTime}
                AND ort.create_time <![CDATA[ <= ]]> #{endTime}
            </if>
        </where>
        order by tr.create_time
    </select>

    <select id="listTradeRecordByOrderRechargeId" resultType="com.bonus.core.pay.common.model.TradeRecord">
        SELECT tr.id,
        tr.related_trade_id,
        tr.cust_id     custId,
        tr.amount,
        tr.trade_state tradeState,
        tr.trade_result tradeResult,
        tr.trade_type tradeType,
        tr.pay_type    payType,
        tr.pay_channel payChannel,
        tr.source_type sourceType,
        tr.out_trade_no outTradeNo,
        tr.canteen_id canteenId,
        tr.create_time createTime,
        tr.param
        FROM trade_record tr INNER JOIN trade_order_recharge ort ON tr.id = ort.trade_id
        WHERE ort.order_recharge_id = #{orderRechargeId}
        <if test="tradeState != null and tradeState.length > 0">
            AND tr.trade_state IN
            <foreach collection="tradeState" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null">
            AND tr.create_time <![CDATA[ >= ]]> #{startTime}
            AND ort.create_time <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            AND tr.create_time <![CDATA[ <= ]]> #{endTime}
            AND ort.create_time <![CDATA[ <= ]]> #{endTime}
        </if>
        ORDER BY tr.id DESC
    </select>


</mapper>
