建议统一成“收益结算 + 提现付款” 按建议的逻辑帮我实现。
This commit is contained in:
1 parent
1151dc2535
commit
be6ac23844
18 files changed
+352
-16
No files matched your search
@@ -492,6 +492,7 @@ public class RentalBusinessTest {
|
||||
Files.readAllBytes(Paths.get("../sql/rent_init.sql")),
|
||||
StandardCharsets.UTF_8)
|
||||
.replaceAll("(?m)^--.*$", "")
|
||||
.replace("GENERATED ALWAYS AS", "AS")
|
||||
.replaceAll("CHARACTER SET ascii COLLATE ascii_bin", "")
|
||||
.replaceAll("(?i)\\bJSON\\b", "LONGTEXT")
|
||||
.replaceAll(
|
||||
@@ -915,6 +916,139 @@ public class RentalBusinessTest {
|
||||
assertEquals(1, activationChecks);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onchainPendingAddressBlocksNewOrderUntilCancelled() {
|
||||
Map<String, Object> original = tx.execute(status -> orders.create(app, 10, onchainRequest("pending-first")));
|
||||
BigDecimal frozen = amount(store.get("account", payer, false), "frozen_amount");
|
||||
long otherApp = store.insert("app", map("user_id", 30, "name", "other-user", "status", "ACTIVE"));
|
||||
expectFailure(() -> tx.execute(status -> orders.create(otherApp, 30, onchainRequest("other-user-order"))));
|
||||
for (String state : new String[] {"WAITING", "EXECUTING", "VERIFYING", "MANUAL_REVIEW"}) {
|
||||
store.update("order", number(original, "id"), map("status", state));
|
||||
expectFailure(() -> tx.execute(status -> orders.create(app, 10, onchainRequest("pending-second"))));
|
||||
assertEquals(frozen, amount(store.get("account", payer, false), "frozen_amount"));
|
||||
}
|
||||
store.update("order", number(original, "id"), map("status", "WAITING"));
|
||||
tx.execute(status -> { orders.cancel(number(original, "id"), 10); return null; });
|
||||
Map<String, Object> next = tx.execute(status -> orders.create(app, 10, onchainRequest("pending-second")));
|
||||
assertNotEquals(number(original, "id"), number(next, "id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onchainPendingIndexRejectsDuplicateEvenWithoutBusinessCheck() {
|
||||
Map<String, Object> original = tx.execute(status -> orders.create(app, 10, onchainRequest("index-first")));
|
||||
Map<String, Object> duplicate = new java.util.HashMap<>(original);
|
||||
duplicate.remove("id");
|
||||
duplicate.remove("onchain_pending_slot");
|
||||
duplicate.put("order_no", "duplicate-index-order");
|
||||
duplicate.put("merchant_order_no", "duplicate-index-merchant");
|
||||
try {
|
||||
tx.execute(status -> store.insert("order", duplicate));
|
||||
fail("数据库必须拒绝同地址的并发待处理订单");
|
||||
} catch (org.springframework.dao.DuplicateKeyException expected) {
|
||||
assertTrue(expected.getMostSpecificCause().getMessage().toLowerCase(java.util.Locale.ROOT)
|
||||
.contains("uk_order_onchain_pending"));
|
||||
}
|
||||
}
|
||||
|
||||
private static void await(CountDownLatch latch) {
|
||||
try {
|
||||
if (!latch.await(10, TimeUnit.SECONDS)) {
|
||||
throw new IllegalStateException("等待并发测试启动超时");
|
||||
}
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
/** 真实冻结与确认记账,链上证明由其他用例覆盖;这里隔离收益结算资金流。 */
|
||||
private long incomeOrder() {
|
||||
return tx.execute(status -> {
|
||||
Map<String, Object> order = orders.create(app, 10, onchainRequest("income-source"));
|
||||
long id = number(order, "id");
|
||||
store.update("order", id, map("status", "SUCCESS", "supplier_account_id", supplier,
|
||||
"supplier_user_id", 20));
|
||||
funds.charge(store.get("order", id, true));
|
||||
return id;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incomeApprovalMovesBalanceExactlyOnceAndCannotPayOrResettle() throws Exception {
|
||||
long order = incomeOrder();
|
||||
long settlement = tx.execute(status -> reviews.settlement(Collections.singletonList(order), 90));
|
||||
expectFailure(() -> tx.execute(status -> reviews.transition(settlement, "approve", 90, "self")));
|
||||
expectFailure(() -> tx.execute(status -> reviews.transition(settlement, "pay", 91, "wrong-mode")));
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
try {
|
||||
Future<?> first = executor.submit(() -> { await(start); tx.execute(status -> reviews.transition(settlement, "approve", 91, "审核")); });
|
||||
Future<?> second = executor.submit(() -> { await(start); tx.execute(status -> reviews.transition(settlement, "approve", 92, "重试")); });
|
||||
start.countDown();
|
||||
first.get(15, TimeUnit.SECONDS);
|
||||
second.get(15, TimeUnit.SECONDS);
|
||||
} finally { executor.shutdownNow(); }
|
||||
assertEquals("SETTLED", text(store.get("settlement", settlement, false), "status"));
|
||||
assertNotNull(store.get("settlement", settlement, false).get("settled_at"));
|
||||
assertNull(store.get("settlement", settlement, false).get("paid_at"));
|
||||
assertEquals(money("0.163457"), amount(store.get("account", supplier, false), "available_amount"));
|
||||
assertEquals(ZERO, amount(store.get("account", supplier, false), "supplier_pending_amount"));
|
||||
assertEquals(1, store.rows("SELECT id FROM rent_ledger WHERE business_key=?", "INCOME_SETTLE:" + settlement).size());
|
||||
expectFailure(() -> tx.execute(status -> reviews.settlement(Collections.singletonList(order), 90)));
|
||||
expectFailure(() -> tx.execute(status -> reviews.transition(settlement, "reject", 91, "too-late")));
|
||||
expectFailure(() -> tx.execute(status -> { reviews.paid(settlement, "fake", "{}", 91); return null; }));
|
||||
|
||||
// 结算收入进入现有提现入口,不需要供应商专用付款或重复的结算付款逻辑。
|
||||
org.springframework.core.env.StandardEnvironment env = new org.springframework.core.env.StandardEnvironment();
|
||||
env.getPropertySources().addFirst(new org.springframework.core.env.MapPropertySource("withdraw-income", map(
|
||||
"rental.enabled", true, "rental.withdraw.enabled", true, "rental.withdraw.min-amount-usdt", "0.01",
|
||||
"rental.withdraw.nile.payout-addresses", "T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb")));
|
||||
RentalWithdrawals withdraw = new RentalWithdrawals(store, funds, null, env,
|
||||
new ObjectMapper(), null, tx.getTransactionManager());
|
||||
Map<String, Object> request = map("network", "NILE", "receiver_address", ADDRESS,
|
||||
"client_request_no", "income-withdraw", "amount", "0.1");
|
||||
withdraw.create(20, request);
|
||||
withdraw.create(20, request);
|
||||
assertEquals(money("0.063457"), amount(store.get("account", supplier, false), "available_amount"));
|
||||
assertEquals(money("0.100000"), amount(store.get("account", supplier, false), "frozen_amount"));
|
||||
assertEquals(ZERO, amount(store.get("account", supplier, false), "supplier_pending_amount"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incomeRejectReleasesSourcesAndFailedApprovalRollsBack() {
|
||||
long order = incomeOrder();
|
||||
long first = tx.execute(status -> reviews.settlement(Collections.singletonList(order), 90));
|
||||
store.update("account", supplier, map("status", "DISABLED"));
|
||||
expectFailure(() -> tx.execute(status -> reviews.transition(first, "approve", 91, "blocked")));
|
||||
assertEquals("PENDING", text(store.get("settlement", first, false), "status"));
|
||||
assertEquals(ZERO, amount(store.get("account", supplier, false), "available_amount"));
|
||||
assertEquals(money("0.163457"), amount(store.get("account", supplier, false), "supplier_pending_amount"));
|
||||
tx.execute(status -> reviews.transition(first, "reject", 91, "重新核对"));
|
||||
store.update("account", supplier, map("status", "ACTIVE"));
|
||||
long next = tx.execute(status -> reviews.settlement(Collections.singletonList(order), 90));
|
||||
tx.execute(status -> reviews.transition(next, "approve", 91, "重新结算"));
|
||||
assertEquals(money("0.163457"), amount(store.get("account", supplier, false), "available_amount"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void legacyPaidDoesNotCreditWithdrawableBalance() {
|
||||
long order = incomeOrder();
|
||||
long id = tx.execute(status -> reviews.settlement(Collections.singletonList(order), 90));
|
||||
// 模拟升级前已存在的结算单;迁移脚本默认所有历史行均为LEGACY_PAYOUT。
|
||||
store.update("settlement", id, map("settlement_mode", "LEGACY_PAYOUT"));
|
||||
tx.execute(status -> {
|
||||
reviews.transition(id, "approve", 91, "legacy");
|
||||
reviews.transition(id, "pay", 91, "legacy");
|
||||
reviews.paid(id, "NILE:legacy:0", "{}", 91);
|
||||
reviews.paid(id, "NILE:legacy:0", "{}", 91);
|
||||
return null;
|
||||
});
|
||||
assertEquals("PAID", text(store.get("settlement", id, false), "status"));
|
||||
assertEquals(ZERO, amount(store.get("account", supplier, false), "available_amount"));
|
||||
assertEquals(ZERO, amount(store.get("account", supplier, false), "supplier_pending_amount"));
|
||||
expectFailure(() -> tx.execute(status -> reviews.settlement(Collections.singletonList(order), 90)));
|
||||
}
|
||||
|
||||
private Map<String, Object> onchainRequest(String merchant) {
|
||||
return map("order_type", "ADDRESS_ONCHAIN", "network", "NILE", "merchant_order_no", merchant,
|
||||
"receiver_address", ADDRESS, "expected_amount", "0.173457");
|
||||
@@ -1305,7 +1439,7 @@ public class RentalBusinessTest {
|
||||
@Test
|
||||
public void historyCleanupReadsRealBusinessSchemaAndKeepsFunds() throws Exception {
|
||||
String ddl = new String(Files.readAllBytes(Paths.get("../sql/rent_history_init.sql")), StandardCharsets.UTF_8)
|
||||
.replaceAll("(?m)^--.*$", "")
|
||||
.replaceAll("(?m)^--.*$", "").replace("GENERATED ALWAYS AS", "AS")
|
||||
.replace("ENGINE=InnoDB ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", "");
|
||||
for (String statement : ddl.split(";")) {
|
||||
if (!statement.trim().isEmpty()) { store.execute(statement); }
|
||||
@@ -2225,6 +2359,8 @@ public class RentalBusinessTest {
|
||||
tx.execute(status -> reviews.settlement(Collections.singletonList(id), 90));
|
||||
tx.execute(
|
||||
status -> {
|
||||
// 仅历史模式仍允许领取付款授权,验证旧单UNKNOWN/PAYING占用保护。
|
||||
store.update("settlement", settlement, map("settlement_mode", "LEGACY_PAYOUT"));
|
||||
reviews.transition(settlement, "approve", 91, "test");
|
||||
reviews.transition(settlement, "pay", 91, "test");
|
||||
return null;
|
||||
|
||||
@@ -43,7 +43,7 @@ public class RentalHistoryCleanupTest {
|
||||
store = new RentalStore(jdbc);
|
||||
manager = new DataSourceTransactionManager(source);
|
||||
String ddl = new String(Files.readAllBytes(Paths.get("../sql/rent_history_init.sql")), StandardCharsets.UTF_8)
|
||||
.replaceAll("(?m)^--.*$", "")
|
||||
.replaceAll("(?m)^--.*$", "").replace("GENERATED ALWAYS AS", "AS")
|
||||
.replace("ENGINE=InnoDB ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", "");
|
||||
for (String statement : ddl.split(";")) {
|
||||
if (!statement.trim().isEmpty()) { jdbc.execute(statement); }
|
||||
|
||||
@@ -51,7 +51,7 @@ public class RentalHistoryTest {
|
||||
jdbc.execute("CREATE ALIAS UTC_TIMESTAMP FOR \"com.ruoyi.system.rental.RentalHistoryTest.utcTimestamp\"");
|
||||
store = new RentalStore(jdbc);
|
||||
String ddl = new String(Files.readAllBytes(Paths.get("../sql/rent_history_init.sql")),
|
||||
StandardCharsets.UTF_8).replaceAll("(?m)^--.*$", "")
|
||||
StandardCharsets.UTF_8).replaceAll("(?m)^--.*$", "").replace("GENERATED ALWAYS AS", "AS")
|
||||
.replace("ENGINE=InnoDB ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", "");
|
||||
for (String statement : ddl.split(";")) {
|
||||
if (!statement.trim().isEmpty()) {
|
||||
|
||||
@@ -64,7 +64,7 @@ public class RentalTopupTest {
|
||||
store = new RentalStore(jdbc);
|
||||
for (String file : Arrays.asList("rent_init.sql", "rent_topup_init.sql")) {
|
||||
String ddl = new String(Files.readAllBytes(Paths.get("../sql/" + file)), StandardCharsets.UTF_8)
|
||||
.replaceAll("(?m)^--.*$", "").replace("CHARACTER SET ascii COLLATE ascii_bin", "")
|
||||
.replaceAll("(?m)^--.*$", "").replace("GENERATED ALWAYS AS", "AS").replace("CHARACTER SET ascii COLLATE ascii_bin", "")
|
||||
.replaceAll("(?i)\\bJSON\\b", "LONGTEXT")
|
||||
.replace("ENGINE=InnoDB ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", "");
|
||||
for (String sql : ddl.split(";")) if (!sql.trim().isEmpty()) jdbc.execute(sql);
|
||||
|
||||
@@ -54,7 +54,7 @@ public class RentalWithdrawTest {
|
||||
JdbcTemplate jdbc = new JdbcTemplate(source);
|
||||
store = new RentalStore(jdbc);
|
||||
String ddl = new String(Files.readAllBytes(Paths.get("../sql/rent_init.sql")), StandardCharsets.UTF_8)
|
||||
.replaceAll("(?m)^--.*$", "").replace("CHARACTER SET ascii COLLATE ascii_bin", "")
|
||||
.replaceAll("(?m)^--.*$", "").replace("GENERATED ALWAYS AS", "AS").replace("CHARACTER SET ascii COLLATE ascii_bin", "")
|
||||
.replaceAll("(?i)\\bJSON\\b", "LONGTEXT")
|
||||
.replace("ENGINE=InnoDB ROW_FORMAT=DYNAMIC DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", "");
|
||||
for (String sql : ddl.split(";")) { if (!sql.trim().isEmpty()) { jdbc.execute(sql); } }
|
||||
|
||||
Reference in new issue
Block a user