无关风月
2025-01-22 99367ea1c11a68b420936e7f7db5fa7367da4f44
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package com.xinquan.user.controller.client;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinquan.common.core.constant.CacheConstants;
import com.xinquan.common.core.constant.SecurityConstants;
import com.xinquan.common.core.domain.R;
import com.xinquan.common.core.exception.ServiceException;
import com.xinquan.common.core.utils.JuHeFuUtil;
import com.xinquan.common.core.utils.WebUtils;
import com.xinquan.common.core.utils.page.CollUtils;
import com.xinquan.common.core.utils.page.PageDTO;
import com.xinquan.common.core.web.domain.BaseModel;
import com.xinquan.common.log.enums.BusinessType;
import com.xinquan.common.redis.service.RedisService;
import com.xinquan.common.security.service.TokenService;
import com.xinquan.course.api.domain.Course;
import com.xinquan.meditation.api.domain.Meditation;
import com.xinquan.order.api.domain.Order;
import com.xinquan.system.api.domain.*;
import com.xinquan.system.api.model.AppLoginUser;
import com.xinquan.user.api.domain.dto.OrderListDTO;
import com.xinquan.system.api.model.LoginUser;
import com.xinquan.user.domain.export.WithdrawExport;
import com.xinquan.user.service.AppUserBankService;
import com.xinquan.user.service.AppUserService;
import com.xinquan.user.service.AppUserWalletRecordService;
import com.xinquan.user.service.AppUserWithdrawService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 用户提现表 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/client/app-user-withdraw")
public class ClientAppUserWithdrawController {
    @Resource
    private AppUserService appUserService;
    @Resource
    private AppUserWithdrawService withdrawService;
    @Resource
    private AppUserBankService appUserBankService;
    @Autowired
    private TokenService tokenService;
    @Resource
    private AppUserWalletRecordService appUserWalletRecordService;
    @Autowired
    private RedisService redisService;
 
 
    @PostMapping("/withdrawList")
    @ApiOperation(value = "提现列表-分页", tags = {"管理后台-提现管理"})
    public R<PageDTO<AppUserWithdraw>> withdrawList(@RequestBody OrderListDTO courseDTO) {
        LambdaQueryWrapper<AppUserWithdraw> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(courseDTO.getBuyContent())){
                wrapper.like(AppUserWithdraw::getCode,courseDTO.getBuyContent());
        }
        if (StringUtils.hasLength(courseDTO.getUserNameOrPhone())){
            List<Long> collect = appUserService.lambdaQuery().like(AppUser::getCellPhone, courseDTO.getUserNameOrPhone())
                    .list().stream().map(AppUser::getId)
                    .collect(Collectors.toList());
            List<Long> collect1 = appUserService.lambdaQuery().like(AppUser::getNickname, courseDTO.getUserNameOrPhone())
                    .list().stream().map(AppUser::getId)
                    .collect(Collectors.toList());
            collect.addAll(collect1);
            if (collect.isEmpty())collect.add(-1L);
            wrapper.in(AppUserWithdraw::getAppUserId,collect);
        }
        if (StringUtils.hasLength(courseDTO.getPaymentStatus()) ){
            List<Integer> integers = new ArrayList<>();
            if (courseDTO.getPaymentStatus().contains("1")){
                integers.add(0);
            }
            if (courseDTO.getPaymentStatus().contains("2")){
                integers.add(1);
            }
            if (courseDTO.getPaymentStatus().contains("3")){
                integers.add(2);
            }
            if (!integers.isEmpty()){
                wrapper.in(AppUserWithdraw::getWithdrawStatus,integers);
            }
        }
        if (StringUtils.hasLength(courseDTO.getTime())){
            String startTime =null;
            String endTime =null;
            String[] split = courseDTO.getTime().split(" - ");
            startTime = split[0]+" 00:00:00";
            endTime = split[1]+" 23:59:59";
            wrapper.between(AppUserWithdraw::getCreateTime,startTime,endTime);
        }
        wrapper.orderByDesc(BaseModel::getCreateTime);
        Page<AppUserWithdraw> list = withdrawService.page(new Page<>(courseDTO.getPageCurr(), courseDTO.getPageSize()), wrapper);
        if (CollUtils.isEmpty(list.getRecords())) {
            return R.ok(PageDTO.empty(list));
        }
        for (AppUserWithdraw record : list.getRecords()) {
            record.setUid(record.getId()+"");
            AppUser byId = appUserService.getById(record.getAppUserId());
            if(byId!=null){
                record.setUserName(byId.getNickname());
                record.setCellPhone(byId.getCellPhone());
                record.setAvatar(byId.getAvatar());
            }
        }
        return R.ok(PageDTO.of(list, AppUserWithdraw.class));
    }
    @ApiOperation(value = "提现管理导出", tags = {"管理后台-提现管理"})
    @PutMapping("/export")
    public void export(@RequestBody OrderListDTO courseDTO)
    {
        LambdaQueryWrapper<AppUserWithdraw> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(courseDTO.getIds())){
            wrapper.in(AppUserWithdraw::getId, Arrays.asList(courseDTO.getIds().split(",")));
        }
        if (StringUtils.hasLength(courseDTO.getBuyContent())){
            wrapper.like(AppUserWithdraw::getCode,courseDTO.getBuyContent());
        }
        if (StringUtils.hasLength(courseDTO.getUserNameOrPhone())){
            List<Long> collect = appUserService.lambdaQuery().like(AppUser::getCellPhone, courseDTO.getUserNameOrPhone())
                    .list().stream().map(AppUser::getId)
                    .collect(Collectors.toList());
            if (collect.isEmpty())collect.add(-1L);
            wrapper.in(AppUserWithdraw::getAppUserId,collect);
        }
        if (StringUtils.hasLength(courseDTO.getPaymentStatus()) ){
            List<Integer> integers = new ArrayList<>();
            if (courseDTO.getPaymentStatus().contains("1")){
                integers.add(0);
            }
            if (courseDTO.getPaymentStatus().contains("2")){
                integers.add(1);
            }
            if (courseDTO.getPaymentStatus().contains("3")){
                integers.add(2);
            }
            if (!integers.isEmpty()){
                wrapper.in(AppUserWithdraw::getWithdrawStatus,integers);
            }
        }
        if (StringUtils.hasLength(courseDTO.getTime())){
            String startTime =null;
            String endTime =null;
            String[] split = courseDTO.getTime().split(" - ");
            startTime = split[0]+" 00:00:00";
            endTime = split[1]+" 23:59:59";
            wrapper.between(AppUserWithdraw::getWithdrawTime,startTime,endTime);
        }
        List<AppUserWithdraw> list = withdrawService.list(wrapper);
        List<WithdrawExport> withdrawExports = new ArrayList<>();
 
        for (AppUserWithdraw record : list) {
            WithdrawExport withdrawExport = new WithdrawExport();
            record.setUid(record.getId()+"");
            AppUser byId = appUserService.getById(record.getAppUserId());
            if(byId!=null){
                withdrawExport.setUserName(byId.getNickname());
                withdrawExport.setCellphone(byId.getCellPhone());
            }
            withdrawExport.setCode(record.getCode());
 
            withdrawExport.setAmount("¥"+record.getAmount()+"");
            withdrawExport.setWithdrawType("1");
            withdrawExport.setWithdrawStatus(record.getWithdrawStatus()+"");
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String format = df.format(record.getCreateTime());
            withdrawExport.setCreateTime(format);
            withdrawExports.add(withdrawExport);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), WithdrawExport.class, withdrawExports);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("提现管理导出.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @PostMapping("/withdrawInfo")
    @ApiOperation(value = "提现列表-查看详情", tags = {"管理后台-提现管理"})
    public R<AppUserWithdraw> withdrawList(String uid){
        AppUserWithdraw byId = withdrawService.getById(uid);
        return R.ok(byId);
    }
    /**
     * 远程调用获取提现记录
     */
    @PostMapping("/getWithdrawList")
    public R<List<AppUserWithdraw>> getWithdraw(@RequestBody OrderListDTO courseDTO) {
        LambdaQueryWrapper<AppUserWithdraw> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(courseDTO.getBuyContent())){
            if (!courseDTO.getBuyContent().contains("提现")){
                wrapper.like(AppUserWithdraw::getCode,courseDTO.getBuyContent());
            }
        }
        if (StringUtils.hasLength(courseDTO.getUserNameOrPhone())){
            List<Long> collect = appUserService.lambdaQuery().like(AppUser::getCellPhone, courseDTO.getUserNameOrPhone())
                    .list().stream().map(AppUser::getId)
                    .collect(Collectors.toList());
            if (collect.isEmpty())collect.add(-1L);
            wrapper.in(AppUserWithdraw::getAppUserId,collect);
        }
        if (StringUtils.hasLength(courseDTO.getOrderFrom()) && (!courseDTO.getOrderFrom().contains("5"))){
            wrapper.eq(AppUserWithdraw::getId,0);
        }
        if (StringUtils.hasLength(courseDTO.getPayType()) && (!courseDTO.getPayType().contains("4"))){
            wrapper.eq(AppUserWithdraw::getId,0);
        }
        if (StringUtils.hasLength(courseDTO.getPaymentStatus()) ){
            List<Integer> integers = new ArrayList<>();
            if (courseDTO.getPaymentStatus().contains("1")){
                integers.add(0);
            }
            if (courseDTO.getPaymentStatus().contains("2")){
                integers.add(1);
            }
            if (courseDTO.getPaymentStatus().contains("3")||courseDTO.getPaymentStatus().contains("4")){
                integers.add(2);
            }
            if (!integers.isEmpty()){
                wrapper.in(AppUserWithdraw::getWithdrawStatus,integers);
            }
        }
        if (StringUtils.hasLength(courseDTO.getTime())){
            String startTime =null;
            String endTime =null;
            String[] split = courseDTO.getTime().split(" - ");
            startTime = split[0]+" 00:00:00";
            endTime = split[1]+" 23:59:59";
            wrapper.between(AppUserWithdraw::getWithdrawTime,startTime,endTime);
        }
        List<AppUserWithdraw> list = withdrawService.list(wrapper);
        return R.ok(list);
    }
    @PostMapping("/withdraw")
    @ApiOperation(value = "提现", tags = {"钱包"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "银行卡id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "money", value = "提现金额", dataType = "String", required = true)
    })
    public synchronized R withdraw(Long bankId,String money) throws Exception {
 
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser==null){
            return R.tokenError("登录失效");
        }
        Long userId = loginUser.getUserid();
        AppUser byId = appUserService.getById(userId);
        if (byId.getBalance().compareTo(new BigDecimal(money))<0){
            return R.fail("提现金额不能超过钱包余额");
        }
        AppUserBank byId1 = appUserBankService.getById(bankId);
        if (!Objects.equals(byId1.getAppUserId(), userId)){
            return R.fail("银行卡不属于当前登录用户");
        }
        byId.setBalance(byId.getBalance().subtract(new BigDecimal(money)));
        BigDecimal bigDecimal = new BigDecimal(money);
        AppUserWithdraw appUserWithdraw = new AppUserWithdraw();
        appUserWithdraw.setAppUserId(userId);
        appUserWithdraw.setBankId(bankId);
        appUserWithdraw.setAmount(bigDecimal);
        appUserWithdraw.setWithdrawType(1);
        appUserWithdraw.setWithdrawStatus(0);
        // todo 提现流水号
        String s = UuidUtils.generateUuid();
        String replace = s.replace("-", "").substring(0, 16);
        // 商户余额充值到用户余额
        appUserWithdraw.setCode("TX"+replace);
        appUserWithdraw.setWithdrawTime(LocalDateTime.now());
        appUserWithdraw.setCreateTime(LocalDateTime.now());
        // 先扣去余额
        AppUserWalletRecord appUserWalletRecord = new AppUserWalletRecord();
        appUserWalletRecord.setAppUserId(userId);
        appUserWalletRecord.setChangeType(2);
        appUserWalletRecord.setReason("提现");
        appUserWalletRecord.setAmount(new BigDecimal(money));
        BigDecimal bigDecimal1 = new BigDecimal(money).setScale(2, RoundingMode.HALF_DOWN);
        String s1 = JuHeFuUtil.updateAccount(byId.getFenzhangId(), byId1.getCardholder(), byId1.getCellPhone(), byId1.getCardNo());
        if (s1.contains("false")){
            // 修改报错 弹出提示
            return R.fail("银行卡信息或持卡人信息有误,请核对后输入");
        }
        // 从商户余额分账到用户分账接收方
        JSONObject jsonObject = JuHeFuUtil.balancePay("CZ"+replace, byId.getFenzhangId(), bigDecimal1 + ""
                , "https://xq.xqzhihui.com/api/user/client/app-user-withdraw/base/callback");
        if (jsonObject.getString("error_msg")!=null){
            return R.fail("系统繁忙,5分钟后重试");
//            return R.fail(jsonObject.getString("error_msg"));
        }
        JSONObject jsonObject2 = JuHeFuUtil.balanceWithdraw("TX"+replace, bigDecimal1+"", byId.getFenzhangId(),
                "https://xq.xqzhihui.com/api/user/client/app-user-withdraw/base/callbackA");
        if (jsonObject2.getString("error_msg")!=null){
            return R.fail("系统繁忙,5分钟后重试");
        }
        System.err.println("执行完毕=====");
        appUserWithdraw.setCode("TX"+replace);
        appUserService.updateById(byId);
        withdrawService.save(appUserWithdraw);
        appUserWalletRecord.setOrderId(appUserWithdraw.getId());
        appUserWalletRecordService.save(appUserWalletRecord);
        return R.ok();
    }
    private static final String AES_KEY = "6d548eb01bad44bbbb4a23743e733103";
    public static String decrypt(String strToDecrypt) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(AES_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
        } catch (Exception e) {
            System.out.println("Error while decrypting: " + e.toString());
        }
        return null;}
