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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package com.tcm.training.admin.trade.service;
 
import cn.idev.excel.FastExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcm.training.admin.common.DateTimes;
import com.tcm.training.admin.common.PageResponse;
import com.tcm.training.admin.common.PhoneMasker;
import com.tcm.training.admin.trade.entity.PaymentFlow;
import com.tcm.training.admin.trade.excel.PaymentFlowExcelRow;
import com.tcm.training.admin.trade.excel.RefundFlowExcelRow;
import com.tcm.training.admin.trade.mapper.PaymentFlowMapper;
import com.tcm.training.admin.trade.row.PaymentFlowRow;
import com.tcm.training.admin.trade.row.PaymentFlowSyncRow;
import com.tcm.training.admin.trade.vo.PaymentFlowListItemVO;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
@Service
public class PaymentFlowAppService {
 
    private static final String BUSINESS_TYPE_COURSE_PURCHASE = "COURSE_PURCHASE";
    private static final String BUSINESS_TYPE_MEMBER_PURCHASE = "MEMBER_PURCHASE";
    private static final String FLOW_TYPE_PAYMENT = "PAYMENT";
    private static final String FLOW_TYPE_REFUND = "REFUND";
    private static final String TRADE_STATUS_SUCCESS = "SUCCESS";
    private static final String TRADE_STATUS_REFUNDED = "REFUNDED";
    private static final String SOURCE_TYPE_COURSE_PAYMENT = "COURSE_PAYMENT";
    private static final String SOURCE_TYPE_COURSE_REFUND = "COURSE_REFUND";
    private static final String SOURCE_TYPE_MEMBER_PAYMENT = "MEMBER_PAYMENT";
    private static final String SOURCE_TYPE_MEMBER_REFUND = "MEMBER_REFUND";
    private static final String PAYMENT_METHOD_WECHAT = "WECHAT";
    private static final String PAYMENT_METHOD_ALIPAY = "ALIPAY";
 
    private final PaymentFlowMapper paymentFlowMapper;
 
    public PaymentFlowAppService(PaymentFlowMapper paymentFlowMapper) {
        this.paymentFlowMapper = paymentFlowMapper;
    }
 
