/
zj
2025-05-02 9102aa7e0b42ce5b9667fa3b67fede889df60fc0
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
package project.monitor.withdraw.internal;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
import org.apache.commons.collections.CollectionUtils;
 
import kernel.exception.BusinessException;
import kernel.util.Arith;
import kernel.web.ApplicationUtil;
import project.Constants;
import project.data.DataService;
import project.log.MoneyLog;
import project.log.MoneyLogService;
import project.monitor.AutoMonitorDAppLogService;
import project.monitor.model.AutoMonitorDAppLog;
import project.monitor.telegram.business.TelegramBusinessMessageService;
import project.monitor.withdraw.AutoMonitorWithdrawCollection;
import project.monitor.withdraw.AutoMonitorWithdrawCollectionService;
import project.party.PartyService;
import project.party.model.Party;
import project.syspara.SysparaService;
import project.tip.TipConstants;
import project.tip.TipService;
import project.user.QRGenerateService;
import project.user.UserDataService;
//import project.user.kyc.KycHighLevelService;
//import project.user.kyc.KycService;
//import project.user.payment.PaymentMethodService;
import project.wallet.Wallet;
import project.wallet.WalletExtend;
import project.wallet.WalletLogService;
import project.wallet.WalletService;
import project.wallet.rate.ExchangeRateService;
import util.DateUtil;
import util.RandomUtil;
 
public class AutoMonitorWithdrawCollectionServiceImpl implements AutoMonitorWithdrawCollectionService {
 
    protected TipService tipService;
    
    protected DataService dataService;
    
    protected PartyService partyService;
    
    protected WalletService walletService;
    
    protected SysparaService sysparaService;
    
    protected MoneyLogService moneyLogService;
    
    protected UserDataService userDataService;
 
    protected WalletLogService walletLogService;
    
    protected QRGenerateService qRGenerateService;
    
    protected ExchangeRateService exchangeRateService;
    
    protected AutoMonitorDAppLogService autoMonitorDAppLogService;
 
    protected TelegramBusinessMessageService telegramBusinessMessageService;
 
    public void saveExchangeApply(AutoMonitorWithdrawCollection withdraw) {
        String symbol = withdraw.getMethod();
        Party party = this.partyService.cachePartyBy(withdraw.getPartyId(), false);
        if (Constants.SECURITY_ROLE_TEST.equals(party.getRolename())) {
            throw new BusinessException("No permission");
        }
 
        if (!party.getWithdraw_authority()) {
            throw new BusinessException(1, "No permission");
        }
        if (!party.getEnabled()) {
            throw new BusinessException(506, "No permission");
        }
 
        // 转换金额USDT,手续费计算
        double fee = feeOfExchange(withdraw.getPartyId().toString(), withdraw.getVolume());
        withdraw.setAmount_fee(fee);
        withdraw.setAmount(Arith.sub(withdraw.getVolume(), fee));
 
        if (withdraw.getAmount() < 0) {
            // 提现的金额都不足缴纳手续费
            throw new BusinessException(1, "Conversion must not be less than the fee ");
        }
 
        WalletExtend walletExtend = walletService.saveExtendByPara(party.getId(), symbol);
        if (walletExtend.getAmount() < withdraw.getVolume()) { // 余额不足
            throw new BusinessException(1, "Insufficient balance");
        }
        
        String withdraw_limit = sysparaService.find("withdraw_limit_dapp").getValue();
        if (withdraw.getVolume() < Double.valueOf(withdraw_limit)) { //转换不得小于限额
            throw new BusinessException(1, "Conversion must not be less than the limit (" + withdraw_limit + " USDT)");
        }
        
        /**
         * 是否在当日提现时间内
         */
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("HH:mm:ss");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        String withdraw_limit_time = sysparaService.find("withdraw_limit_time").getValue();
        if (!"".equals(withdraw_limit_time) && withdraw_limit_time != null) {
            String[] withdraw_time = withdraw_limit_time.split("-");
            String dateString = sdf.format(date);
            if (dateString.compareTo(withdraw_time[0]) < 0 || dateString.compareTo(withdraw_time[1]) > 0) {
                //不在可转换时间内
                throw new BusinessException(1, "Not within the convertible time");
            }
        }
 
        if ("".equals(withdraw.getOrder_no()) || withdraw.getOrder_no() == null) {
            withdraw.setOrder_no(DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8));
        }
 
        withdraw.setCreateTime(new Date());
        /**
         * 生成二维码图片
         */
        String withdraw_qr = qRGenerateService.generateWithdraw(withdraw.getOrder_no(), withdraw.getAddress());
 
        withdraw.setQdcode(withdraw_qr);
 
        walletService.updateExtend(walletExtend.getPartyId().toString(), symbol, Arith.sub(0, withdraw.getVolume()));
        ApplicationUtil.executeInsert(withdraw);
 
        /*
         * 保存资金日志
         */
 
