<?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.ai.da.mapper.primary.PaymentInfoMapper">
    <select id="selectPageOrderList" resultType="com.ai.da.model.vo.OrderListVO">
        SELECT
            p.id AS id,
            p.payer_total AS amount,
            p.payment_type AS paymentMethod,
            p.trade_state AS state,
            o.title AS orderType,
            p.hosted_invoice_url AS invoiceLink,
            p.create_time
        FROM
            `t_payment_info` p
                LEFT JOIN
            `t_order_info` o
            ON
                p.order_no = o.order_no
        WHERE
            o.account_id = #{accountId}
            <if test="id !=null and id gt 0">
                AND p.id = #{id}
            </if>
          AND p.create_time BETWEEN #{startTime} AND #{endTime}
        ORDER BY
            p.id DESC
            LIMIT #{pageSize} OFFSET #{offset};
    </select>

    <select id="queryOrderListTotalCount" parameterType="map" resultType="int">
        SELECT
            COUNT(*)
        FROM
            `t_payment_info` p
                LEFT JOIN
            `t_order_info` o
            ON
                p.order_no = o.order_no
        WHERE
            o.account_id = #{accountId}
            <if test="id !=null and id gt 0">
                AND p.id = #{id}
            </if>
          AND p.create_time BETWEEN #{startTime} AND #{endTime};
    </select>

    <select id="queryPaymentInfo" resultType="com.ai.da.model.vo.PaymentInfoVO">
        SELECT
        p.id,
        a.user_name payer,
        p.payment_type platform,
        p.payer_total,
        p.type,
        p.payment_method,
        p.last4,
        p.country,
        p.city,
        p.create_time,
        CASE

        WHEN p.trade_state IN ( 'paid', 'COMPLETED', 'complete', 'liquidated' ) THEN
        'Success'
        WHEN p.trade_state IN ( 'failed', 'expired', 'VOIDED', 'void', 'uncollectible' ) THEN
        'Fail' ELSE 'Pending'
        END AS status
        FROM
        t_payment_info p
        LEFT JOIN
        t_order_info o ON p.order_no = o.order_no
        LEFT JOIN
        t_account a ON a.id = o.account_id
        WHERE
        1 = 1
        <if test="paymentType != null and paymentType != ''">
            AND p.payment_type = #{paymentType}
        </if>
        <if test="payerTotal != null and payerTotal != ''">
            AND p.payer_total = #{payerTotal}
        </if>
        <if test="type != null and type != ''">
            AND p.type = #{type}
        </if>
        <if test="status != null and status != ''">
            AND
            CASE
            WHEN p.trade_state IN ('paid', 'COMPLETED', 'complete', 'liquidated') THEN 'Success'
            WHEN p.trade_state IN ('failed', 'expired', 'VOIDED', 'void', 'uncollectible') THEN 'Fail'
            ELSE 'Pending'
            END = #{status}
        </if>
        <if test="country != null and country != ''">
            AND p.country = #{country}
        </if>
        <if test="city != null and city != ''">
            AND p.city = #{city}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND p.create_time BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="payer != null and payer != ''">
            AND a.user_name = #{payer}
        </if>
        AND p.transaction_id NOT LIKE 'cs_test%'
        ORDER BY
        p.id ${order}
        LIMIT ${limit} OFFSET ${offset}
    </select>

    <select id="queryPaymentInfoCount" resultType="java.lang.Long">
        SELECT
        COUNT(*)
        FROM
        t_payment_info p
        LEFT JOIN
        t_order_info o ON p.order_no = o.order_no
        LEFT JOIN
        t_account a ON a.id = o.account_id
        WHERE
        1 = 1
        <if test="paymentType != null and paymentType != ''">
            AND p.payment_type = #{paymentType}
        </if>
        <if test="payerTotal != null and payerTotal != ''">
            AND p.payer_total = #{payerTotal}
        </if>
        <if test="type != null and type != ''">
            AND p.type = #{type}
        </if>
        <if test="status != null and status != ''">
            AND
            CASE
            WHEN p.trade_state IN ('paid', 'COMPLETED', 'complete', 'liquidated') THEN 'Success'
            WHEN p.trade_state IN ('failed', 'expired', 'VOIDED', 'void', 'uncollectible') THEN 'Fail'
            ELSE 'Pending'
            END = #{status}
        </if>
        <if test="country != null and country != ''">
            AND p.country = #{country}
        </if>
        <if test="city != null and city != ''">
            AND p.city = #{city}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND p.create_time BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="payer != null and payer != ''">
            AND a.user_name = #{payer}
        </if>
        AND p.transaction_id NOT LIKE 'cs_test%'
    </select>

    <select id="getCities" resultType="java.util.Map">
        SELECT DISTINCT city
        FROM t_payment_info
        WHERE city IS NOT NULL
          AND TRIM(city) != ''
    </select>

    <select id="getCountries" resultType="java.util.Map">
        SELECT DISTINCT country
        FROM t_payment_info
        WHERE country IS NOT NULL
          AND TRIM(country) != ''
    </select>

    <insert id="insertIgnore" parameterType="com.ai.da.mapper.primary.entity.PaymentInfo">
        INSERT IGNORE INTO
               t_payment_info (order_no, transaction_id, payment_type, trade_state, payer_total,
                                    type, content, notified, payment_method, last4, hosted_invoice_url,
                                country, city, ip_address, promotion_code, create_time)
               VALUES (#{paymentInfo.orderNo}, #{paymentInfo.transactionId}, #{paymentInfo.paymentType},
        #{paymentInfo.tradeState}, #{paymentInfo.payerTotal},#{paymentInfo.type}, #{paymentInfo.content},
        #{paymentInfo.notified},#{paymentInfo.paymentMethod}, #{paymentInfo.last4},#{paymentInfo.hostedInvoiceUrl},
        #{paymentInfo.country},#{paymentInfo.city},#{paymentInfo.ipAddress},#{paymentInfo.promotionCode},#{paymentInfo.createTime});
    </insert>

    <select id="selectPaidPaymentsByAccountAndPromotion" resultType="com.ai.da.mapper.primary.entity.PaymentInfo">
        SELECT pi.*
        FROM t_order_info oi
                 LEFT JOIN t_payment_info pi ON oi.order_no = pi.order_no
        WHERE oi.account_id = #{accountId}
          AND pi.promotion_code = #{promotionCode}
          AND pi.trade_state = 'paid'
    </select>


</mapper>
