接入租赁平台
This commit is contained in:
1 parent
c035263bc6
commit
1c2d15b2a9
51 files changed
+3631
-15
No files matched your search
@@ -0,0 +1,105 @@
|
||||
package com.ruoyi.system.chain;
|
||||
|
||||
import com.ruoyi.system.domain.UsdtAggregationTaskItemPlus;
|
||||
import com.ruoyi.system.domain.WalletAddressPlus;
|
||||
import com.ruoyi.system.service.IUsdtAggregationTaskItemService;
|
||||
import com.ruoyi.system.service.IWalletAddressService;
|
||||
import com.ruoyi.system.service.PayoutProcessingService;
|
||||
import com.ruoyi.system.service.WalletLedgerPostingService;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 支付后端同步最终广播结果。执行器不修改商户余额,不绕过已有代付链上确认和结算流程。
|
||||
* 结果同步和 reconciled 标记同一事务提交;后台重启后可继续,业务行锁防止并发落账。
|
||||
*/
|
||||
@Component
|
||||
public class ChainExecutionReconciler {
|
||||
private final ChainExecutorClient client;
|
||||
private final ChainExecutionStore store;
|
||||
private final PayoutProcessingService payouts;
|
||||
private final IUsdtAggregationTaskItemService items;
|
||||
private final IWalletAddressService wallets;
|
||||
private final WalletLedgerPostingService ledger;
|
||||
private final TransactionTemplate transactions;
|
||||
|
||||
public ChainExecutionReconciler(ChainExecutorClient client, ChainExecutionStore store,
|
||||
PayoutProcessingService payouts, IUsdtAggregationTaskItemService items,
|
||||
IWalletAddressService wallets, WalletLedgerPostingService ledger, TransactionTemplate transactions) {
|
||||
this.client = client;
|
||||
this.store = store;
|
||||
this.payouts = payouts;
|
||||
this.items = items;
|
||||
this.wallets = wallets;
|
||||
this.ledger = ledger;
|
||||
this.transactions = transactions;
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 5000)
|
||||
public void reconcile() {
|
||||
if (!client.isEnabled()) return;
|
||||
List<Map<String, Object>> rows = store.jdbc().queryForList("select execution_id from usdt_chain_execution "
|
||||
+ "where reconciled=0 and status in ('BROADCASTED','FAILED') order by created_at limit 50");
|
||||
for (Map<String, Object> row : rows) {
|
||||
try {
|
||||
transactions.execute(status -> {
|
||||
apply((String) row.get("execution_id"));
|
||||
return null;
|
||||
});
|
||||
} catch (Exception ignored) {
|
||||
// 业务事务失败下次继续,禁止因此重新广播。
|
||||
}
|
||||
}
|
||||
finishAggregationTasks();
|
||||
}
|
||||
|
||||
private void apply(String id) {
|
||||
Map<String, Object> row = store.jdbc().queryForMap(
|
||||
"select * from usdt_chain_execution where execution_id=? for update", id);
|
||||
if (((Number) row.get("reconciled")).intValue() != 0) return;
|
||||
ChainCommand command = store.command(row);
|
||||
boolean success = "BROADCASTED".equals(row.get("status"));
|
||||
String hash = (String) row.get("transaction_id");
|
||||
if (command.businessKey.startsWith("PAYOUT:")) {
|
||||
Long orderId = Long.valueOf(command.businessKey.substring(7));
|
||||
Integer current = store.jdbc().queryForObject(
|
||||
"select status from usdt_payout_order where id=? for update", Integer.class, orderId);
|
||||
if (Integer.valueOf(PayoutProcessingService.EXECUTING).equals(current)) {
|
||||
if (success) payouts.markBroadcasted(orderId, hash);
|
||||
else payouts.markFailed(orderId, (String) row.get("error_message"));
|
||||
}
|
||||
} else if (command.businessKey.startsWith("AGGREGATION:")) {
|
||||
Long itemId = Long.valueOf(command.businessKey.substring(12));
|
||||
Integer current = store.jdbc().queryForObject(
|
||||
"select status from usdt_aggregation_task_item where id=? for update", Integer.class, itemId);
|
||||
if (Integer.valueOf(4).equals(current)) {
|
||||
UsdtAggregationTaskItemPlus item = items.getById(itemId);
|
||||
if (success) {
|
||||
WalletAddressPlus wallet = wallets.getById(command.walletId);
|
||||
ledger.completeAggregationItem(item, wallet, hash, "执行成功;交易哈希:" + hash);
|
||||
} else {
|
||||
item.setStatus(2L);
|
||||
item.setRemark((String) row.get("error_message"));
|
||||
item.setUpdatedAt(new java.util.Date());
|
||||
items.updateById(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
store.jdbc().update("update usdt_chain_execution set reconciled=1 where execution_id=?", id);
|
||||
}
|
||||
|
||||
private void finishAggregationTasks() {
|
||||
// 仅关闭使用新通道且已无待执行/执行中明细的任务,避免干扰原密码批处理。
|
||||
store.jdbc().update("update usdt_aggregation_task t set status=case when exists "
|
||||
+ "(select 1 from usdt_aggregation_task_item i where i.parent_id=t.id and i.status=2 and i.deleted_at is null) "
|
||||
+ "then 3 else 2 end,remark='链上执行结果已同步',updated_at=CURRENT_TIMESTAMP "
|
||||
+ "where t.status=1 and t.remark<>'正在分发链上执行任务' and exists (select 1 from usdt_aggregation_task_item i "
|
||||
+ "join usdt_chain_execution e on e.business_key=concat('AGGREGATION:',i.id) where i.parent_id=t.id) "
|
||||
+ "and not exists (select 1 from usdt_aggregation_task_item i where i.parent_id=t.id "
|
||||
+ "and i.status in (0,4) and i.deleted_at is null)");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user