新版仿ok交易所-后端
C2C
zyy
2025-10-20 3cd5a88ce846894d82f83daf967b424d0bf841db
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
package com.yami.trading.service.c2c.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.c2c.C2cAdvert;
import com.yami.trading.bean.c2c.C2cOrder;
import com.yami.trading.bean.c2c.C2cUser;
import com.yami.trading.bean.constans.WalletConstants;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.model.*;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.constants.RedisKeys;
import com.yami.trading.common.constants.TipConstants;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.*;
import com.yami.trading.dao.c2c.C2cOrderMapper;
import com.yami.trading.service.*;
import com.yami.trading.service.c2c.C2cAdvertService;
import com.yami.trading.service.c2c.C2cOrderService;
import com.yami.trading.service.c2c.C2cPaymentMethodService;
import com.yami.trading.service.c2c.C2cUserService;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.impl.WalletServiceImpl;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.syspara.SysparaService;
import com.yami.trading.service.system.LogService;
import com.yami.trading.service.system.TipService;
import com.yami.trading.service.user.UserDataService;
import com.yami.trading.service.user.UserService;
import com.yami.trading.service.user.WalletLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
@Service
@Slf4j
public class C2cOrderServiceImpl extends ServiceImpl<C2cOrderMapper, C2cOrder> implements C2cOrderService {
 
    @Autowired
    WalletService walletService;
 
    @Autowired
    C2cPaymentMethodService c2cPaymentMethodService;
 
    @Autowired
    RechargeBlockchainOrderService rechargeBlockchainOrderService;
 
    @Autowired
    SysparaService sysparaService;
 
    @Autowired
    RedisTemplate redisTemplate;
 
    @Autowired
    MoneyLogService moneyLogService;
 
    @Autowired
    TipService tipService;
 
    @Autowired
    UserService userService;
 
    @Autowired
    WalletLogService walletLogService;
 
    @Autowired
    C2cAdvertService c2cAdvertService;
 
    @Autowired
    LogService logService;
 
    @Autowired
    C2cUserService c2cUserService;
 
    @Autowired
    UserDataService userDataService;
 
 
    @Autowired
    AwsS3OSSFileService awsS3OSSFileService;
 
    @Autowired
    ItemService itemService;
 
    @Autowired
    DataService dataService;
 
    @Autowired
    RealNameAuthRecordService realNameAuthRecordService;
 
    @Autowired
    HighLevelAuthRecordService highLevelAuthRecordService;
 
    @Override
    public long getTodayCancelOrderCount(String userId) {
 
        return count(Wrappers.<C2cOrder>query().lambda().eq(C2cOrder::getPartyId, userId).eq(C2cOrder::getState, 4));
    }
 
    @Override
    /*
     * 查询未完结订单数量,根据广告ID
     */
    public Long findNoEndingOrdersCountByAdvertId(String c2cAdvertId) {
      return   count(Wrappers.<C2cOrder>query().lambda().eq(C2cOrder::getC2cAdvertId,c2cAdvertId).in(C2cOrder::getState,"0","1","2","5"));
    }
 
