peternameyakj
2024-08-08 cbc26f3f148e9f328f4fa1536c30b16946360a81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package project.wallet.internal;
 
import kernel.exception.BusinessException;
import kernel.util.Arith;
import kernel.util.StringUtils;
import kernel.util.UUIDGenerator;
import kernel.web.ApplicationUtil;
import kernel.web.ResultObject;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import project.data.DataService;
import project.data.model.Realtime;
import project.party.PartyService;
import project.redis.RedisHandler;
import project.syspara.SysparaService;
import project.wallet.*;
 
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
public class WalletGatherServiceImpl implements WalletGatherService {
 
    private JdbcTemplate jdbcTemplate;
    private RedisHandler redisHandler;
    private WalletService walletService;
 
    // 构造函数,传入 DataSource 初始化 jdbcTemplate
    public WalletGatherServiceImpl(JdbcTemplate jdbcTemplate,RedisHandler redisHandler,WalletService walletService) {
        this.jdbcTemplate = jdbcTemplate;
        this.redisHandler = redisHandler;
        this.walletService = walletService;
    }
 
 
    @Override
    public WalletGather getWalletGatherByPartyId(Serializable partyId, DataService dataService) {
        //先从redis中获取
//        WalletGather walletGather = (WalletGather)redisHandler.get(WalletRedisKeys.WALLET_GATHER_PARTY_ID + partyId.toString());
        WalletGather walletGather = null;
        if(null == walletGather){
            List<WalletGather> walletGathers = jdbcTemplate.query(
                    "SELECT UUID id, PARTY_ID partyId, USDT_MONEY usdtMoney, BTC_MONEY btcMoney, ETH_MONEY ethMoney, USDC_MONEY usdcMoney FROM T_WALLET_GATHER WHERE PARTY_ID=?",
                    new Object[]{partyId},
                    BeanPropertyRowMapper.newInstance(WalletGather.class)
            );
            // 处理查询结果,可以检查列表是否为空
            if (!walletGathers.isEmpty()) {
                walletGather = walletGathers.get(0);
            }
        }
        if(null == walletGather){
            walletGather = save(partyId.toString());
        }
        System.out.println("判断dataService是否为空"+dataService);
        if(null != dataService){
            //查总数
            List<String> symbols = new ArrayList<>();
            if(walletGather.getBtcMoney() > 0){
                symbols.add("btc");
            }
            if(walletGather.getEthMoney() > 0){
                symbols.add("eth");
            }
            if(walletGather.getUsdcMoney() > 0){
                symbols.add("usdc");
            }
            System.out.println("组装symbols"+symbols);
            System.out.println("组装symbols"+symbols.size());
            String data_symbol = "";
            double money_coin = 0;
            if(symbols.size() > 0){
                for (int i = 0; i < symbols.size(); i++) {
                    if (i != 0) {
                        data_symbol = data_symbol + "," + symbols.get(i);
                    } else {
                        data_symbol = symbols.get(i);
                    }
                }
                System.out.println("组装data_symbol"+data_symbol);
                List<Realtime> realtime_all = dataService.realtime(data_symbol);
                System.out.println("查询realtime_all"+realtime_all);
                if (realtime_all.size() <= 0) {
                    throw new BusinessException("系统错误,请稍后重试");
                }
                Realtime realtime = null;
                for (int i = 0; i < symbols.size(); i++) {
                    for (Realtime real : realtime_all) {
                        realtime = null;
                        if (real.getSymbol().equals(symbols.get(i).toLowerCase())) {
                            realtime = real;
                        }
                        if (realtime != null) {
                            if("btc".equals(symbols.get(i))){
                                money_coin = Arith.add(money_coin, Arith.mul(realtime.getClose(), walletGather.getBtcMoney()));
                            }else if("eth".equals(symbols.get(i))){
                                money_coin = Arith.add(money_coin, Arith.mul(realtime.getClose(), walletGather.getEthMoney()));
                            }else if("usdc".equals(symbols.get(i))){
                                money_coin = Arith.add(money_coin, Arith.mul(realtime.getClose(), walletGather.getUsdcMoney()));
                            }
                        }
                    }
                }
            }else {
                money_coin = walletGather.getUsdtMoney();
            }
            walletGather.setTotalMoney(money_coin);
        }
        return walletGather;
    }
 
    @Override
    public WalletGather save(String partyId) {
        WalletGather walletGather = new WalletGather();
        walletGather.setId(UUIDGenerator.getUUID());
        walletGather.setPartyId(partyId);
        ApplicationUtil.executeInsert(walletGather);
        redisHandler.setAsyn(WalletRedisKeys.WALLET_GATHER_PARTY_ID + walletGather.getPartyId().toString(),walletGather);
        return walletGather;
    }
 
