feat: problems local stats, user status, admin panel enhancements, rating text

- Problems page: replace Luogu pass rate with local submission stats
  (local_submit_count, local_ac_count)
- Problems page: add user AC/fail status column (user_ac, user_fail_count)
- Admin users: add total_submissions and total_ac columns
- Admin users: add detail panel with submissions/rating/redeem tabs
- Admin: new endpoint GET /api/v1/admin/users/{id}/rating-history
- Rating history: note field includes problem title via JOIN
- Me page: translate task codes to friendly labels with icons
- Me page: problem links in rating history are clickable
- Wrong book service, learning note scoring, note image controller
- Backend SQL uses batch queries for performance

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
这个提交包含在:
cryptocommuniums-afk
2026-02-16 17:35:22 +08:00
父节点 7860414ae5
当前提交 cfbe9a0363
修改 22 个文件,包含 1366 行新增26 行删除

查看文件

@@ -1,17 +1,26 @@
"use client";
import { useEffect, useState } from "react";
import React, { useEffect, useState } from "react";
import { apiFetch } from "@/lib/api";
import { apiFetch, type RatingHistoryItem } from "@/lib/api";
import { readToken } from "@/lib/auth";
import { useI18nText } from "@/lib/i18n";
import { RefreshCw, Save, Shield, UserCog, Users } from "lucide-react";
import {
ChevronDown,
ChevronUp,
RefreshCw,
Save,
Shield,
Users,
} from "lucide-react";
type AdminUser = {
id: number;
username: string;
rating: number;
created_at: number;
total_submissions: number;
total_ac: number;
};
type ListResp = {
@@ -21,17 +30,155 @@ type ListResp = {
page_size: number;
};
type SubmissionRow = {
id: number;
problem_id: number;
status: string;
score: number;
language: string;
created_at: number;
};
type RedeemRow = {
id: number;
item_name: string;
quantity: number;
day_type: string;
total_cost: number;
created_at: number;
};
function fmtTs(v: number): string {
if (!v) return "-";
return new Date(v * 1000).toLocaleString();
}
function DetailPanel({ userId, tx }: { userId: number; tx: (zh: string, en: string) => string }) {
const [tab, setTab] = useState<"subs" | "rating" | "redeem">("subs");
const [subs, setSubs] = useState<SubmissionRow[]>([]);
const [ratingH, setRatingH] = useState<RatingHistoryItem[]>([]);
const [redeems, setRedeems] = useState<RedeemRow[]>([]);
const [loading, setLoading] = useState(false);
const token = readToken() ?? "";
useEffect(() => {
setLoading(true);
const loadTab = async () => {
try {
if (tab === "subs") {
const d = await apiFetch<{ items: SubmissionRow[] }>(
`/api/v1/submissions?user_id=${userId}&page=1&page_size=50`,
undefined, token
);
setSubs(d.items ?? []);
} else if (tab === "rating") {
const d = await apiFetch<RatingHistoryItem[]>(
`/api/v1/admin/users/${userId}/rating-history?limit=100`,
undefined, token
);
setRatingH(Array.isArray(d) ? d : []);
} else {
const d = await apiFetch<RedeemRow[]>(
`/api/v1/admin/redeem-records?user_id=${userId}&limit=100`,
undefined, token
);
setRedeems(Array.isArray(d) ? d : []);
}
} catch { /* ignore */ }
setLoading(false);
};
void loadTab();
}, [tab, userId, token]);
const tabCls = (t: string) =>
`px-3 py-1 text-xs border ${tab === t ? "bg-zinc-900 text-white" : "bg-white text-zinc-700 hover:bg-zinc-100"}`;
return (
<div className="bg-zinc-50 border-t p-3 text-xs">
<div className="flex gap-1 mb-2">
<button className={tabCls("subs")} onClick={() => setTab("subs")}>
{tx("提交记录", "Submissions")}
</button>
<button className={tabCls("rating")} onClick={() => setTab("rating")}>
{tx("积分历史", "Rating History")}
</button>
<button className={tabCls("redeem")} onClick={() => setTab("redeem")}>
{tx("兑换记录", "Redeem Records")}
</button>
</div>
{loading && <p className="text-zinc-500">{tx("加载中...", "Loading...")}</p>}
{!loading && tab === "subs" && (
<div className="max-h-48 overflow-y-auto">
<table className="min-w-full text-xs">
<thead><tr className="text-left text-zinc-500">
<th className="pr-2">ID</th><th className="pr-2">{tx("题目", "Problem")}</th>
<th className="pr-2">{tx("状态", "Status")}</th><th className="pr-2">{tx("分数", "Score")}</th>
<th>{tx("时间", "Time")}</th>
</tr></thead>
<tbody>
{subs.map((s) => (
<tr key={s.id} className="border-t border-zinc-200">
<td className="pr-2">{s.id}</td>
<td className="pr-2">P{s.problem_id}</td>
<td className={`pr-2 font-bold ${s.status === "AC" ? "text-emerald-600" : "text-red-600"}`}>{s.status}</td>
<td className="pr-2">{s.score}</td>
<td className="text-zinc-500">{fmtTs(s.created_at)}</td>
</tr>
))}
{subs.length === 0 && <tr><td colSpan={5} className="text-zinc-400 py-2">{tx("无记录", "No records")}</td></tr>}
</tbody>
</table>
</div>
)}
{!loading && tab === "rating" && (
<div className="max-h-48 overflow-y-auto space-y-1">
{ratingH.map((item, i) => (
<div key={i} className="flex justify-between border-b border-zinc-200 pb-1">
<span>
<span className={`font-bold ${item.change > 0 ? "text-emerald-600" : "text-red-600"}`}>
{item.change > 0 ? `+${item.change}` : item.change}
</span>
<span className="ml-2 text-zinc-600">{item.note}</span>
</span>
<span className="text-zinc-400">{fmtTs(item.created_at)}</span>
</div>
))}
{ratingH.length === 0 && <p className="text-zinc-400">{tx("无记录", "No records")}</p>}
</div>
)}
{!loading && tab === "redeem" && (
<div className="max-h-48 overflow-y-auto">
<table className="min-w-full text-xs">
<thead><tr className="text-left text-zinc-500">
<th className="pr-2">{tx("物品", "Item")}</th><th className="pr-2">{tx("数量", "Qty")}</th>
<th className="pr-2">{tx("花费", "Cost")}</th><th>{tx("时间", "Time")}</th>
</tr></thead>
<tbody>
{redeems.map((r) => (
<tr key={r.id} className="border-t border-zinc-200">
<td className="pr-2">{r.item_name}</td>
<td className="pr-2">{r.quantity}</td>
<td className="pr-2 text-red-600">-{r.total_cost}</td>
<td className="text-zinc-500">{fmtTs(r.created_at)}</td>
</tr>
))}
{redeems.length === 0 && <tr><td colSpan={4} className="text-zinc-400 py-2">{tx("无记录", "No records")}</td></tr>}
</tbody>
</table>
</div>
)}
</div>
);
}
export default function AdminUsersPage() {
const { tx } = useI18nText();
const [items, setItems] = useState<AdminUser[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [msg, setMsg] = useState("");
const [expandedId, setExpandedId] = useState<number | null>(null);
const load = async () => {
setLoading(true);
@@ -110,13 +257,16 @@ export default function AdminUsersPage() {
<Shield size={14} />
Rating
</th>
<th className="px-3 py-2">{tx("提交", "Subs")}</th>
<th className="px-3 py-2">AC</th>
<th className="px-3 py-2">{tx("创建时间", "Created At")}</th>
<th className="px-3 py-2">{tx("操作", "Action")}</th>
</tr>
</thead>
<tbody>
{items.map((user) => (
<tr key={user.id} className="border-t">
<React.Fragment key={user.id}>
<tr className="border-t">
<td className="px-3 py-2">{user.id}</td>
<td className="px-3 py-2">{user.username}</td>
<td className="px-3 py-2">
@@ -133,8 +283,10 @@ export default function AdminUsersPage() {
}}
/>
</td>
<td className="px-3 py-2">{user.total_submissions}</td>
<td className="px-3 py-2 text-emerald-700 font-bold">{user.total_ac}</td>
<td className="px-3 py-2 text-zinc-600">{fmtTs(user.created_at)}</td>
<td className="px-3 py-2">
<td className="px-3 py-2 flex items-center gap-1">
<button
className="rounded border px-3 py-1 text-xs hover:bg-zinc-100 flex items-center gap-1"
onClick={() => void updateRating(user.id, Math.max(0, Number(user.rating) || 0))}
@@ -142,12 +294,22 @@ export default function AdminUsersPage() {
<Save size={12} />
{tx("保存", "Save")}
</button>
<button
className="rounded border px-2 py-1 text-xs hover:bg-zinc-100"
onClick={() => setExpandedId(expandedId === user.id ? null : user.id)}
>
{expandedId === user.id ? <ChevronUp size={14} /> : <ChevronDown size={14} />}
</button>
</td>
</tr>
{expandedId === user.id && (
<tr><td colSpan={7} className="p-0"><DetailPanel userId={user.id} tx={tx} /></td></tr>
)}
</React.Fragment>
))}
{!loading && items.length === 0 && (
<tr>
<td className="px-3 py-6 text-center text-zinc-500" colSpan={5}>
<td className="px-3 py-6 text-center text-zinc-500" colSpan={7}>
{tx("暂无用户数据", "No users found")}
</td>
</tr>
@@ -165,6 +327,9 @@ export default function AdminUsersPage() {
<p className="text-xs text-zinc-500">
{tx("创建时间:", "Created: ")}
{fmtTs(user.created_at)}
{" | "}
{tx("提交:", "Subs: ")}{user.total_submissions}
{" | AC: "}{user.total_ac}
</p>
<div className="flex items-center gap-2">
<span className="text-xs text-zinc-600">Rating</span>
@@ -186,7 +351,14 @@ export default function AdminUsersPage() {
>
{tx("保存", "Save")}
</button>
<button
className="rounded border px-2 py-1 text-xs"
onClick={() => setExpandedId(expandedId === user.id ? null : user.id)}
>
{expandedId === user.id ? tx("收起", "Hide") : tx("详情", "Detail")}
</button>
</div>
{expandedId === user.id && <DetailPanel userId={user.id} tx={tx} />}
</div>
))}
{!loading && items.length === 0 && (