diff --git a/docs/rental-account-statement.md b/docs/rental-account-statement.md new file mode 100644 index 0000000..fcb239a --- /dev/null +++ b/docs/rental-account-statement.md @@ -0,0 +1,27 @@ +# 本人租赁资金对账单 + +租赁账户与供应商首页顶部新增“资金对账单”。展开后选择网络与记账日期,点击“下载 Excel”。默认沿用首页网络及近7/14/30天;导出日期独立于看板统计。执行 `sql/rent_statement_export_menu.sql`,为 PROXY_ACCOUNT、PROXY_SUPPLIER 授予 `rent:statement:export`,重新登录后生效。父菜单需要已有 `rent:portal:view` 页面。 + +本功能复用现有rent_account及rent_ledger,无需新增表或迁移历史资金数据。权限SQL需部署方执行,不会由页面自动修改角色。 + +## 文件内容和口径 + +- 账户收支汇总:账户编号、网络、时区、起止日期、生成时间、期初/期末总资产、资产增加/减少及校验;同时列出可用、冻结、待结算三个分户的期初、增、减、期末。 +- 业务分类汇总:按 event_type 汇总笔数、资产增减和三个分户净变化。 +- 资金流水明细:流水ID、记账时间、事件类型、资产与分户变动、记账后余额、订单/结算/退款/提现/冲正关联ID及记账说明。只导出明确白名单字段,不含私钥、API Secret、账户凭证或内部付款证据。 + +总资产=可用余额+冻结余额+待结算收益。逐笔三个分户变动之和大于0为资产增加,小于0为资产减少,0为内部划转/无净变化。因此冻结、解冻、收益结算转入可用不会重复增加收入或支出。供应商毛收入和平台扣费保留为独立事件,扣费体现在供应商待结算减少,不将平台自身收入算作本人资产。该口径是资金账本资产变化,不能等同链上钱包收付款或利润表。 + +同一用户双角色共用同网络资金账户,导出完整账本,不按角色遗漏其中一侧流水。NILE与MAINNET独立,禁止跨网汇总。日期按服务器/JVM本地时区,包括起止当天,数据库使用左闭右开区间;当天数据截至生成本次一致性快照。冻结中的资金仍属于资产。无流水区间仍输出期初、期末余额。 + +期末余额由同一事务的当前账户余额减去结束日期之后的流水变化还原;期初=期末-区间净变化。不修改历史数据,不触发链上查询,不创建账户。明细按不可变流水ID升序,金额及编号为Excel文本,保持6位小数及大整数精度。备注以文本输出,不执行公式。 + +## 接口与限制 + +`POST /rental/statements/export?network=NILE&from=2026-11-01&to=2026-11-03`,需登录与 `rent:statement:export` 权限。仅支持NILE/MAINNET、单次最多366天/50000条,结束日期不能晚于服务器当天。越限明确拒绝并提示分段导出,禁止静默截断。未开通账户返回业务错误。客户端不传userId/accountId,即使额外提交也不参与查询。 + +成功返回xlsx二进制,失败返回原有JSON错误;前端识别错误Blob而不下载损坏文件。响应不缓存,不在浏览器本地存储账本。SXSSF临时文件在完成/失败后均清理。按服务端导出开始、快照生成、导出完成/失败日志中的userId/network/date/rows排查,不记录金额、流水正文和凭证。 + +## 验证记录 + +JDK8隔离租赁回归186项通过。新增用例覆盖跨用户/网络过滤、冻结与结算内部划转、真实消费与提现扣款、历史期后变动还原、左闭右开日期边界、空区间和大金额文本精度,并通过POI重新读取生成的三个工作表、验证备注不是公式。前端模板/脚本检查及实际Vue组件桌面/手机交互验证通过;演示下载使用隔离测试生成的xlsx,未连接真实资金账户。impeccable独立收尾评审结论ship。 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatementController.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatementController.java new file mode 100644 index 0000000..4c56658 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatementController.java @@ -0,0 +1,50 @@ +package com.ruoyi.system.rental; + +import com.ruoyi.common.utils.SecurityUtils; +import java.io.IOException; +import java.time.LocalDate; +import javax.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** 本人账单下载;不接受userId/accountId,避免通过导出接口越权读取资金明细。 */ +@RestController +public class RentalStatementController { + private static final Logger LOG = LoggerFactory.getLogger(RentalStatementController.class); + private final RentalStatements statements; + + public RentalStatementController(RentalStatements statements) { + this.statements = statements; + } + + @PostMapping("/rental/statements/export") + @PreAuthorize("@ss.hasPermi('rent:statement:export')") + public void export(@RequestParam String network, + @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate from, + @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate to, + HttpServletResponse response) throws IOException { + Long user = SecurityUtils.getUserId(); + RentalValues.require(user != null, "请先登录"); + LOG.info("租赁对账导出开始 userId={} network={} from={} to={}", user, network, from, to); + try { + // 所有参数、归属和行数校验在写HTTP头之前完成,失败可以返回正常JSON业务错误。 + RentalStatements.Statement data = statements.snapshot(user, network, from, to); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setHeader("Content-Disposition", "attachment; filename=RentalStatement-" + data.network + + "-" + data.from + "-" + data.to + ".xlsx"); + response.setHeader("Cache-Control", "no-store"); + RentalStatementExcel.write(data, response.getOutputStream()); + LOG.info("租赁对账导出完成 userId={} accountId={} network={} rows={}", + user, data.accountId, data.network, data.rows.size()); + } catch (IOException | RuntimeException exception) { + LOG.warn("租赁对账导出失败 userId={} network={} from={} to={} exceptionType={}", + user, network, from, to, exception.getClass().getSimpleName()); + throw exception; + } + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatementExcel.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatementExcel.java new file mode 100644 index 0000000..52d321a --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatementExcel.java @@ -0,0 +1,134 @@ +package com.ruoyi.system.rental; + +import static com.ruoyi.system.rental.RentalValues.*; + +import java.io.IOException; +import java.io.OutputStream; +import java.math.BigDecimal; +import java.time.format.DateTimeFormatter; +import java.util.LinkedHashMap; +import java.util.Map; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; + +/** + * 资金对账单Excel输出。金额和编号一律写文本,避免Excel的15位数字精度丢失; + * 不写公式,用户填写的备注即使以=、+等开头也只是文本,不会被当作公式执行。 + */ +final class RentalStatementExcel { + private static final DateTimeFormatter TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); + + private RentalStatementExcel() { } + + static void write(RentalStatements.Statement data, OutputStream output) throws IOException { + SXSSFWorkbook book = new SXSSFWorkbook(100); + book.setCompressTempFiles(true); + try { + CellStyle header = book.createCellStyle(); + Font font = book.createFont(); + font.setBold(true); + font.setColor(IndexedColors.DARK_BLUE.getIndex()); + header.setFont(font); + Sheet summary = sheet(book, "账户收支汇总", header, "项目", "金额或说明"); + line(summary, "账户编号", String.valueOf(data.accountId)); + line(summary, "网络 / 币种", data.network + " / USDT"); + line(summary, "开始日期(含)", data.from.toString()); + line(summary, "结束日期(含)", data.to.toString()); + line(summary, "生成时间", TIME.format(data.generated)); + line(summary, "服务器时区", zone().getId()); + line(summary, "流水笔数", String.valueOf(data.rows.size())); + BigDecimal opening = ZERO; + BigDecimal closing = ZERO; + for (Map bucket : data.buckets) { + opening = opening.add(amount(bucket, "opening")); + closing = closing.add(amount(bucket, "closing")); + } + line(summary, "期初总资产", decimal(opening)); + line(summary, "区间资产增加", decimal(data.inflow)); + line(summary, "区间资产减少", decimal(data.outflow)); + line(summary, "区间资产净变动", decimal(data.inflow.subtract(data.outflow))); + line(summary, "期末总资产", decimal(closing)); + line(summary, "对账校验", opening.add(data.inflow).subtract(data.outflow).compareTo(closing) == 0 ? "一致" : "不一致,请联系管理员"); + line(summary, "口径", "总资产=可用+冻结+待结算;资产增减为账本净变化,不等于链上转账金额。"); + line(summary, "内部划转", "冻结、解冻、收益结算内部转入余额不重复算收支;分户增减包含内部划转。"); + line(summary, "范围", "本人同网络资金账户全部流水;双角色共用账户不按角色拆分;当天导出截至本次数据库快照。"); + line(summary, "空区间", "无流水时保留期初期末余额,明细仅有表头。"); + line(summary, "精度", "金额为6位小数文本,编号为文本,避免Excel自动舍入;业务类型以代码为准。"); + for (Map bucket : data.buckets) { + line(summary, text(bucket, "name") + ":期初 / 增加 / 减少 / 期末", + decimal(amount(bucket, "opening")) + " / " + decimal(amount(bucket, "increase")) + + " / " + decimal(amount(bucket, "decrease")) + " / " + decimal(amount(bucket, "closing"))); + } + summary.setColumnWidth(0, 36 * 256); + summary.setColumnWidth(1, 100 * 256); + + Sheet events = sheet(book, "业务分类汇总", header, "业务类型代码", "笔数", "资产增加USDT", + "资产减少USDT", "可用净变动USDT", "冻结净变动USDT", "待结算净变动USDT"); + Map totals = new LinkedHashMap<>(); + for (Map row : data.rows) { + BigDecimal[] values = totals.computeIfAbsent(text(row, "event_type"), key -> + new BigDecimal[] {BigDecimal.ZERO, ZERO, ZERO, ZERO, ZERO, ZERO}); + values[0] = values[0].add(BigDecimal.ONE); + BigDecimal net = RentalStatements.net(row); + values[net.signum() >= 0 ? 1 : 2] = values[net.signum() >= 0 ? 1 : 2].add(net.abs()); + values[3] = values[3].add(amount(row, "delta_available")); + values[4] = values[4].add(amount(row, "delta_frozen")); + values[5] = values[5].add(amount(row, "delta_supplier_pending")); + } + totals.forEach((type, v) -> line(events, type, v[0].toPlainString(), decimal(v[1]), decimal(v[2]), + decimal(v[3]), decimal(v[4]), decimal(v[5]))); + + Sheet details = sheet(book, "资金流水明细", header, "流水编号", "记账时间(服务器本地)", "业务类型代码", + "资产增加USDT", "资产减少USDT", "资产净变动USDT", "可用变动USDT", "冻结变动USDT", + "待结算变动USDT", "变更后可用USDT", "变更后冻结USDT", "变更后待结算USDT", + "租赁订单ID", "结算单ID", "退款单ID", "提现单ID", "原冲正流水ID", "记账说明"); + for (Map row : data.rows) { + BigDecimal net = RentalStatements.net(row); + line(details, text(row, "id"), TIME.format(time(row.get("created_at")).atZone(zone())), + text(row, "event_type"), decimal(net.max(ZERO)), decimal(net.min(ZERO).negate()), decimal(net), + decimal(amount(row, "delta_available")), decimal(amount(row, "delta_frozen")), + decimal(amount(row, "delta_supplier_pending")), decimal(amount(row, "after_available")), + decimal(amount(row, "after_frozen")), decimal(amount(row, "after_supplier_pending")), + text(row, "order_id"), text(row, "settlement_id"), text(row, "refund_id"), + text(row, "withdraw_id"), text(row, "reversal_of_id"), text(row, "reason")); + } + events.setAutoFilter(new org.apache.poi.ss.util.CellRangeAddress(0, events.getLastRowNum(), 0, 6)); + details.setAutoFilter(new org.apache.poi.ss.util.CellRangeAddress(0, details.getLastRowNum(), 0, 17)); + book.write(output); + } finally { + try { + book.close(); + } finally { + // SXSSF的窗口文件含用户资金明细,不在磁盘长期保留。 + book.dispose(); + } + } + } + + private static Sheet sheet(SXSSFWorkbook book, String name, CellStyle header, String... titles) { + Sheet sheet = book.createSheet(name); + line(sheet, titles); + for (Cell cell : sheet.getRow(0)) { + cell.setCellStyle(header); + sheet.setColumnWidth(cell.getColumnIndex(), 24 * 256); + } + sheet.createFreezePane(0, 1); + return sheet; + } + + private static void line(Sheet sheet, String... values) { + Row row = sheet.createRow(sheet.getPhysicalNumberOfRows() == 0 ? 0 : sheet.getLastRowNum() + 1); + for (int index = 0; index < values.length; index++) { + row.createCell(index).setCellValue(values[index]); + } + } + + private static String decimal(BigDecimal value) { + return value.setScale(6).toPlainString(); + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatements.java b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatements.java new file mode 100644 index 0000000..eb23a28 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalStatements.java @@ -0,0 +1,135 @@ +package com.ruoyi.system.rental; + +import static com.ruoyi.system.rental.RentalValues.*; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Transactional; + +/** + * 本人资金对账快照。只读查询,不创建账户,不调用链上服务,也不改变财务状态。 + * 同一用户同时具有两种角色时仍共用同网络资金账户,不能按订单角色截断账本。 + */ +@Service +public class RentalStatements { + private static final Logger LOG = LoggerFactory.getLogger(RentalStatements.class); + private static final int MAX_DAYS = 366; + private static final int MAX_ROWS = 50000; + private static final List DELTAS = Arrays.asList( + "delta_available", "delta_frozen", "delta_supplier_pending"); + private final RentalStore store; + + public RentalStatements(RentalStore store) { + this.store = store; + } + + /** + * 日期包含首尾两天,均按服务器本地时区解释;SQL采用[start,end)避免毫秒边界遗漏。 + * REPEATABLE_READ保证账户余额、期后流水和区间明细来自同一个数据库快照。 + * 超出限制明确拒绝,不截断后生成金额不完整的对账单。 + */ + @Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ) + public Statement snapshot(long user, String selectedNetwork, LocalDate from, LocalDate to) { + require(user > 0, "请先登录"); + String network = network(selectedNetwork); + require(from != null && to != null && !from.isAfter(to), "请选择有效的起止日期"); + require(ChronoUnit.DAYS.between(from, to) < MAX_DAYS, "单次最多导出366天,请缩小日期范围"); + LocalDateTime generated = now(); + require(!to.isAfter(generated.toLocalDate()), "结束日期不能晚于服务器当天"); + Map account = store.one( + "SELECT id,available_amount,frozen_amount,supplier_pending_amount FROM rent_account" + + " WHERE user_id=? AND network=?", user, network); + require(account != null, "当前网络尚未开通资金账户"); + long accountId = number(account, "id"); + LocalDateTime start = from.atStartOfDay(); + LocalDateTime end = to.plusDays(1).atStartOfDay(); + List> rows = store.rows( + "SELECT id,event_type,created_at,delta_available,delta_frozen,delta_supplier_pending," + + "after_available,after_frozen,after_supplier_pending,order_id,settlement_id," + + "refund_id,withdraw_id,reversal_of_id,reason FROM rent_ledger" + + " WHERE account_id=? AND created_at>=? AND created_at later = store.one("SELECT COALESCE(SUM(delta_available),0) delta_available," + + "COALESCE(SUM(delta_frozen),0) delta_frozen," + + "COALESCE(SUM(delta_supplier_pending),0) delta_supplier_pending" + + " FROM rent_ledger WHERE account_id=? AND created_at>=?", accountId, end); + List> buckets = new ArrayList<>(); + String[] balanceKeys = {"available_amount", "frozen_amount", "supplier_pending_amount"}; + String[] names = {"可用余额", "冻结余额", "待结算收益"}; + for (int index = 0; index < DELTAS.size(); index++) { + BigDecimal increase = ZERO; + BigDecimal decrease = ZERO; + for (Map row : rows) { + BigDecimal delta = amount(row, DELTAS.get(index)); + if (delta.signum() > 0) { + increase = increase.add(delta); + } else { + decrease = decrease.subtract(delta); + } + } + BigDecimal closing = amount(account, balanceKeys[index]).subtract(amount(later, DELTAS.get(index))); + BigDecimal opening = closing.subtract(increase).add(decrease); + buckets.add(map("name", names[index], "opening", opening, "increase", increase, + "decrease", decrease, "closing", closing)); + } + BigDecimal inflow = ZERO; + BigDecimal outflow = ZERO; + for (Map row : rows) { + BigDecimal net = net(row); + if (net.signum() > 0) { + inflow = inflow.add(net); + } else { + outflow = outflow.subtract(net); + } + } + LOG.info("租赁对账快照生成 userId={} accountId={} network={} from={} to={} rows={}", + user, accountId, network, from, to, rows.size()); + return new Statement(accountId, network, from, to, generated, rows, buckets, inflow, outflow); + } + + /** 三个资金分户之和才是总资产变化。内部冻结/结算划转的净变动为0。 */ + static BigDecimal net(Map row) { + return amount(row, "delta_available").add(amount(row, "delta_frozen")) + .add(amount(row, "delta_supplier_pending")); + } + + /** 仅供导出器使用的内部快照,不作为带敏感字段的通用数据库响应。 */ + static final class Statement { + final long accountId; + final String network; + final LocalDate from; + final LocalDate to; + final LocalDateTime generated; + final List> rows; + final List> buckets; + final BigDecimal inflow; + final BigDecimal outflow; + + Statement(long accountId, String network, LocalDate from, LocalDate to, LocalDateTime generated, + List> rows, List> buckets, + BigDecimal inflow, BigDecimal outflow) { + this.accountId = accountId; + this.network = network; + this.from = from; + this.to = to; + this.generated = generated; + this.rows = rows; + this.buckets = buckets; + this.inflow = inflow; + this.outflow = outflow; + } + } +} diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java index 6399e80..3c11bb2 100644 --- a/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java +++ b/ruoyi-system/src/test/java/com/ruoyi/system/rental/RentalBusinessTest.java @@ -46,6 +46,92 @@ public class RentalBusinessTest { private static final String OWNER = "TR7NHqjeKQxGTCi8q8ZY4pL8otgjLj6t"; private final ObjectMapper json = new ObjectMapper(); + /** 全账本核对:内部划转不重复计收支,历史区间扣除期后变动,跨用户/跨网络隔离。 */ + @Test + public void accountStatementReconcilesTransfersAndHistoricalBoundary() throws Exception { + java.time.LocalDate from = now().toLocalDate().minusDays(4); + java.time.LocalDate to = from.plusDays(2); + java.time.LocalDateTime at = from.atStartOfDay(); + store.execute("UPDATE rent_ledger SET created_at=? WHERE account_id=?", at.minusDays(1), payer); + statementPost(payer, "HOLD", "-10", "10", "0", at); + statementPost(payer, "CHARGE", "0", "-10", "0", at.plusHours(1)); + statementPost(payer, "REFUND", "2", "0", "0", at.plusHours(2)); + statementPost(payer, "SUPPLIER_EARN", "0", "0", "5", at.plusHours(3)); + statementPost(payer, "SUPPLIER_FEE", "0", "0", "-1", at.plusHours(4)); + statementPost(payer, "INCOME_SETTLE", "4", "0", "-4", at.plusHours(5)); + statementPost(payer, "WITHDRAW_HOLD", "-3", "3", "0", at.plusHours(6)); + statementPost(payer, "WITHDRAW_COMPLETE", "0", "-3", "0", at.plusHours(7)); + statementPost(payer, "TOPUP", "7", "0", "0", to.plusDays(1).atStartOfDay()); + statementPost(supplier, "SUPPLIER_EARN", "0", "0", "400", at); + long mainnet = number(funds.account(10, "MAINNET"), "id"); + statementPost(mainnet, "TOPUP", "80", "0", "0", at); + + RentalStatements.Statement report = tx.execute(status -> new RentalStatements(store).snapshot(10, "NILE", from, to)); + assertEquals(payer, report.accountId); + assertEquals(8, report.rows.size()); + assertEquals(money("7"), report.inflow); + assertEquals(money("14"), report.outflow); + assertEquals(money("100"), amount(report.buckets.get(0), "opening")); + assertEquals(money("93"), amount(report.buckets.get(0), "closing")); + assertEquals(money("0"), amount(report.buckets.get(1), "closing")); + assertEquals(money("0"), amount(report.buckets.get(2), "closing")); + RentalStatements.Statement foreign = tx.execute(status -> new RentalStatements(store).snapshot(20, "NILE", from, to)); + assertEquals(1, foreign.rows.size()); + assertEquals(money("400"), foreign.inflow); + + java.io.ByteArrayOutputStream bytes = new java.io.ByteArrayOutputStream(); + RentalStatementExcel.write(report, bytes); + // 留存隔离数据生成的工作簿,供本地页面下载联调;不含真实账户信息。 + Files.write(Paths.get("target/rental-statement-preview.xlsx"), bytes.toByteArray()); + try (org.apache.poi.xssf.usermodel.XSSFWorkbook book = new org.apache.poi.xssf.usermodel.XSSFWorkbook( + new java.io.ByteArrayInputStream(bytes.toByteArray()))) { + assertEquals(3, book.getNumberOfSheets()); + org.apache.poi.ss.usermodel.Sheet details = book.getSheet("资金流水明细"); + assertEquals(8, details.getLastRowNum()); + assertEquals("0.000000", details.getRow(1).getCell(5).getStringCellValue()); + assertEquals("-10.000000", details.getRow(1).getCell(6).getStringCellValue()); + assertEquals(org.apache.poi.ss.usermodel.CellType.STRING, details.getRow(1).getCell(17).getCellType()); + assertEquals("=untrusted-note", details.getRow(1).getCell(17).getStringCellValue()); + } + } + + /** 无流水仍需期初期末一致;非法日期/网络、未开通账户不能创建或导出其他账户。 */ + @Test + public void accountStatementValidatesScopeAndExportsEmptyPrecision() throws Exception { + java.time.LocalDate date = now().toLocalDate().minusDays(1); + RentalStatements service = new RentalStatements(store); + expectFailure(() -> service.snapshot(10, "UNKNOWN", date, date)); + expectFailure(() -> service.snapshot(10, "NILE", date, date.minusDays(1))); + expectFailure(() -> service.snapshot(10, "NILE", date.minusDays(366), date)); + expectFailure(() -> service.snapshot(10, "NILE", date, now().toLocalDate().plusDays(1))); + expectFailure(() -> service.snapshot(999, "NILE", date, date)); + assertNull(store.one("SELECT id FROM rent_account WHERE user_id=999")); + store.execute("UPDATE rent_ledger SET created_at=? WHERE account_id=?", date.minusDays(1).atStartOfDay(), payer); + statementPost(payer, "TOPUP", "999999999999999.123456", "0", "0", date.minusDays(1).atStartOfDay()); + RentalStatements.Statement empty = tx.execute(status -> service.snapshot(10, "NILE", date, date)); + assertTrue(empty.rows.isEmpty()); + assertEquals(money("1000000000000099.123456"), amount(empty.buckets.get(0), "closing")); + assertEquals(amount(empty.buckets.get(0), "closing"), amount(empty.buckets.get(0), "opening")); + java.io.ByteArrayOutputStream bytes = new java.io.ByteArrayOutputStream(); + RentalStatementExcel.write(empty, bytes); + try (org.apache.poi.xssf.usermodel.XSSFWorkbook book = new org.apache.poi.xssf.usermodel.XSSFWorkbook( + new java.io.ByteArrayInputStream(bytes.toByteArray()))) { + assertEquals(0, book.getSheet("资金流水明细").getLastRowNum()); + assertEquals("1000000000000099.123456", book.getSheetAt(0).getRow(8).getCell(1).getStringCellValue()); + } + } + + private void statementPost(long account, String type, String available, String frozen, String pending, + java.time.LocalDateTime at) { + String key = "STATEMENT-TEST:" + UUID.randomUUID(); + tx.execute(status -> { + funds.post(account, key, type, money(available), money(frozen), money(pending), ZERO, + null, null, null, 0, null, "=untrusted-note"); + store.execute("UPDATE rent_ledger SET created_at=? WHERE business_key=?", at, key); + return null; + }); + } + /** 待验证地址仅本人可删除,验证后或状态变化后必须保留。 */ @Test public void pendingWalletDeletionChecksOwnershipAndVerification() { diff --git a/sql/rent_statement_export_menu.sql b/sql/rent_statement_export_menu.sql new file mode 100644 index 0000000..32374bb --- /dev/null +++ b/sql/rent_statement_export_menu.sql @@ -0,0 +1,14 @@ +-- 租赁用户/供应商本人账单导出权限。可重复执行;不增加管理员读取他人账户的权限。 +-- 在已有租赁工作台菜单下添加按钮,父菜单必须已经部署。 +START TRANSACTION; +SET @statement_parent=(SELECT menu_id FROM sys_menu WHERE perms='rent:portal:view' AND menu_type='C' ORDER BY menu_id LIMIT 1); +INSERT INTO sys_menu(menu_name,parent_id,order_num,path,is_frame,is_cache,menu_type,visible,status,perms,icon,create_by,create_time) +SELECT '导出本人资金对账单',@statement_parent,30,'#',1,0,'F','0','0','rent:statement:export','#','admin',NOW() +WHERE @statement_parent IS NOT NULL AND NOT EXISTS(SELECT 1 FROM sys_menu WHERE perms='rent:statement:export'); +INSERT INTO sys_role_menu(role_id,menu_id) +SELECT r.role_id,m.menu_id FROM sys_role r JOIN sys_menu m ON m.perms='rent:statement:export' +WHERE r.role_key IN ('PROXY_ACCOUNT','PROXY_SUPPLIER') AND r.del_flag='0' + AND NOT EXISTS(SELECT 1 FROM sys_role_menu x WHERE x.role_id=r.role_id AND x.menu_id=m.menu_id); +COMMIT; +-- 若下面结果为空,说明租赁工作台菜单尚不存在;先部署工作台菜单再重新执行本脚本。 +SELECT menu_id,menu_name,parent_id,perms FROM sys_menu WHERE perms='rent:statement:export';