添加对账单

This commit is contained in:
lu7213 committed 2026-10-18 09:37:46 -04:00
1 parent e62e2fe969
commit 64ba6cd28e
6 files changed
+220

No files matched your search

@@ -8,6 +8,7 @@ import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AppsBalanceLedger; import com.ruoyi.system.domain.AppsBalanceLedger;
import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery; import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery;
import com.ruoyi.system.domain.vo.AppsMonthlyStatementRow;
import com.ruoyi.system.service.IAppsBalanceLedgerService; import com.ruoyi.system.service.IAppsBalanceLedgerService;
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;
@@ -92,4 +93,24 @@ public class AppsBalanceLedgerController extends BaseController
public AjaxResult userStatistics(AppsLedgerStatisticsQuery query) { public AjaxResult userStatistics(AppsLedgerStatisticsQuery query) {
return AjaxResult.success(appsBalanceLedgerService.statistics(query, true)); 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<AppsMonthlyStatementRow> rows = appsBalanceLedgerService.monthlyStatement(appId, month, false);
return new ExcelUtil<AppsMonthlyStatementRow>(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<AppsMonthlyStatementRow> rows = appsBalanceLedgerService.monthlyStatement(appId, month, true);
return new ExcelUtil<AppsMonthlyStatementRow>(AppsMonthlyStatementRow.class)
.exportExcel(rows, "应用月度对账单_" + appId + "_" + month);
}
} }
@@ -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; }
}
@@ -9,6 +9,8 @@ import com.ruoyi.system.domain.AppsBalanceLedgerPlus;
import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery; import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery;
import com.ruoyi.system.domain.vo.WalletLedgerSummaryVo; import com.ruoyi.system.domain.vo.WalletLedgerSummaryVo;
import com.ruoyi.system.domain.vo.WalletLedgerTrendVo; import com.ruoyi.system.domain.vo.WalletLedgerTrendVo;
import java.math.BigDecimal;
import java.util.Date;
/** /**
* 应用账面余额流水Mapper接口 * 应用账面余额流水Mapper接口
@@ -43,6 +45,12 @@ public interface AppsBalanceLedgerMapper extends BaseMapper<AppsBalanceLedgerPlu
List<WalletLedgerTrendVo> selectStatisticsTrend(AppsLedgerStatisticsQuery query); List<WalletLedgerTrendVo> selectStatisticsTrend(AppsLedgerStatisticsQuery query);
List<AppsBalanceLedger> 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);
/** /**
* 新增应用账面余额流水 * 新增应用账面余额流水
* *
@@ -7,6 +7,7 @@ import com.ruoyi.system.domain.AppsBalanceLedger;
import com.ruoyi.system.domain.AppsBalanceLedgerPlus; import com.ruoyi.system.domain.AppsBalanceLedgerPlus;
import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery; import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery;
import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo; import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo;
import com.ruoyi.system.domain.vo.AppsMonthlyStatementRow;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -44,6 +45,9 @@ public interface IAppsBalanceLedgerService extends IService<AppsBalanceLedgerPlu
*/ */
WalletLedgerStatisticsVo statistics(AppsLedgerStatisticsQuery query, boolean isUser); WalletLedgerStatisticsVo statistics(AppsLedgerStatisticsQuery query, boolean isUser);
/** 生成指定应用和自然月的对账单行,服务端负责应用归属校验。 */
List<AppsMonthlyStatementRow> monthlyStatement(Long appId, String month, boolean isUser);
List<AppsBalanceLedgerPlus> getByIds(List<Long> ids,boolean isUser, SFunction<AppsBalanceLedgerPlus, ?>... columns); List<AppsBalanceLedgerPlus> getByIds(List<Long> ids,boolean isUser, SFunction<AppsBalanceLedgerPlus, ?>... columns);
AppsBalanceLedgerPlus getById(Long id, SFunction<AppsBalanceLedgerPlus, ?>... columns); AppsBalanceLedgerPlus getById(Long id, SFunction<AppsBalanceLedgerPlus, ?>... columns);
@@ -10,12 +10,20 @@ import com.ruoyi.system.domain.AppsBalanceLedger;
import com.ruoyi.system.domain.AppsBalanceLedgerPlus; import com.ruoyi.system.domain.AppsBalanceLedgerPlus;
import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery; import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery;
import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo; 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.AppsBalanceLedgerMapper;
import com.ruoyi.system.mapper.AppsMapper;
import com.ruoyi.system.service.IAppsBalanceLedgerService; import com.ruoyi.system.service.IAppsBalanceLedgerService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; 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业务层处理 * 应用账面余额流水Service业务层处理
* *
@@ -27,6 +35,11 @@ public class AppsBalanceLedgerServiceImpl extends ServiceImpl<AppsBalanceLedgerM
{ {
@Autowired @Autowired
private AppsBalanceLedgerMapper appsBalanceLedgerMapper; private AppsBalanceLedgerMapper appsBalanceLedgerMapper;
@Autowired
private AppsMapper appsMapper;
private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
private static final List<String> STATEMENT_CURRENCIES = Arrays.asList("USDT", "TRX");
@Override @Override
public List<AppsBalanceLedgerPlus> getByIds(List<Long> ids ,boolean isUser, SFunction<AppsBalanceLedgerPlus, ?>... columns) { public List<AppsBalanceLedgerPlus> getByIds(List<Long> ids ,boolean isUser, SFunction<AppsBalanceLedgerPlus, ?>... columns) {
@@ -146,6 +159,112 @@ public class AppsBalanceLedgerServiceImpl extends ServiceImpl<AppsBalanceLedgerM
appsBalanceLedgerMapper.selectStatisticsSummary(safeQuery), appsBalanceLedgerMapper.selectStatisticsSummary(safeQuery),
appsBalanceLedgerMapper.selectStatisticsTrend(safeQuery)); appsBalanceLedgerMapper.selectStatisticsTrend(safeQuery));
} }
@Override
public List<AppsMonthlyStatementRow> 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<AppsBalanceLedger> details = appsBalanceLedgerMapper
.selectMonthlyStatementDetails(appId, beginTime, endTime);
List<AppsMonthlyStatementRow> rows = new ArrayList<AppsMonthlyStatementRow>();
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<AppsBalanceLedger> 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;
}
/** /**
* 查询应用账面余额流水 * 查询应用账面余额流水
* *
@@ -191,6 +191,22 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
order by period, currency order by period, currency
</select> </select>
<select id="selectMonthlyStatementDetails" resultMap="AppsBalanceLedgerResult">
<include refid="selectAppsBalanceLedgerVo"/>
where app_id = #{appId}
and created_at &gt;= #{beginTime}
and created_at &lt; #{endTime}
order by created_at asc, id asc
</select>
<select id="selectLatestAvailableBalanceBefore" resultType="java.math.BigDecimal">
select available_after
from apps_balance_ledger
where app_id = #{appId} and currency = #{currency} and created_at &lt; #{boundary}
order by created_at desc, id desc
limit 1
</select>
<update id="updateAppsBalanceLedger" parameterType="AppsBalanceLedger"> <update id="updateAppsBalanceLedger" parameterType="AppsBalanceLedger">
update apps_balance_ledger update apps_balance_ledger
<trim prefix="SET" suffixOverrides=","> <trim prefix="SET" suffixOverrides=",">