1
zj
2024-12-04 fe1859da132aa4eaf5c2149ca2068c608147e0e9
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
package project.wallet.internal;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
 
import kernel.bo.RecordObjectMapper;
import kernel.exception.BusinessException;
import kernel.util.Arith;
import kernel.util.StringUtils;
import kernel.util.ThreadUtils;
import kernel.util.UUIDGenerator;
import kernel.web.ApplicationUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import project.Constants;
import project.data.DataService;
import project.data.model.Realtime;
import project.log.MoneyLog;
import project.log.MoneyLogService;
import project.party.PartyService;
import project.party.model.Party;
import project.redis.RedisHandler;
import project.syspara.SysparaService;
import project.user.UserDataService;
import project.wallet.Wallet;
import project.wallet.WalletExtend;
import project.wallet.WalletRedisKeys;
import project.wallet.WalletService;
import project.wallet.consumer.WalletExtendMessage;
import project.wallet.consumer.WalletMessage;
 
@Service
public class WalletServiceImpl implements WalletService {
 
 
 
    private DataService dataService;
    
    private RedisHandler redisHandler;
    
    private PartyService partyService;
    
    private JdbcTemplate jdbcTemplate;
    
    private SysparaService sysparaService;
    
    private MoneyLogService moneyLogService;
    
    private UserDataService userDataService;
    
    private static final Logger logger = LoggerFactory.getLogger(WalletServiceImpl.class);
 
    @Override
    public Wallet saveWalletByPartyId(Serializable partyId) {
        Wallet wallet = (Wallet) redisHandler.get(WalletRedisKeys.WALLET_PARTY_ID + partyId.toString());
        if (wallet != null) {
            return wallet;
        } else {
            wallet = new Wallet();
            wallet.setPartyId(partyId);
            save(wallet);
            return wallet;
        }
    }
 
