From 0ff174e872ed74ed8daebde8e3ff398cad521fba Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Tue, 13 Oct 2026 10:32:18 -0400 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=94=AF=E4=BB=98=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E6=97=B6=EF=BC=8C=E4=BA=8B=E5=8A=A1=E5=8C=96=E5=86=99?= =?UTF-8?q?=E5=85=A5=20USDT=20=E6=94=B6=E5=85=A5=E6=B5=81=E6=B0=B4?= =?UTF-8?q?=E3=80=82=20=E5=BD=92=E9=9B=86=E6=98=8E=E7=BB=86=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E6=97=B6=EF=BC=8C=E4=BA=8B=E5=8A=A1=E5=8C=96=E5=86=99?= =?UTF-8?q?=E5=85=A5=20USDT=20=E6=94=AF=E5=87=BA=E6=B5=81=E6=B0=B4?= =?UTF-8?q?=E3=80=82=20=E4=BD=BF=E7=94=A8=E5=94=AF=E4=B8=80=E5=B9=82?= =?UTF-8?q?=E7=AD=89=E9=94=AE=E9=98=B2=E6=AD=A2=E8=AE=A2=E5=8D=95=E9=87=8D?= =?UTF-8?q?=E6=89=AB=E3=80=81=E5=BD=92=E9=9B=86=E9=87=8D=E8=AF=95=E4=BA=A7?= =?UTF-8?q?=E7=94=9F=E9=87=8D=E5=A4=8D=E6=B5=81=E6=B0=B4=E3=80=82=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8C=89=E5=BA=94=E7=94=A8=E3=80=81=E5=B8=81?= =?UTF-8?q?=E7=A7=8D=E3=80=81=E6=97=B6=E9=97=B4=E8=8C=83=E5=9B=B4=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E6=97=A5/=E6=9C=88/=E5=B9=B4=E7=BB=B4=E5=BA=A6?= =?UTF-8?q?=E7=9A=84=E7=BB=9F=E8=AE=A1=E6=8E=A5=E5=8F=A3=E3=80=82=20?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E5=92=8C=E5=95=86=E6=88=B7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=9D=87=E6=94=B9=E4=B8=BA=E5=8F=AA=E8=AF=BB=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=B5=81=E6=B0=B4=E6=96=B0=E5=A2=9E=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E3=80=81=E5=88=A0=E9=99=A4=E5=85=A5=E5=8F=A3?= =?UTF-8?q?=E3=80=82=20=E5=95=86=E6=88=B7=E6=9F=A5=E8=AF=A2=E7=94=B1?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E5=BC=BA=E5=88=B6=E9=99=90=E5=AE=9A=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E7=94=A8=E6=88=B7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/task/TronPaymentTask.java | 14 +- .../WalletAddressLedgerController.java | 97 ++---------- .../vo/WalletLedgerStatisticsQuery.java | 32 ++++ .../domain/vo/WalletLedgerStatisticsVo.java | 20 +++ .../domain/vo/WalletLedgerSummaryVo.java | 23 +++ .../system/domain/vo/WalletLedgerTrendVo.java | 11 ++ .../ruoyi/system/mapper/UsdtOrdersMapper.java | 7 + .../mapper/WalletAddressLedgerMapper.java | 10 ++ .../service/IWalletAddressLedgerService.java | 46 +----- .../UsdtAggregationWorkflowService.java | 13 +- .../service/WalletLedgerPostingService.java | 119 +++++++++++++++ .../impl/WalletAddressLedgerServiceImpl.java | 139 ++++-------------- .../mapper/system/UsdtOrdersMapper.xml | 25 +++- .../system/WalletAddressLedgerMapper.xml | 64 +++++++- sql/wallet_address_ledger_indexes.sql | 11 ++ 15 files changed, 377 insertions(+), 254 deletions(-) create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerStatisticsQuery.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerStatisticsVo.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerSummaryVo.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerTrendVo.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/service/WalletLedgerPostingService.java create mode 100644 sql/wallet_address_ledger_indexes.sql diff --git a/ruoyi-admin/src/main/java/com/ruoyi/task/TronPaymentTask.java b/ruoyi-admin/src/main/java/com/ruoyi/task/TronPaymentTask.java index 182b1cd..cae290f 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/task/TronPaymentTask.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/task/TronPaymentTask.java @@ -7,6 +7,7 @@ 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.WalletLedgerPostingService; import com.ruoyi.system.service.TronServiceProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,7 +18,6 @@ import org.springframework.web.client.RestTemplate; import java.math.BigDecimal; import java.math.RoundingMode; -import java.util.Date; /** * 轮询已确认 TRC20 转账,按地址、六位金额和唯一交易哈希入账。 @@ -30,6 +30,8 @@ public class TronPaymentTask { private IUsdtOrdersService orders; @Autowired private TronServiceProvider tronServiceProvider; + @Autowired + private WalletLedgerPostingService ledgerPostingService; private final RestTemplate http = new RestTemplate(); @Scheduled(fixedDelay = 30000L) @@ -55,13 +57,9 @@ public class TronPaymentTask { if (value.compareTo(order.getActualAmount().setScale(6, RoundingMode.DOWN)) != 0) continue; String hash = tx.getString("transaction_id"); if (hash == null || hash.trim().isEmpty()) continue; - if (orders.count(new LambdaQueryWrapper().eq(UsdtOrdersPlus::getBlockTransactionId, hash)) > 0) - continue; - order.setBlockTransactionId(hash); - order.setStatus(2L); - order.setUpdatedAt(new Date()); - orders.updateById(order); - break; + if (ledgerPostingService.settlePaidOrder(order.getId(), hash)) { + break; + } } } catch (Exception e) { // 单笔订单查询失败不能中断其他订单,日志中禁止输出凭证和钱包私钥。 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controller/WalletAddressLedgerController.java b/ruoyi-system/src/main/java/com/ruoyi/system/controller/WalletAddressLedgerController.java index b6bde31..38d91eb 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controller/WalletAddressLedgerController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controller/WalletAddressLedgerController.java @@ -5,11 +5,9 @@ import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; -import com.ruoyi.common.select.Ids; -import com.ruoyi.common.select.SelectItem; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.system.domain.WalletAddressLedger; -import com.ruoyi.system.domain.WalletAddressLedgerPlus; +import com.ruoyi.system.domain.vo.WalletLedgerStatisticsQuery; import com.ruoyi.system.service.IWalletAddressLedgerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; @@ -29,34 +27,6 @@ public class WalletAddressLedgerController extends BaseController { @Autowired private IWalletAddressLedgerService walletAddressLedgerService; - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:getByIds')") - @PostMapping("/getByIds") - public AjaxResult getByIds(@RequestBody Ids ids) - { - List list = walletAddressLedgerService.getByIds(ids.getLongIds(),false,WalletAddressLedgerPlus::getWalletAddress,WalletAddressLedgerPlus::getId); - return AjaxResult.success(list); - } - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:user-getByIds')") - @PostMapping("/user/getByIds") - public AjaxResult userGetByIds(@RequestBody Ids ids) - { - List list = walletAddressLedgerService.getByIds(ids.getLongIds(),true,WalletAddressLedgerPlus::getUserId,WalletAddressLedgerPlus::getWalletAddress,WalletAddressLedgerPlus::getId); - return AjaxResult.success(list); - } - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:select')") - @GetMapping("/select") - public AjaxResult select(String keyword) - { - List list = walletAddressLedgerService.select(keyword,false); - return AjaxResult.success(list); - } - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:user-select')") - @GetMapping("/user/select") - public AjaxResult userSelect(String keyword) - { - List list = walletAddressLedgerService.select(keyword,true); - return AjaxResult.success(list); - } /** * 查询钱包地址资金流水列表 */ @@ -110,61 +80,16 @@ public class WalletAddressLedgerController extends BaseController { return AjaxResult.success(walletAddressLedgerService.selectWalletAddressLedgerById(id,true)); } - /** - * 新增钱包地址资金流水 - */ - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:add')") - @Log(title = "钱包地址资金流水", businessType = BusinessType.INSERT) - @PostMapping - public AjaxResult add(@RequestBody WalletAddressLedger walletAddressLedger) - { - return toAjax(walletAddressLedgerService.insertWalletAddressLedger(walletAddressLedger,false)); + + @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:list')") + @GetMapping("/statistics") + public AjaxResult statistics(WalletLedgerStatisticsQuery query) { + return AjaxResult.success(walletAddressLedgerService.statistics(query, false)); } - /** - * 新增钱包地址资金流水 - */ - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:user-add')") - @Log(title = "钱包地址资金流水", businessType = BusinessType.INSERT) - @PostMapping("/user") - public AjaxResult userAdd(@RequestBody WalletAddressLedger walletAddressLedger) - { - return toAjax(walletAddressLedgerService.insertWalletAddressLedger(walletAddressLedger,true)); - } - /** - * 修改钱包地址资金流水 - */ - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:edit')") - @Log(title = "钱包地址资金流水", businessType = BusinessType.UPDATE) - @PutMapping - public AjaxResult edit(@RequestBody WalletAddressLedger walletAddressLedger) - { - return toAjax(walletAddressLedgerService.updateWalletAddressLedger(walletAddressLedger,false)); - } - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:user-edit')") - @Log(title = "钱包地址资金流水", businessType = BusinessType.UPDATE) - @PutMapping("/user") - public AjaxResult userEdit(@RequestBody WalletAddressLedger walletAddressLedger) - { - return toAjax(walletAddressLedgerService.updateWalletAddressLedger(walletAddressLedger,true)); - } - /** - * 删除钱包地址资金流水 - */ - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:remove')") - @Log(title = "钱包地址资金流水", businessType = BusinessType.DELETE) - @DeleteMapping("/{ids}") - public AjaxResult remove(@PathVariable Long[] ids) - { - return toAjax(walletAddressLedgerService.deleteWalletAddressLedgerByIds(ids,false)); - } - /** - * 删除钱包地址资金流水 - */ - @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:user-remove')") - @Log(title = "钱包地址资金流水", businessType = BusinessType.DELETE) - @DeleteMapping("/user/{ids}") - public AjaxResult userRemove(@PathVariable Long[] ids) - { - return toAjax(walletAddressLedgerService.deleteWalletAddressLedgerByIds(ids,true)); + + @PreAuthorize("@ss.hasPermi('system:walletAddressLedger:user-list')") + @GetMapping("/user/statistics") + public AjaxResult userStatistics(WalletLedgerStatisticsQuery query) { + return AjaxResult.success(walletAddressLedgerService.statistics(query, true)); } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerStatisticsQuery.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerStatisticsQuery.java new file mode 100644 index 0000000..e409735 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerStatisticsQuery.java @@ -0,0 +1,32 @@ +package com.ruoyi.system.domain.vo; + +import java.util.Date; + +import org.springframework.format.annotation.DateTimeFormat; + +/** 钱包流水统计查询条件。 */ +public class WalletLedgerStatisticsQuery { + private Long appId; + private Long userId; + private String currency; + private String granularity; + + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date beginTime; + + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date endTime; + + public Long getAppId() { return appId; } + public void setAppId(Long appId) { this.appId = appId; } + public Long getUserId() { return userId; } + public void setUserId(Long userId) { this.userId = userId; } + public String getCurrency() { return currency; } + public void setCurrency(String currency) { this.currency = currency; } + public String getGranularity() { return granularity; } + public void setGranularity(String granularity) { this.granularity = granularity; } + public Date getBeginTime() { return beginTime; } + public void setBeginTime(Date beginTime) { this.beginTime = beginTime; } + public Date getEndTime() { return endTime; } + public void setEndTime(Date endTime) { this.endTime = endTime; } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerStatisticsVo.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerStatisticsVo.java new file mode 100644 index 0000000..ae4af4a --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerStatisticsVo.java @@ -0,0 +1,20 @@ +package com.ruoyi.system.domain.vo; + +import java.util.List; + +/** 钱包流水统计响应。 */ +public class WalletLedgerStatisticsVo { + private List summaries; + private List trends; + + public WalletLedgerStatisticsVo(List summaries, + List trends) { + this.summaries = summaries; + this.trends = trends; + } + + public List getSummaries() { return summaries; } + public void setSummaries(List summaries) { this.summaries = summaries; } + public List getTrends() { return trends; } + public void setTrends(List trends) { this.trends = trends; } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerSummaryVo.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerSummaryVo.java new file mode 100644 index 0000000..17273c9 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerSummaryVo.java @@ -0,0 +1,23 @@ +package com.ruoyi.system.domain.vo; + +import java.math.BigDecimal; + +/** 单币种钱包流水汇总。 */ +public class WalletLedgerSummaryVo { + private String currency; + private BigDecimal incomeAmount; + private BigDecimal expenseAmount; + private BigDecimal netAmount; + private Long count; + + public String getCurrency() { return currency; } + public void setCurrency(String currency) { this.currency = currency; } + public BigDecimal getIncomeAmount() { return incomeAmount; } + public void setIncomeAmount(BigDecimal incomeAmount) { this.incomeAmount = incomeAmount; } + public BigDecimal getExpenseAmount() { return expenseAmount; } + public void setExpenseAmount(BigDecimal expenseAmount) { this.expenseAmount = expenseAmount; } + public BigDecimal getNetAmount() { return netAmount; } + public void setNetAmount(BigDecimal netAmount) { this.netAmount = netAmount; } + public Long getCount() { return count; } + public void setCount(Long count) { this.count = count; } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerTrendVo.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerTrendVo.java new file mode 100644 index 0000000..d0763ab --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/WalletLedgerTrendVo.java @@ -0,0 +1,11 @@ +package com.ruoyi.system.domain.vo; + +import java.math.BigDecimal; + +/** 钱包流水趋势点。 */ +public class WalletLedgerTrendVo extends WalletLedgerSummaryVo { + private String period; + + public String getPeriod() { return period; } + public void setPeriod(String period) { this.period = period; } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/UsdtOrdersMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/UsdtOrdersMapper.java index 8bb2bb9..4895895 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/UsdtOrdersMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/UsdtOrdersMapper.java @@ -15,6 +15,13 @@ import com.ruoyi.system.domain.UsdtOrdersPlus; */ public interface UsdtOrdersMapper extends BaseMapper { + UsdtOrdersPlus selectByIdForUpdate(@Param("id") Long id); + + int countPaidByTransactionId(@Param("transactionId") String transactionId, + @Param("excludeOrderId") Long excludeOrderId); + + int markOrderPaid(@Param("id") Long id, @Param("transactionId") String transactionId, + @Param("updatedAt") java.util.Date updatedAt); /** * 查询订单 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/WalletAddressLedgerMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/WalletAddressLedgerMapper.java index 290e10d..3ba234b 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/WalletAddressLedgerMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/WalletAddressLedgerMapper.java @@ -6,6 +6,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; import com.ruoyi.system.domain.WalletAddressLedger; import com.ruoyi.system.domain.WalletAddressLedgerPlus; +import com.ruoyi.system.domain.vo.WalletLedgerStatisticsQuery; +import com.ruoyi.system.domain.vo.WalletLedgerSummaryVo; +import com.ruoyi.system.domain.vo.WalletLedgerTrendVo; /** * 钱包地址资金流水Mapper接口 @@ -31,6 +34,13 @@ public interface WalletAddressLedgerMapper extends BaseMapper selectWalletAddressLedgerList(WalletAddressLedger walletAddressLedger); + /** 由系统业务链路幂等写入流水,重复幂等键不产生新记录。 */ + int insertSystemLedgerIgnoreDuplicate(WalletAddressLedger walletAddressLedger); + + List selectStatisticsSummary(WalletLedgerStatisticsQuery query); + + List selectStatisticsTrend(WalletLedgerStatisticsQuery query); + /** * 新增钱包地址资金流水 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IWalletAddressLedgerService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IWalletAddressLedgerService.java index f845d7e..b26c51f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/IWalletAddressLedgerService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IWalletAddressLedgerService.java @@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.ruoyi.common.select.SelectItem; import com.ruoyi.system.domain.WalletAddressLedger; import com.ruoyi.system.domain.WalletAddressLedgerPlus; +import com.ruoyi.system.domain.vo.WalletLedgerStatisticsQuery; +import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo; import java.util.List; import java.util.Map; @@ -34,37 +36,7 @@ public interface IWalletAddressLedgerService extends IService selectWalletAddressLedgerList(WalletAddressLedger walletAddressLedger,boolean isUser); - /** - * 新增钱包地址资金流水 - * - * @param walletAddressLedger 钱包地址资金流水 - * @return 结果 - */ - public int insertWalletAddressLedger(WalletAddressLedger walletAddressLedger,boolean isUser); - - /** - * 修改钱包地址资金流水 - * - * @param walletAddressLedger 钱包地址资金流水 - * @return 结果 - */ - public int updateWalletAddressLedger(WalletAddressLedger walletAddressLedger,boolean isUser); - - /** - * 批量删除钱包地址资金流水 - * - * @param ids 需要删除的钱包地址资金流水ID - * @return 结果 - */ - public int deleteWalletAddressLedgerByIds(Long[] ids,boolean isUser); - - /** - * 删除钱包地址资金流水信息 - * - * @param id 钱包地址资金流水ID - * @return 结果 - */ - public int deleteWalletAddressLedgerById(Long id,boolean isUser); + WalletLedgerStatisticsVo statistics(WalletLedgerStatisticsQuery query, boolean isUser); List getByIds(List ids,boolean isUser, SFunction... columns); @@ -76,16 +48,4 @@ public interface IWalletAddressLedgerService extends IService getMapByIds(List ids); - /** - * @param field sum 字段 - * @param where 条件 - * @param tClass 返回类型 - * @param - * @return - */ - T sum(String field, String where, Class tClass); - - Long count(String where); - - List countByGroupBy(String select, String where, String groupby, Class tClass); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java index afd704b..55b22d8 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/UsdtAggregationWorkflowService.java @@ -37,6 +37,7 @@ public class UsdtAggregationWorkflowService { private final UsdtAggregationTaskItemMapper itemMapper; private final WalletOperationEstimateService estimateService; private final WalletSigningService walletSigningService; + private final WalletLedgerPostingService ledgerPostingService; public UsdtAggregationWorkflowService(IAppsService appsService, IWalletAddressService walletService, @@ -45,7 +46,8 @@ public class UsdtAggregationWorkflowService { UsdtAggregationTaskMapper taskMapper, UsdtAggregationTaskItemMapper itemMapper, WalletOperationEstimateService estimateService, - WalletSigningService walletSigningService) { + WalletSigningService walletSigningService, + WalletLedgerPostingService ledgerPostingService) { this.appsService = appsService; this.walletService = walletService; this.taskService = taskService; @@ -54,6 +56,7 @@ public class UsdtAggregationWorkflowService { this.itemMapper = itemMapper; this.estimateService = estimateService; this.walletSigningService = walletSigningService; + this.ledgerPostingService = ledgerPostingService; } @Transactional(rollbackFor = Exception.class) @@ -198,11 +201,9 @@ public class UsdtAggregationWorkflowService { String tx = session.execute(wallet, (tron, api, current) -> tron.usdtTransWithBroadcastRetry(api, current.getToken(), item.getDistTokenAddress(), item.getOpeAmount(), MAX_ATTEMPTS)); - item.setStatus(1L); - item.setRemark("执行成功;同一签名交易最多广播 " + MAX_ATTEMPTS - + " 次;交易哈希:" + tx); - item.setUpdatedAt(DateUtils.getNowDate()); - itemService.updateById(item); + String successRemark = "执行成功;同一签名交易最多广播 " + MAX_ATTEMPTS + + " 次;交易哈希:" + tx; + ledgerPostingService.completeAggregationItem(item, wallet, tx, successRemark); return true; } catch (Exception ex) { markFailed(item, "执行失败;同一签名交易已最多广播 " + MAX_ATTEMPTS diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/WalletLedgerPostingService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/WalletLedgerPostingService.java new file mode 100644 index 0000000..a6fa99b --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/WalletLedgerPostingService.java @@ -0,0 +1,119 @@ +package com.ruoyi.system.service; + +import com.ruoyi.common.exception.CustomException; +import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.system.domain.UsdtAggregationTaskItemPlus; +import com.ruoyi.system.domain.UsdtOrdersPlus; +import com.ruoyi.system.domain.WalletAddressLedger; +import com.ruoyi.system.domain.WalletAddressPlus; +import com.ruoyi.system.mapper.UsdtOrdersMapper; +import com.ruoyi.system.mapper.WalletAddressLedgerMapper; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; + +/** + * 系统业务流水入账服务。 + * 链上广播必须在调用本服务前完成,本服务只负责数据库状态与流水的原子落库。 + */ +@Service +public class WalletLedgerPostingService { + private static final long STATUS_PENDING = 1L; + private static final long STATUS_PAID = 2L; + + private final UsdtOrdersMapper ordersMapper; + private final WalletAddressLedgerMapper ledgerMapper; + private final IWalletAddressService walletService; + private final IUsdtAggregationTaskItemService aggregationItemService; + + public WalletLedgerPostingService(UsdtOrdersMapper ordersMapper, + WalletAddressLedgerMapper ledgerMapper, + IWalletAddressService walletService, + IUsdtAggregationTaskItemService aggregationItemService) { + this.ordersMapper = ordersMapper; + this.ledgerMapper = ledgerMapper; + this.walletService = walletService; + this.aggregationItemService = aggregationItemService; + } + + /** 将待支付订单结算为成功,同一订单重复扫描不会重复产生流水。 */ + @Transactional(rollbackFor = Exception.class) + public boolean settlePaidOrder(Long orderId, String transactionId) { + UsdtOrdersPlus order = ordersMapper.selectByIdForUpdate(orderId); + if (order == null) { + throw new CustomException("订单不存在"); + } + if (Long.valueOf(STATUS_PAID).equals(order.getStatus())) { + return transactionId.equals(order.getBlockTransactionId()); + } + if (!Long.valueOf(STATUS_PENDING).equals(order.getStatus())) { + return false; + } + if (ordersMapper.countPaidByTransactionId(transactionId, orderId) > 0) { + return false; + } + + WalletAddressPlus wallet = walletService.getByTokenAndAppId(order.getToken(), order.getAppId()); + Date now = DateUtils.getNowDate(); + WalletAddressLedger ledger = baseLedger(now); + ledger.setTradeNo(order.getTradeId()); + ledger.setIdempotencyKey("ORDER_RECEIVE:" + order.getId() + ":USDT"); + ledger.setAppId(order.getAppId()); + ledger.setUserId(order.getUserId()); + ledger.setWalletAddressId(wallet == null ? null : wallet.getId()); + ledger.setWalletAddress(order.getToken()); + ledger.setCurrency("USDT"); + ledger.setBusinessType("ORDER_RECEIVE"); + ledger.setDirection(1); + ledger.setAmount(order.getActualAmount()); + ledger.setOrderId(order.getId()); + ledger.setMerchantOrderId(order.getOrderId()); + ledger.setTradeId(order.getTradeId()); + ledger.setTransactionId(transactionId); + ledger.setSourceType("ORDER"); + ledger.setRemark("订单支付成功"); + ledgerMapper.insertSystemLedgerIgnoreDuplicate(ledger); + return ordersMapper.markOrderPaid(order.getId(), transactionId, now) == 1; + } + + /** 将已广播成功的归集明细与对应支出流水原子落库。 */ + @Transactional(rollbackFor = Exception.class) + public void completeAggregationItem(UsdtAggregationTaskItemPlus item, + WalletAddressPlus wallet, String transactionId, String successRemark) { + Date now = DateUtils.getNowDate(); + WalletAddressLedger ledger = baseLedger(now); + ledger.setTradeNo("AGG-" + item.getParentId() + "-" + item.getId()); + ledger.setIdempotencyKey("AGGREGATION_OUT:" + item.getId() + ":USDT"); + ledger.setAppId(item.getAppId()); + ledger.setUserId(item.getUserId()); + ledger.setWalletAddressId(wallet.getId()); + ledger.setWalletAddress(item.getTokenAddress()); + ledger.setCounterpartyAddress(item.getDistTokenAddress()); + ledger.setCurrency("USDT"); + ledger.setBusinessType("AGGREGATION_OUT"); + ledger.setDirection(2); + ledger.setAmount(item.getOpeAmount()); + ledger.setAggregationTaskId(item.getParentId()); + ledger.setAggregationItemId(item.getId()); + ledger.setTransactionId(transactionId); + ledger.setSourceType("AGGREGATION"); + ledger.setRemark("USDT归集成功"); + ledgerMapper.insertSystemLedgerIgnoreDuplicate(ledger); + + item.setStatus(1L); + item.setRemark(successRemark); + item.setUpdatedAt(now); + if (!aggregationItemService.updateById(item)) { + throw new CustomException("归集明细状态更新失败"); + } + } + + private WalletAddressLedger baseLedger(Date now) { + WalletAddressLedger ledger = new WalletAddressLedger(); + ledger.setStatus(1); + ledger.setCreatedAt(now); + ledger.setUpdatedAt(now); + return ledger; + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WalletAddressLedgerServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WalletAddressLedgerServiceImpl.java index b54d927..7f85f86 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WalletAddressLedgerServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WalletAddressLedgerServiceImpl.java @@ -1,19 +1,18 @@ package com.ruoyi.system.service.impl; -import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.select.SelectItem; -import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.common.exception.CustomException; import com.ruoyi.common.utils.SecurityUtils; -import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.WalletAddressLedger; import com.ruoyi.system.domain.WalletAddressLedgerPlus; +import com.ruoyi.system.domain.vo.WalletLedgerStatisticsQuery; +import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo; import com.ruoyi.system.mapper.WalletAddressLedgerMapper; import com.ruoyi.system.service.IWalletAddressLedgerService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import java.util.*; @@ -28,8 +27,6 @@ public class WalletAddressLedgerServiceImpl extends ServiceImpl getByIds(List ids ,boolean isUser, SFunction... columns) { @@ -122,48 +119,33 @@ public class WalletAddressLedgerServiceImpl extends ServiceImpl T sum(String field, String where, Class tClass) { - StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append("select "); - stringBuffer.append(" sum(" + field + ") as total "); - stringBuffer.append(" from wallet_address_ledger "); - if (StringUtils.isNotBlank(where)) { - stringBuffer.append(" where "); - stringBuffer.append(where); + @Override + public WalletLedgerStatisticsVo statistics(WalletLedgerStatisticsQuery query, boolean isUser) { + WalletLedgerStatisticsQuery safeQuery = query == null + ? new WalletLedgerStatisticsQuery() : query; + String granularity = safeQuery.getGranularity(); + if (granularity == null || granularity.trim().isEmpty()) { + safeQuery.setGranularity("DAY"); + } else { + granularity = granularity.trim().toUpperCase(Locale.ENGLISH); + if (!Arrays.asList("DAY", "MONTH", "YEAR").contains(granularity)) { + throw new CustomException("统计粒度仅支持 DAY、MONTH、YEAR"); + } + safeQuery.setGranularity(granularity); } - T result = jdbcTemplate.queryForObject(stringBuffer.toString(), tClass); - return result; - } - - public Long count(String where) { - StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append("select "); - stringBuffer.append(" count(1)"); - stringBuffer.append(" from "); - stringBuffer.append(" wallet_address_ledger "); - if (StringUtils.isNotBlank(where)) { - stringBuffer.append(" where "); - stringBuffer.append(where); + if (safeQuery.getCurrency() != null && !safeQuery.getCurrency().trim().isEmpty()) { + String currency = safeQuery.getCurrency().trim().toUpperCase(Locale.ENGLISH); + if (!Arrays.asList("USDT", "TRX").contains(currency)) { + throw new CustomException("币种仅支持 USDT、TRX"); + } + safeQuery.setCurrency(currency); } - Long result = jdbcTemplate.queryForObject(stringBuffer.toString(), Long.class); - return result; - } - - public List countByGroupBy(String select, String where, String groupby, Class tClass) { - StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append("select "); - stringBuffer.append(select); - stringBuffer.append(" from "); - stringBuffer.append(" wallet_address_ledger "); - if (StringUtils.isNotBlank(where)) { - stringBuffer.append(" where "); - stringBuffer.append(where); + if (isUser) { + safeQuery.setUserId(SecurityUtils.getUserId()); } - stringBuffer.append(" group by "); - stringBuffer.append(groupby); - List result = jdbcTemplate.queryForList(stringBuffer.toString()); - List tList = JSONArray.parseArray(JSONArray.toJSONString(result), tClass); - return tList; + return new WalletLedgerStatisticsVo( + walletAddressLedgerMapper.selectStatisticsSummary(safeQuery), + walletAddressLedgerMapper.selectStatisticsTrend(safeQuery)); } /** * 查询钱包地址资金流水 @@ -175,6 +157,9 @@ public class WalletAddressLedgerServiceImpl extends ServiceImpl list= getByIds(Arrays.asList(ids),false,WalletAddressLedgerPlus::getUserId,WalletAddressLedgerPlus::getId); - checkUserPlus(list); - } - return walletAddressLedgerMapper.deleteWalletAddressLedgerByIds(ids); - } - - /** - * 删除钱包地址资金流水信息 - * - * @param id 钱包地址资金流水ID - * @return 结果 - */ - @Override - public int deleteWalletAddressLedgerById(Long id,boolean isUser) - { - if(isUser){ - WalletAddressLedgerPlus vo = getById(id,WalletAddressLedgerPlus::getUserId,WalletAddressLedgerPlus::getId); - checkUser(vo); - } - return walletAddressLedgerMapper.deleteWalletAddressLedgerById(id); - } } diff --git a/ruoyi-system/src/main/resources/mapper/system/UsdtOrdersMapper.xml b/ruoyi-system/src/main/resources/mapper/system/UsdtOrdersMapper.xml index 3ca0e8e..14590bf 100644 --- a/ruoyi-system/src/main/resources/mapper/system/UsdtOrdersMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/UsdtOrdersMapper.xml @@ -49,6 +49,29 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where id = #{id} + + + + + + + update orders + set block_transaction_id = #{transactionId}, status = 2, updated_at = #{updatedAt} + where id = #{id} and status = 1 + insert into orders @@ -129,4 +152,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{id} - \ No newline at end of file + diff --git a/ruoyi-system/src/main/resources/mapper/system/WalletAddressLedgerMapper.xml b/ruoyi-system/src/main/resources/mapper/system/WalletAddressLedgerMapper.xml index a878b67..ad9cd51 100644 --- a/ruoyi-system/src/main/resources/mapper/system/WalletAddressLedgerMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/WalletAddressLedgerMapper.xml @@ -86,7 +86,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and error_message = #{errorMessage} and created_at = #{createdAt} and updated_at = #{updatedAt} + + and created_at >= #{params.beginTime} + + + and created_at <= #{params.endTime} + + order by created_at desc, id desc + select currency, + coalesce(sum(case when direction = 1 then amount else 0 end), 0) as incomeAmount, + coalesce(sum(case when direction = 2 then amount else 0 end), 0) as expenseAmount, + coalesce(sum(case when direction = 1 then amount else -amount end), 0) as netAmount, + count(1) as count + from wallet_address_ledger + where + group by currency + order by currency + + + + update wallet_address_ledger @@ -227,4 +289,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{id} - \ No newline at end of file + diff --git a/sql/wallet_address_ledger_indexes.sql b/sql/wallet_address_ledger_indexes.sql new file mode 100644 index 0000000..3401a08 --- /dev/null +++ b/sql/wallet_address_ledger_indexes.sql @@ -0,0 +1,11 @@ +-- 钱包流水幂等与报表查询索引。 +-- 执行前请先通过 SHOW INDEX FROM wallet_address_ledger 核对生产库是否已有等价索引。 +ALTER TABLE `wallet_address_ledger` + MODIFY COLUMN `amount` DECIMAL(30,6) NOT NULL COMMENT '变动金额,始终为正数', + MODIFY COLUMN `balance_before` DECIMAL(30,6) NULL COMMENT '操作前链上余额', + MODIFY COLUMN `balance_after` DECIMAL(30,6) NULL COMMENT '操作后链上余额', + MODIFY COLUMN `fee_amount` DECIMAL(30,6) NULL COMMENT '实际手续费', + MODIFY COLUMN `burned_trx` DECIMAL(30,6) NULL COMMENT '资源不足燃烧的TRX', + ADD UNIQUE KEY `uk_wallet_ledger_idempotency` (`idempotency_key`), + ADD KEY `idx_wallet_ledger_app_time` (`app_id`, `created_at`), + ADD KEY `idx_wallet_ledger_address_time` (`wallet_address`, `created_at`);