    @Override
    public void update(String partyId, String currency, double amount,String mark) {
        System.out.println("update 方法:参数partyId="+partyId+",参数currency="+currency+",参数amount="+amount+",参数mark="+mark);
        WalletGather walletGather = getWalletGatherByPartyId(partyId,null);
        if("usdt".equals(currency)){
            if("add".equals(mark)){
                walletGather.setUsdtMoney(Arith.add(walletGather.getUsdtMoney(), amount));
            }else {
                walletGather.setUsdtMoney(Arith.sub(walletGather.getUsdtMoney(), amount));
            }
        }else if("btc".equals(currency)){
            if("add".equals(mark)){
                walletGather.setBtcMoney(Arith.add(walletGather.getBtcMoney(), amount));
            }else {
                walletGather.setBtcMoney(Arith.sub(walletGather.getBtcMoney(), amount));
            }
        }else if("eth".equals(currency)){
            if("add".equals(mark)){
                walletGather.setEthMoney(Arith.add(walletGather.getEthMoney(), amount));
            }else {
                walletGather.setEthMoney(Arith.sub(walletGather.getEthMoney(), amount));
            }
        }else if("usdc".equals(currency)){
            if("add".equals(mark)){
                walletGather.setUsdcMoney(Arith.add(walletGather.getUsdcMoney(), amount));
            }else {
                walletGather.setUsdcMoney(Arith.sub(walletGather.getUsdcMoney(), amount));
            }
        }
        ApplicationUtil.executeUpdate(walletGather);
        redisHandler.setAsyn(WalletRedisKeys.WALLET_GATHER_PARTY_ID + walletGather.getPartyId().toString(),walletGather);
    }
 
    @Override
    public ResultObject transfer(String partyId,String currency, String fromTo, String amount,String safeword,PartyService partyService,SysparaService sysparaService) {
        // 交易所提现是否需要资金密码
        String exchange_withdraw_need_safeword = sysparaService.find("exchange_withdraw_need_safeword").getValue();
        if(StringUtils.isEmptyString(exchange_withdraw_need_safeword)) {
            throw new BusinessException("系统参数错误");
        }
 
        if ("true".equals(exchange_withdraw_need_safeword)) {
            if (StringUtils.isEmptyString(safeword)) {
                ResultObject resultObject = new ResultObject();
                resultObject.setCode("1");
                resultObject.setMsg("资金密码不能为空");
                return resultObject;
            }
 
            if (safeword.length() < 6 || safeword.length() > 12) {
                ResultObject resultObject = new ResultObject();
                resultObject.setCode("1");
                resultObject.setMsg("资金密码必须6-12位");
                return resultObject;
            }
 
            if (!partyService.checkSafeword(safeword, partyId)) {
                ResultObject resultObject = new ResultObject();
                resultObject.setCode("1");
                resultObject.setMsg("资金密码错误");
                return resultObject;
            }
        }
 
        WalletGather walletGather = getWalletGatherByPartyId(partyId,null);
        List<WalletGather> walletGathers = jdbcTemplate.query(
                "SELECT UUID id, PARTY_ID partyId, USDT_MONEY usdtMoney, BTC_MONEY btcMoney, ETH_MONEY ethMoney, USDC_MONEY usdcMoney FROM T_WALLET_GATHER WHERE PARTY_ID=?",
                new Object[]{partyId},
                BeanPropertyRowMapper.newInstance(WalletGather.class)
        );
        // 处理查询结果,可以检查列表是否为空
        if (!walletGathers.isEmpty()) {
            walletGather = walletGathers.get(0);
        }
        WalletExtend walletExtend = walletService.saveExtendByPara(partyId, currency);
        double volume = Double.parseDouble(amount);
        if("1".equals(fromTo)){
            //资金账号=>交易账户
            System.out.println("资金账号=>交易账户");
            return this.transferGathertoExtend(walletGather,walletExtend,currency,volume);
        }else {
            //交易账户=>资金账户
            System.out.println("交易账户=>资金账户");
            return this.transferExtendtoGather(walletGather,walletExtend,currency,volume);
        }
    }
 