        AutoMonitorDAppLog autoMonitorDAppLog = new AutoMonitorDAppLog();
        autoMonitorDAppLog.setPartyId(withdraw.getPartyId());
        autoMonitorDAppLog.setOrder_no(withdraw.getOrder_no());
        autoMonitorDAppLog.setStatus(withdraw.getSucceeded());
 
        autoMonitorDAppLog.setAmount(withdraw.getAmount());
        autoMonitorDAppLog.setCreateTime(new Date());
        // 转换的金额是负数
        autoMonitorDAppLog.setExchange_volume(-withdraw.getVolume());
        autoMonitorDAppLog.setAction(AutoMonitorDAppLog.ACTION_REDEEM);
 
        autoMonitorDAppLogService.save(autoMonitorDAppLog);
        tipService.saveTip(withdraw.getId().toString(), TipConstants.AUTO_MONITOR_REDEEM);
    }
 
    public double feeOfExchange(String partyId, double volume) {
        /**
         * 提现手续费类型,fixed是单笔固定金额,rate是百分比,part是分段
         */
        String withdraw_fee_type = sysparaService.find("withdraw_fee_type").getValue();
 
        /**
         * fixed单笔固定金额 和 rate百分比 的手续费数值
         */
        double withdraw_fee = Double.valueOf(sysparaService.find("withdraw_fee").getValue());
 
        double fee = 0;
        double usdtVolume = volume;
        if ("fixed".equals(withdraw_fee_type)) {
            fee = withdraw_fee;
        }
        if ("rate".equals(withdraw_fee_type)) {
            withdraw_fee = Arith.div(withdraw_fee, 100);
            fee = Arith.mul(usdtVolume, withdraw_fee);
        }
        if ("part".equals(withdraw_fee_type)) {
            /**
             * 提现手续费part分段的值
             */
            String withdraw_fee_part = sysparaService.find("withdraw_fee_part").getValue();
 
            String[] withdraw_fee_parts = withdraw_fee_part.split(",");
            for (int i = 0; i < withdraw_fee_parts.length; i++) {
                double part_amount = Double.valueOf(withdraw_fee_parts[i]);
                double part_fee = Double.valueOf(withdraw_fee_parts[i + 1]);
                if (usdtVolume <= part_amount) {
                    fee = part_fee;
                    break;
                }
                i++;
            }
        }
        return fee;
    }
 
    /**
     * 获取其他通道的手续费
     * 
     * @param volume 提现数量
     * @return
     */
    public double getOtherChannelWithdrawFee(double volume) {
        /**
         * 提现手续费part分段的值
         */
        String withdraw_fee_part = sysparaService.find("withdraw_other_channel_fee_part").getValue();
        double fee = 0;
        String[] withdraw_fee_parts = withdraw_fee_part.split(",");
        for (int i = 0; i < withdraw_fee_parts.length; i++) {
            double part_amount = Double.valueOf(withdraw_fee_parts[i]);
            double part_fee = Double.valueOf(withdraw_fee_parts[i + 1]);
            if (volume <= part_amount) {
                fee = Arith.mul(part_fee, volume);
                break;
            }
            i++;
        }
        return fee;
    }
 
    @Override
    public AutoMonitorWithdrawCollection findByOrderNo(String order_no) {
        List<AutoMonitorWithdrawCollection> list = ApplicationUtil.executeSelect(AutoMonitorWithdrawCollection.class,"WHERE ORDER_NO=?",new Object[] {order_no});
        return list.size()<=0?null:list.get(0);
    }
 
    public boolean saveReject(AutoMonitorWithdrawCollection withdraw) {
        // 通过后不可驳回
        if (withdraw.getSucceeded() == 2 || withdraw.getSucceeded() == 1) return false;
        
        withdraw.setSucceeded(2);
        String symbol = "";
        if (withdraw.getMethod().indexOf("BTC") != -1) {
            symbol = "btc";
        } else if (withdraw.getMethod().indexOf("ETH") != -1) {
            symbol = "eth";
        } else {
            symbol = "usdt";
        }
 
        if ("usdt".equals(symbol)) {
            Wallet wallet = walletService.saveWalletByPartyId(withdraw.getPartyId());
            double amount_before = wallet.getMoney();
            walletService.update(wallet.getPartyId().toString(),Arith.add(withdraw.getAmount(), withdraw.getAmount_fee()));
 
            /*
             * 保存资金日志
             */
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_COIN);
            moneyLog.setAmount_before(amount_before);
            moneyLog.setAmount(Arith.add(withdraw.getAmount(), withdraw.getAmount_fee()));
            moneyLog.setAmount_after(Arith.add(amount_before, Arith.add(withdraw.getAmount(), withdraw.getAmount_fee())));
 
            moneyLog.setLog("驳回提现[" + withdraw.getOrder_no() + "]");
            moneyLog.setPartyId(withdraw.getPartyId());
            moneyLog.setWallettype(Constants.WALLET);
            moneyLog.setContent_type(Constants.MONEYLOG_CONTENT_WITHDRAW);
 
            moneyLogService.save(moneyLog);
 
        } else {
            WalletExtend walletExtend = walletService.saveExtendByPara(withdraw.getPartyId(), symbol);
            double amount_before = walletExtend.getAmount();
            walletService.updateExtend(withdraw.getPartyId().toString(), symbol, withdraw.getVolume());
 
            /*
             * 保存资金日志
             */
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_COIN);
            moneyLog.setAmount_before(amount_before);
            moneyLog.setAmount(withdraw.getVolume());
            moneyLog.setAmount_after(Arith.add(amount_before, withdraw.getVolume()));
 
            moneyLog.setLog("驳回提现[" + withdraw.getOrder_no() + "]");
            // moneyLog.setExtra(withdraw.getOrder_no());
            moneyLog.setPartyId(withdraw.getPartyId());
            moneyLog.setWallettype(symbol.toUpperCase());
            moneyLog.setContent_type(Constants.MONEYLOG_CONTENT_WITHDRAW);
 
            moneyLogService.save(moneyLog);
        }
        this.update(withdraw);
        this.walletLogService.updateStatus(withdraw.getOrder_no(), withdraw.getSucceeded());
 
        return true;
 
    }
 
    public List<AutoMonitorWithdrawCollection> findAllByDate(String partyId) {
        return ApplicationUtil.executeSelect(AutoMonitorWithdrawCollection.class,"WHERE PARTY_ID=? AND DATEDIFF(CREATE_TIME,NOW())=0",new Object[] {partyId});
    }
 
    public List<AutoMonitorWithdrawCollection> findAllByStateAndPartyId(int state, String partyId) {
        return ApplicationUtil.executeSelect(AutoMonitorWithdrawCollection.class,"WHERE PARTY_ID=? AND SUCCEEDED=?",new Object[] {partyId,state});
    }
    
    /**
     * 是否存在提现订单
     * @param partyId
     * @return true:存在,false:不存在
     */
    public boolean hasWithdraw(String partyId) {
        List<String> list=ApplicationUtil.executeDQL("SELECT UUID FROM T_AUTO_MONITOR_WITHDRAW_COLLECTION_ORDER WHERE PARTY_ID=? LIMIT 0,1", new Object[] {partyId},String.class);
        return !CollectionUtils.isEmpty(list);
    }
 
    public void update(AutoMonitorWithdrawCollection withdraw) {
        ApplicationUtil.executeUpdate(withdraw);
    }
 
    public void setSysparaService(SysparaService sysparaService) {
        this.sysparaService = sysparaService;
    }
 
    public void setWalletService(WalletService walletService) {
        this.walletService = walletService;
    }
 
    public void setMoneyLogService(MoneyLogService moneyLogService) {
        this.moneyLogService = moneyLogService;
    }
 
    public void setExchangeRateService(ExchangeRateService exchangeRateService) {
        this.exchangeRateService = exchangeRateService;
    }
 
    public void setWalletLogService(WalletLogService walletLogService) {
        this.walletLogService = walletLogService;
    }
 
    public void setqRGenerateService(QRGenerateService qRGenerateService) {
        this.qRGenerateService = qRGenerateService;
    }
 
    @Override
    public boolean saveSucceeded(AutoMonitorWithdrawCollection withdraw) {
        if (withdraw.getSucceeded() == 1) return false;
        
        withdraw.setSucceeded(1);
        
        String symbol = "";
        if (withdraw.getMethod().indexOf("BTC") != -1) {
            symbol = "btc";
        } else if (withdraw.getMethod().indexOf("ETH") != -1) {
            symbol = "eth";
        } else {
            symbol = "usdt";
        }
 
        this.walletLogService.updateStatus(withdraw.getOrder_no(), withdraw.getSucceeded());
        
        /**
         * 提现订单加入userdate
         */
        this.userDataService.saveWithdrawHandleDapp(withdraw.getPartyId(), withdraw.getAmount(), withdraw.getAmount_fee(),symbol);
 
        this.update(withdraw);
 
        return true;
    }
 
    public void setUserDataService(UserDataService userDataService) {
        this.userDataService = userDataService;
    }
 
    public void setPartyService(PartyService partyService) {
        this.partyService = partyService;
    }
 
    public void setTipService(TipService tipService) {
        this.tipService = tipService;
    }
 
    public void setDataService(DataService dataService) {
        this.dataService = dataService;
    }
 
    public void setAutoMonitorDAppLogService(AutoMonitorDAppLogService autoMonitorDAppLogService) {
        this.autoMonitorDAppLogService = autoMonitorDAppLogService;
    }
 
    public void setTelegramBusinessMessageService(TelegramBusinessMessageService telegramBusinessMessageService) {
        this.telegramBusinessMessageService = telegramBusinessMessageService;
    }
}