优化代码
This commit is contained in:
1 parent
ed738d3da6
commit
4048428169
14 files changed
+381
-309
No files matched your search
@@ -7,6 +7,9 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ruoyi.system.domain.UsdtOrdersPlus;
|
||||
import com.ruoyi.system.service.ITronService;
|
||||
import com.ruoyi.system.service.IUsdtOrdersService;
|
||||
import com.ruoyi.system.service.TronServiceProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -21,14 +24,23 @@ import java.util.Date;
|
||||
*/
|
||||
@Component
|
||||
public class TronPaymentTask {
|
||||
private static final Logger log = LoggerFactory.getLogger(TronPaymentTask.class);
|
||||
|
||||
@Autowired
|
||||
private IUsdtOrdersService orders;
|
||||
@Autowired
|
||||
private ITronService tron;
|
||||
private TronServiceProvider tronServiceProvider;
|
||||
private final RestTemplate http = new RestTemplate();
|
||||
|
||||
@Scheduled(fixedDelay = 30000L)
|
||||
public void scan() {
|
||||
ITronService tron;
|
||||
try {
|
||||
tron = tronServiceProvider.getCurrentService();
|
||||
} catch (RuntimeException e) {
|
||||
log.warn("无法读取 TRON 全局网络配置,跳过本轮支付扫描:{}", e.getMessage());
|
||||
return;
|
||||
}
|
||||
for (UsdtOrdersPlus order : orders.list(new LambdaQueryWrapper<UsdtOrdersPlus>().eq(UsdtOrdersPlus::getStatus, 1))) {
|
||||
if (order.getToken() == null || order.getActualAmount() == null) continue;
|
||||
try {
|
||||
@@ -51,8 +63,11 @@ public class TronPaymentTask {
|
||||
orders.updateById(order);
|
||||
break;
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
} catch (Exception e) {
|
||||
// 单笔订单查询失败不能中断其他订单,日志中禁止输出凭证和钱包私钥。
|
||||
log.warn("查询 TRON 支付交易失败,订单ID={},网络={}", order.getId(), tron.type());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.ruoyi.task;
|
||||
|
||||
import com.ruoyi.system.domain.UsdtAggregationTaskItemPlus;
|
||||
import com.ruoyi.system.domain.UsdtAggregationTaskPlus;
|
||||
import com.ruoyi.system.domain.WalletAddressPlus;
|
||||
import com.ruoyi.system.service.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后台逐地址执行归集任务;每个明细独立失败并继续后续地址。
|
||||
*/
|
||||
@Component
|
||||
@EnableScheduling
|
||||
public class UsdtAggregationExecutorTask {
|
||||
private static final Logger log = LoggerFactory.getLogger(UsdtAggregationExecutorTask.class);
|
||||
@Autowired
|
||||
private IUsdtAggregationTaskService taskService;
|
||||
@Autowired
|
||||
private IUsdtAggregationTaskItemService itemService;
|
||||
@Autowired
|
||||
private IWalletAddressService walletService;
|
||||
@Autowired
|
||||
private TronServiceProvider tronServiceProvider;
|
||||
|
||||
@Scheduled(fixedDelay = 30000L)
|
||||
public void executePending() {
|
||||
ITronService tronService;
|
||||
try {
|
||||
tronService = tronServiceProvider.getCurrentService();
|
||||
} catch (RuntimeException e) {
|
||||
log.warn("无法读取 TRON 全局网络配置,跳过本轮归集任务:{}", e.getMessage());
|
||||
return;
|
||||
}
|
||||
List<UsdtAggregationTaskPlus> tasks = taskService.getListByStatusAndLtExeTime(1, new java.util.Date(System.currentTimeMillis() + 1));
|
||||
for (UsdtAggregationTaskPlus task : tasks) executeTask(task, tronService);
|
||||
}
|
||||
|
||||
private void executeTask(UsdtAggregationTaskPlus task, ITronService tronService) {
|
||||
List<UsdtAggregationTaskItemPlus> items = itemService.getListByStatusAndParentId(0, task.getId());
|
||||
boolean failed = false;
|
||||
for (UsdtAggregationTaskItemPlus item : items) {
|
||||
WalletAddressPlus wallet = walletService.getByTokenAndAppId(item.getTokenAddress(), item.getAppId());
|
||||
String tx = null;
|
||||
Exception last = null;
|
||||
for (int attempt = 0; attempt < 4 && tx == null; attempt++) {
|
||||
org.tron.trident.core.ApiWrapper api = null;
|
||||
try {
|
||||
api = tronService.buildApiWrapper();
|
||||
tx = tronService.usdtTrans(api, wallet.getToken(), item.getDistTokenAddress(), item.getOpeAmount().toPlainString());
|
||||
} catch (Exception ex) {
|
||||
last = ex;
|
||||
} finally {
|
||||
if (api != null) api.close();
|
||||
}
|
||||
}
|
||||
if (tx == null || tx.trim().isEmpty()) {
|
||||
fail(item, "重试耗尽:" + (last == null ? "交易失败" : last.getMessage()));
|
||||
failed = true;
|
||||
} else {
|
||||
item.setStatus(1L);
|
||||
item.setRemark("交易哈希:" + tx);
|
||||
itemService.updateById(item);
|
||||
}
|
||||
}
|
||||
task.setStatus(failed ? 3 : 2);
|
||||
taskService.updateById(task);
|
||||
}
|
||||
|
||||
private void fail(UsdtAggregationTaskItemPlus item, String reason) {
|
||||
item.setStatus(2L);
|
||||
item.setRemark(reason);
|
||||
itemService.updateById(item);
|
||||
}
|
||||
}
|
||||
@@ -57,10 +57,6 @@ logging:
|
||||
level:
|
||||
com.ruoyi: debug
|
||||
org.springframework: warn
|
||||
|
||||
tron:
|
||||
env:
|
||||
type: nile
|
||||
# Spring配置
|
||||
spring:
|
||||
resources:
|
||||
|
||||
Reference in new issue
Block a user