流水记录优化
This commit is contained in:
1 parent
dcd8eadd20
commit
abdafd5c9d
11 files changed
+238
-28
No files matched your search
@@ -0,0 +1,35 @@
|
|||||||
|
package com.ruoyi.task;
|
||||||
|
|
||||||
|
import com.ruoyi.system.service.WalletLedgerPostingService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/** 定期结算已检测链上交易的订单,并补齐已支付订单缺失的流水。 */
|
||||||
|
@Component
|
||||||
|
public class PaymentLedgerReconciliationTask {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(PaymentLedgerReconciliationTask.class);
|
||||||
|
private static final int BATCH_SIZE = 100;
|
||||||
|
|
||||||
|
private final WalletLedgerPostingService ledgerPostingService;
|
||||||
|
|
||||||
|
public PaymentLedgerReconciliationTask(WalletLedgerPostingService ledgerPostingService) {
|
||||||
|
this.ledgerPostingService = ledgerPostingService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@org.springframework.scheduling.annotation.Scheduled(fixedDelay = 30000L)
|
||||||
|
public void reconcile() {
|
||||||
|
List<Long> orderIds = ledgerPostingService.findOrderIdsRequiringSettlement(BATCH_SIZE);
|
||||||
|
for (Long orderId : orderIds) {
|
||||||
|
try {
|
||||||
|
if (ledgerPostingService.settleDetectedOrRepairPaidOrder(orderId)) {
|
||||||
|
log.info("支付订单账务补偿完成,订单ID={}", orderId);
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error("支付订单账务补偿失败,订单ID={},原因={}", orderId, ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,8 @@ public class PaymentOrderExpireTask {
|
|||||||
public void expireOrders() {
|
public void expireOrders() {
|
||||||
LambdaQueryWrapper<UsdtOrdersPlus> query = new LambdaQueryWrapper<UsdtOrdersPlus>()
|
LambdaQueryWrapper<UsdtOrdersPlus> query = new LambdaQueryWrapper<UsdtOrdersPlus>()
|
||||||
.eq(UsdtOrdersPlus::getStatus, 1)
|
.eq(UsdtOrdersPlus::getStatus, 1)
|
||||||
|
.and(wrapper -> wrapper.isNull(UsdtOrdersPlus::getBlockTransactionId)
|
||||||
|
.or().eq(UsdtOrdersPlus::getBlockTransactionId, ""))
|
||||||
.isNotNull(UsdtOrdersPlus::getCreatedAt)
|
.isNotNull(UsdtOrdersPlus::getCreatedAt)
|
||||||
.lt(UsdtOrdersPlus::getCreatedAt, new Date(System.currentTimeMillis() - 10 * 60 * 1000L));
|
.lt(UsdtOrdersPlus::getCreatedAt, new Date(System.currentTimeMillis() - 10 * 60 * 1000L));
|
||||||
for (UsdtOrdersPlus order : orders.list(query)) {
|
for (UsdtOrdersPlus order : orders.list(query)) {
|
||||||
|
|||||||
@@ -47,7 +47,10 @@ public class TronPaymentTask {
|
|||||||
log.warn("无法读取 TRON 全局网络配置,跳过本轮支付扫描:{}", e.getMessage());
|
log.warn("无法读取 TRON 全局网络配置,跳过本轮支付扫描:{}", e.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (UsdtOrdersPlus order : orders.list(new LambdaQueryWrapper<UsdtOrdersPlus>().eq(UsdtOrdersPlus::getStatus, 1))) {
|
for (UsdtOrdersPlus order : orders.list(new LambdaQueryWrapper<UsdtOrdersPlus>()
|
||||||
|
.eq(UsdtOrdersPlus::getStatus, 1)
|
||||||
|
.and(wrapper -> wrapper.isNull(UsdtOrdersPlus::getBlockTransactionId)
|
||||||
|
.or().eq(UsdtOrdersPlus::getBlockTransactionId, "")))) {
|
||||||
if (order.getToken() == null || order.getActualAmount() == null) continue;
|
if (order.getToken() == null || order.getActualAmount() == null) continue;
|
||||||
try {
|
try {
|
||||||
String url = tron.tronscanApiUrl() + "/v1/accounts/" + order.getToken() + "/transactions/trc20?limit=200&only_confirmed=true&contract_address=" + tron.contractAddress();
|
String url = tron.tronscanApiUrl() + "/v1/accounts/" + order.getToken() + "/transactions/trc20?limit=200&only_confirmed=true&contract_address=" + tron.contractAddress();
|
||||||
@@ -63,9 +66,19 @@ public class TronPaymentTask {
|
|||||||
if (value.compareTo(order.getActualAmount().setScale(6, RoundingMode.DOWN)) != 0) continue;
|
if (value.compareTo(order.getActualAmount().setScale(6, RoundingMode.DOWN)) != 0) continue;
|
||||||
String hash = tx.getString("transaction_id");
|
String hash = tx.getString("transaction_id");
|
||||||
if (hash == null || hash.trim().isEmpty()) continue;
|
if (hash == null || hash.trim().isEmpty()) continue;
|
||||||
|
if (!ledgerPostingService.recordDetectedPayment(order.getId(), hash)) {
|
||||||
|
log.warn("记录链上支付交易失败,订单ID={}", order.getId());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
try {
|
||||||
if (ledgerPostingService.settlePaidOrder(order.getId(), hash)) {
|
if (ledgerPostingService.settlePaidOrder(order.getId(), hash)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} catch (RuntimeException settlementException) {
|
||||||
|
log.error("支付订单账务结算失败,订单ID={},原因={}",
|
||||||
|
order.getId(), settlementException.getMessage());
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 单笔订单查询失败不能中断其他订单,日志中禁止输出凭证和钱包私钥。
|
// 单笔订单查询失败不能中断其他订单,日志中禁止输出凭证和钱包私钥。
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ public interface AppsBalanceLedgerMapper extends BaseMapper<AppsBalanceLedgerPlu
|
|||||||
/** 系统业务幂等写入,重复幂等键不生成新流水。 */
|
/** 系统业务幂等写入,重复幂等键不生成新流水。 */
|
||||||
int insertSystemLedgerIgnoreDuplicate(AppsBalanceLedger ledger);
|
int insertSystemLedgerIgnoreDuplicate(AppsBalanceLedger ledger);
|
||||||
|
|
||||||
|
int countByIdempotencyKey(@Param("idempotencyKey") String idempotencyKey);
|
||||||
|
|
||||||
List<WalletLedgerSummaryVo> selectStatisticsSummary(AppsLedgerStatisticsQuery query);
|
List<WalletLedgerSummaryVo> selectStatisticsSummary(AppsLedgerStatisticsQuery query);
|
||||||
|
|
||||||
List<WalletLedgerTrendVo> selectStatisticsTrend(AppsLedgerStatisticsQuery query);
|
List<WalletLedgerTrendVo> selectStatisticsTrend(AppsLedgerStatisticsQuery query);
|
||||||
|
|||||||
@@ -17,11 +17,18 @@ public interface UsdtOrdersMapper extends BaseMapper<UsdtOrdersPlus>
|
|||||||
{
|
{
|
||||||
UsdtOrdersPlus selectByIdForUpdate(@Param("id") Long id);
|
UsdtOrdersPlus selectByIdForUpdate(@Param("id") Long id);
|
||||||
|
|
||||||
int countPaidByTransactionId(@Param("transactionId") String transactionId,
|
int countByTransactionId(@Param("transactionId") String transactionId,
|
||||||
@Param("excludeOrderId") Long excludeOrderId);
|
@Param("excludeOrderId") Long excludeOrderId);
|
||||||
|
|
||||||
|
/** 在独立事务中记录链上已确认交易,订单仍保持待支付状态等待账务结算。 */
|
||||||
|
int markPaymentDetected(@Param("id") Long id, @Param("transactionId") String transactionId,
|
||||||
|
@Param("updatedAt") java.util.Date updatedAt);
|
||||||
|
|
||||||
int markOrderPaid(@Param("id") Long id, @Param("transactionId") String transactionId,
|
int markOrderPaid(@Param("id") Long id, @Param("transactionId") String transactionId,
|
||||||
@Param("updatedAt") java.util.Date updatedAt);
|
@Param("updatedAt") java.util.Date updatedAt);
|
||||||
|
|
||||||
|
/** 查询链上已确认待结算或已支付但流水不完整的订单,用于后台幂等补偿。 */
|
||||||
|
List<Long> selectOrderIdsRequiringSettlement(@Param("limit") int limit);
|
||||||
/**
|
/**
|
||||||
* 查询订单
|
* 查询订单
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ public interface WalletAddressLedgerMapper extends BaseMapper<WalletAddressLedge
|
|||||||
/** 由系统业务链路幂等写入流水,重复幂等键不产生新记录。 */
|
/** 由系统业务链路幂等写入流水,重复幂等键不产生新记录。 */
|
||||||
int insertSystemLedgerIgnoreDuplicate(WalletAddressLedger walletAddressLedger);
|
int insertSystemLedgerIgnoreDuplicate(WalletAddressLedger walletAddressLedger);
|
||||||
|
|
||||||
|
int countByIdempotencyKey(@Param("idempotencyKey") String idempotencyKey);
|
||||||
|
|
||||||
List<WalletLedgerSummaryVo> selectStatisticsSummary(WalletLedgerStatisticsQuery query);
|
List<WalletLedgerSummaryVo> selectStatisticsSummary(WalletLedgerStatisticsQuery query);
|
||||||
|
|
||||||
List<WalletLedgerTrendVo> selectStatisticsTrend(WalletLedgerStatisticsQuery query);
|
List<WalletLedgerTrendVo> selectStatisticsTrend(WalletLedgerStatisticsQuery query);
|
||||||
|
|||||||
@@ -134,6 +134,8 @@ public class PaymentOrderOpenApiService {
|
|||||||
UsdtOrdersPlus previous = ordersMapper.selectOne(new LambdaQueryWrapper<UsdtOrdersPlus>()
|
UsdtOrdersPlus previous = ordersMapper.selectOne(new LambdaQueryWrapper<UsdtOrdersPlus>()
|
||||||
.eq(UsdtOrdersPlus::getAppId, appId).eq(UsdtOrdersPlus::getToken, token)
|
.eq(UsdtOrdersPlus::getAppId, appId).eq(UsdtOrdersPlus::getToken, token)
|
||||||
.eq(UsdtOrdersPlus::getActualAmount, amount).eq(UsdtOrdersPlus::getStatus, 1L)
|
.eq(UsdtOrdersPlus::getActualAmount, amount).eq(UsdtOrdersPlus::getStatus, 1L)
|
||||||
|
.and(wrapper -> wrapper.isNull(UsdtOrdersPlus::getBlockTransactionId)
|
||||||
|
.or().eq(UsdtOrdersPlus::getBlockTransactionId, ""))
|
||||||
.isNull(UsdtOrdersPlus::getDeletedAt).orderByDesc(UsdtOrdersPlus::getId).last("limit 1"));
|
.isNull(UsdtOrdersPlus::getDeletedAt).orderByDesc(UsdtOrdersPlus::getId).last("limit 1"));
|
||||||
if (previous == null) return null;
|
if (previous == null) return null;
|
||||||
previous.setStatus(3L);
|
previous.setStatus(3L);
|
||||||
|
|||||||
+125
-24
@@ -17,10 +17,12 @@ import com.ruoyi.system.mapper.WalletAddressLedgerMapper;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.support.TransactionSynchronization;
|
import org.springframework.transaction.support.TransactionSynchronization;
|
||||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,28 +65,121 @@ public class WalletLedgerPostingService {
|
|||||||
/** 将待支付订单结算为成功,同一订单重复扫描不会重复产生流水。 */
|
/** 将待支付订单结算为成功,同一订单重复扫描不会重复产生流水。 */
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public boolean settlePaidOrder(Long orderId, String transactionId) {
|
public boolean settlePaidOrder(Long orderId, String transactionId) {
|
||||||
|
if (transactionId == null || transactionId.trim().isEmpty()) {
|
||||||
|
throw new CustomException("链上交易哈希不能为空");
|
||||||
|
}
|
||||||
UsdtOrdersPlus order = ordersMapper.selectByIdForUpdate(orderId);
|
UsdtOrdersPlus order = ordersMapper.selectByIdForUpdate(orderId);
|
||||||
if (order == null) {
|
if (order == null) {
|
||||||
throw new CustomException("订单不存在");
|
throw new CustomException("订单不存在");
|
||||||
}
|
}
|
||||||
if (Long.valueOf(STATUS_PAID).equals(order.getStatus())) {
|
return settleLockedOrder(order, transactionId);
|
||||||
log.warn("订单已经支付 {}",orderId);
|
|
||||||
return transactionId.equals(order.getBlockTransactionId());
|
|
||||||
}
|
}
|
||||||
if (!Long.valueOf(STATUS_PENDING).equals(order.getStatus())) {
|
|
||||||
log.warn("订单状态不正确 {}",orderId);
|
private boolean settleLockedOrder(UsdtOrdersPlus order, String transactionId) {
|
||||||
|
if (Long.valueOf(STATUS_PAID).equals(order.getStatus())) {
|
||||||
|
if (order.getBlockTransactionId() == null
|
||||||
|
|| (transactionId != null && !transactionId.equals(order.getBlockTransactionId()))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (ordersMapper.countPaidByTransactionId(transactionId, orderId) > 0) {
|
ensureOrderLedgers(order, order.getBlockTransactionId(), DateUtils.getNowDate());
|
||||||
log.warn("订单transactionId已经存在 {}",orderId);
|
return true;
|
||||||
|
}
|
||||||
|
if (!Long.valueOf(STATUS_PENDING).equals(order.getStatus())) {
|
||||||
|
log.warn("订单状态不正确 {}", order.getId());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String detectedTransactionId = order.getBlockTransactionId();
|
||||||
|
if (detectedTransactionId != null && !detectedTransactionId.trim().isEmpty()
|
||||||
|
&& !detectedTransactionId.equals(transactionId)) {
|
||||||
|
log.warn("订单已绑定其他链上交易,订单ID={}", order.getId());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (ordersMapper.countByTransactionId(transactionId, order.getId()) > 0) {
|
||||||
|
log.warn("订单transactionId已经存在 {}", order.getId());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WalletAddressPlus wallet = walletService.getByTokenAndAppId(order.getToken(), order.getAppId());
|
|
||||||
Date now = DateUtils.getNowDate();
|
Date now = DateUtils.getNowDate();
|
||||||
|
ensureOrderLedgers(order, transactionId, now);
|
||||||
|
|
||||||
|
if (ordersMapper.markOrderPaid(order.getId(), transactionId, now) != 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
releasePaymentSlotAfterCommit(order);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 先于账务结算独立保存链上交易哈希。即使后续流水写入失败,补偿任务仍可准确重试,
|
||||||
|
* 不再依赖 TronScan 下一轮仍返回同一笔交易。
|
||||||
|
*/
|
||||||
|
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
|
||||||
|
public boolean recordDetectedPayment(Long orderId, String transactionId) {
|
||||||
|
if (orderId == null || transactionId == null || transactionId.trim().isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
UsdtOrdersPlus order = ordersMapper.selectByIdForUpdate(orderId);
|
||||||
|
if (order == null || !Long.valueOf(STATUS_PENDING).equals(order.getStatus())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String existing = order.getBlockTransactionId();
|
||||||
|
if (existing != null && !existing.trim().isEmpty()) {
|
||||||
|
return existing.equals(transactionId);
|
||||||
|
}
|
||||||
|
if (ordersMapper.countByTransactionId(transactionId, orderId) > 0) {
|
||||||
|
log.warn("链上交易已绑定其他订单,订单ID={}", orderId);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ordersMapper.markPaymentDetected(orderId, transactionId, DateUtils.getNowDate()) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 修复已经支付但流水不完整的订单;该方法可安全重复执行。 */
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public boolean repairPaidOrderLedgers(Long orderId) {
|
||||||
|
UsdtOrdersPlus order = ordersMapper.selectByIdForUpdate(orderId);
|
||||||
|
if (order == null || !Long.valueOf(STATUS_PAID).equals(order.getStatus())
|
||||||
|
|| order.getBlockTransactionId() == null
|
||||||
|
|| order.getBlockTransactionId().trim().isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ensureOrderLedgers(order, order.getBlockTransactionId(), DateUtils.getNowDate());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 结算已持久化链上交易的待支付订单,或补齐已支付订单缺失流水。 */
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public boolean settleDetectedOrRepairPaidOrder(Long orderId) {
|
||||||
|
UsdtOrdersPlus order = ordersMapper.selectByIdForUpdate(orderId);
|
||||||
|
if (order == null || order.getBlockTransactionId() == null
|
||||||
|
|| order.getBlockTransactionId().trim().isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!Long.valueOf(STATUS_PENDING).equals(order.getStatus())
|
||||||
|
&& !Long.valueOf(STATUS_PAID).equals(order.getStatus())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return settleLockedOrder(order, order.getBlockTransactionId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Long> findOrderIdsRequiringSettlement(int limit) {
|
||||||
|
int safeLimit = Math.max(1, Math.min(limit, 500));
|
||||||
|
return ordersMapper.selectOrderIdsRequiringSettlement(safeLimit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureOrderLedgers(UsdtOrdersPlus order, String transactionId, Date now) {
|
||||||
|
ensureWalletOrderLedger(order, transactionId, now);
|
||||||
|
postOrderToAppBalance(order, transactionId, now);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureWalletOrderLedger(UsdtOrdersPlus order, String transactionId, Date now) {
|
||||||
|
String idempotencyKey = "ORDER_RECEIVE:" + order.getId() + ":USDT";
|
||||||
|
if (ledgerMapper.countByIdempotencyKey(idempotencyKey) > 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
WalletAddressPlus wallet = walletService.getByTokenAndAppId(order.getToken(), order.getAppId());
|
||||||
WalletAddressLedger ledger = baseLedger(now);
|
WalletAddressLedger ledger = baseLedger(now);
|
||||||
ledger.setTradeNo(order.getTradeId());
|
ledger.setTradeNo(order.getTradeId());
|
||||||
ledger.setIdempotencyKey("ORDER_RECEIVE:" + order.getId() + ":USDT");
|
ledger.setIdempotencyKey(idempotencyKey);
|
||||||
ledger.setAppId(order.getAppId());
|
ledger.setAppId(order.getAppId());
|
||||||
ledger.setUserId(order.getUserId());
|
ledger.setUserId(order.getUserId());
|
||||||
ledger.setWalletAddressId(wallet == null ? null : wallet.getId());
|
ledger.setWalletAddressId(wallet == null ? null : wallet.getId());
|
||||||
@@ -99,14 +194,9 @@ public class WalletLedgerPostingService {
|
|||||||
ledger.setTransactionId(transactionId);
|
ledger.setTransactionId(transactionId);
|
||||||
ledger.setSourceType("ORDER");
|
ledger.setSourceType("ORDER");
|
||||||
ledger.setRemark("订单支付成功");
|
ledger.setRemark("订单支付成功");
|
||||||
ledgerMapper.insertSystemLedgerIgnoreDuplicate(ledger);
|
if (ledgerMapper.insertSystemLedgerIgnoreDuplicate(ledger) != 1) {
|
||||||
|
throw new CustomException("钱包订单流水写入失败");
|
||||||
postOrderToAppBalance(order, transactionId, now);
|
|
||||||
if (ordersMapper.markOrderPaid(order.getId(), transactionId, now) != 1) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
releasePaymentSlotAfterCommit(order);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 数据库确认支付事务提交后再释放 Redis 槽位,避免回滚时提前开放相同地址和金额。 */
|
/** 数据库确认支付事务提交后再释放 Redis 槽位,避免回滚时提前开放相同地址和金额。 */
|
||||||
@@ -132,6 +222,13 @@ public class WalletLedgerPostingService {
|
|||||||
if (app == null) {
|
if (app == null) {
|
||||||
throw new CustomException("订单所属应用不存在");
|
throw new CustomException("订单所属应用不存在");
|
||||||
}
|
}
|
||||||
|
String receiveKey = "ORDER_RECEIVE:" + order.getId() + ":USDT";
|
||||||
|
String feeKey = "ORDER_FEE:" + order.getId() + ":TRX";
|
||||||
|
boolean needReceive = appsLedgerMapper.countByIdempotencyKey(receiveKey) == 0;
|
||||||
|
boolean needFee = appsLedgerMapper.countByIdempotencyKey(feeKey) == 0;
|
||||||
|
if (!needReceive && !needFee) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
UsdtConfig config = configMapper.selectFirstForUpdate();
|
UsdtConfig config = configMapper.selectFirstForUpdate();
|
||||||
if (config == null || config.getReceiveUsdtNum() == null) {
|
if (config == null || config.getReceiveUsdtNum() == null) {
|
||||||
throw new CustomException("全局配置未设置订单代收手续费");
|
throw new CustomException("全局配置未设置订单代收手续费");
|
||||||
@@ -143,17 +240,21 @@ public class WalletLedgerPostingService {
|
|||||||
|
|
||||||
BigDecimal usdtBefore = zeroIfNull(app.getUsdtBalance());
|
BigDecimal usdtBefore = zeroIfNull(app.getUsdtBalance());
|
||||||
BigDecimal trxBefore = zeroIfNull(app.getTrxBalance());
|
BigDecimal trxBefore = zeroIfNull(app.getTrxBalance());
|
||||||
BigDecimal usdtAfter = usdtBefore.add(order.getActualAmount());
|
BigDecimal usdtAfter = needReceive ? usdtBefore.add(order.getActualAmount()) : usdtBefore;
|
||||||
BigDecimal trxAfter = trxBefore.subtract(fee);
|
BigDecimal trxAfter = needFee ? trxBefore.subtract(fee) : trxBefore;
|
||||||
BigDecimal usdtFrozen = zeroIfNull(app.getUsdtFrozenBalance());
|
BigDecimal usdtFrozen = zeroIfNull(app.getUsdtFrozenBalance());
|
||||||
BigDecimal trxFrozen = zeroIfNull(app.getTrxFrozenBalance());
|
BigDecimal trxFrozen = zeroIfNull(app.getTrxFrozenBalance());
|
||||||
|
|
||||||
appsLedgerMapper.insertSystemLedgerIgnoreDuplicate(buildAppLedger(order, transactionId,
|
if (needReceive && appsLedgerMapper.insertSystemLedgerIgnoreDuplicate(buildAppLedger(
|
||||||
"ORDER_RECEIVE", "USDT", 1, order.getActualAmount(), usdtBefore, usdtAfter,
|
order, transactionId, "ORDER_RECEIVE", "USDT", 1, order.getActualAmount(),
|
||||||
usdtFrozen, "订单代收到账", now, app.getUserId()));
|
usdtBefore, usdtAfter, usdtFrozen, "订单代收到账", now, app.getUserId())) != 1) {
|
||||||
appsLedgerMapper.insertSystemLedgerIgnoreDuplicate(buildAppLedger(order, transactionId,
|
throw new CustomException("应用订单收入流水写入失败");
|
||||||
"ORDER_FEE", "TRX", 2, fee, trxBefore, trxAfter,
|
}
|
||||||
trxFrozen, "订单代收手续费", now, app.getUserId()));
|
if (needFee && appsLedgerMapper.insertSystemLedgerIgnoreDuplicate(buildAppLedger(
|
||||||
|
order, transactionId, "ORDER_FEE", "TRX", 2, fee, trxBefore, trxAfter,
|
||||||
|
trxFrozen, "订单代收手续费", now, app.getUserId())) != 1) {
|
||||||
|
throw new CustomException("应用订单手续费流水写入失败");
|
||||||
|
}
|
||||||
|
|
||||||
if (appsMapper.updateAvailableBalances(app.getId(), usdtAfter, trxAfter, now) != 1) {
|
if (appsMapper.updateAvailableBalances(app.getId(), usdtAfter, trxAfter, now) != 1) {
|
||||||
throw new CustomException("应用余额更新失败");
|
throw new CustomException("应用余额更新失败");
|
||||||
|
|||||||
@@ -143,6 +143,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
on duplicate key update id = id
|
on duplicate key update id = id
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
<select id="countByIdempotencyKey" resultType="int">
|
||||||
|
select count(1) from apps_balance_ledger where idempotency_key = #{idempotencyKey}
|
||||||
|
</select>
|
||||||
|
|
||||||
<sql id="statisticsConditions">
|
<sql id="statisticsConditions">
|
||||||
1 = 1
|
1 = 1
|
||||||
<if test="appId != null">and app_id = #{appId}</if>
|
<if test="appId != null">and app_id = #{appId}</if>
|
||||||
|
|||||||
@@ -59,20 +59,58 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
for update
|
for update
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="countPaidByTransactionId" resultType="int">
|
<select id="countByTransactionId" resultType="int">
|
||||||
select count(1)
|
select count(1)
|
||||||
from orders
|
from orders
|
||||||
where block_transaction_id = #{transactionId}
|
where block_transaction_id = #{transactionId}
|
||||||
and status = 2
|
|
||||||
and id != #{excludeOrderId}
|
and id != #{excludeOrderId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<update id="markPaymentDetected">
|
||||||
|
update orders
|
||||||
|
set block_transaction_id = #{transactionId}, updated_at = #{updatedAt}
|
||||||
|
where id = #{id}
|
||||||
|
and status = 1
|
||||||
|
and (block_transaction_id is null or block_transaction_id = '' or block_transaction_id = #{transactionId})
|
||||||
|
and not exists (
|
||||||
|
select 1
|
||||||
|
from (select id, block_transaction_id from orders) existing_order
|
||||||
|
where existing_order.id != #{id}
|
||||||
|
and existing_order.block_transaction_id = #{transactionId}
|
||||||
|
)
|
||||||
|
</update>
|
||||||
|
|
||||||
<update id="markOrderPaid">
|
<update id="markOrderPaid">
|
||||||
update orders
|
update orders
|
||||||
set block_transaction_id = #{transactionId}, status = 2, updated_at = #{updatedAt}
|
set block_transaction_id = #{transactionId}, status = 2, updated_at = #{updatedAt}
|
||||||
where id = #{id} and status = 1
|
where id = #{id} and status = 1
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
<select id="selectOrderIdsRequiringSettlement" resultType="long">
|
||||||
|
select o.id
|
||||||
|
from orders o
|
||||||
|
where o.deleted_at is null
|
||||||
|
and o.block_transaction_id is not null
|
||||||
|
and o.block_transaction_id != ''
|
||||||
|
and (
|
||||||
|
o.status = 1
|
||||||
|
or (o.status = 2 and (not exists (
|
||||||
|
select 1 from wallet_address_ledger w
|
||||||
|
where w.idempotency_key = concat('ORDER_RECEIVE:', o.id, ':USDT')
|
||||||
|
)
|
||||||
|
or not exists (
|
||||||
|
select 1 from apps_balance_ledger a
|
||||||
|
where a.idempotency_key = concat('ORDER_RECEIVE:', o.id, ':USDT')
|
||||||
|
)
|
||||||
|
or not exists (
|
||||||
|
select 1 from apps_balance_ledger f
|
||||||
|
where f.idempotency_key = concat('ORDER_FEE:', o.id, ':TRX')
|
||||||
|
)))
|
||||||
|
)
|
||||||
|
order by o.id
|
||||||
|
limit #{limit}
|
||||||
|
</select>
|
||||||
|
|
||||||
<insert id="insertUsdtOrders" parameterType="UsdtOrders" useGeneratedKeys="true" keyProperty="id">
|
<insert id="insertUsdtOrders" parameterType="UsdtOrders" useGeneratedKeys="true" keyProperty="id">
|
||||||
insert into orders
|
insert into orders
|
||||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
|||||||
@@ -197,6 +197,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
on duplicate key update id = id
|
on duplicate key update id = id
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
<select id="countByIdempotencyKey" resultType="int">
|
||||||
|
select count(1) from wallet_address_ledger where idempotency_key = #{idempotencyKey}
|
||||||
|
</select>
|
||||||
|
|
||||||
<sql id="statisticsConditions">
|
<sql id="statisticsConditions">
|
||||||
status = 1
|
status = 1
|
||||||
<if test="appId != null">and app_id = #{appId}</if>
|
<if test="appId != null">and app_id = #{appId}</if>
|
||||||
|
|||||||
Reference in new issue
Block a user