供应商和租赁账户首页不合理,帮我重新设计,应该是包含各自角色相关的统计图表信息,和监控状态信息。
This commit is contained in:
1 parent
8dcb313454
commit
02dd5668d5
7 files changed
+383
No files matched your search
@@ -0,0 +1,182 @@
|
||||
package com.ruoyi.system.rental;
|
||||
|
||||
import static com.ruoyi.system.rental.RentalValues.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Isolation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 本人租赁看板的只读聚合。禁止通过通用列表截取50行后在前端统计;
|
||||
* 每项查询均在数据库中完成归属及网络过滤,不触发RPC、创建账户或修改业务状态。
|
||||
*/
|
||||
@Service
|
||||
public class RentalDashboard {
|
||||
private final RentalStore store;
|
||||
|
||||
public RentalDashboard(RentalStore store) {
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
/** 同一事务快照保证本次余额、图表、监控口径一致;所有时间为UTC。 */
|
||||
@Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ)
|
||||
public Map<String, Object> overview(long user, String persona, String selectedNetwork, int days) {
|
||||
require(user > 0 && Arrays.asList("supplier", "account").contains(persona), "看板身份不合法");
|
||||
require(days == 7 || days == 14 || days == 30, "仅支持近7、14、30天");
|
||||
String network = network(selectedNetwork);
|
||||
boolean supplier = "supplier".equals(persona);
|
||||
LocalDateTime timestamp = now();
|
||||
LocalDateTime start = timestamp.toLocalDate().minusDays(days - 1).atStartOfDay();
|
||||
// 本人既是供应商又是租赁用户时,两种订单视角必须分开,不能使用OR合并。
|
||||
String owned = supplier ? "o.supplier_user_id=?"
|
||||
: "EXISTS(SELECT 1 FROM rent_app a WHERE a.id=o.app_id AND a.user_id=?)";
|
||||
String scope = " FROM rent_order o WHERE " + owned + " AND o.network=?";
|
||||
Map<String, Object> orders = store.one("SELECT COUNT(*) AS total,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='SUCCESS' THEN 1 ELSE 0 END),0) AS success,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='FAILED' THEN 1 ELSE 0 END),0) AS failed,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='SUCCESS' AND resource_type='ENERGY' THEN resource_quantity ELSE 0 END),0) AS energy,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='SUCCESS' AND resource_type='BANDWIDTH' THEN resource_quantity ELSE 0 END),0) AS bandwidth"
|
||||
+ scope + " AND o.created_at>=? AND o.created_at<=?", user, network, start, timestamp);
|
||||
Map<String, Object> monitoring = store.one("SELECT"
|
||||
+ " COALESCE(SUM(CASE WHEN status IN ('WAITING','EXECUTING','VERIFYING') THEN 1 ELSE 0 END),0) AS processing,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='MANUAL_REVIEW' OR reclaim_status='MANUAL_REVIEW' THEN 1 ELSE 0 END),0) AS manual,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='SUCCESS' AND service_expires_at>? AND reclaim_status<>'DONE' THEN 1 ELSE 0 END),0) AS active_leases,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='SUCCESS' AND service_expires_at<=? AND reclaim_status<>'DONE' THEN 1 ELSE 0 END),0) AS reclaim_pending"
|
||||
+ scope, timestamp, timestamp, user, network);
|
||||
Map<String, Object> account = store.one("SELECT status,available_amount,frozen_amount,supplier_pending_amount"
|
||||
+ " FROM rent_account WHERE user_id=? AND network=?", user, network);
|
||||
Map<String, Object> balance = map("status", account == null ? "NOT_OPEN" : account.get("status"),
|
||||
"available", decimal(account, "available_amount"), "frozen", decimal(account, "frozen_amount"));
|
||||
if (supplier) {
|
||||
balance.put("pending", decimal(account, "supplier_pending_amount"));
|
||||
}
|
||||
|
||||
// 按实际记账时间统计,冻结/解冻不算消费,结算转出不算收入。
|
||||
String moneyExpression = supplier
|
||||
? "CASE WHEN l.event_type IN ('SUPPLIER_EARN','SUPPLIER_FEE','REVERSAL') THEN l.delta_supplier_pending ELSE 0 END"
|
||||
: "CASE WHEN l.event_type='CHARGE' THEN -l.delta_frozen WHEN l.event_type='REFUND' THEN -l.delta_available ELSE 0 END";
|
||||
String ledger = " FROM rent_ledger l JOIN rent_account a ON a.id=l.account_id"
|
||||
+ " WHERE a.user_id=? AND a.network=? AND l.created_at>=? AND l.created_at<=?";
|
||||
Map<String, Object> money = store.one("SELECT COALESCE(SUM(" + moneyExpression + "),0) AS amount"
|
||||
+ ledger, user, network, start, timestamp);
|
||||
orders.put("amount", decimal(money, "amount"));
|
||||
|
||||
List<Map<String, Object>> distribution = store.rows("SELECT status,COUNT(*) AS total" + scope
|
||||
+ " AND o.created_at>=? AND o.created_at<=? GROUP BY status ORDER BY status",
|
||||
user, network, start, timestamp);
|
||||
List<Map<String, Object>> dailyOrders = store.rows("SELECT CAST(o.created_at AS DATE) AS day,COUNT(*) AS total,"
|
||||
+ " SUM(CASE WHEN status='SUCCESS' THEN 1 ELSE 0 END) AS success" + scope
|
||||
+ " AND o.created_at>=? AND o.created_at<=? GROUP BY CAST(o.created_at AS DATE)",
|
||||
user, network, start, timestamp);
|
||||
List<Map<String, Object>> dailyMoney = store.rows("SELECT CAST(l.created_at AS DATE) AS day,"
|
||||
+ " SUM(" + moneyExpression + ") AS amount" + ledger + " GROUP BY CAST(l.created_at AS DATE)",
|
||||
user, network, start, timestamp);
|
||||
Map<String, Map<String, Object>> calendar = new LinkedHashMap<>();
|
||||
for (int index = 0; index < days; index++) {
|
||||
String day = start.toLocalDate().plusDays(index).toString();
|
||||
calendar.put(day, map("day", day, "total", 0L, "success", 0L, "amount", "0.000000"));
|
||||
}
|
||||
for (Map<String, Object> row : dailyOrders) {
|
||||
Map<String, Object> day = calendar.get(text(row, "day"));
|
||||
if (day != null) {
|
||||
day.put("total", row.get("total"));
|
||||
day.put("success", row.get("success"));
|
||||
}
|
||||
}
|
||||
for (Map<String, Object> row : dailyMoney) {
|
||||
Map<String, Object> day = calendar.get(text(row, "day"));
|
||||
if (day != null) {
|
||||
day.put("amount", decimal(row, "amount"));
|
||||
}
|
||||
}
|
||||
List<Map<String, Object>> recent = store.rows("SELECT o.order_no,o.resource_type,o.resource_quantity,"
|
||||
+ "o.status,o.reclaim_status,o.created_at,o.service_expires_at" + scope
|
||||
+ " ORDER BY o.created_at DESC,o.id DESC LIMIT 6", user, network);
|
||||
timestamps(recent, "created_at", "service_expires_at");
|
||||
Map<String, Object> result = map("persona", persona, "network", network, "days", days,
|
||||
"generated_at", timestamp.toInstant(java.time.ZoneOffset.UTC).toEpochMilli(), "timezone", "UTC",
|
||||
"orders", orders, "monitoring", monitoring, "balance", balance, "distribution", distribution,
|
||||
"trend", new ArrayList<>(calendar.values()), "recent_orders", recent);
|
||||
if (supplier) {
|
||||
supplier(result, user, network, timestamp);
|
||||
} else {
|
||||
account(result, user, network, timestamp);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void supplier(Map<String, Object> result, long user, String network, LocalDateTime timestamp) {
|
||||
String online = "connection_status='ONLINE' AND heartbeat_expires_at>?";
|
||||
result.put("nodes", store.one("SELECT COUNT(*) AS total,"
|
||||
+ " COALESCE(SUM(CASE WHEN " + online + " THEN 1 ELSE 0 END),0) AS online,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='ACTIVE' AND " + online + " THEN 1 ELSE 0 END),0) AS active_online,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='PENDING' THEN 1 ELSE 0 END),0) AS pending"
|
||||
+ " FROM rent_node WHERE user_id=?", timestamp, timestamp, user));
|
||||
// 可用量仅展示有效且已核验的钱包投影,不推断实际链上可调度量。
|
||||
String fresh = "w.status='ACTIVE' AND w.ownership_verified_at IS NOT NULL"
|
||||
+ " AND w.snapshot_expires_at>? AND w.snapshot_block_height>=w.snapshot_min_block_height";
|
||||
result.put("wallets", store.one("SELECT COUNT(*) AS total,"
|
||||
+ " COALESCE(SUM(CASE WHEN " + fresh + " THEN 1 ELSE 0 END),0) AS fresh,"
|
||||
+ " COALESCE(SUM(CASE WHEN " + fresh + " AND w.energy_enabled=1 THEN GREATEST(0,w.energy_available) ELSE 0 END),0) AS energy,"
|
||||
+ " COALESCE(SUM(CASE WHEN " + fresh + " AND w.bandwidth_enabled=1 THEN GREATEST(0,w.bandwidth_available) ELSE 0 END),0) AS bandwidth"
|
||||
+ " FROM rent_wallet w JOIN rent_node n ON n.id=w.node_id WHERE n.user_id=? AND w.network=?",
|
||||
timestamp, timestamp, timestamp, user, network));
|
||||
List<Map<String, Object>> nodes = store.rows("SELECT n.node_no,n.name,n.status,n.connection_status,"
|
||||
+ "n.heartbeat_at,n.heartbeat_expires_at,n.agent_version FROM rent_node n WHERE n.user_id=?"
|
||||
+ " ORDER BY CASE WHEN n.connection_status='ONLINE' AND n.heartbeat_expires_at>? THEN 1 ELSE 0 END,"
|
||||
+ "n.heartbeat_at DESC,n.id DESC LIMIT 8", user, timestamp);
|
||||
for (Map<String, Object> node : nodes) {
|
||||
boolean live = "ONLINE".equals(text(node, "connection_status"))
|
||||
&& node.get("heartbeat_expires_at") != null
|
||||
&& time(node.get("heartbeat_expires_at")).isAfter(timestamp.toInstant(java.time.ZoneOffset.UTC));
|
||||
node.put("online", live);
|
||||
}
|
||||
timestamps(nodes, "heartbeat_at", "heartbeat_expires_at");
|
||||
result.put("node_status", nodes);
|
||||
result.put("tasks", store.rows("SELECT t.status,COUNT(*) AS total FROM rent_task t"
|
||||
+ " JOIN rent_node n ON n.id=t.node_id WHERE n.user_id=? AND t.network=?"
|
||||
+ " AND t.status IN ('PENDING','ACCEPTED','BROADCASTED','UNKNOWN') GROUP BY t.status",
|
||||
user, network));
|
||||
result.put("settlements", store.one("SELECT COUNT(*) AS pending FROM rent_settlement s"
|
||||
+ " JOIN rent_account a ON a.id=s.account_id WHERE a.user_id=? AND a.network=?"
|
||||
+ " AND s.status IN ('PENDING','APPROVED','PAYING','UNKNOWN')", user, network));
|
||||
}
|
||||
|
||||
private void account(Map<String, Object> result, long user, String network, LocalDateTime timestamp) {
|
||||
result.put("apps", store.one("SELECT COUNT(*) AS total,"
|
||||
+ " COALESCE(SUM(CASE WHEN status='ACTIVE' THEN 1 ELSE 0 END),0) AS active"
|
||||
+ " FROM rent_app WHERE user_id=?", user));
|
||||
result.put("credentials", store.one("SELECT COUNT(*) AS active FROM rent_credential c"
|
||||
+ " JOIN rent_app a ON a.id=c.owner_id WHERE c.owner_type='APP' AND a.user_id=?"
|
||||
+ " AND a.status='ACTIVE' AND c.status='ACTIVE' AND (c.expires_at IS NULL OR c.expires_at>?)",
|
||||
user, timestamp));
|
||||
result.put("webhooks", store.rows("SELECT h.status,COUNT(*) AS total FROM rent_webhook h"
|
||||
+ " JOIN rent_order o ON o.id=h.order_id JOIN rent_app a ON a.id=o.app_id"
|
||||
+ " WHERE a.user_id=? AND o.network=? GROUP BY h.status", user, network));
|
||||
result.put("package", store.one("SELECT p.name,p.network,p.resource_type,p.resource_quantity,"
|
||||
+ "p.status,p.duration_seconds FROM rent_user_package b JOIN rent_package p ON p.id=b.package_id"
|
||||
+ " WHERE b.user_id=? AND b.status='ACTIVE' AND b.default_slot=1", user));
|
||||
result.put("refunds", store.one("SELECT COUNT(*) AS pending FROM rent_refund r"
|
||||
+ " JOIN rent_order o ON o.id=r.order_id JOIN rent_app a ON a.id=o.app_id"
|
||||
+ " WHERE a.user_id=? AND o.network=? AND r.status='PENDING'", user, network));
|
||||
}
|
||||
|
||||
/** 金额返回精确十进制字符串,浏览器仅在图形坐标上做近似映射。 */
|
||||
private String decimal(Map<String, Object> row, String key) {
|
||||
return (row == null || row.get(key) == null ? BigDecimal.ZERO : new BigDecimal(text(row, key)))
|
||||
.setScale(6).toPlainString();
|
||||
}
|
||||
|
||||
private void timestamps(List<Map<String, Object>> rows, String... fields) {
|
||||
for (Map<String, Object> row : rows) {
|
||||
for (String field : fields) {
|
||||
if (row.get(field) != null) {
|
||||
row.put(field, time(row.get(field)).toEpochMilli());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.ruoyi.system.rental;
|
||||
|
||||
import static com.ruoyi.system.rental.RentalValues.require;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/** 角色看板仅查询当前登录用户;不接受客户端user_id,不提供全平台聚合旁路。 */
|
||||
@RestController
|
||||
@RequestMapping("/rental/dashboard")
|
||||
public class RentalDashboardController {
|
||||
private final RentalDashboard dashboard;
|
||||
|
||||
public RentalDashboardController(RentalDashboard dashboard) {
|
||||
this.dashboard = dashboard;
|
||||
}
|
||||
|
||||
@GetMapping("/{persona}")
|
||||
@PreAuthorize("@ss.hasPermi('rent:portal:view')")
|
||||
public AjaxResult overview(@PathVariable String persona,
|
||||
@RequestParam(defaultValue = "NILE") String network,
|
||||
@RequestParam(defaultValue = "14") int days) {
|
||||
Long user = SecurityUtils.getUserId();
|
||||
require(user != null, "请先登录");
|
||||
return AjaxResult.success(dashboard.overview(user, persona, network, days));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user