    public PageResponse<PaymentFlowListItemVO> pagePaymentFlows(int page,
                                                                int pageSize,
                                                                String flowNo,
                                                                String businessOrderNo,
                                                                String userPhone,
                                                                String productKeyword,
                                                                String businessType,
                                                                String paymentMethod,
                                                                LocalDateTime tradeFrom,
                                                                LocalDateTime tradeTo) {
        syncFlows();
        IPage<PaymentFlowRow> result = queryFlowPage(page, pageSize, flowNo, businessOrderNo, userPhone, productKeyword,
                businessType, FLOW_TYPE_PAYMENT, paymentMethod, TRADE_STATUS_SUCCESS, tradeFrom, tradeTo);
        return PageResponse.of(toList(result.getRecords()), result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    public PageResponse<PaymentFlowListItemVO> pageRefundFlows(int page,
                                                               int pageSize,
                                                               String flowNo,
                                                               String businessOrderNo,
                                                               String userPhone,
                                                               String productKeyword,
                                                               String businessType,
                                                               String paymentMethod,
                                                               LocalDateTime tradeFrom,
                                                               LocalDateTime tradeTo) {
        syncFlows();
        IPage<PaymentFlowRow> result = queryFlowPage(page, pageSize, flowNo, businessOrderNo, userPhone, productKeyword,
                businessType, FLOW_TYPE_REFUND, paymentMethod, TRADE_STATUS_REFUNDED, tradeFrom, tradeTo);
        return PageResponse.of(toList(result.getRecords()), result.getTotal(), result.getCurrent(), result.getSize());
    }
 
    public void exportPaymentFlows(HttpServletResponse response,
                                   String flowNo,
                                   String businessOrderNo,
                                   String userPhone,
                                   String productKeyword,
                                   String businessType,
                                   String paymentMethod,
                                   LocalDateTime tradeFrom,
                                   LocalDateTime tradeTo) throws IOException {
        syncFlows();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String filename = URLEncoder.encode("支付流水", "UTF-8").replace("+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + filename + ".xlsx");
 
        List<PaymentFlowRow> rows = queryFlowList(flowNo, businessOrderNo, userPhone, productKeyword, businessType,
                FLOW_TYPE_PAYMENT, paymentMethod, TRADE_STATUS_SUCCESS, tradeFrom, tradeTo);
        List<PaymentFlowExcelRow> exports = new ArrayList<PaymentFlowExcelRow>();
        for (int i = 0; i < rows.size(); i++) {
            PaymentFlowRow row = rows.get(i);
            PaymentFlowExcelRow export = new PaymentFlowExcelRow();
            export.setSerialNo(i + 1);
            export.setBusinessOrderNo(defaultText(row.getBusinessOrderNo()));
            export.setFlowNo(defaultText(row.getFlowNo()));
            export.setUserNickname(defaultText(row.getUserNickname()));
            export.setUserPhone(PhoneMasker.mask(row.getUserPhone()));
            export.setBusinessType(orderSourceText(row.getBusinessType()));
            export.setProductNameSnapshot(defaultText(row.getProductNameSnapshot()));
            export.setPaymentMethod(paymentMethodText(row.getPaymentMethod()));
            export.setTransactionAmount(formatAmount(row.getTransactionAmount()));
            export.setTradeTime(DateTimes.format(row.getTradeTime()));
            exports.add(export);
        }
        FastExcel.write(response.getOutputStream(), PaymentFlowExcelRow.class)
                .sheet("支付流水")
                .doWrite(exports);
    }
 
    public void exportRefundFlows(HttpServletResponse response,
                                  String flowNo,
                                  String businessOrderNo,
                                  String userPhone,
                                  String productKeyword,
                                  String businessType,
                                  String paymentMethod,
                                  LocalDateTime tradeFrom,
                                  LocalDateTime tradeTo) throws IOException {
        syncFlows();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String filename = URLEncoder.encode("退费流水", "UTF-8").replace("+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + filename + ".xlsx");
 
        List<PaymentFlowRow> rows = queryFlowList(flowNo, businessOrderNo, userPhone, productKeyword, businessType,
                FLOW_TYPE_REFUND, paymentMethod, TRADE_STATUS_REFUNDED, tradeFrom, tradeTo);
        List<RefundFlowExcelRow> exports = new ArrayList<RefundFlowExcelRow>();
        for (int i = 0; i < rows.size(); i++) {
            PaymentFlowRow row = rows.get(i);
            RefundFlowExcelRow export = new RefundFlowExcelRow();
            export.setSerialNo(i + 1);
            export.setBusinessOrderNo(defaultText(row.getBusinessOrderNo()));
            export.setFlowNo(defaultText(row.getFlowNo()));
            export.setUserNickname(defaultText(row.getUserNickname()));
            export.setUserPhone(PhoneMasker.mask(row.getUserPhone()));
            export.setBusinessType(orderSourceText(row.getBusinessType()));
            export.setProductNameSnapshot(defaultText(row.getProductNameSnapshot()));
            export.setPaymentMethod(paymentMethodText(row.getPaymentMethod()));
            export.setTransactionAmount(formatAmount(row.getTransactionAmount()));
            export.setTradeTime(DateTimes.format(row.getTradeTime()));
            exports.add(export);
        }
        FastExcel.write(response.getOutputStream(), RefundFlowExcelRow.class)
                .sheet("退费流水")
                .doWrite(exports);
    }
 
    public void syncFlows() {
        syncMissingFlows(paymentFlowMapper.selectMissingCoursePaymentRows(), BUSINESS_TYPE_COURSE_PURCHASE, FLOW_TYPE_PAYMENT,
                TRADE_STATUS_SUCCESS, SOURCE_TYPE_COURSE_PAYMENT);
        syncMissingFlows(paymentFlowMapper.selectMissingCourseRefundRows(), BUSINESS_TYPE_COURSE_PURCHASE, FLOW_TYPE_REFUND,
                TRADE_STATUS_REFUNDED, SOURCE_TYPE_COURSE_REFUND);
        syncMissingFlows(paymentFlowMapper.selectMissingMemberPaymentRows(), BUSINESS_TYPE_MEMBER_PURCHASE, FLOW_TYPE_PAYMENT,
                TRADE_STATUS_SUCCESS, SOURCE_TYPE_MEMBER_PAYMENT);
        syncMissingFlows(paymentFlowMapper.selectMissingMemberRefundRows(), BUSINESS_TYPE_MEMBER_PURCHASE, FLOW_TYPE_REFUND,
                TRADE_STATUS_REFUNDED, SOURCE_TYPE_MEMBER_REFUND);
    }
 
    private void syncMissingFlows(List<PaymentFlowSyncRow> rows,
                                  String businessType,
                                  String flowType,
                                  String tradeStatus,
                                  String sourceType) {
        for (PaymentFlowSyncRow row : rows) {
            PaymentFlow flow = new PaymentFlow();
            flow.setFlowNo(buildFlowNo(businessType, flowType, row.getBusinessOrderNo()));
            flow.setBusinessType(businessType);
            flow.setBusinessOrderId(row.getBusinessOrderId());
            flow.setBusinessOrderNo(row.getBusinessOrderNo());
            flow.setFlowType(flowType);
            flow.setTradeStatus(tradeStatus);
            flow.setAppUserId(row.getAppUserId());
            flow.setUserNickname(row.getUserNickname());
            flow.setUserPhone(row.getUserPhone());
            flow.setProductNameSnapshot(row.getProductNameSnapshot());
            flow.setPaymentMethod(row.getPaymentMethod());
            flow.setTransactionAmount(defaultAmount(row.getTransactionAmount()));
            flow.setRemark(trimToNull(row.getRemark()));
            flow.setTradeTime(row.getTradeTime());
            flow.setSourceType(sourceType);
            flow.setSourceId(row.getSourceId());
            try {
                paymentFlowMapper.insert(flow);
            } catch (DuplicateKeyException ignored) {
            }
        }
    }
 
    private IPage<PaymentFlowRow> queryFlowPage(int page,
                                                int pageSize,
                                                String flowNo,
                                                String businessOrderNo,
                                                String userPhone,
                                                String productKeyword,
                                                String businessType,
                                                String flowType,
                                                String paymentMethod,
                                                String tradeStatus,
                                                LocalDateTime tradeFrom,
                                                LocalDateTime tradeTo) {
        return paymentFlowMapper.selectPaymentFlowPage(
                new Page<PaymentFlowRow>(page, pageSize),
                trimToNull(flowNo),
                trimToNull(businessOrderNo),
                trimToNull(userPhone),
                trimToNull(productKeyword),
                trimToNull(businessType),
                flowType,
                trimToNull(paymentMethod),
                tradeStatus,
                tradeFrom,
                tradeTo
        );
    }
 
    private List<PaymentFlowRow> queryFlowList(String flowNo,
                                               String businessOrderNo,
                                               String userPhone,
                                               String productKeyword,
                                               String businessType,
                                               String flowType,
                                               String paymentMethod,
                                               String tradeStatus,
                                               LocalDateTime tradeFrom,
                                               LocalDateTime tradeTo) {
        return paymentFlowMapper.selectPaymentFlowList(
                trimToNull(flowNo),
                trimToNull(businessOrderNo),
                trimToNull(userPhone),
                trimToNull(productKeyword),
                trimToNull(businessType),
                flowType,
                trimToNull(paymentMethod),
                tradeStatus,
                tradeFrom,
                tradeTo
        );
    }
 
    private List<PaymentFlowListItemVO> toList(List<PaymentFlowRow> rows) {
        List<PaymentFlowListItemVO> list = new ArrayList<PaymentFlowListItemVO>();
        for (PaymentFlowRow row : rows) {
            PaymentFlowListItemVO item = new PaymentFlowListItemVO();
            item.setId(row.getId());
            item.setFlowNo(row.getFlowNo());
            item.setBusinessType(row.getBusinessType());
            item.setBusinessOrderId(row.getBusinessOrderId());
            item.setBusinessOrderNo(row.getBusinessOrderNo());
            item.setFlowType(row.getFlowType());
            item.setTradeStatus(row.getTradeStatus());
            item.setUserNickname(row.getUserNickname());
            item.setUserPhone(PhoneMasker.mask(row.getUserPhone()));
            item.setProductNameSnapshot(row.getProductNameSnapshot());
            item.setPaymentMethod(row.getPaymentMethod());
            item.setTransactionAmount(row.getTransactionAmount());
            item.setThirdPartyNo(row.getThirdPartyNo());
            item.setRemark(row.getRemark());
            item.setTradeTime(row.getTradeTime());
            list.add(item);
        }
        return list;
    }
 
    private String buildFlowNo(String businessType, String flowType, String businessOrderNo) {
        String businessPrefix = BUSINESS_TYPE_COURSE_PURCHASE.equals(businessType) ? "COURSE" : "MEMBER";
        String flowPrefix = FLOW_TYPE_PAYMENT.equals(flowType) ? "PAY" : "REFUND";
        return flowPrefix + "-" + businessPrefix + "-" + businessOrderNo;
    }
 
    private BigDecimal defaultAmount(BigDecimal amount) {
        return amount == null ? BigDecimal.ZERO : amount;
    }
 
    private String trimToNull(String value) {
        if (value == null) {
            return null;
        }
        String trimmed = value.trim();
        return trimmed.isEmpty() ? null : trimmed;
    }
 
    private String defaultText(String value) {
        return value == null || value.trim().isEmpty() ? "-" : value.trim();
    }
 
    private String formatAmount(BigDecimal value) {
        BigDecimal amount = value == null ? BigDecimal.ZERO : value;
        return amount.stripTrailingZeros().toPlainString();
    }
 
    private String orderSourceText(String value) {
        if (BUSINESS_TYPE_COURSE_PURCHASE.equals(value)) {
            return "养生课程";
        }
        if (BUSINESS_TYPE_MEMBER_PURCHASE.equals(value)) {
            return "会员";
        }
        return "-";
    }
 
    public String paymentMethodText(String value) {
        if (PAYMENT_METHOD_WECHAT.equals(value)) {
            return "微信支付";
        }
        if (PAYMENT_METHOD_ALIPAY.equals(value)) {
            return "支付宝支付";
        }
        return "-";
    }
 
}