address和USDTmanage/address token管理页面,参考归集任务页面布局和功能,添加统计功能,优化页面布局。 商户和管理员都去掉删除和修改,商户的详情页面去掉对地址的各种金额操作,只保留刷新链上数据功能。

This commit is contained in:
lu7213 committed 2026-10-19 05:33:31 -04:00
1 parent c516124eff
commit e4ac32e8d7
4 files changed
+140 -9

No files matched your search

@@ -174,6 +174,15 @@ public class AppsController extends BaseController {
return toAjax(appsService.updateApps(apps, false));
}
/** TRX 与 USDT 使用同一财务权限和行锁,冻结余额不参与人工调整。 */
@PreAuthorize("@ss.hasPermi('system:apps:balance-adjust')")
@Log(title = "调整应用 TRX 余额", businessType = BusinessType.UPDATE)
@PostMapping("/{id}/trx-balance-adjustment")
public AjaxResult adjustTrxBalance(@PathVariable("id") Long id,
@RequestBody AppUsdtBalanceAdjustmentRequest request) {
return AjaxResult.success(appsService.adjustTrxBalance(id, request));
}
/** 管理员人工增加或减少应用 USDT 可用余额,余额与流水在同一事务内更新。 */
@PreAuthorize("@ss.hasPermi('system:apps:balance-adjust')")
@Log(title = "调整应用 USDT 余额", businessType = BusinessType.UPDATE)
@@ -28,6 +28,9 @@ public interface IAppsService extends IService<AppsPlus>
/** 管理员调整应用 USDT 可用余额,并在同一事务中记录资金流水。 */
Apps adjustUsdtBalance(Long id, AppUsdtBalanceAdjustmentRequest request);
/** 管理员调整 TRX 可用余额,锁定应用并原子记录币种流水。 */
Apps adjustTrxBalance(Long id, AppUsdtBalanceAdjustmentRequest request);
List<SelectItem> select(String keyword,boolean isUser);
/**
* 查询应用管理
@@ -100,10 +100,22 @@ public class AppsServiceImpl extends ServiceImpl<AppsMapper, AppsPlus> implement
@Override
@Transactional(rollbackFor = Exception.class)
public Apps adjustUsdtBalance(Long id, AppUsdtBalanceAdjustmentRequest request) {
return adjustAvailableBalance(id, request, false);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Apps adjustTrxBalance(Long id, AppUsdtBalanceAdjustmentRequest request) {
return adjustAvailableBalance(id, request, true);
}
/** 两个公开事务入口共用校验、应用行锁及流水写入,避免币种逻辑分叉。 */
private Apps adjustAvailableBalance(Long id, AppUsdtBalanceAdjustmentRequest request, boolean trx) {
String currency = trx ? "TRX" : "USDT";
if (id == null || request == null) {
throw new CustomException("应用和调整参数不能为空");
}
String type = StringUtils.trim(request.getType()).toUpperCase(Locale.ROOT);
String type = request.getType() == null ? "" : request.getType().trim().toUpperCase(Locale.ROOT);
if (!BALANCE_ADD.equals(type) && !BALANCE_SUBTRACT.equals(type)) {
throw new CustomException("调整类型只能为增加或减少");
}
@@ -127,24 +139,28 @@ public class AppsServiceImpl extends ServiceImpl<AppsMapper, AppsPlus> implement
if (app == null) {
throw new CustomException("商户不存在或已删除");
}
BigDecimal before = app.getUsdtBalance() == null ? BigDecimal.ZERO : app.getUsdtBalance();
BigDecimal currentBalance = trx ? app.getTrxBalance() : app.getUsdtBalance();
BigDecimal before = currentBalance == null ? BigDecimal.ZERO : currentBalance;
BigDecimal after = BALANCE_ADD.equals(type) ? before.add(amount) : before.subtract(amount);
if (after.signum() < 0) {
throw new CustomException("USDT 可用余额不足,当前可用余额为 " + before.toPlainString());
if (BALANCE_SUBTRACT.equals(type) && after.signum() < 0) {
throw new CustomException(currency + " 可用余额不足,当前可用余额为 " + before.toPlainString());
}
if (after.precision() - after.scale() > 24) {
throw new CustomException("调整后余额超出 DECIMAL(30,6) 范围");
}
Date now = DateUtils.getNowDate();
if (appsMapper.updateAvailableBalances(app.getId(), after,
app.getTrxBalance() == null ? BigDecimal.ZERO : app.getTrxBalance(), now) != 1) {
if (appsMapper.updateAvailableBalances(app.getId(), trx ? app.getUsdtBalance() : after,
trx ? after : app.getTrxBalance(), now) != 1) {
throw new CustomException("应用余额更新失败");
}
String tradeNo = "ADJ" + UUID.randomUUID().toString().replace("-", "").toUpperCase(Locale.ROOT);
AppsBalanceLedger ledger = new AppsBalanceLedger();
ledger.setTradeNo(tradeNo);
ledger.setIdempotencyKey("ADMIN_USDT_ADJUST:" + tradeNo);
ledger.setIdempotencyKey("ADMIN_" + currency + "_ADJUST:" + tradeNo);
ledger.setAppId(app.getId());
ledger.setUserId(app.getUserId());
ledger.setCurrency("USDT");
ledger.setCurrency(currency);
ledger.setBalanceType(1);
ledger.setBusinessType(BALANCE_ADD.equals(type) ? "ADMIN_BALANCE_ADD" : "ADMIN_BALANCE_SUBTRACT");
ledger.setDirection(BALANCE_ADD.equals(type) ? 1 : 2);
@@ -153,7 +169,8 @@ public class AppsServiceImpl extends ServiceImpl<AppsMapper, AppsPlus> implement
ledger.setBalanceAfter(after);
ledger.setAvailableBefore(before);
ledger.setAvailableAfter(after);
BigDecimal frozen = app.getUsdtFrozenBalance() == null ? BigDecimal.ZERO : app.getUsdtFrozenBalance();
BigDecimal frozenBalance = trx ? app.getTrxFrozenBalance() : app.getUsdtFrozenBalance();
BigDecimal frozen = frozenBalance == null ? BigDecimal.ZERO : frozenBalance;
ledger.setFrozenBefore(frozen);
ledger.setFrozenAfter(frozen);
ledger.setOperatorId(SecurityUtils.getUserId());
@@ -163,7 +180,11 @@ public class AppsServiceImpl extends ServiceImpl<AppsMapper, AppsPlus> implement
if (appsBalanceLedgerMapper.insertAppsBalanceLedger(ledger) != 1) {
throw new CustomException("应用余额流水记录失败");
}
if (trx) {
app.setTrxBalance(after);
} else {
app.setUsdtBalance(after);
}
app.setUpdatedAt(now);
return app;
}
@@ -0,0 +1,98 @@
package com.ruoyi.system.service;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.Apps;
import com.ruoyi.system.domain.AppsBalanceLedger;
import com.ruoyi.system.domain.vo.AppUsdtBalanceAdjustmentRequest;
import com.ruoyi.system.mapper.AppsMapper;
import com.ruoyi.system.mapper.AppsBalanceLedgerMapper;
import com.ruoyi.system.service.impl.AppsServiceImpl;
import org.junit.After;
import org.junit.Test;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
/** 人工 TRX 调整的账务边界测试;Mapper 使用隔离替身,不访问生产数据库。 */
public class AppsTrxAdjustmentTest {
@After
public void clearLogin() { SecurityContextHolder.clearContext(); }
@Test
public void trxAdjustmentKeepsOtherCurrencyAndFrozenBalances() throws Exception {
List<AppsBalanceLedger> entries = new ArrayList<>();
AppsServiceImpl service = service("10", entries);
service.adjustTrxBalance(1L, request("ADD", "2.123456"));
AppsBalanceLedger entry = entries.get(0);
assertEquals("TRX", entry.getCurrency());
assertEquals("ADMIN_BALANCE_ADD", entry.getBusinessType());
assertEquals(0, entry.getAvailableBefore().compareTo(new BigDecimal("10")));
assertEquals(0, entry.getAvailableAfter().compareTo(new BigDecimal("12.123456")));
assertEquals(0, entry.getFrozenBefore().compareTo(new BigDecimal("3")));
assertEquals(entry.getFrozenBefore(), entry.getFrozenAfter());
service.adjustTrxBalance(1L, request("SUBTRACT", "1"));
assertEquals("ADMIN_BALANCE_SUBTRACT", entries.get(1).getBusinessType());
assertEquals(entry.getAvailableAfter(), entries.get(1).getAvailableBefore());
}
@Test
public void insufficientTrxCannotWriteLedger() throws Exception {
List<AppsBalanceLedger> entries = new ArrayList<>();
try {
service("1", entries).adjustTrxBalance(1L, request("SUBTRACT", "2"));
fail("Should reject insufficient balance");
} catch (CustomException expected) {
assertTrue(entries.isEmpty());
}
}
@Test
public void negativeFeeBalanceCanBeReplenished() throws Exception {
List<AppsBalanceLedger> entries = new ArrayList<>();
Apps app = service("-10", entries).adjustTrxBalance(1L, request("ADD", "2"));
assertEquals(0, app.getTrxBalance().compareTo(new BigDecimal("-8")));
assertEquals(1, entries.size());
}
private AppsServiceImpl service(String trx, List<AppsBalanceLedger> entries) throws Exception {
SysUser user = new SysUser(); user.setUserId(1L); user.setUserName("finance");
LoginUser login = new LoginUser(); login.setUser(user);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(login, null));
Apps app = new Apps(); app.setId(1L); app.setUserId(2L);
app.setUsdtBalance(new BigDecimal("100")); app.setTrxBalance(new BigDecimal(trx));
app.setTrxFrozenBalance(new BigDecimal("3"));
AppsServiceImpl service = new AppsServiceImpl();
inject(service, "appsMapper", Proxy.newProxyInstance(AppsMapper.class.getClassLoader(), new Class[]{AppsMapper.class}, (proxy, method, args) -> {
if ("selectAppsByIdForUpdate".equals(method.getName())) return app;
if ("updateAvailableBalances".equals(method.getName())) {
assertEquals(app.getUsdtBalance(), args[1]);
return 1;
}
throw new UnsupportedOperationException(method.getName());
}));
inject(service, "appsBalanceLedgerMapper", Proxy.newProxyInstance(AppsBalanceLedgerMapper.class.getClassLoader(), new Class[]{AppsBalanceLedgerMapper.class}, (proxy, method, args) -> {
if ("insertAppsBalanceLedger".equals(method.getName())) { entries.add((AppsBalanceLedger) args[0]); return 1; }
throw new UnsupportedOperationException(method.getName());
}));
return service;
}
private void inject(Object target, String name, Object value) throws Exception {
Field field = target.getClass().getDeclaredField(name); field.setAccessible(true); field.set(target, value);
}
private AppUsdtBalanceAdjustmentRequest request(String type, String amount) {
AppUsdtBalanceAdjustmentRequest request = new AppUsdtBalanceAdjustmentRequest();
request.setType(type); request.setAmount(new BigDecimal(amount)); request.setRemark("财务调整测试");
return request;
}
}