    @Override
    public ResultObject getParameter(String partyId, String currency, String fromTo) {
        ResultObject resultObject = new ResultObject();
        if(org.apache.commons.lang3.StringUtils.isBlank(partyId) || org.apache.commons.lang3.StringUtils.isBlank(currency)  || org.apache.commons.lang3.StringUtils.isBlank(fromTo)){
            resultObject.setCode("1");
            resultObject.setCode("参数错误");
            return resultObject;
        }
        resultObject.setCode("0");
        resultObject.setCode("操作成功");
        if("1".equals(fromTo)){
            WalletGather walletGather = this.getWalletGatherByPartyId(partyId,null);
            if("usdt".equals(currency)){
                resultObject.setData(walletGather.getUsdtMoney());
            }else if("btc".equals(currency)){
                resultObject.setData(walletGather.getBtcMoney());
            }else if("eth".equals(currency)){
                resultObject.setData(walletGather.getEthMoney());
            }else if("usdc".equals(currency)){
                resultObject.setData(walletGather.getUsdcMoney());
            }
            return resultObject;
        }else {
            //交易账户=>资金账户
            if("usdt".equals(currency)){
                resultObject.setData(walletService.saveWalletByPartyId(partyId).getMoney());
            }else {
                WalletExtend walletExtend = walletService.saveExtendByPara(partyId, currency);
                resultObject.setData(walletExtend.getAmount());
            }
            return resultObject;
        }
    }
 
    public ResultObject transferExtendtoGather(WalletGather walletGather,WalletExtend walletExtend,String currency,double amount){
        ResultObject resultObject = new ResultObject();
        resultObject.setCode("0");
        if(new BigDecimal(walletExtend.getAmount()).compareTo(new BigDecimal(amount)) >= 0){
            if("usdt".equals(currency)){
                walletGather.setUsdtMoney(Arith.add(walletGather.getUsdtMoney(), amount));
            }else if("btc".equals(currency)){
                walletGather.setBtcMoney(Arith.add(walletGather.getBtcMoney(), amount));
            }else if("eth".equals(currency)){
                walletGather.setEthMoney(Arith.add(walletGather.getEthMoney(), amount));
            }else if("usdc".equals(currency)){
                walletGather.setUsdcMoney(Arith.add(walletGather.getUsdcMoney(), amount));
            }
        }else {
            resultObject.setCode("1");
            resultObject.setMsg("划转资金不足");
            return resultObject;
        }
 
        ApplicationUtil.executeUpdate(walletGather);
        redisHandler.setAsyn(WalletRedisKeys.WALLET_GATHER_PARTY_ID + walletGather.getPartyId().toString(),walletGather);
        if("usdt".equals(currency)){
            walletService.update(walletGather.getPartyId().toString(),-amount);
        }else {
            walletService.updateExtend(walletExtend.getPartyId().toString(), walletExtend.getWallettype(), -amount);
        }
        resultObject.setMsg("划转成功");
        return resultObject;
    }
 
    public ResultObject transferGathertoExtend(WalletGather walletGather,WalletExtend walletExtend,String currency,double amount){
        ResultObject resultObject = new ResultObject();
        resultObject.setCode("0");
        if("usdt".equals(currency)){
            if(new BigDecimal(walletGather.getUsdtMoney()).compareTo(new BigDecimal(amount)) >= 0){
                walletGather.setUsdtMoney(Arith.sub(walletGather.getUsdtMoney(), amount));
            }else {
                resultObject.setCode("1");
                resultObject.setMsg("划转资金不足");
                return resultObject;
            }
        }else if("btc".equals(currency)){
            if(new BigDecimal(walletGather.getUsdtMoney()).compareTo(new BigDecimal(amount)) >= 0){
                walletGather.setBtcMoney(Arith.sub(walletGather.getBtcMoney(), amount));
            }else {
                resultObject.setCode("1");
                resultObject.setMsg("划转资金不足");
                return resultObject;
            }
        }else if("eth".equals(currency)){
            if(new BigDecimal(walletGather.getUsdtMoney()).compareTo(new BigDecimal(amount)) >= 0){
                walletGather.setEthMoney(Arith.sub(walletGather.getEthMoney(), amount));
            }else {
                resultObject.setCode("1");
                resultObject.setMsg("划转资金不足");
                return resultObject;
            }
        }else if("usdc".equals(currency)){
            if(new BigDecimal(walletGather.getUsdtMoney()).compareTo(new BigDecimal(amount)) >= 0){
                walletGather.setUsdcMoney(Arith.sub(walletGather.getUsdcMoney(), amount));
            }else {
                resultObject.setCode("1");
                resultObject.setMsg("划转资金不足");
                return resultObject;
            }
        }
        ApplicationUtil.executeUpdate(walletGather);
        redisHandler.setAsyn(WalletRedisKeys.WALLET_GATHER_PARTY_ID + walletGather.getPartyId().toString(),walletGather);
        if("usdt".equals(currency)){
            walletService.update(walletGather.getPartyId().toString(),amount);
        }else {
            walletService.updateExtend(walletExtend.getPartyId().toString(), walletExtend.getWallettype(), amount);
        }
        resultObject.setMsg("划转成功");
        return resultObject;
    }
}