xiaoyong931011
2022-11-18 df26ab5b4134cf7282cc8b348f79904d1d620f7c
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
package cc.mrbird.febs.dapp.service.impl;
 
import cc.mrbird.febs.common.configure.i18n.MessageSourceUtils;
import cc.mrbird.febs.common.contants.AppContants;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.dapp.chain.ChainEnum;
import cc.mrbird.febs.dapp.chain.ChainService;
import cc.mrbird.febs.dapp.chain.ContractChainService;
import cc.mrbird.febs.dapp.dto.*;
import cc.mrbird.febs.dapp.entity.*;
import cc.mrbird.febs.dapp.enumerate.DataDictionaryEnum;
import cc.mrbird.febs.dapp.mapper.*;
import cc.mrbird.febs.dapp.service.DappMemberService;
import cc.mrbird.febs.dapp.service.DappSystemService;
import cc.mrbird.febs.dapp.service.DappWalletService;
import cc.mrbird.febs.dapp.utils.BoxUtil;
import cc.mrbird.febs.dapp.vo.*;
import cc.mrbird.febs.rabbit.producer.UsdtUpdateProducer;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
/**
 * @author
 * @date 2022-03-18
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class DappWalletServiceImpl implements DappWalletService {
 
    private final DappMemberDao dappMemberDao;
    private final DappWalletMineDao dappWalletMineDao;
    private final DappWalletCoinDao dappWalletCoinDao;
    private final DappFundFlowDao dappFundFlowDao;
    private final DappAccountMoneyChangeDao dappAccountMoneyChangeDao;
    private final RedisUtils redisUtils;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final DappSystemService dappSystemService;
    private final DappNftActivationDao dappNftActivationDao;
    private final DappMemberService dappMemberService;
    private final MemberCoinWithdrawDao memberCoinWithdrawDao;
    private final IgtOnHookPlanOrderItemDao igtOnHookPlanOrderItemdao;
    private final DappBankDao dappBankDao;
    private final UsdtUpdateProducer usdtUpdateProducer;
 
    private final RedisTemplate<String, Object> redisTemplate;
 
    @Override
    public WalletInfoVo walletInfo() {
        DappMemberEntity member = LoginUserUtil.getAppUser();
 
        Map<String, BigDecimal> map = dappFundFlowDao.selectAmountTotalByType(member.getId());
        WalletInfoVo walletInfo = new WalletInfoVo();
        List<DappMemberEntity> direct = dappMemberDao.selectChildMemberDirectOrNot(member.getInviteId(), 1);
        List<DappMemberEntity> notDirect = dappMemberDao.selectChildMemberDirectOrNot(member.getInviteId(), 2);
        BigDecimal childHoldAmount = dappMemberDao.selectChildHoldAmount(member.getInviteId());
 
        DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(member.getId());
 
        walletInfo.setTotalChild(notDirect.size());
        walletInfo.setDirectCnt(direct.size());
        walletInfo.setTotalChildCoin(childHoldAmount);
        walletInfo.setTeamReward(map.get("teamReward"));
        walletInfo.setMiningAmount(map.get("mine"));
        walletInfo.setInviteId(member.getInviteId());
        walletInfo.setBoxCnt(walletCoin.getBoxCnt());
        return walletInfo;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void change(WalletOperateDto walletOperateDto) {
        DappMemberEntity member = LoginUserUtil.getAppUser();
 
        BigDecimal ethUsdtPrice = (BigDecimal) redisUtils.get(AppContants.REDIS_KEY_ETH_NEW_PRICE);
 
        DappWalletMineEntity walletMine = dappWalletMineDao.selectByMemberId(member.getId());
        if (walletOperateDto.getAmount().compareTo(walletMine.getAvailableAmount()) > 0) {
            throw new FebsException("可用金额不足");
        }
 
        DappFundFlowEntity fund = new DappFundFlowEntity(member.getId(), walletOperateDto.getAmount(), AppContants.MONEY_TYPE_CHANGE, null, null);
        dappFundFlowDao.insert(fund);
 
        BigDecimal preEthAmount = walletMine.getAvailableAmount();
 
        // TODO 并发加悲观锁
        // 更新eth金额
        walletMine.setAvailableAmount(walletMine.getAvailableAmount().subtract(walletOperateDto.getAmount()));
        dappWalletMineDao.updateById(walletMine);
 
        DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(member.getId());
        BigDecimal preUsdtAmount = walletCoin.getAvailableAmount();
 
        // 更新usdt金额
        BigDecimal usdt = walletOperateDto.getAmount().multiply(ethUsdtPrice);
        walletCoin.setAvailableAmount(walletCoin.getAvailableAmount().add(usdt));
        walletCoin.setTotalAmount(walletCoin.getTotalAmount().add(usdt));
        dappWalletCoinDao.updateById(walletCoin);
 
        DappAccountMoneyChangeEntity ethChange = new DappAccountMoneyChangeEntity(member.getId(), preEthAmount, walletOperateDto.getAmount(), walletMine.getAvailableAmount(), "ETH兑换USDT-ETH, 兑换价格为:" + ethUsdtPrice, AppContants.MONEY_TYPE_CHANGE);
        DappAccountMoneyChangeEntity usdtChange = new DappAccountMoneyChangeEntity(member.getId(), preUsdtAmount, usdt, walletCoin.getAvailableAmount(), "ETH兑换USDT-USDT, 兑换价格为:" + ethUsdtPrice, AppContants.MONEY_TYPE_CHANGE);
        dappAccountMoneyChangeDao.insert(ethChange);
        dappAccountMoneyChangeDao.insert(usdtChange);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void withdraw(WalletOperateDto walletOperateDto) {
        DappMemberEntity member = LoginUserUtil.getAppUser();
 
        List<DappFundFlowEntity> fundFlows = dappFundFlowDao.selectListForMemberAndDay(member.getId(), 2);
        if (CollUtil.isNotEmpty(fundFlows)) {
            throw new FebsException("一天只能提现一次");
        }
 
        DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(member.getId());
        if (walletOperateDto.getAmount().compareTo(walletCoin.getAvailableAmount()) > 0) {
            throw new FebsException("可用金额不足");
        }
 
        Integer fee = (Integer) redisUtils.get(AppContants.REDIS_KEY_CHANGE_FEE);
 
        //TODO 并发加悲观锁
        BigDecimal preAmount = walletCoin.getAvailableAmount();
        walletCoin.setAvailableAmount(walletCoin.getAvailableAmount().subtract(walletOperateDto.getAmount()));
        walletCoin.setFrozenAmount(walletCoin.getFrozenAmount().add(walletOperateDto.getAmount()));
        dappWalletCoinDao.updateById(walletCoin);
 
        DappFundFlowEntity fund = new DappFundFlowEntity(member.getId(), walletOperateDto.getAmount(), AppContants.MONEY_TYPE_WITHDRAWAL, 1, new BigDecimal(fee));
        dappFundFlowDao.insert(fund);
 
        DappAccountMoneyChangeEntity usdtChange = new DappAccountMoneyChangeEntity(member.getId(), preAmount, walletOperateDto.getAmount(), walletCoin.getAvailableAmount(), "USDT申请提现", AppContants.MONEY_TYPE_WITHDRAWAL);
        dappAccountMoneyChangeDao.insert(usdtChange);
    }
 
    @Override
    public List<DappFundFlowEntity> recordInPage(RecordInPageDto recordInPageDto) {
        Page<DappFundFlowEntity> page = new Page<>(recordInPageDto.getPageNum(), recordInPageDto.getPageSize());
 
        DappMemberEntity member = LoginUserUtil.getAppUser();
        DappFundFlowEntity dappFundFlowEntity = new DappFundFlowEntity();
        if (recordInPageDto.getType() != null && recordInPageDto.getType() != 0) {
            dappFundFlowEntity.setType(recordInPageDto.getType());
        }
        dappFundFlowEntity.setMemberId(member.getId());
        dappFundFlowEntity.setStatus(2);
 
        IPage<DappFundFlowEntity> records = dappFundFlowDao.selectInPage(page, dappFundFlowEntity);
        return records.getRecords();
    }
 
    @Override
    public IPage<DappFundFlowEntity> fundFlowInPage(DappFundFlowEntity dappFundFlowEntity, QueryRequest request) {
        Page<DappFundFlowEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        return dappFundFlowDao.selectInPage(page, dappFundFlowEntity);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void withdrawAgreeOrNot(Long id, int type) {
        DappFundFlowEntity fundFlow = dappFundFlowDao.selectById(id);
        if (fundFlow == null) {
            throw new FebsException("数据不存在");
        }
 
        DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(fundFlow.getMemberId());
        if (type == 1) {
            walletCoin.setFrozenAmount(walletCoin.getFrozenAmount().subtract(fundFlow.getAmount()));
            walletCoin.setTotalAmount(walletCoin.getTotalAmount().subtract(fundFlow.getAmount()));
            fundFlow.setStatus(DappFundFlowEntity.WITHDRAW_STATUS_AGREE);
        } else if (type == 2) {
            BigDecimal preAmount = walletCoin.getAvailableAmount();
 
            walletCoin.setFrozenAmount(walletCoin.getFrozenAmount().subtract(fundFlow.getAmount()));
            walletCoin.setAvailableAmount(walletCoin.getAvailableAmount().add(fundFlow.getAmount()));
 
            DappAccountMoneyChangeEntity accountMoneyChange = new DappAccountMoneyChangeEntity(walletCoin.getMemberId(), preAmount, fundFlow.getAmount(), walletCoin.getAvailableAmount(), "提现申请被驳回", 2);
            fundFlow.setStatus(DappFundFlowEntity.WITHDRAW_STATUS_DISAGREE);
 
            dappAccountMoneyChangeDao.insert(accountMoneyChange);
        } else {
            throw new FebsException("参数错误");
        }
 
        dappWalletCoinDao.updateById(walletCoin);
        dappFundFlowDao.updateById(fundFlow);
    }
 
    @Override
    public IPage<DappWalletCoinEntity> walletCoinInPage(DappWalletCoinEntity walletCoin, QueryRequest request) {
        Page<DappWalletCoinEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        return dappWalletCoinDao.selectInPage(walletCoin, page);
    }
 
    @Override
    public IPage<DappWalletMineEntity> walletMineInPage(DappWalletMineEntity walletMine, QueryRequest request) {
        Page<DappWalletMineEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        return dappWalletMineDao.selectInPage(walletMine, page);
    }
 
    @Override
    public IPage<DappAccountMoneyChangeEntity> accountMoneyChangeInPage(DappAccountMoneyChangeEntity change, QueryRequest request) {
        Page<DappAccountMoneyChangeEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        return dappAccountMoneyChangeDao.selectInPage(change, page);
    }
 
    @Override
    public Long transfer(TransferDto transferDto) {
        DappMemberEntity member = LoginUserUtil.getAppUser();
 
        if (member.getActiveStatus() == 2) {
            throw new FebsException("请联系邀请人转币激活");
        }
 
        // 每日出U剩余量(卖币)
        BigDecimal usdtRemain = (BigDecimal) redisUtils.get(AppContants.REDIS_KEY_USDT_OUT_LIMIT_REMAIN);
        // 用户24小时可出售量
        BigDecimal saleCoinRemain = (BigDecimal) redisUtils.get(AppContants.REDIS_KEY_COIN_REMAIN + member.getAddress());
 
        // 用户24小时可购买USDT
        BigDecimal buyUsdtMax = (BigDecimal) redisUtils.get(AppContants.REDIS_KEY_IDO_USDT_MAX_BUY_DAILY + member.getAddress());
 
        BigDecimal buyCoinRemain = (BigDecimal) redisUtils.get(AppContants.REDIS_KEY_TRANSFER_POOL_VOL_REMAIN);
        // 铸池中的币的剩余量
        BigDecimal makeCoinRemain = (BigDecimal) redisUtils.get(AppContants.REDIS_KEY_MAKE_POOL_CNT);
 
        DateTime tomorrow = DateUtil.beginOfDay(DateUtil.tomorrow());
        long time = DateUtil.between(new Date(), tomorrow, DateUnit.SECOND, true);
 
        String hasStart = redisUtils.getString(AppContants.SYSTEM_START_FLAG);
        if (transferDto.getId() == null) {
            if (DappFundFlowEntity.TYPE_SALE == transferDto.getType()) {
                if (!"start".equals(hasStart)) {
                    throw new FebsException(MessageSourceUtils.getString("transfer_msg_001"));
                }
 
//                if (transferDto.getAmount().multiply(transferDto.getPrice()).compareTo(usdtRemain) > 0) {
//                    throw new FebsException(MessageSourceUtils.getString("transfer_msg_002"));
//                }
//
//                if (transferDto.getAmount().compareTo(saleCoinRemain) > 0) {
//                    throw new FebsException(MessageSourceUtils.getString("transfer_msg_003"));
//                }
//
//                usdtRemain = usdtRemain.subtract(transferDto.getAmount().multiply(transferDto.getPrice()));
//                saleCoinRemain = saleCoinRemain.subtract(transferDto.getAmount());
//
//                // 修改当日U剩余量
//                redisUtils.set(AppContants.REDIS_KEY_USDT_OUT_LIMIT_REMAIN, usdtRemain);
//                // 修改用户24小时可售量
//                redisUtils.set(AppContants.REDIS_KEY_COIN_REMAIN + member.getAddress(), saleCoinRemain, time);
            } else if (DappFundFlowEntity.TYPE_BUY == transferDto.getType()) {
                // 购买时,前端传来的amount是USDT,卖出amount是TFC
                BigDecimal usdtAmount = transferDto.getAmount();
                BigDecimal coinAmount = transferDto.getAmount().divide(transferDto.getPrice(), 6, RoundingMode.HALF_UP);
                transferDto.setAmount(coinAmount);
 
                if ("start".equals(hasStart)) {
                    if (transferDto.getAmount().compareTo(buyCoinRemain) > 0) {
                        throw new FebsException(MessageSourceUtils.getString("transfer_msg_004"));
                    }
                    buyCoinRemain = buyCoinRemain.subtract(transferDto.getAmount());
 
                    // 修改当日可购买量
                    redisUtils.set(AppContants.REDIS_KEY_TRANSFER_POOL_VOL_REMAIN, buyCoinRemain);
                    // 如果系统还没有启动,则判断铸池中的剩余量
                } else {
                    // 最少购买
//                    DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(AppContants.DIC_TYPE_SYSTEM_SETTING, AppContants.DIC_VALUE_MAKER_MIN_LIMIT);
//                    if (transferDto.getAmount().compareTo(new BigDecimal(dic.getValue())) < 0) {
//                        throw new FebsException(MessageSourceUtils.getString("transfer_msg_005"));
//                    }
 
                    if (buyUsdtMax.compareTo(usdtAmount) < 0) {
                        throw new FebsException(MessageSourceUtils.getString("transfer_msg_007"));
                    }
 
                    if (transferDto.getAmount().compareTo(makeCoinRemain) > 0) {
                        throw new FebsException(MessageSourceUtils.getString("transfer_msg_006"));
                    }
                    makeCoinRemain = makeCoinRemain.subtract(transferDto.getAmount());
                    buyUsdtMax = buyUsdtMax.subtract(usdtAmount);
 
 
                    // 修改每日最大购买USDT量
                    redisUtils.set(AppContants.REDIS_KEY_IDO_USDT_MAX_BUY_DAILY + member.getAddress(), buyUsdtMax, time);
 
                    // 修改铸池量
                    redisUtils.set(AppContants.REDIS_KEY_MAKE_POOL_CNT, makeCoinRemain);
                }
            }
 
            DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), transferDto.getAmount(), transferDto.getType(), 1, transferDto.getFee(), transferDto.getTxHash());
            fundFlow.setNewestPrice(transferDto.getPrice());
            dappFundFlowDao.insert(fundFlow);
            return fundFlow.getId();
        }
 
        if ("success".equals(transferDto.getFlag())) {
            DappFundFlowEntity flow = dappFundFlowDao.selectById(transferDto.getId());
 
            flow.setFromHash(transferDto.getTxHash());
            dappFundFlowDao.updateById(flow);
        } else {
            DappFundFlowEntity flow = dappFundFlowDao.selectById(transferDto.getId());
            if (flow.getStatus() == 1) {
                if (DappFundFlowEntity.TYPE_BUY == flow.getType()) {
                    // 购买时,前端传来的amount是USDT,卖出amount是TFC
                    BigDecimal usdtAmount = transferDto.getAmount();
                    BigDecimal coinAmount = transferDto.getAmount().divide(flow.getNewestPrice(), 6, RoundingMode.HALF_UP);
                    transferDto.setAmount(coinAmount);
 
                    if ("start".equals(hasStart)) {
                        buyCoinRemain = buyCoinRemain.add(flow.getAmount());
 
                        // 修改当日可购买量
                        redisUtils.set(AppContants.REDIS_KEY_TRANSFER_POOL_VOL_REMAIN, buyCoinRemain);
                    } else {
                        makeCoinRemain = makeCoinRemain.add(flow.getAmount());
                        buyUsdtMax = buyUsdtMax.add(usdtAmount);
 
                        // 修改铸池量
                        redisUtils.set(AppContants.REDIS_KEY_MAKE_POOL_CNT, makeCoinRemain);
                        redisUtils.set(AppContants.REDIS_KEY_IDO_USDT_MAX_BUY_DAILY + member.getAddress(), buyUsdtMax, time);
                    }
                } else {
                    usdtRemain = usdtRemain.add(transferDto.getAmount().multiply(transferDto.getPrice()));
                    saleCoinRemain = saleCoinRemain.add(transferDto.getAmount());
 
                    // 修改当日U剩余量
                    redisUtils.set(AppContants.REDIS_KEY_USDT_OUT_LIMIT_REMAIN, usdtRemain);
                    // 修改用户24小时可售量
                    redisUtils.set(AppContants.REDIS_KEY_COIN_REMAIN + member.getAddress(), saleCoinRemain, time);
                }
                dappFundFlowDao.deleteById(transferDto.getId());
            }
        }
        return null;
    }
 
    @Override
    public Map<String, BigDecimal> calPrice(PriceDto priceDto) {
        DappMemberEntity member = LoginUserUtil.getAppUser();
 
        if (!dappSystemService.systemHasStart()) {
            HashMap<String, BigDecimal> map = new HashMap<>();
            map.put("x", new BigDecimal("0.05"));
            map.put("y", new BigDecimal("0.05"));
            return map;
        }
 
        ContractChainService tfcInstance = ChainService.getInstance(ChainEnum.BSC_TFC.name());
        // u剩余数量
        BigDecimal sourceU = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOf(ChainEnum.BSC_USDT_SOURCE.getAddress());
        // 源池代币剩余数量
        BigDecimal sourceCoin = tfcInstance.balanceOf(ChainEnum.BSC_USDT_SOURCE.getAddress());
        // 用户卖出数量
        BigDecimal coin = priceDto.getAmount();
        BigDecimal x = sourceU.divide(sourceCoin, tfcInstance.decimals(), RoundingMode.HALF_UP);
        BigDecimal y = sourceU.divide(sourceCoin.add(coin), tfcInstance.decimals(), RoundingMode.HALF_UP);
 
//        log.info("购买价格:{}, 出卖价格:{}", x, y);
        HashMap<String, BigDecimal> map = new HashMap<>();
        map.put("x", x);
        map.put("y", y);
        return map;
    }
 
    @Override
    public ActiveNftListVo boxSurprise() {
        DappMemberEntity member = LoginUserUtil.getAppUser();
 
        DappWalletCoinEntity walletCoin = dappWalletCoinDao.selectByMemberId(member.getId());
        if (walletCoin.getBoxCnt() < 1) {
            throw new FebsException(MessageSourceUtils.getString("box_surprise_001"));
        }
 
        walletCoin.setBoxCnt(walletCoin.getBoxCnt() - 1);
        dappWalletCoinDao.updateById(walletCoin);
 
        BoxUtil.Box box = BoxUtil.openBox();
 
        Date time = new Date();
        Date expire = DateUtil.offset(time, DateField.HOUR, 48);
        DappNftActivation nftActivation = new DappNftActivation();
        nftActivation.setMemberId(member.getId());
        nftActivation.setCount(box.getIndex());
        nftActivation.setOpenTime(time);
        nftActivation.setExpireTime(expire);
        nftActivation.setStatus(1);
        dappNftActivationDao.insert(nftActivation);
 
        ActiveNftListVo nft = new ActiveNftListVo();
        nft.setCount(box.getIndex());
        nft.setId(nftActivation.getId());
        nft.setRemain(DateUtil.between(time, expire, DateUnit.SECOND));
        return nft;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void activeNft(ActiveDto activeDto) {
        DappMemberEntity member = LoginUserUtil.getAppUser();
 
        DappNftActivation nftActive = dappNftActivationDao.selectById(activeDto.getId());
        if (nftActive == null) {
            throw new FebsException(MessageSourceUtils.getString("nft_active_003"));
        }
 
        if (nftActive.getCount() < activeDto.getCount()) {
            throw new FebsException(MessageSourceUtils.getString("nft_active_004"));
        }
 
        if (DateUtil.between(new Date(), nftActive.getExpireTime(), DateUnit.SECOND) < 0) {
            throw new FebsException(MessageSourceUtils.getString("nft_active_005"));
        }
 
        if (nftActive.getStatus() != 1) {
            throw new FebsException(MessageSourceUtils.getString("nft_active_001"));
        }
 
        PriceDto priceDto = new PriceDto();
        priceDto.setAmount(BigDecimal.ZERO);
        Map<String, BigDecimal> price = calPrice(priceDto);
        DappFundFlowEntity fundFlow = new DappFundFlowEntity(member.getId(), new BigDecimal(activeDto.getCount()), 8, 1, BigDecimal.ZERO, activeDto.getTxHash());
 
        fundFlow.setTargetAmount(AppContants.NFT_ACTIVE_PRICE.multiply(BigDecimal.valueOf(nftActive.getCount())).multiply(price.get("x")));
        fundFlow.setNewestPrice(price.get("x"));
        dappFundFlowDao.insert(fundFlow);
 
        int count = nftActive.getCount() - activeDto.getCount();
        if (count == 0) {
            nftActive.setStatus(3);
        }
 
        nftActive.setCount(count);
        nftActive.setHash(activeDto.getTxHash());
        dappNftActivationDao.updateById(nftActive);
    }
 
    @Override
    public List<ActiveNftListVo> findUnActiveNftList() {
        DappMemberEntity member = LoginUserUtil.getAppUser();
        List<ActiveNftListVo> list = new ArrayList<>();
 
        UpdateWrapper<DappNftActivation> query = new UpdateWrapper<>();
        query.eq("status", 1);
        query.eq("member_id", member.getId());
        query.ge("expire_time", new Date());
        List<DappNftActivation> nftActivations = dappNftActivationDao.selectList(query);
 
        if (CollUtil.isEmpty(nftActivations)) {
            return list;
        }
 
        Date now = new Date();
        nftActivations.forEach(nft -> {
            ActiveNftListVo nftVo = new ActiveNftListVo();
            nftVo.setId(nft.getId());
            nftVo.setRemain(DateUtil.between(now, nft.getExpireTime(), DateUnit.SECOND, false));
            nftVo.setCount(nft.getCount());
            list.add(nftVo);
        });
 
        return list;
    }
 
    @Override
    public FebsResponse findMemberWalletCoin() {
        DappMemberEntity member = LoginUserUtil.getAppUser();
        Long memberId = member.getId();
 
        DappMemberEntity dappMemberEntity = dappMemberDao.selectById(memberId);
        DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(memberId);
        ApiMemberWalletCoinVo apiMemberWalletCoinVo = new ApiMemberWalletCoinVo();
        if(ObjectUtil.isNotEmpty(dappWalletCoinEntity)){
            apiMemberWalletCoinVo.setTotalAmount(dappWalletCoinEntity.getTotalAmount().setScale(4,BigDecimal.ROUND_DOWN));
            apiMemberWalletCoinVo.setFrozenAmount(dappWalletCoinEntity.getFrozenAmount().setScale(4,BigDecimal.ROUND_DOWN));
            apiMemberWalletCoinVo.setAvailableAmount(dappWalletCoinEntity.getAvailableAmount().setScale(4,BigDecimal.ROUND_DOWN));
        }
        return new FebsResponse().success().data(apiMemberWalletCoinVo);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse transferInside(ApiTransferInsideDto apiTransferInsideDto) {
        //判断入参
        BigDecimal balance = apiTransferInsideDto.getBalance() == null ? BigDecimal.ZERO : apiTransferInsideDto.getBalance().setScale(4,BigDecimal.ROUND_DOWN);
        if(BigDecimal.ZERO.compareTo(balance) >= 0){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("balance_err_001"));
        }
//        if(ObjectUtil.isEmpty(apiTransferInsideDto.getInviteId())){
//            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_001"));
//        }
        if(ObjectUtil.isEmpty(apiTransferInsideDto.getUsername())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0011"));
        }
        if(ObjectUtil.isEmpty(apiTransferInsideDto.getTransferCode())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_006"));
        }
        //每日挂机时间段内禁止内转
        DataDictionaryCustom startTimeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.START_TIME.getType(), DataDictionaryEnum.START_TIME.getCode());
        DataDictionaryCustom endTimeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.END_TIME.getType(), DataDictionaryEnum.END_TIME.getCode());
        //获取时间对应的秒数
        Integer dateNow = DateUtil.timeToSecond(DateUtil.formatTime(DateUtil.date()));
        Integer startTime = DateUtil.timeToSecond(startTimeDic.getValue());
        Integer endTime = DateUtil.timeToSecond(endTimeDic.getValue());
        if(startTime <= dateNow && endTime >= dateNow){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0012"));
        }
 
 
//        DappMemberEntity dappMemberEntityOut = LoginUserUtil.getAppUser();
        Long memberIdOut = LoginUserUtil.getAppUser().getId();
        //判断账户是否限制
 
 
        DappMemberEntity dappMemberEntityOut = dappMemberDao.selectById(memberIdOut);
        Integer withdrawAble = dappMemberEntityOut.getWithdrawAble();
        if(2 == withdrawAble){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0017"));
        }
        //判断双方是否是会员
        if(ObjectUtil.isEmpty(dappMemberEntityOut.getInviteId())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_002"));
        }
        String inviteIdOut = dappMemberEntityOut.getInviteId();
        Boolean isMemberOut = dappMemberService.isMember(inviteIdOut);
        if(!isMemberOut){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_002"));
        }
        String username = apiTransferInsideDto.getUsername();
        DappMemberEntity memberEntityIn = dappMemberDao.selectMemberInfoByUsername(username);
        if (ObjectUtil.isEmpty(memberEntityIn)) {
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_003"));
        }
 
        String inviteIdIn = memberEntityIn.getInviteId();
        Boolean isMemberIn = dappMemberService.isMember(inviteIdIn);
//        Boolean isMemberIn = dappMemberService.isMember(apiTransferInsideDto.getInviteId());
        if(!isMemberIn){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_003"));
        }
        //验证资金密码
 
        //RSA解密
        RSA rsa = new RSA(AppContants.PRIVATE_KEY, null);
        String transferPassword = apiTransferInsideDto.getTransferCode();
        transferPassword = rsa.decryptStr(transferPassword, KeyType.PrivateKey);
        Boolean aBoolean = dappMemberService.validateTransferCode(transferPassword, dappMemberEntityOut.getId());
        if(!aBoolean){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_006"));
        }
        //判断内部转账规则
 
 
        DataDictionaryCustom accountRelationDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.ACCOUNT_RELATION.getType(), DataDictionaryEnum.ACCOUNT_RELATION.getCode());
        Integer accountRelation = Integer.parseInt(accountRelationDic.getValue());
        if(1 == accountRelation){
            Boolean relationShip = dappMemberService.isRelationShip(inviteIdOut, inviteIdIn);
            if(!relationShip){
                return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_005"));
            }
        }
        //查询转出会员
        //转出会员当前余额要大于等于划转金额
        DappWalletCoinEntity dappWalletCoinEntityOut = dappWalletCoinDao.selectByMemberId(memberIdOut);
        BigDecimal availableAmountOut = dappWalletCoinEntityOut.getAvailableAmount().setScale(4,BigDecimal.ROUND_DOWN);
        if(availableAmountOut.compareTo(balance) < 0){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("balance_err_002"));
        }
        /**
         * 提取金额小于收益,则不受限制
         * 否则,计算收益占本金的比例。符合条件允许提现
         */
        //获取用户的总收益
        BigDecimal totalProfitOut = igtOnHookPlanOrderItemdao.selectTotalProfitByMemberIdAndStateAndIsgoal(memberIdOut,2);
        if(balance.compareTo(totalProfitOut) > 0){
            BigDecimal totalAmount = dappWalletCoinEntityOut.getTotalAmount();
            //用户总收益率
            BigDecimal divide = totalProfitOut.divide(totalAmount,4,BigDecimal.ROUND_DOWN);
            //提现条件收益率
            DataDictionaryCustom outAccountProfitDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.OUT_ACCOUNT_PROFIT.getType(), DataDictionaryEnum.OUT_ACCOUNT_PROFIT.getCode());
            BigDecimal outAccountProfit = outAccountProfitDic.getValue() == null ? new BigDecimal("0.3") : new BigDecimal(outAccountProfitDic.getValue()).multiply(new BigDecimal(0.01));
            if(divide.compareTo(outAccountProfit) < 0){
                return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_004"));
            }
        }
        //提现次数
        DataDictionaryCustom withdrawTimesDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.WITHDRAW_TIMES.getType(), DataDictionaryEnum.WITHDRAW_TIMES.getCode());
        Integer withdrawTimes = Integer.parseInt(withdrawTimesDic.getValue());
        Integer withdrawTimesReal = memberCoinWithdrawDao.selectByMemberIdAndCreateTime(memberIdOut,DateUtil.format(DateUtil.date(),"yyyy-MM-dd"),"Y");
        if(withdrawTimesReal >= withdrawTimes){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0016"));
        }
        //转出账户,余额减少,冻结增加
        Integer countOut = dappWalletCoinDao.addFrozenAndDelAvailableById(dappWalletCoinEntityOut.getId(), balance);
        if(1 != countOut){
            throw new FebsException(MessageSourceUtils.getString("balance_err_002"));
        }
        //转出账户生成一条内部转账记录
        MemberCoinWithdrawEntity memberCoinWithdrawEntity = new MemberCoinWithdrawEntity();
        memberCoinWithdrawEntity.setAddress(inviteIdIn);
        memberCoinWithdrawEntity.setAmount(balance);
        memberCoinWithdrawEntity.setFeeAmount(BigDecimal.ZERO);
        memberCoinWithdrawEntity.setSymbol("USDT");
        memberCoinWithdrawEntity.setMemberId(memberIdOut);
        memberCoinWithdrawEntity.setStatus(MemberCoinWithdrawEntity.STATUS_YES);
        memberCoinWithdrawEntity.setIsInside(MemberCoinWithdrawEntity.ISINSIDE_YES);
        memberCoinWithdrawDao.insert(memberCoinWithdrawEntity);
 