//    @ResponseBody
//    @PostMapping("/base/callback")
//    public void callback(HttpServletRequest request, HttpServletResponse response) {
//        try {
//            System.err.println("提现到分账接收方回调================="+request);
//            BufferedReader reader = request.getReader();
//            String string1 = reader.toString();
//            StringBuilder requestBody = new StringBuilder();
//            String line;
//            while ((line = reader.readLine()) != null) {
//                requestBody.append(line);
//            }
//            System.err.println("提现回调=================全部请求体"+requestBody);
//            com.alibaba.fastjson2.JSONObject jsonObject = JSONObject.parseObject(requestBody.toString());
//            System.err.println("json串"+jsonObject);
//            String string9 = jsonObject.getString("resCipher");
//            String decrypt = decrypt(string9);
//            System.err.println(decrypt);
//            JSONObject jsonObject1 = JSONObject.parseObject(decrypt);
//            // 系统订单号
//            String string = jsonObject1.getString("order_no");
//            String payId = jsonObject1.getString("pay_id");
//            AppUserWithdraw one = withdrawService.lambdaQuery().eq(AppUserWithdraw::getCode, string).one();
//            if (jsonObject.getString("type").equals("balancePay.succeeded")){
//                if (one!=null){
//                    String s = UuidUtils.generateUuid();
//                    String replace = "TX"+s.replace("-", "");
//                    // 商户余额充值到用户余额
//                    AppUser appUser = appUserService.getById(one.getAppUserId());
//                    if (one.getWithdrawStatus()==1 || one.getWithdrawStatus()==2){
//                        return;
//                    }
//
//                    JSONObject jsonObject2 = JuHeFuUtil.balanceWithdraw(replace, one.getAmount() + "", appUser.getFenzhangId(),
//                            "https://xq.xqzhihui.com/api/user/client/app-user-withdraw/base/callbackA");
//                    if (jsonObject2.getString("error_msg")!=null){
//                        // 调用失败
//                        // 回退余额记录
//                        AppUserWalletRecord appUserWalletRecord = new AppUserWalletRecord();
//                        appUserWalletRecord.setAppUserId(one.getAppUserId());
//                        appUserWalletRecord.setChangeType(1);
//                        appUserWalletRecord.setReason("提现回退");
//                        appUserWalletRecord.setOrderId(one.getId());
//                        appUserWalletRecord.setAmount(one.getAmount());
//                        appUserWalletRecordService.save(appUserWalletRecord);
//                        one.setWithdrawStatus(2);
//                        one.setReason(jsonObject1.getString("error_msg"));
//                        withdrawService.updateById(one);
//                        // 回退用户余额
//                        BigDecimal add = appUser.getBalance().add(one.getAmount());
//                        appUser.setBalance(add);
//                        appUserService.updateById(appUser);
//                        PrintWriter out = response.getWriter();
//                        out.write("succeeded");
//                        out.flush();
//                        out.close();
//                    }
//                    one.setCode(replace);
//                    withdrawService.updateById(one);
//                }
//                PrintWriter out = response.getWriter();
//                out.write("succeeded");
//                out.flush();
//                out.close();
//            }else if (jsonObject.getString("type").equals("balancePay.failed")){
//                // 回退余额记录
//                AppUserWalletRecord appUserWalletRecord = new AppUserWalletRecord();
//                appUserWalletRecord.setAppUserId(one.getAppUserId());
//                appUserWalletRecord.setChangeType(1);
//                appUserWalletRecord.setReason("提现回退");
//                appUserWalletRecord.setOrderId(one.getId());
//                appUserWalletRecord.setAmount(one.getAmount());
//                appUserWalletRecordService.save(appUserWalletRecord);
//                one.setWithdrawStatus(2);
//                one.setReason(jsonObject1.getString("error_msg"));
//                withdrawService.updateById(one);
//                // 回退用户余额
//                AppUser appUser = appUserService.getById(one.getAppUserId());
//                BigDecimal add = appUser.getBalance().add(one.getAmount());
//                appUser.setBalance(add);
//                appUserService.updateById(appUser);
//                PrintWriter out = response.getWriter();
//                out.write("succeeded");
//                out.flush();
//                out.close();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    @ResponseBody
    @PostMapping("/base/callbackA")
    public void callbackA(HttpServletRequest request, HttpServletResponse response) {
        try {
            System.err.println("提现到银行卡回调================="+request);
            BufferedReader reader = request.getReader();
            String string1 = reader.toString();
            StringBuilder requestBody = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                requestBody.append(line);
            }
            System.err.println("提现到银行卡回调=================全部请求体"+requestBody);
            com.alibaba.fastjson2.JSONObject jsonObject = JSONObject.parseObject(requestBody.toString());
            System.err.println("json串"+jsonObject);
            String string9 = jsonObject.getString("resCipher");
            String decrypt = decrypt(string9);
            System.err.println(decrypt);
            JSONObject jsonObject1 = JSONObject.parseObject(decrypt);
            // 系统订单号
            String string = jsonObject1.getString("order_no");
            AppUserWithdraw one = withdrawService.lambdaQuery().eq(AppUserWithdraw::getCode, string).one();
            if (jsonObject.getString("type").equals("withdraw.succeeded")){
                if (one!=null){
                    if (one.getWithdrawStatus()==1){
                        return;
                    }
                    System.err.println("提现成功");
                    one.setWithdrawStatus(1);
                    one.setSerialNo(jsonObject1.getString("withdraw_id"));
                    withdrawService.updateById(one);
                }
                PrintWriter out = response.getWriter();
                out.write("succeeded");
                out.flush();
                out.close();
            }else if (jsonObject.getString("type").equals("withdraw.failed")){
                // 回退余额记录
                AppUserWalletRecord appUserWalletRecord = new AppUserWalletRecord();
                appUserWalletRecord.setAppUserId(one.getAppUserId());
                appUserWalletRecord.setChangeType(1);
                appUserWalletRecord.setReason("提现回退");
                appUserWalletRecord.setOrderId(one.getId());
                appUserWalletRecord.setAmount(one.getAmount());
                appUserWalletRecordService.save(appUserWalletRecord);
                one.setWithdrawStatus(2);
                one.setReason(jsonObject1.getString("error_msg"));
                withdrawService.updateById(one);
                // 回退用户余额
                AppUser appUser = appUserService.getById(one.getAppUserId());
                BigDecimal add = appUser.getBalance().add(one.getAmount());
                appUser.setBalance(add);
                appUserService.updateById(appUser);
                PrintWriter out = response.getWriter();
                out.write("succeeded");
                out.flush();
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @PostMapping("/addBank")
    @ApiOperation(value = "提现-添加银行卡", tags = {"钱包"})
    public R wallet(@RequestBody AppUserBank appUserWithdraw) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser==null){
            return R.tokenError("登录失效");
        }
        Long userId = loginUser.getUserid();
 
        appUserWithdraw.setAppUserId(userId);
//        Boolean flag = redisService.hasKey(CacheConstants.ADD_CARD_PHONE_CODE_PREFIX + appUserWithdraw.getCellPhone());
//        if(!flag){
//            return R.fail("验证码已失效,请重新获取");
//        }
//        String code = redisService.getCacheObject(CacheConstants.ADD_CARD_PHONE_CODE_PREFIX + appUserWithdraw.getCellPhone());
//        if (!appUserWithdraw.getCode().equals(code)) {
//            return R.fail("验证码不正确");
//        }
        if (com.xinquan.common.core.utils.StringUtils.isNotBlank(appUserWithdraw.getCellPhone()) && com.xinquan.common.core.utils.StringUtils.isNotBlank(appUserWithdraw.getCode())) {
            String key = CacheConstants.ADD_CARD_PHONE_CODE_PREFIX + appUserWithdraw.getCellPhone();
            String code = redisService.getCacheObject(key);
            // 万能验证码
            if (appUserWithdraw.getCode().equals("220125") || (com.xinquan.common.core.utils.StringUtils.isNotBlank(code) && code.equals(
                    appUserWithdraw.getCode()))) {
                redisService.deleteObject(key);
            }else{
                return R.fail("验证码不正确");
            }
        }else{
            return R.fail("请输入验证码");
        }
        appUserBankService.save(appUserWithdraw);
        // 查询是否已经开户
        AppUser byId = appUserService.getById(userId);
        if (byId.getFenzhangId()==null){
            // 开户
            long time = new Date().getTime();
            String s = RandomUtil.randomNumbers(4);
            String s1 = "FZ" + time + s;
            String user = JuHeFuUtil.createUser(s1, appUserWithdraw.getCardholder(), "00",
                    appUserWithdraw.getIdentityCard(), appUserWithdraw.getCellPhone(), appUserWithdraw.getCardNo());
            if (user.equals("success")){
                byId.setFenzhangId(s1);
                appUserService.updateById(byId);
            }
        }
        return R.ok();
    }
    @PostMapping("/deleteBank")
    @ApiOperation(value = "提现-删除银行卡", tags = {"钱包"})
    public R deleteBank(Long id) {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser==null){
            return R.tokenError("登录失效");
        }
        Long userId = loginUser.getUserid();
        if(userId ==null || userId == 0)return R.tokenError("登录失效");
        appUserBankService.removeById(id);
        return R.ok();
    }
    @PostMapping("/bankList")
    @ApiOperation(value = "提现-获取银行卡列表", tags = {"钱包"})
    public R<List<AppUserBank>> bankList() {
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser==null){
            return R.tokenError("登录失效");
        }
        Long userId = loginUser.getUserid();
        if(userId ==null || userId == 0)return R.tokenError("登录失效");
        List<AppUserBank> list = appUserBankService.lambdaQuery()
                .eq(AppUserBank::getAppUserId, userId).list();
        return R.ok(list);
    }
 
}