1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?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.tcm.training.admin.trade.mapper.PaymentFlowMapper">
 
    <sql id="PaymentFlowWhere">
        WHERE f.deleted = 0
        <if test="flowNo != null and flowNo.trim() != ''">
            AND f.flow_no LIKE CONCAT('%', #{flowNo}, '%')
        </if>
        <if test="businessOrderNo != null and businessOrderNo.trim() != ''">
            AND f.business_order_no LIKE CONCAT('%', #{businessOrderNo}, '%')
        </if>
        <if test="userPhone != null and userPhone.trim() != ''">
            AND f.user_phone LIKE CONCAT('%', #{userPhone}, '%')
        </if>
        <if test="productKeyword != null and productKeyword.trim() != ''">
            AND f.product_name_snapshot LIKE CONCAT('%', #{productKeyword}, '%')
        </if>
        <if test="businessType != null and businessType.trim() != ''">
            AND f.business_type = #{businessType}
        </if>
        <if test="flowType != null and flowType.trim() != ''">
            AND f.flow_type = #{flowType}
        </if>
        <if test="paymentMethod != null and paymentMethod.trim() != ''">
            AND f.payment_method = #{paymentMethod}
        </if>
        <if test="tradeStatus != null and tradeStatus.trim() != ''">
            AND f.trade_status = #{tradeStatus}
        </if>
        <if test="tradeFrom != null">
            AND f.trade_time <![CDATA[>=]]> #{tradeFrom}
        </if>
        <if test="tradeTo != null">
            AND f.trade_time <![CDATA[<=]]> #{tradeTo}
        </if>
    </sql>
 
    <sql id="PaymentFlowColumns">
        f.id,
        f.flow_no AS flowNo,
        f.business_type AS businessType,
        f.business_order_id AS businessOrderId,
        f.business_order_no AS businessOrderNo,
        f.flow_type AS flowType,
        f.trade_status AS tradeStatus,
        f.user_nickname AS userNickname,
        f.user_phone AS userPhone,
        f.product_name_snapshot AS productNameSnapshot,
        f.payment_method AS paymentMethod,
        f.transaction_amount AS transactionAmount,
        CASE
            WHEN f.flow_type = 'REFUND' THEN COALESCE(f.third_party_refund_no, f.third_party_trade_no)
            ELSE COALESCE(f.third_party_trade_no, f.third_party_refund_no)
        END AS thirdPartyNo,
        f.remark AS remark,
        f.trade_time AS tradeTime
    </sql>
 
    <select id="selectPaymentFlowPage" resultType="com.tcm.training.admin.trade.row.PaymentFlowRow">
        SELECT
            <include refid="PaymentFlowColumns"/>
        FROM tcm_payment_flow f
        <include refid="PaymentFlowWhere"/>
        ORDER BY f.trade_time DESC, f.id DESC
    </select>
 
    <select id="selectPaymentFlowList" resultType="com.tcm.training.admin.trade.row.PaymentFlowRow">
        SELECT
            <include refid="PaymentFlowColumns"/>
        FROM tcm_payment_flow f
        <include refid="PaymentFlowWhere"/>
        ORDER BY f.trade_time DESC, f.id DESC
    </select>
 
    <select id="selectMissingCoursePaymentRows" resultType="com.tcm.training.admin.trade.row.PaymentFlowSyncRow">
        SELECT
            o.id AS sourceId,
            o.id AS businessOrderId,
            o.order_no AS businessOrderNo,
            o.app_user_id AS appUserId,
            o.user_nickname AS userNickname,
            o.user_phone AS userPhone,
            o.product_name_snapshot AS productNameSnapshot,
            o.payment_method AS paymentMethod,
            o.order_amount AS transactionAmount,
            NULL AS remark,
            o.paid_at AS tradeTime
        FROM tcm_course_purchase_order o
        LEFT JOIN tcm_payment_flow f ON f.source_type = 'COURSE_PAYMENT' AND f.source_id = o.id AND f.deleted = 0
        WHERE o.deleted = 0
          AND o.paid_at IS NOT NULL
          AND o.status IN ('PAID', 'REFUNDED')
          AND f.id IS NULL
        ORDER BY o.id ASC
    </select>
 
    <select id="selectMissingCourseRefundRows" resultType="com.tcm.training.admin.trade.row.PaymentFlowSyncRow">
        SELECT
            o.id AS sourceId,
            o.id AS businessOrderId,
            o.order_no AS businessOrderNo,
            o.app_user_id AS appUserId,
            o.user_nickname AS userNickname,
            o.user_phone AS userPhone,
            o.product_name_snapshot AS productNameSnapshot,
            o.payment_method AS paymentMethod,
            COALESCE(o.refund_amount, o.order_amount) AS transactionAmount,
            o.refund_remark AS remark,
            o.refunded_at AS tradeTime
        FROM tcm_course_purchase_order o
        LEFT JOIN tcm_payment_flow f ON f.source_type = 'COURSE_REFUND' AND f.source_id = o.id AND f.deleted = 0
        WHERE o.deleted = 0
          AND o.status = 'REFUNDED'
          AND o.refunded_at IS NOT NULL
          AND f.id IS NULL
        ORDER BY o.id ASC
    </select>
 
    <select id="selectMissingMemberPaymentRows" resultType="com.tcm.training.admin.trade.row.PaymentFlowSyncRow">
        SELECT
            o.id AS sourceId,
            o.id AS businessOrderId,
            o.order_no AS businessOrderNo,
            o.app_user_id AS appUserId,
            o.user_nickname AS userNickname,
            o.user_phone AS userPhone,
            CASE
                WHEN o.member_type = 'MONTH_CARD' THEN '月卡会员'
                WHEN o.member_type = 'QUARTER_CARD' THEN '季卡会员'
                ELSE '会员购买'
            END AS productNameSnapshot,
            o.payment_method AS paymentMethod,
            o.order_amount AS transactionAmount,
            NULL AS remark,
            o.paid_at AS tradeTime
        FROM tcm_member_purchase_order o
        LEFT JOIN tcm_payment_flow f ON f.source_type = 'MEMBER_PAYMENT' AND f.source_id = o.id AND f.deleted = 0
        WHERE o.deleted = 0
          AND o.paid_at IS NOT NULL
          AND o.status IN ('PAID', 'REFUNDED')
          AND f.id IS NULL
        ORDER BY o.id ASC
    </select>
 
    <select id="selectMissingMemberRefundRows" resultType="com.tcm.training.admin.trade.row.PaymentFlowSyncRow">
        SELECT
            o.id AS sourceId,
            o.id AS businessOrderId,
            o.order_no AS businessOrderNo,
            o.app_user_id AS appUserId,
            o.user_nickname AS userNickname,
            o.user_phone AS userPhone,
            CASE
                WHEN o.member_type = 'MONTH_CARD' THEN '月卡会员'
                WHEN o.member_type = 'QUARTER_CARD' THEN '季卡会员'
                ELSE '会员购买'
            END AS productNameSnapshot,
            o.payment_method AS paymentMethod,
            COALESCE(o.refund_amount, o.order_amount) AS transactionAmount,
            o.refund_remark AS remark,
            o.refunded_at AS tradeTime
        FROM tcm_member_purchase_order o
        LEFT JOIN tcm_payment_flow f ON f.source_type = 'MEMBER_REFUND' AND f.source_id = o.id AND f.deleted = 0
        WHERE o.deleted = 0
          AND o.status = 'REFUNDED'
          AND o.refunded_at IS NOT NULL
          AND f.id IS NULL
        ORDER BY o.id ASC
    </select>
 
</mapper>