//        usdtUpdateProducer.sendMemberCoinInside(memberCoinWithdrawEntity.getId());
        //转出账户,总额减少,冻结减少
        dappWalletCoinDao.delTotalAndDelFrozenById(dappWalletCoinEntityOut.getId(),balance);
 
        String isInside = memberCoinWithdrawEntity.getIsInside();
        String content = "";
        Integer type = 0;
        if(MemberCoinWithdrawEntity.ISINSIDE_NO.equals(isInside)){
            content = "提现";
            type = 2;
        }else{
            content = "转账";
            type = 4;
        }
        //转出账户生成一条账户资金变化记录
        DappAccountMoneyChangeEntity dappAccountMoneyChangeEntityOut = new DappAccountMoneyChangeEntity(memberIdOut,
                dappWalletCoinEntityOut.getTotalAmount().setScale(4,BigDecimal.ROUND_DOWN),
                balance.negate(),
                dappWalletCoinEntityOut.getTotalAmount().setScale(4,BigDecimal.ROUND_DOWN).subtract(balance),
                content,
                type);
        dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntityOut);
 
        //转入账户,总额增加,余额增加
        //转账
        if(MemberCoinWithdrawEntity.ISINSIDE_YES.equals(isInside)){
            String addressIn = memberCoinWithdrawEntity.getAddress();
            DappMemberEntity dappMemberEntityIn = dappMemberDao.selectMemberInfoByInviteId(addressIn);
            if(ObjectUtil.isEmpty(dappMemberEntityIn)){
                throw new FebsException(MessageSourceUtils.getString("Operation_002"));
            }
            DappWalletCoinEntity dappWalletCoinEntityIn = dappWalletCoinDao.selectByMemberId(dappMemberEntityIn.getId());
            Integer countIn = dappWalletCoinDao.addTotalAndaddAvailableById(dappWalletCoinEntityIn.getId(), memberCoinWithdrawEntity.getAmount());
            if(1 != countIn){
                throw new FebsException(MessageSourceUtils.getString("Operation_002"));
            }
            //生成流水记录
            DappAccountMoneyChangeEntity dappAccountMoneyChangeEntityIn = new DappAccountMoneyChangeEntity(dappMemberEntityIn.getId(),
                    dappWalletCoinEntityIn.getTotalAmount().setScale(4,BigDecimal.ROUND_DOWN),
                    balance,
                    dappWalletCoinEntityIn.getTotalAmount().setScale(4,BigDecimal.ROUND_DOWN).add(balance),
                    "转账",
                    4);
            dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntityIn);
        }
        return new FebsResponse().success().message(MessageSourceUtils.getString("Operation_001"));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse transferPasswordSet(ApiTransferPasswordDto apiTransferPasswordDto) {
        if(ObjectUtil.isEmpty(apiTransferPasswordDto.getOldTransferPassword())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_007"));
        }
        if(ObjectUtil.isEmpty(apiTransferPasswordDto.getNewTransferPassword())
                || ObjectUtil.isEmpty(apiTransferPasswordDto.getNewTransferPasswordAgain())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_008"));
        }
 
        //RSA解密
        RSA rsa = new RSA(AppContants.PRIVATE_KEY, null);
        String newTransferPassword = apiTransferPasswordDto.getNewTransferPassword();
        newTransferPassword = rsa.decryptStr(newTransferPassword, KeyType.PrivateKey);
        String newTransferPasswordAgain = apiTransferPasswordDto.getNewTransferPasswordAgain();
        newTransferPasswordAgain = rsa.decryptStr(newTransferPasswordAgain, KeyType.PrivateKey);
        if(!newTransferPassword.equals(newTransferPasswordAgain)){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_009"));
        }
 
        DappMemberEntity dappMemberEntity = LoginUserUtil.getAppUser();
        Long memberId = dappMemberEntity.getId();
        DappMemberEntity memberEntity = dappMemberDao.selectById(memberId);
        memberEntity.setTransferCode(SecureUtil.md5(newTransferPassword));
        dappMemberDao.updateById(memberEntity);
 
        return new FebsResponse().success().message(MessageSourceUtils.getString("Operation_001"));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse transferOutside(ApiTransferOutsideDto apiTransferOutsideDto) {
        DappMemberEntity dappMemberEntity = LoginUserUtil.getAppUser();
        Long memberId = dappMemberEntity.getId();
 
        if(ObjectUtil.isEmpty(apiTransferOutsideDto.getAddress())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0010"));
        }
        String address = apiTransferOutsideDto.getAddress();
 
        if(ObjectUtil.isEmpty(apiTransferOutsideDto.getBalance())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("balance_err_001"));
        }
 
        BigDecimal balance = apiTransferOutsideDto.getBalance();
        if(BigDecimal.ZERO.compareTo(balance) >= 0){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("balance_err_001"));
        }
        DataDictionaryCustom withDrawDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.WITHDRAW_MAX.getType()
                , DataDictionaryEnum.WITHDRAW_MAX.getCode());
        BigDecimal withDrawMax = withDrawDic.getValue() == null ? new BigDecimal("100") : new BigDecimal(withDrawDic.getValue());
        if(withDrawMax.compareTo(balance) > 0){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("balance_err_003"));
        }
        DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(memberId);
        BigDecimal availableAmount = dappWalletCoinEntity.getAvailableAmount();
        if(balance.compareTo(availableAmount) > 0){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("balance_err_002"));
        }
 
        DappMemberEntity dappMemberEntityOut = dappMemberDao.selectById(memberId);
        //判断账户是否限制
        Integer withdrawAble = dappMemberEntityOut.getWithdrawAble();
        if(2 == withdrawAble){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0017"));
        }
        //验证资金密码
        //RSA解密
        RSA rsa = new RSA(AppContants.PRIVATE_KEY, null);
        String transferPassword = apiTransferOutsideDto.getTransferCode();
        transferPassword = rsa.decryptStr(transferPassword, KeyType.PrivateKey);
        Boolean aBoolean = dappMemberService.validateTransferCode(transferPassword, memberId);
        if(!aBoolean){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_006"));
        }
 
        /**
         * 提取金额小于收益,则不受限制
         * 否则,计算收益占本金的比例。符合条件允许提现
         */
        //获取用户的总收益
        BigDecimal totalProfit = igtOnHookPlanOrderItemdao.selectTotalProfitByMemberIdAndStateAndIsgoal(memberId,2);
        if(balance.compareTo(totalProfit) >= 0){
            BigDecimal totalAmount = dappWalletCoinEntity.getTotalAmount();
            //用户总收益率
            BigDecimal divide = totalProfit.divide(totalAmount,4,BigDecimal.ROUND_DOWN);
            //提现条件收益率
            DataDictionaryCustom outAccountProfitDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.OUT_ACCOUNT_PROFIT.getType(), DataDictionaryEnum.OUT_ACCOUNT_PROFIT.getCode());
            BigDecimal outAccountProfit = outAccountProfitDic.getValue() == null ? new BigDecimal("0.3") : new BigDecimal(outAccountProfitDic.getValue()).multiply(new BigDecimal(0.01));
            if(divide.compareTo(outAccountProfit) < 0){
                return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_004"));
            }
        }
        //(2)每24小时只能提现一次
        // 提现次数
        DataDictionaryCustom withdrawOutTimesDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.WITHDRAW_OUT_TIMES.getType()
                , DataDictionaryEnum.WITHDRAW_OUT_TIMES.getCode());
        String withdrawOutTimesStr = withdrawOutTimesDic.getValue() == null ? "1" : withdrawOutTimesDic.getValue();
        int withdrawOutTimes = Integer.parseInt(withdrawOutTimesStr);
        Integer withdrawTimesReal = memberCoinWithdrawDao.selectByMemberIdAndCreateTime(memberId,DateUtil.format(DateUtil.date(),"yyyy-MM-dd"),"N");
        if(withdrawOutTimes <= withdrawTimesReal){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0014"));
        }
 
        //余额减少冻结增加
        Integer count = dappWalletCoinDao.addFrozenAndDelAvailableById(dappWalletCoinEntity.getId(), balance);
        if(1 != count){
            throw new FebsException(MessageSourceUtils.getString("balance_err_002"));
        }
        DataDictionaryCustom serviceFeeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.SERVICE_FEE.getType(), DataDictionaryEnum.SERVICE_FEE.getCode());
        BigDecimal serviceFee = new BigDecimal(serviceFeeDic.getValue());
 
        //转出账户生成一条记录
        MemberCoinWithdrawEntity memberCoinWithdrawEntity = new MemberCoinWithdrawEntity();
        memberCoinWithdrawEntity.setAddress(address);
        memberCoinWithdrawEntity.setAmount(balance);
        memberCoinWithdrawEntity.setFeeAmount(serviceFee);
        if(1 == apiTransferOutsideDto.getType()){
            DappBank dappBank = dappBankDao.selectBankListByMemberIdAndBankNo(memberId, address);
            memberCoinWithdrawEntity.setTag(dappBank.getMemberName()+"-银行卡");
            memberCoinWithdrawEntity.setSymbol("$");
        }else{
            memberCoinWithdrawEntity.setTag("钱包");
            memberCoinWithdrawEntity.setSymbol("USDT");
        }
        memberCoinWithdrawEntity.setMemberId(memberId);
        memberCoinWithdrawEntity.setStatus(MemberCoinWithdrawEntity.STATUS_DOING);
        memberCoinWithdrawEntity.setIsInside(MemberCoinWithdrawEntity.ISINSIDE_NO);
        memberCoinWithdrawDao.insert(memberCoinWithdrawEntity);
        return new FebsResponse().success().message(MessageSourceUtils.getString("Operation_001"));
    }
 
    @Override
    public FebsResponse transferPassword(ApiTransferPasswordDto apiTransferPasswordDto) {
        DappMemberEntity dappMemberEntity = LoginUserUtil.getAppUser();
        Long memberId = dappMemberEntity.getId();
 
        if(ObjectUtil.isEmpty(apiTransferPasswordDto.getRealname())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0013"));
        }
        if(ObjectUtil.isEmpty(apiTransferPasswordDto.getPhone())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0013"));
        }
        if(ObjectUtil.isEmpty(apiTransferPasswordDto.getEmail())
                && ObjectUtil.isEmpty(apiTransferPasswordDto.getWahtsApp())
                && ObjectUtil.isEmpty(apiTransferPasswordDto.getTelegram())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_0013"));
        }
        if(ObjectUtil.isEmpty(apiTransferPasswordDto.getNewTransferPassword())
                || ObjectUtil.isEmpty(apiTransferPasswordDto.getNewTransferPasswordAgain())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_008"));
        }
 
        //RSA解密
        RSA rsa = new RSA(AppContants.PRIVATE_KEY, null);
        String newTransferPassword = apiTransferPasswordDto.getNewTransferPassword();
        newTransferPassword = rsa.decryptStr(newTransferPassword, KeyType.PrivateKey);
        String newTransferPasswordAgain = apiTransferPasswordDto.getNewTransferPasswordAgain();
        newTransferPasswordAgain = rsa.decryptStr(newTransferPasswordAgain, KeyType.PrivateKey);
        if(!newTransferPassword.equals(newTransferPasswordAgain)){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_009"));
        }
        DappMemberEntity memberEntity = dappMemberDao.selectById(memberId);
 
        String realname = apiTransferPasswordDto.getRealname();
        String phone = apiTransferPasswordDto.getPhone();
        String email = apiTransferPasswordDto.getEmail();
        String wahtsApp = apiTransferPasswordDto.getWahtsApp();
        String telegram = apiTransferPasswordDto.getTelegram();
        memberEntity.setTransferCode(SecureUtil.md5(newTransferPassword));
        memberEntity.setRealname(realname);
        memberEntity.setPhone(phone);
        memberEntity.setEmail(email);
        memberEntity.setWahtsApp(wahtsApp);
        memberEntity.setTelegram(telegram);
        dappMemberDao.updateById(memberEntity);
 
        return new FebsResponse().success().message(MessageSourceUtils.getString("Operation_001"));
    }
 
    @Override
    public FebsResponse updatePassword(ApiUpdatePasswordDto apiUpdatePasswordDto) {
        //验证验证码是否正确
        // 根据前端传回的token在redis中找对应的value
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        if(ObjectUtil.isEmpty(apiUpdatePasswordDto.getCodeToken()) || ObjectUtil.isEmpty(apiUpdatePasswordDto.getCodeValue())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("verification_code_err_001"));
        }
        String codeToken = apiUpdatePasswordDto.getCodeToken();
        String codeValue = apiUpdatePasswordDto.getCodeValue();
        if (redisTemplate.hasKey(codeToken)) {
            //验证通过, 删除对应的key
            if (valueOperations.get(codeToken).equals(codeValue)) {
                redisTemplate.delete(codeToken);
            } else {
                return new FebsResponse().fail().message(MessageSourceUtils.getString("verification_code_err_002"));
            }
        } else {
            return new FebsResponse().fail().message(MessageSourceUtils.getString("verification_code_err_003"));
        }
        String username = apiUpdatePasswordDto.getUsername();
        DappMemberEntity memberEntity = dappMemberDao.selectMemberInfoByUsername(username);
        if (ObjectUtil.isEmpty(memberEntity)) {
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_003"));
        }
 
        String inviteIdIn = memberEntity.getInviteId();
        Boolean isMemberIn = dappMemberService.isMember(inviteIdIn);
        if(!isMemberIn){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_003"));
        }
        //验证资金密码
 
        //RSA解密
        String transferCode = apiUpdatePasswordDto.getTransferCode();
        RSA rsa = new RSA(AppContants.PRIVATE_KEY, null);
        transferCode = rsa.decryptStr(transferCode, KeyType.PrivateKey);
        Boolean aBoolean = dappMemberService.validateTransferCode(transferCode, memberEntity.getId());
        if(!aBoolean){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_006"));
        }
 
        if(ObjectUtil.isEmpty(apiUpdatePasswordDto.getNewTransferPassword())
                || ObjectUtil.isEmpty(apiUpdatePasswordDto.getNewTransferPasswordAgain())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_008"));
        }
 
        String newTransferPassword = apiUpdatePasswordDto.getNewTransferPassword();
        newTransferPassword = rsa.decryptStr(newTransferPassword, KeyType.PrivateKey);
        String newTransferPasswordAgain = apiUpdatePasswordDto.getNewTransferPasswordAgain();
        newTransferPasswordAgain = rsa.decryptStr(newTransferPasswordAgain, KeyType.PrivateKey);
        if(!newTransferPassword.equals(newTransferPasswordAgain)){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_009"));
        }
 
        memberEntity.setPassword(SecureUtil.md5(newTransferPassword));
        dappMemberDao.updateById(memberEntity);
 
        String redisKey = AppContants.REDIS_KEY_SIGN + memberEntity.getId();
        redisUtils.del(redisKey);
 
        return new FebsResponse().success().message(MessageSourceUtils.getString("Operation_001"));
    }
 
    @Override
    public FebsResponse rebitTest() {
        return null;
    }
 
    @Override
    public FebsResponse resetPassword(ApiResetPasswordDto apiResetPasswordDto) {
        DappMemberEntity dappMemberEntity = LoginUserUtil.getAppUser();
        Long memberId = dappMemberEntity.getId();
 
        if(ObjectUtil.isEmpty(apiResetPasswordDto.getNewPassword())
                || ObjectUtil.isEmpty(apiResetPasswordDto.getNewPasswordAgain())){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_008"));
        }
 
        //RSA解密
        RSA rsa = new RSA(AppContants.PRIVATE_KEY, null);
        String newPassword = apiResetPasswordDto.getNewPassword();
        newPassword = rsa.decryptStr(newPassword, KeyType.PrivateKey);
        String newPasswordAgain = apiResetPasswordDto.getNewPasswordAgain();
        newPasswordAgain = rsa.decryptStr(newPasswordAgain, KeyType.PrivateKey);
        if(!newPassword.equals(newPasswordAgain)){
            return new FebsResponse().fail().message(MessageSourceUtils.getString("member_err_009"));
        }
        DappMemberEntity memberEntity = dappMemberDao.selectById(memberId);
        memberEntity.setPassword(SecureUtil.md5(newPassword));
        dappMemberDao.updateById(memberEntity);
 
        String redisKey = AppContants.REDIS_KEY_SIGN + memberEntity.getId();
        redisUtils.del(redisKey);
 
        return new FebsResponse().success().message(MessageSourceUtils.getString("Operation_001"));
    }
    @Override
    public BigDecimal updateLSYJYLFC(List<String> refererIdList,BigDecimal totalProfit,long id) {
        //计算盈利分成
        BigDecimal profitSharingTotal = BigDecimal.ZERO;
        if(BigDecimal.ZERO.compareTo(totalProfit)>=0){
            return profitSharingTotal;
        }
        if(CollUtil.isNotEmpty(refererIdList)){
            String LEVEL_IB = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_IB.getCode());
            if(!DataDictionaryEnum.LEVEL_IB.getCode().equals(LEVEL_IB)){
                BigDecimal multiply = totalProfit.multiply(getProfitSharing(DataDictionaryEnum.LEVEL_IB.getCode()));
                DappMemberEntity dappMemberEntityLEVEL_IB = dappMemberDao.selectMemberInfoByInviteId(LEVEL_IB);
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_IB.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                    || 2 != dappMemberEntityLEVEL_IB.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_IB.getId(),multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_IB.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply),"盈利分成", 8,id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
            String LEVEL_FIB = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_FIB.getCode());
            if(!DataDictionaryEnum.LEVEL_FIB.getCode().equals(LEVEL_FIB)){
                BigDecimal multiply = BigDecimal.ZERO;
                if(!DataDictionaryEnum.LEVEL_IB.getCode().equals(LEVEL_IB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_FIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_IB.getCode()));
                }else{
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_FIB.getCode());
                }
 
                multiply = totalProfit.multiply(multiply);
                DappMemberEntity dappMemberEntityLEVEL_FIB = dappMemberDao.selectMemberInfoByInviteId(LEVEL_FIB);
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_FIB.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_FIB.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_FIB.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_FIB.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "盈利分成", 8, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
            String LEVEL_CIB = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_CIB.getCode());
            if(!DataDictionaryEnum.LEVEL_CIB.getCode().equals(LEVEL_CIB)){
                BigDecimal multiply = BigDecimal.ZERO;
                if(!DataDictionaryEnum.LEVEL_FIB.getCode().equals(LEVEL_FIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_CIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_FIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_IB.getCode().equals(LEVEL_IB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_CIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_IB.getCode()));
                }else{
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_CIB.getCode());
                }
                multiply = totalProfit.multiply(multiply);
                DappMemberEntity dappMemberEntityLEVEL_CIB = dappMemberDao.selectMemberInfoByInviteId(LEVEL_CIB);
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_CIB.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_CIB.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_CIB.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_CIB.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "盈利分成", 8, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
            String LEVEL_AIB = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_AIB.getCode());
            if(!DataDictionaryEnum.LEVEL_AIB.getCode().equals(LEVEL_AIB)){
                BigDecimal multiply = BigDecimal.ZERO;
                if(!DataDictionaryEnum.LEVEL_CIB.getCode().equals(LEVEL_CIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_AIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_CIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_FIB.getCode().equals(LEVEL_FIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_AIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_FIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_IB.getCode().equals(LEVEL_IB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_AIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_IB.getCode()));
                }else{
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_AIB.getCode());
                }
                multiply = totalProfit.multiply(multiply);
                DappMemberEntity dappMemberEntityLEVEL_AIB = dappMemberDao.selectMemberInfoByInviteId(LEVEL_AIB);
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_AIB.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_AIB.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_AIB.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_AIB.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "盈利分成", 8, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
            String LEVEL_GIB = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_GIB.getCode());
            if(!DataDictionaryEnum.LEVEL_GIB.getCode().equals(LEVEL_GIB)){
                BigDecimal multiply = BigDecimal.ZERO;
                if(!DataDictionaryEnum.LEVEL_AIB.getCode().equals(LEVEL_AIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_AIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_CIB.getCode().equals(LEVEL_CIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_CIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_FIB.getCode().equals(LEVEL_FIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_FIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_IB.getCode().equals(LEVEL_IB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GIB.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_IB.getCode()));
                }else{
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GIB.getCode());
                }
                multiply = totalProfit.multiply(multiply);
                DappMemberEntity dappMemberEntityLEVEL_GIB = dappMemberDao.selectMemberInfoByInviteId(LEVEL_GIB);
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_GIB.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_GIB.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_GIB.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_GIB.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "盈利分成", 8, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
            String LEVEL_BP = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_BP.getCode());
            if(!DataDictionaryEnum.LEVEL_BP.getCode().equals(LEVEL_BP)){
                BigDecimal multiply = BigDecimal.ZERO;
                if(!DataDictionaryEnum.LEVEL_GIB.getCode().equals(LEVEL_GIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_BP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_GIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_AIB.getCode().equals(LEVEL_AIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_BP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_AIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_CIB.getCode().equals(LEVEL_CIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_BP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_CIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_FIB.getCode().equals(LEVEL_FIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_BP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_FIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_IB.getCode().equals(LEVEL_IB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_BP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_IB.getCode()));
                }else{
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_BP.getCode());
                }
                multiply = totalProfit.multiply(multiply);
                DappMemberEntity dappMemberEntityLEVEL_BP = dappMemberDao.selectMemberInfoByInviteId(LEVEL_BP);
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_BP.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_BP.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_BP.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_BP.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "盈利分成", 8, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
            String LEVEL_SP = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_SP.getCode());
            if(!DataDictionaryEnum.LEVEL_SP.getCode().equals(LEVEL_SP)){
                BigDecimal multiply = BigDecimal.ZERO;
                if(!DataDictionaryEnum.LEVEL_BP.getCode().equals(LEVEL_BP)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_SP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_BP.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_GIB.getCode().equals(LEVEL_GIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_SP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_GIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_AIB.getCode().equals(LEVEL_AIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_SP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_AIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_CIB.getCode().equals(LEVEL_CIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_SP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_CIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_FIB.getCode().equals(LEVEL_FIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_SP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_FIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_IB.getCode().equals(LEVEL_IB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_SP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_IB.getCode()));
                }else{
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_SP.getCode());
                }
                multiply = totalProfit.multiply(multiply);
                DappMemberEntity dappMemberEntityLEVEL_SP = dappMemberDao.selectMemberInfoByInviteId(LEVEL_SP);
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_SP.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_SP.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_SP.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_SP.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "盈利分成", 8, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
            String LEVEL_GP = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_GP.getCode());
            if(!DataDictionaryEnum.LEVEL_GP.getCode().equals(LEVEL_GP)){
                BigDecimal multiply = BigDecimal.ZERO;
                if(!DataDictionaryEnum.LEVEL_SP.getCode().equals(LEVEL_SP)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_SP.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_BP.getCode().equals(LEVEL_BP)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_BP.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_GIB.getCode().equals(LEVEL_GIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_GIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_AIB.getCode().equals(LEVEL_AIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_AIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_CIB.getCode().equals(LEVEL_CIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_CIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_FIB.getCode().equals(LEVEL_FIB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_FIB.getCode()));
                }else if(!DataDictionaryEnum.LEVEL_IB.getCode().equals(LEVEL_IB)){
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GP.getCode()).subtract(getProfitSharing(DataDictionaryEnum.LEVEL_IB.getCode()));
                }else{
                    multiply = getProfitSharing(DataDictionaryEnum.LEVEL_GP.getCode());
                }
                multiply = totalProfit.multiply(multiply);
                DappMemberEntity dappMemberEntityLEVEL_GP = dappMemberDao.selectMemberInfoByInviteId(LEVEL_GP);
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_GP.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_GP.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_GP.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_GP.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "盈利分成", 8, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
 
 
        }
        //计算流水佣金
        if(CollUtil.isNotEmpty(refererIdList)){
            String LEVEL_AIB = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_AIB.getCode());
            if(!DataDictionaryEnum.LEVEL_AIB.getCode().equals(LEVEL_AIB)){
                BigDecimal multiply = totalProfit.multiply(getRunningCommission(DataDictionaryEnum.LEVEL_AIB.getCode()));
                DappMemberEntity dappMemberEntityLEVEL_AIB = dappMemberDao.selectMemberInfoByInviteId(LEVEL_AIB);
 
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_AIB.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_AIB.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_AIB.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_AIB.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "流水佣金", 7, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
 
            String LEVEL_GIB = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_GIB.getCode());
            if(!DataDictionaryEnum.LEVEL_GIB.getCode().equals(LEVEL_GIB)){
                BigDecimal multiply = totalProfit.multiply(getRunningCommission(DataDictionaryEnum.LEVEL_GIB.getCode()));
                DappMemberEntity dappMemberEntityLEVEL_GIB = dappMemberDao.selectMemberInfoByInviteId(LEVEL_GIB);
 
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_GIB.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_GIB.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_GIB.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_GIB.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "流水佣金", 7, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
 
            String LEVEL_BP = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_BP.getCode());
            if(!DataDictionaryEnum.LEVEL_BP.getCode().equals(LEVEL_BP)){
                BigDecimal multiply = totalProfit.multiply(getRunningCommission(DataDictionaryEnum.LEVEL_BP.getCode()));
                DappMemberEntity dappMemberEntityLEVEL_BP = dappMemberDao.selectMemberInfoByInviteId(LEVEL_BP);
 
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_BP.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_BP.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_BP.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_BP.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "流水佣金", 7, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
 
            String LEVEL_SP = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_SP.getCode());
            if(!DataDictionaryEnum.LEVEL_SP.getCode().equals(LEVEL_SP)){
                BigDecimal multiply = totalProfit.multiply(getRunningCommission(DataDictionaryEnum.LEVEL_SP.getCode()));
                DappMemberEntity dappMemberEntityLEVEL_SP = dappMemberDao.selectMemberInfoByInviteId(LEVEL_SP);
 
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_SP.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_SP.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_SP.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_SP.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "流水佣金", 7, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
 
            String LEVEL_GP = isIdentity(refererIdList, DataDictionaryEnum.LEVEL_GP.getCode());
            if(!DataDictionaryEnum.LEVEL_GP.getCode().equals(LEVEL_GP)){
                BigDecimal multiply = totalProfit.multiply(getRunningCommission(DataDictionaryEnum.LEVEL_GP.getCode()));
                DappMemberEntity dappMemberEntityLEVEL_GP = dappMemberDao.selectMemberInfoByInviteId(LEVEL_GP);
 
                DappWalletCoinEntity dappWalletCoinEntity = dappWalletCoinDao.selectByMemberId(dappMemberEntityLEVEL_GP.getId());
                if(AppContants.ONHOOK_BASIC_AMOUNT.compareTo(dappWalletCoinEntity.getAvailableAmount())<=0
                        || 2 != dappMemberEntityLEVEL_GP.getIsOnHook()){
                    dappWalletCoinDao.addTotalAndaddAvailableByMemberId(dappMemberEntityLEVEL_GP.getId(), multiply);
                    DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(
                            dappMemberEntityLEVEL_GP.getId(),dappWalletCoinEntity.getAvailableAmount(),
                            multiply, dappWalletCoinEntity.getAvailableAmount().add(multiply), "流水佣金", 7, id);
                    dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
                    profitSharingTotal = profitSharingTotal.add(multiply);
                }
            }
        }
        return profitSharingTotal;
    }
 
    @Override
    public BigDecimal updatePTFC(Long memberId, BigDecimal totalProfit,long id) {
        if(BigDecimal.ZERO.compareTo(totalProfit)>=0){
            return BigDecimal.ZERO;
        }
        DappAccountMoneyChangeEntity dappAccountMoneyChangeEntity = new DappAccountMoneyChangeEntity(65L,
                totalProfit, "系统", 9,id);
        dappAccountMoneyChangeDao.insert(dappAccountMoneyChangeEntity);
        return totalProfit;
    }
 
    @Override
    public Integer isGoal(String num) {
        Set set = new HashSet();
        num = StrUtil.subSuf(num,1);
        char[] chars = num.toCharArray();
        for(char c:chars) {
            set.add(c);
        }
        if(set.size()==num.length()){
            return 1;
        }else{
            return 2;
        }
    }
 
    private final MemberCoinChargeDao memberCoinChargeDao;
 
    @Override
    public IPage<AdminMoneyTotalVo> moneyTotal(DappAccountMoneyChangeEntity record, QueryRequest request) {
        Page<DappAccountMoneyChangeEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        //获取充值提现的所有日期
        IPage<AdminMoneyTotalVo> adminMoneyTotalVoIPage = dappAccountMoneyChangeDao.selectMoneyTotalInPage(record, page);
        List<AdminMoneyTotalVo> records = adminMoneyTotalVoIPage.getRecords();
        if(CollUtil.isNotEmpty(records)){
            for(AdminMoneyTotalVo adminMoneyTotalVo : records){
                Date createTime = adminMoneyTotalVo.getCreateTime();
                //每日充值统计
                AdminMemberChargeVo adminMemberChargeVos = memberCoinChargeDao.selectTotalAmountByCreateTimeAndInviteId(createTime,record.getDescription());
                adminMoneyTotalVo.setTotalCharge(adminMemberChargeVos.getTotalCharge().setScale(2,BigDecimal.ROUND_DOWN));
                adminMoneyTotalVo.setSheetIn(adminMemberChargeVos.getSheetIn());
                //每日提现统计
                AdminMemberWithdrawVo adminMemberWithdrawVo = memberCoinWithdrawDao.selectTotalAmountByCreateTimeAndInviteId(createTime,record.getDescription());
                adminMoneyTotalVo.setTotalWithdraw(adminMemberWithdrawVo.getTotalWithdraw().setScale(2,BigDecimal.ROUND_DOWN));
                adminMoneyTotalVo.setSheetOut(adminMemberWithdrawVo.getSheetOut());
            }
        }
        return adminMoneyTotalVoIPage;
    }
 
    public static void main(String[] args) {
 
        DateTime date = DateUtil.date();
        DateTime endTime = DateUtil.parseTimeToday("09:08:03");
        if(DateUtil.compare(date,endTime)>=0){
            //
            System.out.print(endTime);
        }
    }
 
    private String isIdentity(List<String> refererIds,String levelCode){
        String flag = levelCode;
        for(String str : refererIds){
            DappMemberEntity dappMemberEntity = dappMemberDao.selectMemberInfoByInviteId(str);
            if(ObjectUtil.isNotEmpty(dappMemberEntity)){
                String identity = dappMemberEntity.getIdentity();
                if(levelCode.equals(identity)){
                    flag = str;
                    return flag;
                }
            }
        }
        return flag;
    }
 
    private BigDecimal getRunningCommission(String identity){
        DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.LEVEL_MB.getType(), identity);
        String dataDictionaryCustomValue = dataDictionaryCustom.getValue();
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(dataDictionaryCustomValue);
        String runningCommission = jsonObject.get("runningCommission").toString();
        return new BigDecimal(runningCommission).multiply(new BigDecimal(0.01));
    }
 
    private BigDecimal getProfitSharing(String identity){
        DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.LEVEL_MB.getType(), identity);
        String dataDictionaryCustomValue = dataDictionaryCustom.getValue();
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(dataDictionaryCustomValue);
        String profitSharing = jsonObject.get("profitSharing").toString();
        return new BigDecimal(profitSharing).multiply(new BigDecimal(0.01));
    }
 
}