    /**
     * 订单放行
     */
    public void saveOrderPass(C2cOrder c2cOrder) {
 
        DecimalFormat df = new DecimalFormat("#.########");
        C2cAdvert c2cAdvert = this.c2cAdvertService.getById(c2cOrder.getC2cAdvertId());
        if (null == c2cAdvert) {
            throw new YamiShopBindException("广告不存在");
        }
//        double symbolClose = symbolClose(c2cOrder.getSymbol());
        double symbolClose = c2cAdvert.getSymbolClose();
        if (C2cOrder.DIRECTION_BUY.equals(c2cOrder.getDirection())) {
            // 买币
            // 给用户账户添加相应的币种数量
            double amountBefore = 0d;
            double amountAfter = 0d;
            if ("usdt".equalsIgnoreCase(c2cOrder.getSymbol())) {
                Wallet wallet = this.walletService.saveWalletByPartyId(c2cOrder.getPartyId());
                amountBefore = wallet.getMoney().doubleValue();
                amountAfter = Double.valueOf(df.format(Arith.add(wallet.getMoney().doubleValue(), c2cOrder.getCoinAmount()))).doubleValue();
                this.walletService.update(c2cOrder.getPartyId().toString(), c2cOrder.getCoinAmount());
            } else {
                WalletExtend walletExtend = this.walletService.saveExtendByPara(c2cOrder.getPartyId(), c2cOrder.getSymbol());
                amountBefore = walletExtend.getAmount();
                amountAfter = Double.valueOf(df.format(Arith.add(walletExtend.getAmount(), c2cOrder.getCoinAmount()))).doubleValue();
                this.walletService.updateExtend(c2cOrder.getPartyId().toString(), c2cOrder.getSymbol(), c2cOrder.getCoinAmount());
            }
            // 保存资金日志
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_C2C);
            moneyLog.setAmount_before(new BigDecimal(amountBefore));
            moneyLog.setAmount(new BigDecimal(c2cOrder.getCoinAmount()));
            moneyLog.setAmount_after(new BigDecimal(amountAfter));
            moneyLog.setLog("c2c订单购买放行,订单号[" + c2cOrder.getOrderNo() + "]");
            moneyLog.setUserId(c2cOrder.getPartyId());
            moneyLog.setWallet_type(c2cOrder.getSymbol());
            moneyLog.setSymbol(c2cOrder.getSymbol());
            moneyLog.setContent_type(Constants.MONEYLOG_CONTENT_OTC_BUY);
            moneyLogService.save(moneyLog);
            // usdt价值=币种数量*行情价
            if ("usdt".equalsIgnoreCase(c2cOrder.getSymbol())) {
                userDataService.saveRechargeHandle(c2cOrder.getPartyId(), c2cOrder.getCoinAmount(), c2cOrder.getSymbol());
            } else {
                this.userDataService.saveRechargeHandle(c2cOrder.getPartyId(), c2cOrder.getCoinAmount(), c2cOrder.getSymbol());
            }
        } else if (C2cOrder.DIRECTION_SELL.equals(c2cOrder.getDirection())) {
            // 卖币
            if (C2cAdvertLock.add(c2cOrder.getC2cAdvertId())) {
                c2cAdvert.setCoinAmount(Double.valueOf(df.format(Arith.add(c2cAdvert.getCoinAmount(), c2cOrder.getCoinAmount()))).doubleValue());
                c2cAdvert.setDeposit(Double.valueOf(df.format(Arith.add(c2cAdvert.getDeposit(), c2cOrder.getAmountUsdt()))).doubleValue());
                this.c2cAdvertService.updateById(c2cAdvert);
            }
            // usdt价值=币种数量*行情价
            if ("usdt".equalsIgnoreCase(c2cOrder.getSymbol())) {
                this.userDataService.saveWithdrawHandle(c2cOrder.getPartyId(), c2cOrder.getCoinAmount(), 0d, "usdt");
            } else {
                this.userDataService.saveWithdrawHandle(c2cOrder.getPartyId(), Double.valueOf(df.format(Arith.mul(c2cOrder.getCoinAmount(), symbolClose))).doubleValue(), 0d, c2cOrder.getSymbol());
            }
        }
        // 订单完成
        c2cOrder.setState("3");
        c2cOrder.setHandleTime(new Date());
        updateById(c2cOrder);
        this.updateNofinishOrderCount(c2cOrder);
        C2cUser c2cUser = this.c2cUserService.getById(c2cOrder.getC2cUserId());
        if (null == c2cUser) {
            throw new YamiShopBindException("承兑商不存在");
        }
        if (C2cOrder.DIRECTION_BUY.equals(c2cOrder.getDirection())) {
            // 买币
            c2cUser.setBuyAmount(c2cUser.getBuyAmount() + c2cOrder.getAmountUsdt());
            c2cUser.setBuySuccessOrders(c2cUser.getBuySuccessOrders() + 1);
        } else if (C2cOrder.DIRECTION_SELL.equals(c2cOrder.getDirection())) {
            // 卖币
            c2cUser.setSellAmount(c2cUser.getSellAmount() + c2cOrder.getAmountUsdt());
            c2cUser.setSellSuccessOrders(c2cUser.getSellSuccessOrders() + 1);
        }
        c2cUser.setTotalAmount(c2cUser.getTotalAmount() + c2cOrder.getAmountUsdt());
        c2cUser.setTotalSuccessOrders(c2cUser.getTotalSuccessOrders() + 1);
        if (C2cOrder.DIRECTION_SELL.equals(c2cOrder.getDirection())) {
            // 卖币
            // 用户出售承兑商付款 相当于给承兑商账户充值了保证金
            c2cUser.setDepositOpen(Double.valueOf(df.format(Arith.add(c2cUser.getDepositOpen(), c2cOrder.getAmountUsdt()))).doubleValue());
        }
        this.c2cUserService.updateById(c2cUser);
        this.tipService.deleteTip(c2cOrder.getUuid().toString());
        //this.c2cSendMessageByState(c2cOrder, "3");
    }
 
    @Override
    public void savePass(C2cOrder c2cOrder, String safeword, String operator_username) {
 
        if ("3".equals(c2cOrder.getState())) {
            throw new YamiShopBindException("订单已完成,无法放行");
        }
        if ("4".equals(c2cOrder.getState())) {
            throw new YamiShopBindException("订单已取消,无法放行");
        }
        saveOrderPass(c2cOrder);
        User order_user = userService.getById(c2cOrder.getPartyId());
        this.saveLog(order_user.getUserName(), operator_username, "订单放行", c2cOrder.getPartyId().toString());
    }
 
    @Override
    public Map<String, Object> detail(C2cOrder order) {
 
        Map<String, String> pmtMap = this.c2cAdvertService
                .getC2cSyspara("c2c_payment_method_type");
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", order.getUuid());
        result.put("party_id", order.getPartyId());
        result.put("c2c_user_id", order.getC2cUserId());
        result.put("c2c_advert_id", order.getC2cAdvertId());
        result.put("payment_method_id", order.getPaymentMethodId());
        result.put("order_type", order.getOrderType());
        result.put("order_no", order.getOrderNo());
        result.put("state", order.getState());
        result.put("c2c_user_type", order.getC2cUserType());
        result.put("c2c_user_code", order.getC2cUserCode());
        result.put("c2c_user_nick_name", order.getC2cUserNickName());
        result.put("c2c_user_party_id", order.getC2cUserPartyId());
        result.put("c2c_user_party_code", order.getC2cUserPartyCode());
        result.put("c2c_user_party_name", order.getC2cUserPartyName());
        result.put("direction", order.getDirection());
        result.put("currency", order.getCurrency());
        result.put("symbol", order.getSymbol().toUpperCase());
        result.put("pay_rate", order.getPayRate());
        result.put("symbol_value", order.getSymbolValue());
        result.put("coin_amount", order.getCoinAmount());
        result.put("expire_time", order.getExpireTime());
        result.put("amount", order.getAmount());
        result.put("method_type", order.getMethodType());
        String methodType = String.valueOf(order.getMethodType());
        result.put("method_type_name", pmtMap.containsKey(methodType) ? pmtMap.get(methodType) : methodType);
        result.put("method_name", order.getMethodName());
        result.put("method_img", Constants.IMAGES_HTTP+order.getMethodImg());
        result.put("real_name", order.getRealName());
        result.put("param_name1", order.getParamName1());
        result.put("param_value1", order.getParamValue1());
        result.put("param_name2", order.getParamName2());
        result.put("param_value2", order.getParamValue2());
        result.put("param_name3", order.getParamName3());
        result.put("param_value3", order.getParamValue3());
        result.put("param_name4", order.getParamName4());
        result.put("param_value4", order.getParamValue4());
        result.put("param_name5", order.getParamName5());
        result.put("param_value5", order.getParamValue5());
        result.put("param_name6", order.getParamName6());
        result.put("param_value6", order.getParamValue6());
        result.put("param_name7", order.getParamName7());
        result.put("param_value7", order.getParamValue7());
        result.put("param_name8", order.getParamName8());
        result.put("param_value8", order.getParamValue8());
        result.put("param_name9", order.getParamName9());
        result.put("param_value9", order.getParamValue9());
        result.put("param_name10", order.getParamName10());
        result.put("param_value10", order.getParamValue10());
        result.put("param_name11", order.getParamName11());
        result.put("param_value11", order.getParamValue11());
        result.put("param_name12", order.getParamName12());
        result.put("param_value12", order.getParamValue12());
        result.put("param_name13", order.getParamName13());
        result.put("param_value13", order.getParamValue13());
        result.put("param_name14", order.getParamName14());
        result.put("param_value14", order.getParamValue14());
        result.put("param_name15", order.getParamName15());
        result.put("param_value15", order.getParamValue15());
        result.put("qrcode", order.getQrcode());
        result.put("remark", order.getRemark());
        result.put("img", Constants.IMAGES_HTTP+order.getImg());
 
        result.put("create_time", DateUtils.format(order.getCreateTime(), DateUtils.DF_yyyyMMddHHmmss));
        result.put("handle_time", DateUtils.format(order.getHandleTime(), DateUtils.DF_yyyyMMddHHmmss));
        result.put("close_time", DateUtils.format(order.getCloseTime(), DateUtils.DF_yyyyMMddHHmmss));
        result.put("pay_time", DateUtils.format(order.getPayTime(), DateUtils.DF_yyyyMMddHHmmss));
        result.put("cancel_time", DateUtils.format(order.getCancelTime(), DateUtils.DF_yyyyMMddHHmmss));
        return result;
    }
 
    /**
     * 订单取消
     *
     * @param role: user/用户;manager/承兑商管理员、root和admin;timer/订单自动取消定时器;
     */
    public void saveOrderCancel(C2cOrder c2cOrder, String role) {
 
        DecimalFormat df = new DecimalFormat("#.########");
        if ("4".equals(c2cOrder.getState())) {
            throw new YamiShopBindException("该订单已取消");
        }
        if ("3".equals(c2cOrder.getState())) {
            throw new YamiShopBindException("该订单已完成");
        }
        if ("buy".equals(c2cOrder.getDirection())) {
            // 充值
        } else if ("sell".equals(c2cOrder.getDirection())) {
            // 提现
            // 用户钱包退还
            Wallet wallet = this.walletService.saveWalletByPartyId(c2cOrder.getPartyId());
            double amountBefore = wallet.getMoney().doubleValue();
            double amountAfter = Double.valueOf(df.format(Arith.add(wallet.getMoney().doubleValue(), c2cOrder.getCoinAmount()))).doubleValue();
            this.walletService.update(c2cOrder.getPartyId().toString(), c2cOrder.getCoinAmount());
            // 保存 资金日志
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_C2C);
            moneyLog.setAmountBefore(new BigDecimal(amountBefore));
            moneyLog.setAmount(new BigDecimal(c2cOrder.getCoinAmount()));
            moneyLog.setAmountAfter(new BigDecimal(amountAfter));
            moneyLog.setLog("C2C订单取消,订单号[" + c2cOrder.getOrderNo() + "]");
            moneyLog.setUserId(c2cOrder.getPartyId());
            moneyLog.setWalletType(c2cOrder.getSymbol());
            moneyLog.setSymbol(c2cOrder.getSymbol());
            moneyLog.setContentType(Constants.MONEYLOG_CONTENT_BANK_CARD_ORDER_CANCEL);
            this.moneyLogService.save(moneyLog);
            // 保存 充提记录
            WalletLog walletLog = new WalletLog();
            walletLog.setCategory(Constants.MONEYLOG_CATEGORY_C2C);
            walletLog.setPartyId(c2cOrder.getPartyId());
            walletLog.setOrderNo(c2cOrder.getOrderNo());
            walletLog.setStatus(Integer.valueOf(c2cOrder.getState()).intValue());
            walletLog.setAmount(c2cOrder.getCoinAmount());
            walletLog.setWallettype(c2cOrder.getSymbol());
            moneyLog.setSymbol(c2cOrder.getSymbol());
            this.walletLogService.save(walletLog);
        }
        c2cOrder.setState("4");
        c2cOrder.setCancelTime(new Date());
        updateById(c2cOrder);
        this.updateNofinishOrderCount(c2cOrder);
        this.tipService.deleteTip(c2cOrder.getUuid().toString());
        // 取消次数加1
        String partyIdCancel = "";
        if ("user".equals(role)) {
            partyIdCancel = c2cOrder.getPartyId();
        }
        if (!"".equals(partyIdCancel)) {
            Map<String, Integer> map = (Map<String, Integer>) redisTemplate.opsForValue().get(RedisKeys.C2C_ORDER_CANCEL_DAY_TIMES);
            if (null == map) {
                map = new ConcurrentHashMap<String, Integer>();
            }
            if (null == map.get(partyIdCancel)) {
                map.put(partyIdCancel, 1);
            } else {
                map.put(partyIdCancel, map.get(partyIdCancel) + 1);
            }
            redisTemplate.opsForValue().set(RedisKeys.C2C_ORDER_CANCEL_DAY_TIMES, map);
        }
    }
 
    /*
     * 用户未结束订单数量(0未付款)减1
     */
    public void updateNofinishOrderCount(C2cOrder entity) {
 
        Map<String, Long> ocMap = (Map<String, Long>) redisTemplate.opsForValue().get(RedisKeys.C2C_NOFINISH_ORDER_COUNT);
        if (null == ocMap) {
            ocMap = new ConcurrentHashMap<String, Long>();
        }
        Long count = ocMap.get(entity.getPartyId());
        if (null == count) {
            ocMap.put(entity.getPartyId(), 0L);
        } else {
            ocMap.put(entity.getPartyId(), count - 1 <= 0 ? 0 : count - 1);
        }
        redisTemplate.opsForValue().set(RedisKeys.C2C_NOFINISH_ORDER_COUNT, ocMap);
    }
 
    /**
     * 手动放行
     *
     */
    @Override
    @Transactional
    public void manualRelease(C2cOrder c2cOrder, String operator_username) {
        if ("3".equals(c2cOrder.getState())) {
            throw new YamiShopBindException("订单已完成,无法放行");
        }
        if ("4".equals(c2cOrder.getState())) {
            throw new YamiShopBindException("订单已取消,无法放行");
        }
 
        DecimalFormat df = new DecimalFormat("#.########");
 
        if ("recharge".equals(c2cOrder.getDirection())) {
            // 充值
 
            // 给用户账户添加相应的币种数量
            Wallet wallet = this.walletService.saveWalletByPartyId(c2cOrder.getPartyId());
            double amountBefore = wallet.getMoney().doubleValue();
            double amountAfter = Double.valueOf(df.format(Arith.add(wallet.getMoney().doubleValue(), c2cOrder.getCoinAmount()))).doubleValue();
            this.walletService.update(c2cOrder.getPartyId().toString(), c2cOrder.getCoinAmount());
 
            // 保存 资金日志
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_BANK_CARD);
            moneyLog.setAmount_before(new BigDecimal(amountBefore));
            moneyLog.setAmount(new BigDecimal(c2cOrder.getCoinAmount()));
            moneyLog.setAmount_after(new BigDecimal(amountAfter));
            moneyLog.setLog("银行卡订单充值放行,订单号[" + c2cOrder.getOrderNo() + "]");
            moneyLog.setUserId(c2cOrder.getPartyId());
            moneyLog.setWalletType(c2cOrder.getSymbol());
            moneyLog.setContent_type(Constants.MONEYLOG_CONTENT_BANK_CARD_RECHARGE);
            this.moneyLogService.save(moneyLog);
 
            // 保存 充提记录
            WalletLog walletLog = new WalletLog();
            walletLog.setCategory(Constants.MONEYLOG_CATEGORY_BANK_CARD_RECHARGE);
            walletLog.setPartyId(c2cOrder.getPartyId());
            walletLog.setOrderNo(c2cOrder.getOrderNo());
            walletLog.setStatus(Integer.valueOf(c2cOrder.getState()).intValue());
            walletLog.setAmount(c2cOrder.getCoinAmount());
            walletLog.setWallettype(c2cOrder.getSymbol());
            walletLog.setCreateTime(new Date());
            this.walletLogService.save(walletLog);
 
            this.userDataService.saveRechargeHandle(c2cOrder.getPartyId(), c2cOrder.getCoinAmount(), c2cOrder.getSymbol());
 
        } else if ("withdraw".equals(c2cOrder.getDirection())) {
            // 提现
 
            this.userDataService.saveWithdrawHandle(c2cOrder.getPartyId(), c2cOrder.getCoinAmount(), 0d, "usdt");
        }
 
        // 订单完成
        c2cOrder.setState("3");
        c2cOrder.setHandleTime(new Date());
        updateById(c2cOrder);
        this.updateNofinishOrderCount(c2cOrder);
 
        this.tipService.deleteTip(c2cOrder.getUuid().toString());
 
        User order_user = userService.getById(c2cOrder.getPartyId());
      //  this.saveLog(order_user.getUserName(), operator_username, "订单放行", c2cOrder.getPartyId().toString());
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_BANK_CARD_RW);
        log.setUsername(operator_username);
        log.setUserId(c2cOrder.getPartyId());
        log.setOperator(operator_username);
        log.setLog("订单放行");
        log.setCreateTime(new Date());
        logService.save(log);
    }
 
    /**
     * 取消订单
     *
     * @param id
     * @param reason
     */
    @Override
    public void orderCancel(String id, String reason) {
//        C2cOrder c2cOrder= getById(id);
//        if (c2cOrder==null){
//            throw  new YamiShopBindException("参数错误!");
//        }
//        if (c2cOrder.getStatus()!=0){
//            throw  new YamiShopBindException("订单已操作过了!");
//        }
//        Date now=new Date();
//        c2cOrder.setStatus(4);
//        c2cOrder.setReason(reason);
//        c2cOrder.setHandleTime(now);
//        c2cOrder.setUpdateTime(now);
//        updateById(c2cOrder);
    }
 
    @Override
    public Page pagedQuery(long pageNo, long pageSize, String status, String orderNo, String userCode, String rolename,
                           String c2cUserCode, String c2cUserType, String c2cUserPartyCode, List<String> direction, String loginPartyId) {
        Page page=new Page(pageNo,pageSize);
        return baseMapper.pagedC2cQuery(page,status,orderNo,userCode,rolename,c2cUserCode,c2cUserType,c2cUserPartyCode,direction);
    }
 
 
    public void saveOpen(C2cOrder c2cOrder,String remark) {
        log.error("saveOpen start:"+remark);
        C2cAdvert c2cAdvert = this.c2cAdvertService.getById(c2cOrder.getC2cAdvertId());
        if (null == c2cAdvert) {
            throw new YamiShopBindException("广告不存在");
        }
 
        if (1 != c2cAdvert.getOnSale()) {
            throw new YamiShopBindException("广告已下架");
        }
 
        User party =userService.getById(c2cOrder.getPartyId());
        if (null == party) {
            throw new YamiShopBindException("用户信息不存在");
        }
 
        C2cUser c2cUser = this.c2cUserService.getById(c2cAdvert.getC2cUserId());
        if (null == c2cUser) {
            throw new YamiShopBindException("承兑商不存在");
        }
        User c2cParty =userService.getById(c2cUser.getC2cUserPartyId());
 
        if (null == c2cParty) {
            throw new YamiShopBindException("承兑商的用户信息不存在");
        }
 
        C2cPaymentMethod method = this.c2cPaymentMethodService.get(c2cOrder.getPaymentMethodId());
        if (null == method) {
            throw new YamiShopBindException("支付方式不存在");
        }
 
        if (C2cAdvert.DIRECTION_SELL.equals(c2cAdvert.getDirection())) {
 
            if (!party.isWithdrawAuthority()) {
                throw new YamiShopBindException( "无权限");
            }
 
            this.checkSellAuth(c2cOrder.getPartyId());
            this.checkSellNum(c2cOrder.getPartyId());
 
            if (!method.getPartyId().equals(c2cOrder.getPartyId())) {
                throw new YamiShopBindException("支付方式不匹配该用户");
            }
        } else {
 
            if (!method.getPartyId().equals(c2cUser.getC2cUserPartyId())) {
                throw new YamiShopBindException("支付方式不匹配该承兑商");
            }
        }
 
        c2cOrder.setC2cUserType(c2cUser.getC2cUserType());
        c2cOrder.setC2cUserCode(c2cUser.getC2cUserCode());
        c2cOrder.setC2cUserNickName(c2cUser.getNickName());
        c2cOrder.setC2cUserHeadImg(c2cUser.getHeadImg());
        c2cOrder.setC2cUserPartyId(c2cUser.getC2cUserPartyId());
        c2cOrder.setC2cUserPartyCode(c2cParty.getUserCode());
        c2cOrder.setC2cUserPartyName(c2cParty.getUserName());
        c2cOrder.setDirection(c2cAdvert.getDirection());
        c2cOrder.setCurrency(c2cAdvert.getCurrency());
        c2cOrder.setSymbol(c2cAdvert.getSymbol());
        c2cOrder.setPayRate(c2cAdvert.getPayRate());
        c2cOrder.setSymbolValue(c2cAdvert.getSymbolValue());
        c2cOrder.setExpireTime(c2cAdvert.getExpireTime());
        c2cOrder.setMethodType(method.getMethodType());
        c2cOrder.setMethodName(method.getMethodName());
        c2cOrder.setMethodImg(method.getMethodImg());
        c2cOrder.setRealName(method.getRealName());
        c2cOrder.setParamName1(method.getParamName1());
        c2cOrder.setParamValue1(method.getParamValue1());
        c2cOrder.setParamName2(method.getParamName2());
        c2cOrder.setParamValue2(method.getParamValue2());
        c2cOrder.setParamName3(method.getParamName3());
        c2cOrder.setParamValue3(method.getParamValue3());
        c2cOrder.setParamName4(method.getParamName4());
        c2cOrder.setParamValue4(method.getParamValue4());
        c2cOrder.setParamName5(method.getParamName5());
        c2cOrder.setParamValue5(method.getParamValue5());
        c2cOrder.setParamName6(method.getParamName6());
        c2cOrder.setParamValue6(method.getParamValue6());
        c2cOrder.setParamName7(method.getParamName7());
        c2cOrder.setParamValue7(method.getParamValue7());
        c2cOrder.setParamName8(method.getParamName8());
        c2cOrder.setParamValue8(method.getParamValue8());
        c2cOrder.setParamName9(method.getParamName9());
        c2cOrder.setParamValue9(method.getParamValue9());
        c2cOrder.setParamName10(method.getParamName10());
        c2cOrder.setParamValue10(method.getParamValue10());
        c2cOrder.setParamName11(method.getParamName11());
        c2cOrder.setParamValue11(method.getParamValue11());
        c2cOrder.setParamName12(method.getParamName12());
        c2cOrder.setParamValue12(method.getParamValue12());
        c2cOrder.setParamName13(method.getParamName13());
        c2cOrder.setParamValue13(method.getParamValue13());
        c2cOrder.setParamName14(method.getParamName14());
        c2cOrder.setParamValue14(method.getParamValue14());
        c2cOrder.setParamName15(method.getParamName15());
        c2cOrder.setParamValue15(method.getParamValue15());
        c2cOrder.setQrcode(method.getQrcode());
        c2cOrder.setCreateTime(new Date());
        c2cOrder.setHandleTime(null);
        c2cOrder.setCloseTime(DateUtils.addMinute(c2cOrder.getCreateTime(), c2cOrder.getExpireTime()));
        c2cOrder.setPayTime(null);
        c2cOrder.setCancelTime(null);
 
        // 币种单价
        double symbolValue = c2cAdvert.getSymbolValue();
        if (C2cAdvert.DIRECTION_BUY.equals(c2cAdvert.getDirection())) {
            // 买币
            this.saveBuy(c2cOrder, c2cAdvert, symbolValue,remark);
        } else if (C2cAdvert.DIRECTION_SELL.equals(c2cAdvert.getDirection())) {
            // 卖币
            this.saveSell(c2cOrder, c2cAdvert, symbolValue,remark);
        } else {
            throw new YamiShopBindException("买卖方式不正确");
        }
 
        this.tipService.saveNewTip(c2cOrder.getUuid().toString(), TipConstants.C2C_ORDER,remark);
      //  this.c2cSendMessageByState(c2cOrder, "0");
        log.error("saveOpen end:"+remark);
    }
 
 
    /**
     * 检测用户可卖币次数
     */
    public void checkSellNum(String partyId) {
        // 当日提现次数是否超过
        Object obj = this.sysparaService.find("c2c_sell_limit_num");
        if (null != obj) {
            double c2c_sell_limit_num = Double.valueOf(this.sysparaService.find("c2c_sell_limit_num").getSvalue());
            List<C2cOrder> c2cOrders = this.findByPartyIdAndToday(partyId, C2cAdvert.DIRECTION_SELL, null);
 
            if (c2c_sell_limit_num > 0 && c2cOrders != null) {
                if (c2cOrders.size() >= c2c_sell_limit_num) {
                    throw new BusinessException(1, "当日可提现次数不足");
                }
            }
        }
    }
 
    /**
     * 检测用户可卖币权限
     */
    public void checkSellAuth(String partyId) {
        RealNameAuthRecord party_kyc = realNameAuthRecordService.getByUserId(partyId);
        Object objKyc = this.sysparaService.find("c2c_sell_by_kyc");
        if (null != objKyc) {
            if ("true".equals(this.sysparaService.find("c2c_sell_by_kyc").getSvalue()) && !(party_kyc.getStatus() == 2)) {
                throw new BusinessException(401, "无权限");
            }
        }
        HighLevelAuthRecord  party_kycHighLevel = highLevelAuthRecordService.findByUserId(partyId);
        Object objKycHigh = this.sysparaService.find("c2c_sell_by_high_kyc");
        if (null != objKycHigh) {
            if ("true".equals(this.sysparaService.find("c2c_sell_by_high_kyc").getSvalue()) && !(party_kycHighLevel.getStatus() == 2)) {
                throw new BusinessException(1, "请先通过高级认证");
            }
        }
    }
 
    private void saveSell(C2cOrder c2cOrder, C2cAdvert c2cAdvert, double symbol_value,String remark) {
        log.error("saveSell start::"+remark);
        DecimalFormat df = new DecimalFormat("#.########");
 
        if (C2cOrder.ORDER_TYPE_BY_AMOUNT.equals(c2cOrder.getOrderType())) {
            // 按支付金额支付
 
            // 币种数量 = 支付金额/币种单价
            double coin_amount = Double.valueOf(df.format(Arith.div(c2cOrder.getAmount(), symbol_value)));
            c2cOrder.setCoinAmount(coin_amount);
            c2cOrder.setAmountUsdt(Double.valueOf(df.format(Arith.mul(coin_amount, c2cAdvert.getSymbolClose()))).doubleValue());
        } else {
            // 按币种数量支付
            c2cOrder.setAmount(Double.valueOf(df.format(Arith.mul(c2cOrder.getCoinAmount(), symbol_value))));
            c2cOrder.setAmountUsdt(Double.valueOf(df.format(Arith.mul(c2cOrder.getCoinAmount(), c2cAdvert.getSymbolClose()))).doubleValue());
        }
        log.error("sell save start::"+remark);
        this.save(c2cOrder);
        log.error("save save end::"+remark);
        // 买入金额需要在区间内
        if (c2cOrder.getAmount() > c2cAdvert.getInvestmentMax() || c2cOrder.getAmount() < c2cAdvert.getInvestmentMin()) {
            throw new YamiShopBindException("金额不在购买区间");
        }
 
        double amountBefore = 0d;
        double amountAfter = 0d;
 
        if ("usdt".equalsIgnoreCase(c2cAdvert.getSymbol())) {
 
            Wallet wallet = this.walletService.saveWalletByPartyId(c2cOrder.getPartyId());
            if (c2cOrder.getCoinAmount() > wallet.getMoney().doubleValue()) {
                throw new YamiShopBindException("用户剩余数量不足");
            }
 
            amountBefore = wallet.getMoney().doubleValue();
            amountAfter = Double.valueOf(df.format(Arith.sub(wallet.getMoney().doubleValue(), c2cOrder.getCoinAmount()))).doubleValue();
            this.walletService.update(c2cOrder.getPartyId(), Arith.sub(0, c2cOrder.getCoinAmount()));
        } else {
 
            WalletExtend walletExtend = this.walletService.saveExtendByPara(c2cOrder.getPartyId(), c2cOrder.getSymbol());
 
            if (c2cOrder.getCoinAmount() > walletExtend.getAmount()) {
                throw new YamiShopBindException("用户剩余数量不足");
            }
 
            amountBefore = walletExtend.getAmount();
            amountAfter = Double.valueOf(df.format(Arith.sub(walletExtend.getAmount(), c2cOrder.getCoinAmount()))).doubleValue();
            this.walletService.updateExtend(c2cOrder.getPartyId(), c2cOrder.getSymbol(), Arith.sub(0, c2cOrder.getCoinAmount()));
        }
 
        // 保存资金日志
        MoneyLog moneylog = new MoneyLog();
        moneylog.setCategory(Constants.MONEYLOG_CATEGORY_C2C);
        moneylog.setAmount_before(new BigDecimal(amountBefore));
        moneylog.setAmount(new BigDecimal(Arith.sub(0, c2cOrder.getCoinAmount())));
        moneylog.setAmount_after(new BigDecimal(amountAfter));
        moneylog.setLog("c2c卖币,币种[" + c2cOrder.getSymbol() + "],订单号[" + c2cOrder.getOrderNo() + "]");
        moneylog.setUserId(c2cOrder.getPartyId());
        moneylog.setWalletType(c2cOrder.getSymbol());
        moneylog.setContent_type(Constants.MONEYLOG_CONTENT_C2C_SELL);
        moneyLogService.save(moneylog);
 
        c2cAdvert.setSortIndex(0);
        this.c2cAdvertService.updateById(c2cAdvert);
        log.error("saveSell end::"+remark);
    }
 
    private void saveBuy(C2cOrder c2cOrder, C2cAdvert c2cAdvert, double symbol_value,String remark) {
        log.error("saveBuy start:"+remark);
        DecimalFormat df = new DecimalFormat("#.########");
 
        if (C2cOrder.ORDER_TYPE_BY_AMOUNT.equals(c2cOrder.getOrderType())) {
            // 按支付金额支付
 
            // 币种数量 = 支付金额/币种单价
            double coin_amount = Double.valueOf(df.format(Arith.div(c2cOrder.getAmount(), symbol_value))).doubleValue();
 
            if (coin_amount > c2cAdvert.getCoinAmount()) {
                throw new YamiShopBindException("该广告剩余数量不足");
            }
 
            c2cOrder.setCoinAmount(coin_amount);
            c2cOrder.setAmountUsdt(Double.valueOf(df.format(Arith.mul(coin_amount, c2cAdvert.getSymbolClose()))).doubleValue());
            c2cAdvert.setCoinAmount(Double.valueOf(df.format(Arith.sub(c2cAdvert.getCoinAmount(), coin_amount))).doubleValue());
            c2cAdvert.setDeposit(Double.valueOf(df.format(Arith.sub(c2cAdvert.getDeposit(), c2cOrder.getAmountUsdt()))).doubleValue());
        } else {
            // 按币种数量支付
 
            if (c2cOrder.getCoinAmount() > c2cAdvert.getCoinAmount()) {
                throw new YamiShopBindException("该广告剩余数量不足");
            }
 
            c2cOrder.setAmount(Double.valueOf(df.format(Arith.mul(c2cOrder.getCoinAmount(), symbol_value))).doubleValue());
            c2cOrder.setAmountUsdt(Double.valueOf(df.format(Arith.mul(c2cOrder.getCoinAmount(), c2cAdvert.getSymbolClose()))).doubleValue());
            c2cAdvert.setCoinAmount(Double.valueOf(df.format(Arith.sub(c2cAdvert.getCoinAmount(), c2cOrder.getCoinAmount()))).doubleValue());
            c2cAdvert.setDeposit(Double.valueOf(df.format(Arith.sub(c2cAdvert.getDeposit(), c2cOrder.getAmountUsdt()))).doubleValue());
        }
        log.error("buy save start:"+remark);
        this.save(c2cOrder);
        log.error("buy save end:"+remark);
        // 买入金额需要在区间内
        if (c2cOrder.getAmount() > c2cAdvert.getInvestmentMax() || c2cOrder.getAmount() < c2cAdvert.getInvestmentMin()) {
            throw new YamiShopBindException("金额不在购买区间");
        }
 
        c2cAdvert.setSortIndex(0);
        this.c2cAdvertService.updateById(c2cAdvert);
        log.error("saveBuy end:"+remark);
    }
 
 
 
    @Override
    public Page pagedBankCardOrderQuery(Page page,  List<String> direction,String state,String userCode,String roleName,String orderNo) {
        return baseMapper.pagedBankCardOrderQuery(page,direction,state,userCode,roleName,
                orderNo );
    }
 
    @Override
    public Page pagedQuery(int pageNo, int pageSize, String direction, String state, String loginPartyId) {
 
        Page page = new Page(pageNo, pageSize);
        baseMapper.pagedQuery(page, direction, state, loginPartyId);
        // 金额默认保留2位
        DecimalFormat df = new DecimalFormat("#.##");
        // 币种默认保留8位
        DecimalFormat dfCoin = new DecimalFormat("#.########");
        for (Map<String, Object> data : (List<Map<String, Object>>) page.getRecords()) {
            data.put("symbol_value", df.format(data.get("symbol_value")));
            data.put("coin_amount", dfCoin.format(data.get("coin_amount")));
            data.put("amount", dfCoin.format(data.get("amount")));
//            if(data.containsKey("symbol")){
//                data.put("symbol", data.get("symbol").toString().toUpperCase());
//            }else{
//                data.put("symbol", "");
//            }
        }
        return page;
    }
 
    public List<C2cOrder> findByPartyIdAndToday(String partyId, String direction, String state) {
 
        Date now = new Date();
        if (StringUtils.isEmptyString(state)) {
            List<C2cOrder> list = list(Wrappers.<C2cOrder>query().lambda().eq(C2cOrder::getPartyId, partyId).eq(C2cOrder::getDirection, direction)
                    .between(C2cOrder::getCreateTime, DateUtil.minDate(now), DateUtil.maxDate(now)));
            return list;
        } else {
            List<C2cOrder> list = list(Wrappers.<C2cOrder>query().lambda().eq(C2cOrder::getPartyId, partyId).eq(C2cOrder::getDirection, direction)
                    .between(C2cOrder::getCreateTime, DateUtil.minDate(now), DateUtil.maxDate(now)).eq(C2cOrder::getState, state));
            return list;
        }
    }
 
    @Override
    public C2cOrder get(String order_no) {
        List<C2cOrder> list = list(Wrappers.<C2cOrder>query().lambda().eq(C2cOrder::getOrderNo, order_no));
        if (list.size() > 0) {
            return (C2cOrder) list.get(0);
        }
        return null;
    }
 
    public List<C2cPaymentMethod> getOrderPayments(String order_no,boolean c2cOrderFlag) {
 
        C2cOrder c2cOrder = get(order_no);
        if (null == c2cOrder) {
            throw new YamiShopBindException("订单不存在");
        }
        List<String> typeList=new ArrayList<>();
        if (c2cOrderFlag){
            C2cAdvert c2cAdvert = c2cAdvertService.getById(c2cOrder.getC2cAdvertId());
            if (null == c2cAdvert) {
                throw new YamiShopBindException("广告不存在");
            }
            String []   types = c2cAdvert.getPayType().split(",");
            typeList = Arrays.asList(types);
 
        }
 
        List<C2cPaymentMethod> list = new ArrayList<C2cPaymentMethod>();
        Map<String, C2cPaymentMethod> methodMap = this.c2cPaymentMethodService.getByPartyId(c2cOrder.getPartyId());
        for (String key : methodMap.keySet()) {
            C2cPaymentMethod method = methodMap.get(key);
            if (null != method) {
                list.add(method);
            }
        }
        C2cPaymentMethod cpm = this.c2cPaymentMethodService.get(c2cOrder.getPaymentMethodId());
        List<C2cPaymentMethod> listRet = new ArrayList<C2cPaymentMethod>();
        // 先添加订单记录的支付方式
        for (C2cPaymentMethod pay : list) {
            if (c2cOrder.getPaymentMethodId().equals(pay.getUuid())) {
                listRet.add(pay);
            }
        }
        // 再添加与订单记录的支付方式类型相同的支付方式
        for (C2cPaymentMethod pay : list) {
            if (!c2cOrder.getPaymentMethodId().equals(pay.getUuid())) {
                if (cpm.getMethodConfigId().equals(pay.getMethodConfigId())) {
                    listRet.add(pay);
                }
            }
        }
        if (!c2cOrderFlag) {
            // 最后添加与广告匹配的支付方式
            for (C2cPaymentMethod pay : list) {
                if (!c2cOrder.getPaymentMethodId().equals(pay.getUuid()) && !cpm.getMethodConfigId().equals(pay.getMethodConfigId())) {
                    if (typeList.contains(pay.getMethodConfigId())) {
                        listRet.add(pay);
                    }
                }
            }
        }
        for (int i = 0; i < listRet.size(); i++) {
            C2cPaymentMethod method = listRet.get(i);
            String methodType = String.valueOf(method.getMethodType());
            Map<String, String> pmtMap = this.c2cAdvertService.getC2cSyspara("c2c_payment_method_type");
            method.setMethodTypeName(pmtMap.containsKey(methodType) ? pmtMap.get(methodType) : methodType);
        }
        return listRet;
    }
 
    @Override
    public void saveOrderPay(String order_no, String safeword, String operator_username, String payment_method_id_order_pay) {
 
        C2cOrder c2cOrder = get(order_no);
        if (null == c2cOrder) {
            throw new YamiShopBindException("订单不存在");
        }
        if (!Arrays.asList("0", "2").contains(c2cOrder.getState())) {
            throw new YamiShopBindException("订单不处于待支付或申诉中状态,无法转账");
        }
        C2cPaymentMethod method = this.c2cPaymentMethodService.get(payment_method_id_order_pay);
        // 更新最终的支付方式
        c2cOrder.setPaymentMethodId(method.getUuid());
        c2cOrder.setMethodType(method.getMethodType());
        c2cOrder.setMethodName(method.getMethodName());
        c2cOrder.setMethodImg(method.getMethodImg());
        c2cOrder.setRealName(method.getRealName());
        c2cOrder.setParamName1(method.getParamName1());
        c2cOrder.setParamValue1(method.getParamValue1());
        c2cOrder.setParamName2(method.getParamName2());
        c2cOrder.setParamValue2(method.getParamValue2());
        c2cOrder.setParamName3(method.getParamName3());
        c2cOrder.setParamValue3(method.getParamValue3());
        c2cOrder.setParamName4(method.getParamName4());
        c2cOrder.setParamValue4(method.getParamValue4());
        c2cOrder.setParamName5(method.getParamName5());
        c2cOrder.setParamValue5(method.getParamValue5());
        c2cOrder.setParamName6(method.getParamName6());
        c2cOrder.setParamValue6(method.getParamValue6());
        c2cOrder.setParamName7(method.getParamName7());
        c2cOrder.setParamValue7(method.getParamValue7());
        c2cOrder.setParamName8(method.getParamName8());
        c2cOrder.setParamValue8(method.getParamValue8());
        c2cOrder.setParamName9(method.getParamName9());
        c2cOrder.setParamValue9(method.getParamValue9());
        c2cOrder.setParamName10(method.getParamName10());
        c2cOrder.setParamValue10(method.getParamValue10());
        c2cOrder.setParamName11(method.getParamName11());
        c2cOrder.setParamValue11(method.getParamValue11());
        c2cOrder.setParamName12(method.getParamName12());
        c2cOrder.setParamValue12(method.getParamValue12());
        c2cOrder.setParamName13(method.getParamName13());
        c2cOrder.setParamValue13(method.getParamValue13());
        c2cOrder.setParamName14(method.getParamName14());
        c2cOrder.setParamValue14(method.getParamValue14());
        c2cOrder.setParamName15(method.getParamName15());
        c2cOrder.setParamValue15(method.getParamValue15());
        c2cOrder.setQrcode(method.getQrcode());
        c2cOrder.setState("1");
        c2cOrder.setPayTime(new Date());
        updateById(c2cOrder);
        //this.c2cOrderService.c2cSendMessageByState(c2cOrder, "1");
        User orderUser = userService.getById(c2cOrder.getPartyId());
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_BANK_CARD_RW);
        log.setUsername(operator_username);
        log.setUserId(c2cOrder.getPartyId());
        log.setOperator(operator_username);
        log.setLog("订单转账完成");
        log.setCreateTime(new Date());
        logService.save(log);
 
    }
 
    public void saveLog(String order_username, String operator, String context, String orderPartyId) {
 
        Log log = new Log();
        log.setCategory(Constants.LOG_CATEGORY_C2C);
        log.setUsername(order_username);
        log.setUserId(orderPartyId);
        log.setOperator(operator);
        log.setLog(context);
        log.setCreateTime(new Date());
        logService.save(log);
    }
 
    public void saveOpen(C2cOrder c2cOrder, User party) {
 
        C2cPaymentMethod method = c2cPaymentMethodService.get(c2cOrder.getPaymentMethodId());
        if (null == method) {
            throw new YamiShopBindException("支付方式不存在");
        }
        if ("withdraw".equals(c2cOrder.getDirection())) {
            String partyId = party.getUserId();
            if (!method.getPartyId().equals(partyId)) {
                throw new YamiShopBindException("支付方式不匹配该用户");
            }
        } else {
//            if (!method.getPartyId().equals("2c948a8282920a9f01829270bcac0000")) {
//                throw new YamiShopBindException("支付方式不匹配官方");
            //  }
        }
        int expireTime = 45;
        Object obj = this.sysparaService.find("bank_card_expire_time");
        if (null != obj) {
            expireTime = Integer.valueOf(this.sysparaService.find("bank_card_expire_time").getSvalue()).intValue();
        }
        c2cOrder.setExpireTime(expireTime);
        c2cOrder.setMethodType(method.getMethodType());
        c2cOrder.setMethodName(method.getMethodName());
        c2cOrder.setMethodImg(method.getMethodImg());
        c2cOrder.setRealName(method.getRealName());
        c2cOrder.setParamName1(method.getParamName1());
        c2cOrder.setParamValue1(method.getParamValue1());
        c2cOrder.setParamName2(method.getParamName2());
        c2cOrder.setParamValue2(method.getParamValue2());
        c2cOrder.setParamName3(method.getParamName3());
        c2cOrder.setParamValue3(method.getParamValue3());
        c2cOrder.setParamName4(method.getParamName4());
        c2cOrder.setParamValue4(method.getParamValue4());
        c2cOrder.setParamName5(method.getParamName5());
        c2cOrder.setParamValue5(method.getParamValue5());
        c2cOrder.setParamName6(method.getParamName6());
        c2cOrder.setParamValue6(method.getParamValue6());
        c2cOrder.setParamName7(method.getParamName7());
        c2cOrder.setParamValue7(method.getParamValue7());
        c2cOrder.setParamName8(method.getParamName8());
        c2cOrder.setParamValue8(method.getParamValue8());
        c2cOrder.setParamName9(method.getParamName9());
        c2cOrder.setParamValue9(method.getParamValue9());
        c2cOrder.setParamName10(method.getParamName10());
        c2cOrder.setParamValue10(method.getParamValue10());
        c2cOrder.setParamName11(method.getParamName11());
        c2cOrder.setParamValue11(method.getParamValue11());
        c2cOrder.setParamName12(method.getParamName12());
        c2cOrder.setParamValue12(method.getParamValue12());
        c2cOrder.setParamName13(method.getParamName13());
        c2cOrder.setParamValue13(method.getParamValue13());
        c2cOrder.setParamName14(method.getParamName14());
        c2cOrder.setParamValue14(method.getParamValue14());
        c2cOrder.setParamName15(method.getParamName15());
        c2cOrder.setParamValue15(method.getParamValue15());
        c2cOrder.setQrcode(method.getQrcode());
        if ("recharge".equals(c2cOrder.getDirection())) {
            // 充值
            this.saveRecharge(c2cOrder, party);
        } else if ("withdraw".equals(c2cOrder.getDirection())) {
            // 提现
            this.saveWithdraw(c2cOrder);
        } else {
            throw new YamiShopBindException("充值或提现不正确");
        }
    }
 
    private void saveWithdraw(C2cOrder c2cOrder) {
 
        DecimalFormat df = new DecimalFormat("#.########");
        Wallet wallet = this.walletService.saveWalletByPartyId(c2cOrder.getPartyId());
        if (c2cOrder.getCoinAmount() > wallet.getMoney().doubleValue()) {
            throw new YamiShopBindException("余额不足");
        }
        double amountBefore = wallet.getMoney().doubleValue();
        double amountAfter = Double.valueOf(df.format(Arith.sub(wallet.getMoney().doubleValue(), c2cOrder.getCoinAmount()))).doubleValue();
        this.walletService.update(c2cOrder.getPartyId(), Arith.sub(0, c2cOrder.getCoinAmount()));
        this.save(c2cOrder);
        this.tipService.saveTip(c2cOrder.getUuid().toString(), TipConstants.BANK_CARD_ORDER);
        // 保存 资金日志
        MoneyLog moneylog = new MoneyLog();
        moneylog.setCategory(Constants.MONEYLOG_CATEGORY_BANK_CARD);
        moneylog.setAmountBefore(new BigDecimal(amountBefore));
        moneylog.setAmount(new BigDecimal(Arith.sub(0, c2cOrder.getCoinAmount())));
        moneylog.setAmountAfter(new BigDecimal(amountAfter));
        moneylog.setLog("银行卡提现,币种[" + c2cOrder.getSymbol() + "],订单号[" + c2cOrder.getOrderNo() + "]");
        moneylog.setUserId(c2cOrder.getPartyId());
        moneylog.setWalletType(c2cOrder.getSymbol());
        moneylog.setSymbol(c2cOrder.getSymbol());
 
        moneylog.setContentType(Constants.MONEYLOG_CONTENT_BANK_CARD_WITHDRAW);
        moneyLogService.save(moneylog);
        // 保存 充提记录
        WalletLog walletLog = new WalletLog();
        walletLog.setCategory(Constants.MONEYLOG_CATEGORY_BANK_CARD_WITHDRAW);
        walletLog.setPartyId(c2cOrder.getPartyId());
        walletLog.setOrderNo(c2cOrder.getOrderNo());
        walletLog.setStatus(Integer.valueOf(c2cOrder.getState()).intValue());
        walletLog.setAmount(c2cOrder.getCoinAmount());
        walletLog.setWallettype(c2cOrder.getSymbol());
        moneylog.setSymbol(c2cOrder.getSymbol());
 
        walletLogService.save(walletLog);
    }
 
    private void saveRecharge(C2cOrder c2cOrder, User party) {
        // 充值申请中的订单是否只能唯一:1唯一,2不限制
        double recharge_only_one = Double.valueOf(this.sysparaService.find("recharge_only_one").getSvalue());
        // 用户未完成USDT订单
        List<RechargeBlockchainOrder> orders = rechargeBlockchainOrderService.findByPartyIdAndSucceeded(party.getUserId(), 0);
        if (null != orders && 1 == recharge_only_one) {
            throw new YamiShopBindException("提交失败,当前有未处理USDT订单");
        }
        // 用户未结束银行卡订单数量
        Long nofinishOrderCount = this.getNofinishOrderCount(c2cOrder.getPartyId());
        if (null != nofinishOrderCount && 0 != nofinishOrderCount.longValue() && 1 == recharge_only_one) {
            throw new YamiShopBindException("提交失败,当前有未处理银行卡订单");
        }
        double recharge_limit_min = Double.valueOf(this.sysparaService.find("recharge_limit_min").getSvalue());
        double recharge_limit_max = Double.valueOf(this.sysparaService.find("recharge_limit_max").getSvalue());
        if (c2cOrder.getCoinAmount() < recharge_limit_min) {
            log.info(c2cOrder.getCoinAmount() + "====================================" + recharge_limit_min);
            throw new YamiShopBindException("充值数量不得小于最小限额");
        }
        if (c2cOrder.getCoinAmount() > recharge_limit_max) {
            throw new YamiShopBindException("充值数量不得大于最大限额");
        }
        this.save(c2cOrder);
        this.tipService.saveTip(c2cOrder.getUuid().toString(), TipConstants.BANK_CARD_ORDER);
    }
 
 
    public List<C2cOrder> getByPayId(String payId) {
       return list(Wrappers.<C2cOrder>query().lambda().eq(C2cOrder::getPaymentMethodId,payId).in(C2cOrder::getState,0,1));
    }
 
 
    /*
     * 获取 用户未结束订单数量
     */
    @Override
    public Long getNofinishOrderCount(String partyId) {
 
        Map<String, Long> ocMap = (Map<String, Long>) redisTemplate.opsForValue().get(RedisKeys.C2C_NOFINISH_ORDER_COUNT);
        if (null == ocMap) {
            return 0L;
        }
        Long count = ocMap.get(partyId);
        if (null == count) {
            return 0L;
        } else {
            return count;
        }
    }
 
}