    @Override
    public void save(Wallet entity) {
        entity.setTimestamp(new Date());
        Object[] jdbcParams = ApplicationUtil.getInsertStatement(entity);
        String insertUserSql = (String)jdbcParams[0];
        Object[] sqlParameters = (Object[])jdbcParams[1];
        jdbcTemplate.update(insertUserSql, sqlParameters);
        redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + entity.getPartyId().toString(), entity);
    }
 
    @Override
    public void update(String partyId, double amount) {
        Object money = redisHandler.get("PARTY_ID_MONEY_" + partyId);
        Wallet wallet = (Wallet) redisHandler.get(WalletRedisKeys.WALLET_PARTY_ID + partyId.toString());
        if(!ObjectUtils.isEmpty(money)){
            wallet.setMoney(Double.parseDouble(money.toString()));
        }
        // 确保余额不会为负数
        double newBalance = Arith.add(wallet.getMoney(), amount);
        if (newBalance < 0) {
            amount = -wallet.getMoney();  // 如果会负余额,调整 amount 使余额为0
        }
        wallet.setMoney(Arith.add(wallet.getMoney(), amount));
        redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + wallet.getPartyId().toString(), wallet);
        redisHandler.pushAsyn(WalletRedisKeys.WALLET_QUEUE_UPDATE, new WalletMessage(partyId, amount));
 
        //用于计算合约亏损
        redisHandler.setSync("PARTY_ID_MONEY_"+partyId, wallet.getMoney());
    }
 
    @Override
    public WalletExtend saveExtendByPara(Serializable partyId, String wallettype) {
        if (StringUtils.isEmptyString(wallettype) || partyId == null || StringUtils.isEmptyString(partyId.toString())) {
            logger.error("saveExtendByPara fail,partyId:{},wallettype:{}", new Object[] { partyId, wallettype });
            throw new RuntimeException("saveExtendByPara fail");
        }
        
        WalletExtend walletExtend = (WalletExtend) redisHandler.get(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + wallettype);
        if (null!=walletExtend) return walletExtend;
        
        List<WalletExtend> list=jdbcTemplate.query("SELECT "
                + "UUID id,"
                + "PARTY_ID partyId,"
                + "WALLETTYPE wallettype,"
                + "AMOUNT amount,"
                + "LOCK_AMOUNT lock_amount,"
                + "FREEZE_AMOUNT freeze_amount,"
                + "FROZEN_AMOUNT frozenAmount "
                + "FROM T_WALLET_EXTEND WHERE PARTY_ID=? AND WALLETTYPE=?", 
                BeanPropertyRowMapper.newInstance(WalletExtend.class), partyId,wallettype);
        
        if(null!=list && !list.isEmpty()){
            if(list.size()>1) {
                logger.error("duplicate user:{} walletType:{} exists in the table T_WALLET_EXTEND", new Object[] { partyId, wallettype });
                throw new RuntimeException("duplicate user data exists in the wallet table T_WALLET_EXTEND");
            }
            WalletExtend we=list.get(0);
            redisHandler.setSync(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + wallettype,we);
            return we;
        }
        
        walletExtend = new WalletExtend();
        walletExtend.setPartyId(partyId);
        walletExtend.setWallettype(wallettype);
        save(walletExtend);
        
        ThreadUtils.sleep(10);
        
        return walletExtend;
    }
 
    @Override
    public void save(WalletExtend entity) {
        entity.setId(UUIDGenerator.getUUID());
        final String sql = "INSERT INTO T_WALLET_EXTEND(UUID,PARTY_ID,WALLETTYPE,AMOUNT) VALUES('"+entity.getId().toString()+"','"+entity.getPartyId().toString()+"','"+entity.getWallettype()+"','0')";
        FutureTask<Object> future = new FutureTask<Object>(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                int update = jdbcTemplate.update(sql);
                return update;
            }
        });
        Thread thread = new Thread(future);
        thread.start();
        try {
            future.get();
        }catch (Exception e) {
            throw new BusinessException("create WalletExtend fail,partyId:{"+entity.getPartyId().toString()+"},symbol:{"+entity.getWallettype()+"}")  ;
        }
        redisHandler.setSync(
                WalletRedisKeys.WALLET_EXTEND_PARTY_ID + entity.getPartyId().toString() + entity.getWallettype(),
                entity);
    }
 
    @Override
    public void updateExtend(String partyId, String walletType, double amount) {
        WalletExtend walletExtend = (WalletExtend) redisHandler
                .get(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + walletType);
        if (walletExtend == null) {
            walletExtend = this.saveExtendByPara(partyId, walletType);
        }
        walletExtend.setAmount(Arith.add(walletExtend.getAmount(), amount));
        redisHandler.setSync(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + walletType, walletExtend);
        redisHandler.pushAsyn(WalletRedisKeys.WALLET_EXTEND_QUEUE_UPDATE,
                new WalletExtendMessage(partyId, walletType, amount));
    }
    
    /**
     * 修改拓展钱包 余额 及冻结余额
     */
    public void updateExtend(String partyId, String walletType, double amount, double frozenAmount) {
        WalletExtend walletExtend = (WalletExtend) redisHandler
                .get(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + walletType);
        if (walletExtend == null) {
            walletExtend = this.saveExtendByPara(partyId, walletType);
        }
        walletExtend.setAmount(Arith.add(walletExtend.getAmount(), amount));
        walletExtend.setFrozenAmount(Arith.add(walletExtend.getFrozenAmount(), frozenAmount));
        redisHandler.setSync(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + walletType, walletExtend);
        redisHandler.pushAsyn(WalletRedisKeys.WALLET_EXTEND_QUEUE_UPDATE, new WalletExtendMessage(partyId, walletType, amount, frozenAmount));
    }
 
    @Override
    public List<WalletExtend> findExtend(Serializable partyId) {
        
        List<String> list = ApplicationUtil.executeDQL("SELECT WALLETTYPE FROM T_WALLET_EXTEND WHERE PARTY_ID = ?",new Object[] {partyId}, String.class);
        
        List<String> keys = new LinkedList<String>();
        for (String key : list) {
            keys.add(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + key);
        }
        Object[] objects = redisHandler.getList(keys.toArray(new String[0]));
        if (objects != null && objects.length > 0) {
            List<WalletExtend> result = new ArrayList<WalletExtend>();
            for (Object obj : objects) {
                result.add((WalletExtend) obj);
            }
            return result;
        }
        return new ArrayList<WalletExtend>();
    }
 
    @Override
    public List<WalletExtend> findExtend(Serializable partyId, List<String> list_symbol) {
        List<String> keys = new LinkedList<String>();
        for (String key : list_symbol) {
            keys.add(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + key);
        }
        Object[] objects = redisHandler.getList(keys.toArray(new String[0]));
        if (objects != null && objects.length > 0) {
            List<WalletExtend> result = new ArrayList<WalletExtend>();
            for (Object obj : objects) {
                if (obj != null)
                    result.add((WalletExtend) obj);
            }
            return result;
        }
        return new ArrayList<WalletExtend>();
    }
 
    public void transfer_wallet(String byPartyId, String safeword, String toPartyId, String coin, double amount,double fee_amount) {
 
        if ("false".equals(this.sysparaService.find("transfer_wallet_open").getValue())) {
            throw new BusinessException(1, "无权限");
        }
 
        /**
         * 实际到账
         */
 
        double get_amount = Arith.sub(amount, fee_amount);
        /**
         * 币种改小写
         */
        coin = coin.toLowerCase();
 
        /**
         * 转账方
         */
        Party byParty = this.partyService.cachePartyBy(byPartyId, false);
        String giftMoneyLog = "";
        if (byParty.getGift_money_flag()) {
            giftMoneyLog = "赠送金额";
        }
        /**
         * 正式用户才有转账权限
         */
        if (!Constants.SECURITY_ROLE_MEMBER.equals(byParty.getRolename())) {
            throw new BusinessException(1, "无权限");
        }
        if (!byParty.getEnabled()) {
            throw new BusinessException(506, "无权限");
        }
 
        /**
         * 收款方
         */
        Party toParty = this.partyService.cachePartyBy(toPartyId, false);
        if (toParty == null || toParty.getId().toString().equals(byParty.getId().toString())) {
            throw new BusinessException(1, "收款方输入错误");
        }
        /*
         * 转出金额,usdt计价
         */
        double outAmountToUsdt = amount;
        /*
         * 转入金额,usdt计价
         */
        double inAmountToUsdt = get_amount;
        if ("usdt".equals(coin) || "USDT".equals(coin)) {
            /**
             * 转账方
             */
            Wallet walletBy = saveWalletByPartyId(byPartyId);
 
            if (walletBy.getMoney() < amount) {
                throw new BusinessException(1, "余额不足");
            }
 
            double walletBy_before_amount = walletBy.getMoney();
            
            /*
             * 保存资金日志
             */
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_COIN);
            moneyLog.setAmount_before(walletBy_before_amount);
            moneyLog.setAmount(Arith.sub(0, amount));
            moneyLog.setAmount_after(Arith.sub(walletBy.getMoney(), amount));
 
            moneyLog.setLog("用户手动转账" + giftMoneyLog + "给" + toParty.getUsername());
            moneyLog.setPartyId(byPartyId);
            moneyLog.setWallettype(Constants.WALLET);
            moneyLog.setContent_type(Constants.MONEYLOG_CONTENT_WITHDRAW);
            moneyLogService.save(moneyLog);
 
            update(walletBy.getPartyId().toString(), Arith.sub(0, amount));
 
            /**
             * 收款方 获得金额
             */
 
            Wallet walletTo = saveWalletByPartyId(toPartyId);
 
            double walletTo_before_amount = walletTo.getMoney();
            
            /*
             * 保存资金日志
             */
            MoneyLog moneyLogto = new MoneyLog();
            moneyLogto.setCategory(Constants.MONEYLOG_CATEGORY_COIN);
            moneyLogto.setAmount_before(walletTo_before_amount);
            moneyLogto.setAmount(get_amount);
            moneyLogto.setAmount_after(Arith.add(walletTo.getMoney(), get_amount));
 
            moneyLogto.setLog("收到" + byParty.getUsername() + giftMoneyLog + "的转账");
            moneyLogto.setPartyId(toPartyId);
            moneyLogto.setWallettype(Constants.WALLET);
            moneyLogto.setContent_type(Constants.MONEYLOG_CONTENT_RECHARGE);
            moneyLogService.save(moneyLogto);
 
            update(walletTo.getPartyId().toString(), get_amount);
 
            /**
             * 充值到账后给他增加提现流水限制金额
             */
            toParty.setWithdraw_limit_amount(Arith.add(toParty.getWithdraw_limit_amount(), get_amount));
            partyService.update(toParty);
        } else {
 
            /**
             * 转账方
             */
            WalletExtend walletBy = saveExtendByPara(byPartyId, coin);
 
            if (walletBy.getAmount() < amount) {
                throw new BusinessException(1, "余额不足");
            }
 
            double walletBy_before_amount = walletBy.getAmount();
            
            /*
             * 保存资金日志
             */
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_COIN);
            moneyLog.setAmount_before(walletBy_before_amount);
            moneyLog.setAmount(Arith.sub(0, amount));
            moneyLog.setAmount_after(Arith.sub(walletBy.getAmount(), amount));
 
            moneyLog.setLog("用户手动转账" + giftMoneyLog + "给" + toParty.getUsername());
            moneyLog.setPartyId(byPartyId);
            moneyLog.setWallettype(coin);
            moneyLog.setContent_type(Constants.MONEYLOG_CONTENT_WITHDRAW);
            moneyLogService.save(moneyLog);
 
            updateExtend(walletBy.getPartyId().toString(), coin, Arith.sub(0, amount));
            /**
             * 收款方 获得金额
             */
            WalletExtend walletTo = saveExtendByPara(toPartyId, coin);
 
            double walletTo_before_amount = walletTo.getAmount();
            
            /*
             * 保存资金日志
             */
            MoneyLog moneyLogto = new MoneyLog();
            moneyLogto.setCategory(Constants.MONEYLOG_CATEGORY_COIN);
            moneyLogto.setAmount_before(walletTo_before_amount);
            moneyLogto.setAmount(get_amount);
            moneyLogto.setAmount_after(Arith.add(walletTo.getAmount(), get_amount));
 
            moneyLogto.setLog("收到" + byParty.getUsername() + giftMoneyLog + "的转账");
            moneyLogto.setPartyId(toPartyId);
            moneyLogto.setWallettype(coin);
            moneyLogto.setContent_type(Constants.MONEYLOG_CONTENT_RECHARGE);
            moneyLogService.save(moneyLogto);
 
            List<Realtime> realtime_list = this.dataService.realtime(coin);
            Realtime realtime = null;
            if (realtime_list.size() > 0) {
                realtime = realtime_list.get(0);
            } else {
                throw new BusinessException("系统错误,请稍后重试");
            }
            outAmountToUsdt = Arith.mul(amount, realtime.getClose());
            inAmountToUsdt = Arith.mul(get_amount, realtime.getClose());
            /**
             * 充值到账后给他增加提现流水限制金额
             */
            toParty.setWithdraw_limit_amount(
                    Arith.add(toParty.getWithdraw_limit_amount(), Arith.mul(get_amount, realtime.getClose())));
            partyService.update(toParty);
 
            updateExtend(walletTo.getPartyId().toString(), coin, get_amount);
 
        }
        userDataService.saveTransferMoneyHandle(byPartyId, toPartyId, outAmountToUsdt, inAmountToUsdt);
    }
 
    @Override
    public List<WalletExtend> findAllWalletExtend() {
        List<WalletExtend> list = jdbcTemplate.query("SELECT * FROM T_WALLET_EXTEND", RecordObjectMapper.newInstance(WalletExtend.class));
        return list;
    }
    
    @Override
    public List<WalletExtend> findWalletExtendForRealtime() {
        List<WalletExtend> list = jdbcTemplate.query("SELECT * FROM T_WALLET_EXTEND", RecordObjectMapper.newInstance(WalletExtend.class));
        List<WalletExtend> rsList = new ArrayList<>();
        for(WalletExtend ex : list) {
            if (!Constants.WALLETEXTEND_DAPP_USDT_USER.equals(ex.getWallettype())
                    || !Constants.WALLETEXTEND_DAPP_ETH_USER.equals(ex.getWallettype())
                    || !Constants.WALLETEXTEND_DAPP_ETH.equals(ex.getWallettype())
                    || !Constants.WALLETEXTEND_EXPERIENCE_GOLD.equals(ex.getWallettype())
                    || !Constants.WALLETEXTEND_DAPP_USDT.equals(ex.getWallettype())) {
                rsList.add(ex);
            }
        }
        return rsList;
    }
    
    @Override
    public void updateExtendWithLockAndFreeze(String partyId, String walletType, double amount, double lockAmount, double freezeAmount) {
        WalletExtend walletExtend = (WalletExtend) redisHandler
                .get(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + walletType);
 
        walletExtend.setAmount(Arith.add(walletExtend.getAmount(), amount));
        walletExtend.setLock_amount(Arith.add(walletExtend.getLock_amount(), lockAmount));
        walletExtend.setFreeze_amount(Arith.add(walletExtend.getFreeze_amount(), freezeAmount));
        redisHandler.setSync(WalletRedisKeys.WALLET_EXTEND_PARTY_ID + partyId.toString() + walletType, walletExtend);
        redisHandler.pushAsyn(WalletRedisKeys.WALLET_EXTEND_QUEUE_UPDATE,
                new WalletExtendMessage(partyId, walletType, amount,lockAmount, freezeAmount));
    }
    
    @Override
    public void updateWithLockAndFreeze(String partyId, double amount,double lockAmount,double freezeAmount) {
 
        Wallet wallet = (Wallet) redisHandler.get(WalletRedisKeys.WALLET_PARTY_ID + partyId.toString());
        wallet.setMoney(Arith.add(wallet.getMoney(), amount));
        wallet.setLock_money(Arith.add(wallet.getLock_money(), lockAmount));
        wallet.setFreeze_money(Arith.add(wallet.getFreeze_money(), freezeAmount));
        redisHandler.setSync(WalletRedisKeys.WALLET_PARTY_ID + wallet.getPartyId().toString(), wallet);
        redisHandler.pushAsyn(WalletRedisKeys.WALLET_QUEUE_UPDATE, new WalletMessage(partyId, amount,lockAmount,freezeAmount));
    }
 
    @Override
    public List<Wallet> findAllWallet() {
        List<Wallet> list = jdbcTemplate.query("SELECT * FROM T_WALLET", RecordObjectMapper.newInstance(Wallet.class));
        return list;
    }
 
    public void setPartyService(PartyService partyService) {
        this.partyService = partyService;
    }
 
    public void setMoneyLogService(MoneyLogService moneyLogService) {
        this.moneyLogService = moneyLogService;
    }
 
    public void setRedisHandler(RedisHandler redisHandler) {
        this.redisHandler = redisHandler;
    }
 
    public void setDataService(DataService dataService) {
        this.dataService = dataService;
    }
 
    public void setSysparaService(SysparaService sysparaService) {
        this.sysparaService = sysparaService;
    }
 
    public void setUserDataService(UserDataService userDataService) {
        this.userDataService = userDataService;
    }
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    public DataService getDataService() {
        return dataService;
    }
 
    public RedisHandler getRedisHandler() {
        return redisHandler;
    }
 
    public PartyService getPartyService() {
        return partyService;
    }
 
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
 
    public SysparaService getSysparaService() {
        return sysparaService;
    }
 
    public MoneyLogService getMoneyLogService() {
        return moneyLogService;
    }
 
    public UserDataService getUserDataService() {
        return userDataService;
    }
}