Files
usdt/ruoyi-system/src/main/java/com/ruoyi/system/rental/RentalReconciliation.java
T
2026-10-22 19:58:57 -04:00

78 lines
3.5 KiB
Java

package com.ruoyi.system.rental;
import static com.ruoyi.system.rental.RentalValues.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
/** 账账核对只报告差异,不自动改余额或释放未知本金;同一可重复读快照避免正常并发误报。 */
@Service
public class RentalReconciliation {
private final RentalStore store;
public RentalReconciliation(RentalStore store) {
this.store = store;
}
@Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ)
public Map<String, Object> check(long afterId) {
List<Map<String, Object>> accounts =
store.rows("SELECT * FROM rent_account WHERE id>? ORDER BY id LIMIT 100", afterId);
List<Map<String, Object>> differences = new ArrayList<>();
long last = afterId;
for (Map<String, Object> account : accounts) {
last = number(account, "id");
Map<String, Object> sum =
store.one(
"SELECT COALESCE(SUM(delta_available),0) AS"
+ " available_amount,COALESCE(SUM(delta_frozen),0) AS"
+ " frozen_amount,COALESCE(SUM(delta_supplier_pending),0) AS"
+ " supplier_pending_amount FROM rent_ledger WHERE account_id=?",
last);
for (String column :
Arrays.asList("available_amount", "frozen_amount", "supplier_pending_amount")) {
if (amount(account, column).compareTo(amount(sum, column)) != 0
|| amount(account, column).signum() < 0) {
differences.add(
map(
"type",
"ACCOUNT_LEDGER",
"id",
last,
"field",
column,
"balance",
account.get(column),
"ledger_total",
sum.get(column)));
}
}
}
return map(
"checked",
accounts.size(),
"next_after_id",
last,
"has_more",
accounts.size() == 100,
"differences",
differences);
}
@Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ)
public List<Map<String, Object>> reservations() {
return store.rows(
"SELECT w.id,w.energy_reserved_sun,w.bandwidth_reserved_sun,COALESCE(SUM(CASE WHEN"
+ " t.resource_type='ENERGY' THEN t.delegate_sun ELSE 0 END),0) AS"
+ " expected_energy,COALESCE(SUM(CASE WHEN t.resource_type='BANDWIDTH' THEN"
+ " t.delegate_sun ELSE 0 END),0) AS expected_bandwidth FROM rent_wallet w LEFT"
+ " JOIN rent_task t ON t.wallet_id=w.id AND t.reservation_status='HELD' GROUP"
+ " BY w.id,w.energy_reserved_sun,w.bandwidth_reserved_sun HAVING"
+ " w.energy_reserved_sun<>expected_energy OR"
+ " w.bandwidth_reserved_sun<>expected_bandwidth LIMIT 100");
}
}