diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controller/AppsBalanceLedgerController.java b/ruoyi-system/src/main/java/com/ruoyi/system/controller/AppsBalanceLedgerController.java index 098df9f..07b3cc3 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controller/AppsBalanceLedgerController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controller/AppsBalanceLedgerController.java @@ -8,6 +8,7 @@ import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.system.domain.AppsBalanceLedger; import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery; +import com.ruoyi.system.domain.vo.AppsMonthlyStatementRow; import com.ruoyi.system.service.IAppsBalanceLedgerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; @@ -92,4 +93,24 @@ public class AppsBalanceLedgerController extends BaseController public AjaxResult userStatistics(AppsLedgerStatisticsQuery query) { return AjaxResult.success(appsBalanceLedgerService.statistics(query, true)); } + + /** 管理员导出指定应用的自然月对账单。 */ + @PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:list')") + @Log(title = "应用月度对账单", businessType = BusinessType.EXPORT) + @GetMapping("/monthly-statement") + public AjaxResult monthlyStatement(@RequestParam Long appId, @RequestParam String month) { + List rows = appsBalanceLedgerService.monthlyStatement(appId, month, false); + return new ExcelUtil(AppsMonthlyStatementRow.class) + .exportExcel(rows, "应用月度对账单_" + appId + "_" + month); + } + + /** 商户导出自己应用的自然月对账单,应用归属在服务端强制校验。 */ + @PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:user-list')") + @Log(title = "商户月度对账单", businessType = BusinessType.EXPORT) + @GetMapping("/user/monthly-statement") + public AjaxResult userMonthlyStatement(@RequestParam Long appId, @RequestParam String month) { + List rows = appsBalanceLedgerService.monthlyStatement(appId, month, true); + return new ExcelUtil(AppsMonthlyStatementRow.class) + .exportExcel(rows, "应用月度对账单_" + appId + "_" + month); + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/AppsMonthlyStatementRow.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/AppsMonthlyStatementRow.java new file mode 100644 index 0000000..47579c9 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/AppsMonthlyStatementRow.java @@ -0,0 +1,52 @@ +package com.ruoyi.system.domain.vo; + +import com.ruoyi.common.annotation.Excel; +import java.math.BigDecimal; +import java.util.Date; + +/** 应用月度对账单导出行,汇总行和流水明细使用同一工作表。 */ +public class AppsMonthlyStatementRow { + @Excel(name = "应用ID") private Long appId; + @Excel(name = "应用名称", width = 24) private String appName; + @Excel(name = "对账月份") private String statementMonth; + @Excel(name = "记录类型") private String rowType; + @Excel(name = "币种") private String currency; + @Excel(name = "期初余额") private BigDecimal openingBalance; + @Excel(name = "收入合计") private BigDecimal incomeAmount; + @Excel(name = "支出合计") private BigDecimal expenseAmount; + @Excel(name = "净变动") private BigDecimal netAmount; + @Excel(name = "期末余额") private BigDecimal closingBalance; + @Excel(name = "流水笔数") private Long ledgerCount; + @Excel(name = "发生时间", width = 22, dateFormat = "yyyy-MM-dd HH:mm:ss") private Date createdAt; + @Excel(name = "平台流水号", width = 34) private String tradeNo; + @Excel(name = "业务类型", width = 20) private String businessType; + @Excel(name = "方向") private String direction; + @Excel(name = "变动金额") private BigDecimal amount; + @Excel(name = "变动前可用余额") private BigDecimal availableBefore; + @Excel(name = "变动后可用余额") private BigDecimal availableAfter; + @Excel(name = "商户订单号", width = 24) private String merchantOrderId; + @Excel(name = "链上交易哈希", width = 48) private String transactionId; + @Excel(name = "备注", width = 45) private String remark; + + public Long getAppId() { return appId; } public void setAppId(Long appId) { this.appId = appId; } + public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } + public String getStatementMonth() { return statementMonth; } public void setStatementMonth(String statementMonth) { this.statementMonth = statementMonth; } + public String getRowType() { return rowType; } public void setRowType(String rowType) { this.rowType = rowType; } + public String getCurrency() { return currency; } public void setCurrency(String currency) { this.currency = currency; } + public BigDecimal getOpeningBalance() { return openingBalance; } public void setOpeningBalance(BigDecimal openingBalance) { this.openingBalance = openingBalance; } + 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 BigDecimal getClosingBalance() { return closingBalance; } public void setClosingBalance(BigDecimal closingBalance) { this.closingBalance = closingBalance; } + public Long getLedgerCount() { return ledgerCount; } public void setLedgerCount(Long ledgerCount) { this.ledgerCount = ledgerCount; } + public Date getCreatedAt() { return createdAt; } public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } + public String getTradeNo() { return tradeNo; } public void setTradeNo(String tradeNo) { this.tradeNo = tradeNo; } + public String getBusinessType() { return businessType; } public void setBusinessType(String businessType) { this.businessType = businessType; } + public String getDirection() { return direction; } public void setDirection(String direction) { this.direction = direction; } + public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal amount) { this.amount = amount; } + public BigDecimal getAvailableBefore() { return availableBefore; } public void setAvailableBefore(BigDecimal availableBefore) { this.availableBefore = availableBefore; } + public BigDecimal getAvailableAfter() { return availableAfter; } public void setAvailableAfter(BigDecimal availableAfter) { this.availableAfter = availableAfter; } + public String getMerchantOrderId() { return merchantOrderId; } public void setMerchantOrderId(String merchantOrderId) { this.merchantOrderId = merchantOrderId; } + public String getTransactionId() { return transactionId; } public void setTransactionId(String transactionId) { this.transactionId = transactionId; } + public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/AppsBalanceLedgerMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/AppsBalanceLedgerMapper.java index edbee51..ef9d9ae 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/AppsBalanceLedgerMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/AppsBalanceLedgerMapper.java @@ -9,6 +9,8 @@ import com.ruoyi.system.domain.AppsBalanceLedgerPlus; import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery; import com.ruoyi.system.domain.vo.WalletLedgerSummaryVo; import com.ruoyi.system.domain.vo.WalletLedgerTrendVo; +import java.math.BigDecimal; +import java.util.Date; /** * 应用账面余额流水Mapper接口 @@ -43,6 +45,12 @@ public interface AppsBalanceLedgerMapper extends BaseMapper selectStatisticsTrend(AppsLedgerStatisticsQuery query); + List selectMonthlyStatementDetails(@Param("appId") Long appId, + @Param("beginTime") Date beginTime, @Param("endTime") Date endTime); + + BigDecimal selectLatestAvailableBalanceBefore(@Param("appId") Long appId, + @Param("currency") String currency, @Param("boundary") Date boundary); + /** * 新增应用账面余额流水 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IAppsBalanceLedgerService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IAppsBalanceLedgerService.java index 1e2281c..f6a9e3d 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/IAppsBalanceLedgerService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IAppsBalanceLedgerService.java @@ -7,6 +7,7 @@ import com.ruoyi.system.domain.AppsBalanceLedger; import com.ruoyi.system.domain.AppsBalanceLedgerPlus; import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery; import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo; +import com.ruoyi.system.domain.vo.AppsMonthlyStatementRow; import java.util.List; import java.util.Map; @@ -44,6 +45,9 @@ public interface IAppsBalanceLedgerService extends IService monthlyStatement(Long appId, String month, boolean isUser); + List getByIds(List ids,boolean isUser, SFunction... columns); AppsBalanceLedgerPlus getById(Long id, SFunction... columns); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppsBalanceLedgerServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppsBalanceLedgerServiceImpl.java index f2d4c5f..cca55cb 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppsBalanceLedgerServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppsBalanceLedgerServiceImpl.java @@ -10,12 +10,20 @@ import com.ruoyi.system.domain.AppsBalanceLedger; import com.ruoyi.system.domain.AppsBalanceLedgerPlus; import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery; import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo; +import com.ruoyi.system.domain.vo.AppsMonthlyStatementRow; +import com.ruoyi.system.domain.Apps; import com.ruoyi.system.mapper.AppsBalanceLedgerMapper; +import com.ruoyi.system.mapper.AppsMapper; import com.ruoyi.system.service.IAppsBalanceLedgerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; +import java.math.BigDecimal; +import java.time.YearMonth; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; /** * 应用账面余额流水Service业务层处理 * @@ -27,6 +35,11 @@ public class AppsBalanceLedgerServiceImpl extends ServiceImpl STATEMENT_CURRENCIES = Arrays.asList("USDT", "TRX"); @Override public List getByIds(List ids ,boolean isUser, SFunction... columns) { @@ -146,6 +159,112 @@ public class AppsBalanceLedgerServiceImpl extends ServiceImpl monthlyStatement(Long appId, String month, boolean isUser) { + if (appId == null) { + throw new CustomException("请选择需要生成对账单的应用"); + } + Apps app = appsMapper.selectAppsById(appId); + if (app == null) { + throw new CustomException("应用不存在或已删除"); + } + Long currentUserId = SecurityUtils.getUserId(); + if (isUser && (currentUserId == null || !currentUserId.equals(app.getUserId()))) { + throw new CustomException("无权导出该应用对账单"); + } + YearMonth statementMonth; + try { + statementMonth = YearMonth.parse(month, MONTH_FORMATTER); + } catch (DateTimeParseException | NullPointerException e) { + throw new CustomException("对账月份格式必须为 yyyy-MM"); + } + ZoneId zoneId = ZoneId.systemDefault(); + Date beginTime = Date.from(statementMonth.atDay(1).atStartOfDay(zoneId).toInstant()); + Date endTime = Date.from(statementMonth.plusMonths(1).atDay(1).atStartOfDay(zoneId).toInstant()); + List details = appsBalanceLedgerMapper + .selectMonthlyStatementDetails(appId, beginTime, endTime); + List rows = new ArrayList(); + for (String currency : STATEMENT_CURRENCIES) { + BigDecimal income = BigDecimal.ZERO; + BigDecimal expense = BigDecimal.ZERO; + long count = 0L; + for (AppsBalanceLedger ledger : details) { + if (!currency.equalsIgnoreCase(ledger.getCurrency())) continue; + BigDecimal amount = ledger.getAmount() == null ? BigDecimal.ZERO : ledger.getAmount(); + if (Integer.valueOf(1).equals(ledger.getDirection())) income = income.add(amount); + if (Integer.valueOf(2).equals(ledger.getDirection())) expense = expense.add(amount); + count++; + } + BigDecimal opening = appsBalanceLedgerMapper + .selectLatestAvailableBalanceBefore(appId, currency, beginTime); + if (opening == null) { + opening = firstAvailableBefore(details, currency); + } + BigDecimal closing = appsBalanceLedgerMapper + .selectLatestAvailableBalanceBefore(appId, currency, endTime); + if (closing == null) { + closing = opening; + } + AppsMonthlyStatementRow summary = baseStatementRow(app, month, currency); + summary.setRowType("月度汇总"); + summary.setOpeningBalance(opening); + summary.setIncomeAmount(income); + summary.setExpenseAmount(expense); + summary.setNetAmount(income.subtract(expense)); + summary.setClosingBalance(closing); + summary.setLedgerCount(count); + rows.add(summary); + } + for (AppsBalanceLedger ledger : details) { + AppsMonthlyStatementRow row = baseStatementRow(app, month, ledger.getCurrency()); + row.setRowType("流水明细"); + row.setCreatedAt(ledger.getCreatedAt()); + row.setTradeNo(ledger.getTradeNo()); + row.setBusinessType(businessTypeName(ledger.getBusinessType())); + row.setDirection(Integer.valueOf(1).equals(ledger.getDirection()) ? "收入" : "支出"); + row.setAmount(ledger.getAmount()); + row.setAvailableBefore(ledger.getAvailableBefore()); + row.setAvailableAfter(ledger.getAvailableAfter()); + row.setMerchantOrderId(ledger.getMerchantOrderId()); + row.setTransactionId(ledger.getTransactionId()); + row.setRemark(ledger.getRemark()); + rows.add(row); + } + return rows; + } + + private AppsMonthlyStatementRow baseStatementRow(Apps app, String month, String currency) { + AppsMonthlyStatementRow row = new AppsMonthlyStatementRow(); + row.setAppId(app.getId()); + row.setAppName(app.getName()); + row.setStatementMonth(month); + row.setCurrency(currency); + return row; + } + + private BigDecimal zeroIfNull(BigDecimal value) { + return value == null ? BigDecimal.ZERO : value; + } + + private BigDecimal firstAvailableBefore(List details, String currency) { + for (AppsBalanceLedger ledger : details) { + if (currency.equalsIgnoreCase(ledger.getCurrency())) { + return zeroIfNull(ledger.getAvailableBefore()); + } + } + return BigDecimal.ZERO; + } + + private String businessTypeName(String type) { + if ("ORDER_RECEIVE".equals(type)) return "订单代收"; + if ("ORDER_FEE".equals(type)) return "订单手续费"; + if ("PAYOUT_SUCCESS".equals(type) || "PAYOUT_PRINCIPAL".equals(type)) return "代付本金"; + if ("PAYOUT_FEE".equals(type)) return "代付手续费"; + if ("ADMIN_BALANCE_ADD".equals(type)) return "管理员增加余额"; + if ("ADMIN_BALANCE_SUBTRACT".equals(type)) return "管理员减少余额"; + return type; + } /** * 查询应用账面余额流水 * diff --git a/ruoyi-system/src/main/resources/mapper/system/AppsBalanceLedgerMapper.xml b/ruoyi-system/src/main/resources/mapper/system/AppsBalanceLedgerMapper.xml index 388c7df..e2905dc 100644 --- a/ruoyi-system/src/main/resources/mapper/system/AppsBalanceLedgerMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/AppsBalanceLedgerMapper.xml @@ -191,6 +191,22 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" order by period, currency + + + + update apps_balance_ledger