管理员可以对apps应用的usdtBalance添加和减少,但是需要记录流水表,保证对账单正确。

This commit is contained in:
lu7213 committed 2026-10-18 09:08:35 -04:00
1 parent 7a7222e69c
commit e62e2fe969
5 files changed
+125

No files matched your search

@@ -11,6 +11,7 @@ import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Apps; import com.ruoyi.system.domain.Apps;
import com.ruoyi.system.domain.AppsPlus; import com.ruoyi.system.domain.AppsPlus;
import com.ruoyi.system.service.IAppsService; import com.ruoyi.system.service.IAppsService;
import com.ruoyi.system.domain.vo.AppUsdtBalanceAdjustmentRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@@ -161,6 +162,15 @@ public class AppsController extends BaseController {
return toAjax(appsService.updateApps(apps, false)); return toAjax(appsService.updateApps(apps, false));
} }
/** 管理员人工增加或减少应用 USDT 可用余额,余额与流水在同一事务内更新。 */
@PreAuthorize("@ss.hasPermi('system:apps:balance-adjust')")
@Log(title = "调整应用 USDT 余额", businessType = BusinessType.UPDATE)
@PostMapping("/{id}/usdt-balance-adjustment")
public AjaxResult adjustUsdtBalance(@PathVariable("id") Long id,
@RequestBody AppUsdtBalanceAdjustmentRequest request) {
return AjaxResult.success(appsService.adjustUsdtBalance(id, request));
}
@PreAuthorize("@ss.hasPermi('system:apps:user-edit')") @PreAuthorize("@ss.hasPermi('system:apps:user-edit')")
@Log(title = "应用管理", businessType = BusinessType.UPDATE) @Log(title = "应用管理", businessType = BusinessType.UPDATE)
@PutMapping("/user") @PutMapping("/user")
@@ -0,0 +1,20 @@
package com.ruoyi.system.domain.vo;
import java.math.BigDecimal;
/** 管理员调整应用 USDT 可用余额的请求参数。 */
public class AppUsdtBalanceAdjustmentRequest {
/** 调整类型:ADD 增加,SUBTRACT 减少。 */
private String type;
/** 调整金额,必须大于 0,最多 6 位小数。 */
private BigDecimal amount;
/** 调整原因,将完整写入资金流水。 */
private String remark;
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public BigDecimal getAmount() { return amount; }
public void setAmount(BigDecimal amount) { this.amount = amount; }
public String getRemark() { return remark; }
public void setRemark(String remark) { this.remark = remark; }
}
@@ -6,6 +6,7 @@ import com.ruoyi.system.domain.AppsPlus;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ruoyi.common.select.SelectItem; import com.ruoyi.common.select.SelectItem;
import com.ruoyi.system.domain.vo.AppUsdtBalanceAdjustmentRequest;
/** /**
* 应用管理Service接口 * 应用管理Service接口
* *
@@ -24,6 +25,9 @@ public interface IAppsService extends IService<AppsPlus>
/** 获取新应用固定使用的全局归集地址。 */ /** 获取新应用固定使用的全局归集地址。 */
String getGlobalAggregationAddress(); String getGlobalAggregationAddress();
/** 管理员调整应用 USDT 可用余额,并在同一事务中记录资金流水。 */
Apps adjustUsdtBalance(Long id, AppUsdtBalanceAdjustmentRequest request);
List<SelectItem> select(String keyword,boolean isUser); List<SelectItem> select(String keyword,boolean isUser);
/** /**
* 查询应用管理 * 查询应用管理
@@ -17,6 +17,9 @@ import com.ruoyi.system.domain.UsdtConfig;
import com.ruoyi.system.service.IAppsService; import com.ruoyi.system.service.IAppsService;
import com.ruoyi.system.service.AppPrivateKeyCipher; import com.ruoyi.system.service.AppPrivateKeyCipher;
import com.ruoyi.system.domain.vo.AppKeyPairResult; import com.ruoyi.system.domain.vo.AppKeyPairResult;
import com.ruoyi.system.domain.vo.AppUsdtBalanceAdjustmentRequest;
import com.ruoyi.system.domain.AppsBalanceLedger;
import com.ruoyi.system.mapper.AppsBalanceLedgerMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -48,6 +51,11 @@ public class AppsServiceImpl extends ServiceImpl<AppsMapper, AppsPlus> implement
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@Autowired @Autowired
private AppPrivateKeyCipher privateKeyCipher; private AppPrivateKeyCipher privateKeyCipher;
@Autowired
private AppsBalanceLedgerMapper appsBalanceLedgerMapper;
private static final String BALANCE_ADD = "ADD";
private static final String BALANCE_SUBTRACT = "SUBTRACT";
/** /**
* 生成与平台加解密约定一致的 RSA 密钥对。 * 生成与平台加解密约定一致的 RSA 密钥对。
@@ -89,6 +97,77 @@ public class AppsServiceImpl extends ServiceImpl<AppsMapper, AppsPlus> implement
return configs.get(0).getTokenAddress(); return configs.get(0).getTokenAddress();
} }
@Override
@Transactional(rollbackFor = Exception.class)
public Apps adjustUsdtBalance(Long id, AppUsdtBalanceAdjustmentRequest request) {
if (id == null || request == null) {
throw new CustomException("应用和调整参数不能为空");
}
String type = StringUtils.trim(request.getType()).toUpperCase(Locale.ROOT);
if (!BALANCE_ADD.equals(type) && !BALANCE_SUBTRACT.equals(type)) {
throw new CustomException("调整类型只能为增加或减少");
}
BigDecimal amount = request.getAmount();
if (amount == null || amount.signum() <= 0) {
throw new CustomException("调整金额必须大于 0");
}
if (amount.scale() > 6 || amount.precision() - amount.scale() > 24) {
throw new CustomException("调整金额最多支持 24 位整数和 6 位小数");
}
String remark = StringUtils.trim(request.getRemark());
if (StringUtils.isBlank(remark)) {
throw new CustomException("请填写余额调整原因");
}
if (remark.length() > 500) {
throw new CustomException("调整原因不能超过 500 个字符");
}
// 订单入账、代付结算和人工调整均锁定同一应用行,保证余额和流水顺序一致。
Apps app = appsMapper.selectAppsByIdForUpdate(id);
if (app == null) {
throw new CustomException("商户不存在或已删除");
}
BigDecimal before = app.getUsdtBalance() == null ? BigDecimal.ZERO : app.getUsdtBalance();
BigDecimal after = BALANCE_ADD.equals(type) ? before.add(amount) : before.subtract(amount);
if (after.signum() < 0) {
throw new CustomException("USDT 可用余额不足,当前可用余额为 " + before.toPlainString());
}
Date now = DateUtils.getNowDate();
if (appsMapper.updateAvailableBalances(app.getId(), after,
app.getTrxBalance() == null ? BigDecimal.ZERO : 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.setAppId(app.getId());
ledger.setUserId(app.getUserId());
ledger.setCurrency("USDT");
ledger.setBalanceType(1);
ledger.setBusinessType(BALANCE_ADD.equals(type) ? "ADMIN_BALANCE_ADD" : "ADMIN_BALANCE_SUBTRACT");
ledger.setDirection(BALANCE_ADD.equals(type) ? 1 : 2);
ledger.setAmount(amount);
ledger.setBalanceBefore(before);
ledger.setBalanceAfter(after);
ledger.setAvailableBefore(before);
ledger.setAvailableAfter(after);
BigDecimal frozen = app.getUsdtFrozenBalance() == null ? BigDecimal.ZERO : app.getUsdtFrozenBalance();
ledger.setFrozenBefore(frozen);
ledger.setFrozenAfter(frozen);
ledger.setOperatorId(SecurityUtils.getUserId());
ledger.setRequestId(tradeNo);
ledger.setRemark("管理员人工调整:" + remark + ";操作账号:" + SecurityUtils.getUsername());
ledger.setCreatedAt(now);
if (appsBalanceLedgerMapper.insertAppsBalanceLedger(ledger) != 1) {
throw new CustomException("应用余额流水记录失败");
}
app.setUsdtBalance(after);
app.setUpdatedAt(now);
return app;
}
@Override @Override
public List<AppsPlus> getByIds(List<Long> ids, boolean isUser, SFunction<AppsPlus, ?>... columns) { public List<AppsPlus> getByIds(List<Long> ids, boolean isUser, SFunction<AppsPlus, ?>... columns) {
LambdaQueryWrapper<AppsPlus> lambdaQueryWrapper = new LambdaQueryWrapper<AppsPlus>() LambdaQueryWrapper<AppsPlus> lambdaQueryWrapper = new LambdaQueryWrapper<AppsPlus>()
+12
View File
@@ -0,0 +1,12 @@
-- 应用 USDT 可用余额人工调整权限。
-- 执行后需在角色管理中将该按钮权限分配给允许操作资金的管理员角色。
INSERT INTO sys_menu
(menu_name, parent_id, order_num, path, component, is_frame, menu_type,
visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '调整应用USDT余额', parent_id, 90, '#', '', 1, 'F', '0', '0',
'system:apps:balance-adjust', '#', 'admin', NOW(), '', NULL,
'管理员增加或减少应用USDT可用余额,并记录应用资金流水'
FROM sys_menu
WHERE perms = 'system:apps:list'
AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:apps:balance-adjust')
LIMIT 1;