feat: 数据概览/操作日志/批量操作 + 首页合格档案数
后台新增「数据概览」(商品/合格/按状态/品牌/分类/待审核) 与「操作日志」(全局审计分页);商品列表支持多选批量改状态/分类。公开首页标题改为「天工」并展示合格档案数;新增公开接口 /api/v1/stats 与后台 /api/stats、/api/audit、/api/products/bulk。合格口径=quality_score≥0.6 且在用。 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -7,9 +7,18 @@ import SubmissionsPage from "./components/SubmissionsPage";
|
||||
import ApiKeysPage from "./components/ApiKeysPage";
|
||||
import CategoriesPage from "./components/CategoriesPage";
|
||||
import BrandsPage from "./components/BrandsPage";
|
||||
import { FolderTree, Inbox, KeyRound, LogOut, Package, Tag } from "lucide-react";
|
||||
import StatsPage from "./components/StatsPage";
|
||||
import AuditLogPage from "./components/AuditLogPage";
|
||||
import { BarChart3, FolderTree, Inbox, KeyRound, LogOut, Package, ScrollText, Tag } from "lucide-react";
|
||||
|
||||
type Tab = "products" | "submissions" | "categories" | "brands" | "keys";
|
||||
type Tab =
|
||||
| "overview"
|
||||
| "products"
|
||||
| "submissions"
|
||||
| "categories"
|
||||
| "brands"
|
||||
| "audit"
|
||||
| "keys";
|
||||
type View = { name: "list" } | { name: "detail"; id: string };
|
||||
|
||||
export default function App() {
|
||||
@@ -74,6 +83,16 @@ export default function App() {
|
||||
天工商品档案公共仓 · 后台
|
||||
</div>
|
||||
<nav className="flex items-center gap-1 text-sm">
|
||||
<button
|
||||
onClick={() => setTab("overview")}
|
||||
className={`px-3 py-1.5 rounded-md flex items-center gap-1.5 ${
|
||||
tab === "overview"
|
||||
? "bg-emerald-50 text-emerald-700"
|
||||
: "text-gray-600 hover:bg-gray-100"
|
||||
}`}
|
||||
>
|
||||
<BarChart3 className="h-4 w-4" /> 数据概览
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
setTab("products");
|
||||
@@ -125,6 +144,16 @@ export default function App() {
|
||||
>
|
||||
<Tag className="h-4 w-4" /> 品牌管理
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTab("audit")}
|
||||
className={`px-3 py-1.5 rounded-md flex items-center gap-1.5 ${
|
||||
tab === "audit"
|
||||
? "bg-emerald-50 text-emerald-700"
|
||||
: "text-gray-600 hover:bg-gray-100"
|
||||
}`}
|
||||
>
|
||||
<ScrollText className="h-4 w-4" /> 操作日志
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTab("keys")}
|
||||
className={`px-3 py-1.5 rounded-md flex items-center gap-1.5 ${
|
||||
@@ -148,7 +177,11 @@ export default function App() {
|
||||
</div>
|
||||
</header>
|
||||
<main className="flex-1 overflow-auto p-6">
|
||||
{tab === "keys" ? (
|
||||
{tab === "overview" ? (
|
||||
<StatsPage />
|
||||
) : tab === "audit" ? (
|
||||
<AuditLogPage />
|
||||
) : tab === "keys" ? (
|
||||
<ApiKeysPage />
|
||||
) : tab === "categories" ? (
|
||||
<CategoriesPage />
|
||||
|
||||
@@ -171,4 +171,22 @@ export const api = {
|
||||
),
|
||||
revokeApiKey: (id: string) =>
|
||||
request<{ status: string }>(`/keys/${id}`, { method: "DELETE" }),
|
||||
stats: () => request<import("./types").AdminStats>("/stats"),
|
||||
auditLog: (page: number, size: number) =>
|
||||
request<{
|
||||
items: import("./types").AuditLogRow[];
|
||||
page: number;
|
||||
size: number;
|
||||
total: number;
|
||||
}>(`/audit?page=${page}&size=${size}`),
|
||||
bulkProducts: (body: {
|
||||
ids: string[];
|
||||
action: "status" | "category";
|
||||
status?: string;
|
||||
category_id?: string | null;
|
||||
}) =>
|
||||
request<{ status: string; affected: number }>("/products/bulk", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
};
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { api, ApiError } from "../api";
|
||||
import type { AuditLogRow } from "../types";
|
||||
import { ScrollText } from "lucide-react";
|
||||
|
||||
const ACTION_LABEL: Record<string, string> = {
|
||||
create: "新建",
|
||||
update: "修改",
|
||||
delete: "删除",
|
||||
add_image: "添加图片",
|
||||
delete_image: "删除图片",
|
||||
bulk_status: "批量改状态",
|
||||
bulk_category: "批量改分类",
|
||||
};
|
||||
|
||||
const ENTITY_LABEL: Record<string, string> = {
|
||||
product: "商品",
|
||||
category: "分类",
|
||||
brand: "品牌",
|
||||
};
|
||||
|
||||
export default function AuditLogPage() {
|
||||
const [rows, setRows] = useState<AuditLogRow[]>([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [page, setPage] = useState(1);
|
||||
const size = 30;
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
setError("");
|
||||
api
|
||||
.auditLog(page, size)
|
||||
.then((r) => {
|
||||
setRows(r.items);
|
||||
setTotal(r.total);
|
||||
})
|
||||
.catch((e) => setError(e instanceof ApiError ? e.message : "加载失败"));
|
||||
}, [page]);
|
||||
|
||||
const pages = Math.max(1, Math.ceil(total / size));
|
||||
|
||||
return (
|
||||
<div className="max-w-5xl">
|
||||
<h2 className="mb-4 flex items-center gap-2 text-lg font-semibold text-gray-800">
|
||||
<ScrollText className="h-5 w-5 text-emerald-600" /> 操作日志
|
||||
<span className="text-sm font-normal text-gray-400">共 {total} 条</span>
|
||||
</h2>
|
||||
|
||||
{error && (
|
||||
<div className="mb-3 rounded bg-red-50 px-4 py-2 text-sm text-red-700">{error}</div>
|
||||
)}
|
||||
|
||||
<div className="overflow-hidden rounded-lg border bg-white">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 text-left text-xs uppercase text-gray-500">
|
||||
<tr>
|
||||
<th className="px-4 py-2 font-medium">时间</th>
|
||||
<th className="px-4 py-2 font-medium">操作人</th>
|
||||
<th className="px-4 py-2 font-medium">动作</th>
|
||||
<th className="px-4 py-2 font-medium">对象</th>
|
||||
<th className="px-4 py-2 font-medium">字段</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y">
|
||||
{rows.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={5} className="px-4 py-8 text-center text-gray-400">
|
||||
暂无记录
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
rows.map((e) => (
|
||||
<tr key={e.id} className="hover:bg-gray-50">
|
||||
<td className="whitespace-nowrap px-4 py-2 text-gray-500">
|
||||
{new Date(e.created_at).toLocaleString()}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-gray-700">{e.actor}</td>
|
||||
<td className="px-4 py-2 text-gray-700">
|
||||
{ACTION_LABEL[e.action] || e.action}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-gray-600">
|
||||
{ENTITY_LABEL[e.entity] || e.entity}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-xs text-gray-400">
|
||||
{e.fields.length ? e.fields.join(", ") : "—"}
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 flex items-center justify-end gap-2 text-sm text-gray-600">
|
||||
<button
|
||||
disabled={page <= 1}
|
||||
onClick={() => setPage((p) => p - 1)}
|
||||
className="rounded border border-gray-300 px-3 py-1 disabled:opacity-50"
|
||||
>
|
||||
上一页
|
||||
</button>
|
||||
<span>
|
||||
{page} / {pages}
|
||||
</span>
|
||||
<button
|
||||
disabled={page >= pages}
|
||||
onClick={() => setPage((p) => p + 1)}
|
||||
className="rounded border border-gray-300 px-3 py-1 disabled:opacity-50"
|
||||
>
|
||||
下一页
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -38,8 +38,13 @@ export default function ProductList({
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set());
|
||||
const [categories, setCategories] = useState<Category[]>([]);
|
||||
const [bulkStatus, setBulkStatus] = useState("");
|
||||
const [bulkCategory, setBulkCategory] = useState("");
|
||||
const [bulkBusy, setBulkBusy] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
function reload() {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
api
|
||||
@@ -50,8 +55,72 @@ export default function ProductList({
|
||||
})
|
||||
.catch((e) => setError(e.message))
|
||||
.finally(() => setLoading(false));
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setSelected(new Set());
|
||||
reload();
|
||||
}, [q, page, size]);
|
||||
|
||||
useEffect(() => {
|
||||
api.listCategories().then((r) => setCategories(r.items)).catch(() => {});
|
||||
}, []);
|
||||
|
||||
function toggle(id: string) {
|
||||
setSelected((prev) => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(id)) next.delete(id);
|
||||
else next.add(id);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function toggleAll() {
|
||||
setSelected((prev) =>
|
||||
prev.size === rows.length ? new Set() : new Set(rows.map((r) => r.id)),
|
||||
);
|
||||
}
|
||||
|
||||
async function applyBulkStatus() {
|
||||
if (!bulkStatus || selected.size === 0) return;
|
||||
setBulkBusy(true);
|
||||
setError("");
|
||||
try {
|
||||
await api.bulkProducts({
|
||||
ids: [...selected],
|
||||
action: "status",
|
||||
status: bulkStatus,
|
||||
});
|
||||
setSelected(new Set());
|
||||
setBulkStatus("");
|
||||
reload();
|
||||
} catch (e) {
|
||||
setError(e instanceof ApiError ? e.message : "批量操作失败");
|
||||
} finally {
|
||||
setBulkBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function applyBulkCategory() {
|
||||
if (selected.size === 0) return;
|
||||
setBulkBusy(true);
|
||||
setError("");
|
||||
try {
|
||||
await api.bulkProducts({
|
||||
ids: [...selected],
|
||||
action: "category",
|
||||
category_id: bulkCategory || null,
|
||||
});
|
||||
setSelected(new Set());
|
||||
setBulkCategory("");
|
||||
reload();
|
||||
} catch (e) {
|
||||
setError(e instanceof ApiError ? e.message : "批量操作失败");
|
||||
} finally {
|
||||
setBulkBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
const pages = Math.max(1, Math.ceil(total / size));
|
||||
|
||||
return (
|
||||
@@ -106,10 +175,75 @@ export default function ProductList({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{selected.size > 0 && (
|
||||
<div className="mb-3 flex flex-wrap items-center gap-3 rounded-lg border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm">
|
||||
<span className="font-medium text-emerald-800">
|
||||
已选 {selected.size} 项
|
||||
</span>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="text-gray-500">改状态</span>
|
||||
<select
|
||||
value={bulkStatus}
|
||||
onChange={(e) => setBulkStatus(e.target.value)}
|
||||
className="rounded border border-gray-300 bg-white px-2 py-1"
|
||||
>
|
||||
<option value="">选择</option>
|
||||
<option value="active">在用</option>
|
||||
<option value="deprecated">已停用</option>
|
||||
<option value="merged">已合并</option>
|
||||
</select>
|
||||
<button
|
||||
onClick={applyBulkStatus}
|
||||
disabled={bulkBusy || !bulkStatus}
|
||||
className="rounded bg-emerald-600 px-3 py-1 text-white hover:bg-emerald-700 disabled:opacity-50"
|
||||
>
|
||||
应用
|
||||
</button>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="text-gray-500">改分类</span>
|
||||
<select
|
||||
value={bulkCategory}
|
||||
onChange={(e) => setBulkCategory(e.target.value)}
|
||||
className="rounded border border-gray-300 bg-white px-2 py-1"
|
||||
>
|
||||
<option value="">(未分类)</option>
|
||||
{categories.map((c) => (
|
||||
<option key={c.id} value={c.id}>
|
||||
{"\u00A0".repeat(c.level * 2)}
|
||||
{c.name_zh}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
onClick={applyBulkCategory}
|
||||
disabled={bulkBusy}
|
||||
className="rounded bg-emerald-600 px-3 py-1 text-white hover:bg-emerald-700 disabled:opacity-50"
|
||||
>
|
||||
应用
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setSelected(new Set())}
|
||||
className="text-gray-500 hover:text-gray-700"
|
||||
>
|
||||
取消选择
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="overflow-hidden rounded-lg border border-gray-200 bg-white">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 text-left text-xs uppercase text-gray-500">
|
||||
<tr>
|
||||
<th className="w-10 px-4 py-3">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={rows.length > 0 && selected.size === rows.length}
|
||||
onChange={toggleAll}
|
||||
aria-label="全选"
|
||||
/>
|
||||
</th>
|
||||
<th className="px-4 py-3">名称</th>
|
||||
<th className="px-4 py-3">品牌</th>
|
||||
<th className="px-4 py-3">条码</th>
|
||||
@@ -122,13 +256,13 @@ export default function ProductList({
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{loading ? (
|
||||
<tr>
|
||||
<td colSpan={7} className="px-4 py-8 text-center text-gray-400">
|
||||
<td colSpan={8} className="px-4 py-8 text-center text-gray-400">
|
||||
加载中…
|
||||
</td>
|
||||
</tr>
|
||||
) : rows.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={7} className="px-4 py-8 text-center text-gray-400">
|
||||
<td colSpan={8} className="px-4 py-8 text-center text-gray-400">
|
||||
暂无数据
|
||||
</td>
|
||||
</tr>
|
||||
@@ -137,8 +271,21 @@ export default function ProductList({
|
||||
<tr
|
||||
key={r.id}
|
||||
onClick={() => onOpen(r.id)}
|
||||
className="cursor-pointer hover:bg-emerald-50/50"
|
||||
className={`cursor-pointer hover:bg-emerald-50/50 ${
|
||||
selected.has(r.id) ? "bg-emerald-50/60" : ""
|
||||
}`}
|
||||
>
|
||||
<td
|
||||
className="px-4 py-3"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.has(r.id)}
|
||||
onChange={() => toggle(r.id)}
|
||||
aria-label="选择"
|
||||
/>
|
||||
</td>
|
||||
<td className="px-4 py-3 font-medium text-gray-800">{r.name}</td>
|
||||
<td className="px-4 py-3 text-gray-600">{r.brand || "—"}</td>
|
||||
<td className="px-4 py-3 font-mono text-xs text-gray-500">
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { api, ApiError } from "../api";
|
||||
import type { AdminStats } from "../types";
|
||||
import { BarChart3, Package, CheckCircle2, Tag, FolderTree, Inbox } from "lucide-react";
|
||||
|
||||
const STATUS_LABEL: Record<string, string> = {
|
||||
active: "在用",
|
||||
merged: "已合并",
|
||||
deprecated: "已停用",
|
||||
};
|
||||
|
||||
function Card({
|
||||
icon,
|
||||
label,
|
||||
value,
|
||||
hint,
|
||||
}: {
|
||||
icon: React.ReactNode;
|
||||
label: string;
|
||||
value: string | number;
|
||||
hint?: string;
|
||||
}) {
|
||||
return (
|
||||
<div className="rounded-lg border bg-white p-5">
|
||||
<div className="flex items-center gap-2 text-sm text-gray-500">
|
||||
{icon}
|
||||
{label}
|
||||
</div>
|
||||
<div className="mt-2 text-2xl font-semibold text-gray-800">{value}</div>
|
||||
{hint && <div className="mt-1 text-xs text-gray-400">{hint}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function StatsPage() {
|
||||
const [stats, setStats] = useState<AdminStats | null>(null);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
api
|
||||
.stats()
|
||||
.then(setStats)
|
||||
.catch((e) => setError(e instanceof ApiError ? e.message : "加载失败"));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="max-w-5xl">
|
||||
<h2 className="mb-4 flex items-center gap-2 text-lg font-semibold text-gray-800">
|
||||
<BarChart3 className="h-5 w-5 text-emerald-600" /> 数据概览
|
||||
</h2>
|
||||
|
||||
{error && (
|
||||
<div className="mb-3 rounded bg-red-50 px-4 py-2 text-sm text-red-700">{error}</div>
|
||||
)}
|
||||
|
||||
{!stats ? (
|
||||
<div className="text-sm text-gray-400">加载中…</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="grid grid-cols-2 gap-4 md:grid-cols-3">
|
||||
<Card
|
||||
icon={<Package className="h-4 w-4" />}
|
||||
label="商品总数"
|
||||
value={stats.products.toLocaleString()}
|
||||
/>
|
||||
<Card
|
||||
icon={<CheckCircle2 className="h-4 w-4 text-emerald-600" />}
|
||||
label="合格档案"
|
||||
value={stats.qualified.toLocaleString()}
|
||||
hint={`质量分 ≥ ${stats.min_score} 且在用`}
|
||||
/>
|
||||
<Card
|
||||
icon={<BarChart3 className="h-4 w-4" />}
|
||||
label="平均质量分"
|
||||
value={Math.round(stats.avg_quality * 100)}
|
||||
hint="满分 100"
|
||||
/>
|
||||
<Card
|
||||
icon={<Tag className="h-4 w-4" />}
|
||||
label="品牌数"
|
||||
value={stats.brands.toLocaleString()}
|
||||
/>
|
||||
<Card
|
||||
icon={<FolderTree className="h-4 w-4" />}
|
||||
label="分类数"
|
||||
value={stats.categories.toLocaleString()}
|
||||
/>
|
||||
<Card
|
||||
icon={<Inbox className="h-4 w-4" />}
|
||||
label="待审核投稿"
|
||||
value={stats.pending_submissions.toLocaleString()}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 rounded-lg border bg-white p-5">
|
||||
<h3 className="mb-3 text-sm font-medium text-gray-700">按状态分布</h3>
|
||||
<div className="space-y-2">
|
||||
{Object.keys(stats.by_status).length === 0 ? (
|
||||
<div className="text-sm text-gray-400">暂无数据</div>
|
||||
) : (
|
||||
Object.entries(stats.by_status).map(([st, n]) => {
|
||||
const pct = stats.products > 0 ? (n / stats.products) * 100 : 0;
|
||||
return (
|
||||
<div key={st} className="flex items-center gap-3 text-sm">
|
||||
<span className="w-16 text-gray-600">
|
||||
{STATUS_LABEL[st] || st}
|
||||
</span>
|
||||
<div className="h-3 flex-1 overflow-hidden rounded bg-gray-100">
|
||||
<div
|
||||
className="h-full bg-emerald-500"
|
||||
style={{ width: `${pct}%` }}
|
||||
/>
|
||||
</div>
|
||||
<span className="w-12 text-right text-gray-500">{n}</span>
|
||||
</div>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -96,6 +96,27 @@ export interface AuditEntry {
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface AuditLogRow {
|
||||
id: string;
|
||||
actor: string;
|
||||
action: string;
|
||||
entity: string;
|
||||
entity_id: string | null;
|
||||
fields: string[];
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface AdminStats {
|
||||
products: number;
|
||||
qualified: number;
|
||||
min_score: number;
|
||||
by_status: Record<string, number>;
|
||||
brands: number;
|
||||
categories: number;
|
||||
pending_submissions: number;
|
||||
avg_quality: number;
|
||||
}
|
||||
|
||||
export interface SubmissionRow {
|
||||
id: string;
|
||||
gtin: string | null;
|
||||
|
||||
@@ -64,8 +64,11 @@ func (h *Handler) Router() http.Handler {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(h.authn.Middleware)
|
||||
r.Get("/api/me", h.Me)
|
||||
r.Get("/api/stats", h.Stats)
|
||||
r.Get("/api/audit", h.ListAllAudit)
|
||||
r.Get("/api/products", h.ListProducts)
|
||||
r.Post("/api/products", h.CreateProduct)
|
||||
r.Post("/api/products/bulk", h.BulkProducts)
|
||||
r.Get("/api/products/{id}", h.GetProduct)
|
||||
r.Put("/api/products/{id}", h.UpdateProduct)
|
||||
r.Get("/api/products/{id}/audit", h.ListAudit)
|
||||
@@ -203,6 +206,76 @@ func (h *Handler) CreateProduct(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusCreated, d)
|
||||
}
|
||||
|
||||
// Stats returns the dashboard overview counters.
|
||||
func (h *Handler) Stats(w http.ResponseWriter, r *http.Request) {
|
||||
st, err := h.store.Stats(r.Context())
|
||||
if h.handleErr(w, err) {
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, st)
|
||||
}
|
||||
|
||||
// ListAllAudit returns a page of the global operations audit log.
|
||||
func (h *Handler) ListAllAudit(w http.ResponseWriter, r *http.Request) {
|
||||
page, size := pageParams(r)
|
||||
items, total, err := h.store.ListAllAudit(r.Context(), size, (page-1)*size)
|
||||
if h.handleErr(w, err) {
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"items": items, "page": page, "size": size, "total": total,
|
||||
})
|
||||
}
|
||||
|
||||
type bulkInput struct {
|
||||
IDs []string `json:"ids"`
|
||||
Action string `json:"action"`
|
||||
Status string `json:"status"`
|
||||
CategoryID *string `json:"category_id"`
|
||||
}
|
||||
|
||||
// BulkProducts applies a status or category change to many products at once.
|
||||
func (h *Handler) BulkProducts(w http.ResponseWriter, r *http.Request) {
|
||||
var in bulkInput
|
||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "invalid body")
|
||||
return
|
||||
}
|
||||
if len(in.IDs) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "未选择任何商品")
|
||||
return
|
||||
}
|
||||
actor := auth.UserFrom(r.Context())
|
||||
var (
|
||||
affected int
|
||||
err error
|
||||
)
|
||||
switch in.Action {
|
||||
case "status":
|
||||
affected, err = h.store.BulkSetStatus(r.Context(), actor, in.IDs, in.Status)
|
||||
case "category":
|
||||
affected, err = h.store.BulkSetCategory(r.Context(), actor, in.IDs, in.CategoryID)
|
||||
default:
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "未知的批量操作")
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, adminstore.ErrInvalidStatus):
|
||||
writeError(w, http.StatusBadRequest, "invalid_status", "无效的状态值")
|
||||
return
|
||||
case errors.Is(err, adminstore.ErrInvalidParent):
|
||||
writeError(w, http.StatusBadRequest, "invalid_parent", "目标分类无效")
|
||||
return
|
||||
case errors.Is(err, adminstore.ErrNoTargets):
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "未选择任何商品")
|
||||
return
|
||||
}
|
||||
if h.handleErr(w, err) {
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"status": "ok", "affected": affected})
|
||||
}
|
||||
|
||||
// UpdateProduct applies an edit.
|
||||
func (h *Handler) UpdateProduct(w http.ResponseWriter, r *http.Request) {
|
||||
var in adminstore.ProductInput
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package adminstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Bulk-operation errors.
|
||||
var (
|
||||
// ErrNoTargets is returned when a bulk request selects no products.
|
||||
ErrNoTargets = errors.New("no products selected")
|
||||
// ErrInvalidStatus is returned for an unknown product status value.
|
||||
ErrInvalidStatus = errors.New("invalid status")
|
||||
)
|
||||
|
||||
var validStatus = map[string]bool{"active": true, "merged": true, "deprecated": true}
|
||||
|
||||
// BulkSetStatus updates the status of every selected product in one statement.
|
||||
func (s *Store) BulkSetStatus(ctx context.Context, actor string, ids []string, status string) (int, error) {
|
||||
if len(ids) == 0 {
|
||||
return 0, ErrNoTargets
|
||||
}
|
||||
if !validStatus[status] {
|
||||
return 0, ErrInvalidStatus
|
||||
}
|
||||
ct, err := s.pool.Exec(ctx,
|
||||
"UPDATE product SET status = $1 WHERE id = ANY($2)", status, ids)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n := int(ct.RowsAffected())
|
||||
_ = s.writeAudit(ctx, actor, "bulk_status", "product", nil,
|
||||
[]string{"status"}, map[string]any{"ids": ids}, map[string]any{"status": status})
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// BulkSetCategory reassigns the category of every selected product, syncing the
|
||||
// GPC brick code and recomputing quality for each one.
|
||||
func (s *Store) BulkSetCategory(ctx context.Context, actor string, ids []string, categoryID *string) (int, error) {
|
||||
if len(ids) == 0 {
|
||||
return 0, ErrNoTargets
|
||||
}
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
var gpc *string
|
||||
if categoryID != nil && *categoryID != "" {
|
||||
if err := tx.QueryRow(ctx, "SELECT gpc_brick_code FROM category WHERE id = $1", *categoryID).Scan(&gpc); err != nil {
|
||||
return 0, ErrInvalidParent
|
||||
}
|
||||
} else {
|
||||
categoryID = nil
|
||||
}
|
||||
|
||||
ct, err := tx.Exec(ctx,
|
||||
"UPDATE product SET category_id = $1, gpc_brick_code = $2 WHERE id = ANY($3)",
|
||||
categoryID, gpc, ids)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, id := range ids {
|
||||
if _, err := s.recomputeQualityTx(ctx, tx, id); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n := int(ct.RowsAffected())
|
||||
cat := ""
|
||||
if categoryID != nil {
|
||||
cat = *categoryID
|
||||
}
|
||||
_ = s.writeAudit(ctx, actor, "bulk_category", "product", nil,
|
||||
[]string{"category"}, map[string]any{"ids": ids}, map[string]any{"category_id": cat})
|
||||
return n, nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package adminstore
|
||||
|
||||
import "context"
|
||||
|
||||
// QualifiedMinScore is the quality_score threshold at or above which a product
|
||||
// counts as "qualified" (合格) in admin and public stats.
|
||||
const QualifiedMinScore = 0.6
|
||||
|
||||
// AdminStats summarizes the catalog for the admin overview dashboard.
|
||||
type AdminStats struct {
|
||||
Products int `json:"products"`
|
||||
Qualified int `json:"qualified"`
|
||||
MinScore float64 `json:"min_score"`
|
||||
ByStatus map[string]int `json:"by_status"`
|
||||
Brands int `json:"brands"`
|
||||
Categories int `json:"categories"`
|
||||
Pending int `json:"pending_submissions"`
|
||||
AvgQuality float64 `json:"avg_quality"`
|
||||
}
|
||||
|
||||
// Stats gathers the dashboard counters in a handful of aggregate queries.
|
||||
func (s *Store) Stats(ctx context.Context) (*AdminStats, error) {
|
||||
out := &AdminStats{MinScore: QualifiedMinScore, ByStatus: map[string]int{}}
|
||||
|
||||
if err := s.pool.QueryRow(ctx, `
|
||||
SELECT count(*),
|
||||
count(*) FILTER (WHERE quality_score >= $1 AND status = 'active'),
|
||||
COALESCE(avg(quality_score), 0)
|
||||
FROM product`, QualifiedMinScore).Scan(&out.Products, &out.Qualified, &out.AvgQuality); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := s.pool.Query(ctx, "SELECT status, count(*) FROM product GROUP BY status")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var st string
|
||||
var n int
|
||||
if err := rows.Scan(&st, &n); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.ByStatus[st] = n
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.pool.QueryRow(ctx, "SELECT count(*) FROM brand").Scan(&out.Brands); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.pool.QueryRow(ctx, "SELECT count(*) FROM category").Scan(&out.Categories); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.pool.QueryRow(ctx,
|
||||
"SELECT count(*) FROM submission WHERE status = 'pending'").Scan(&out.Pending); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package adminstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStatsAndBulk(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
ctx := context.Background()
|
||||
|
||||
base, err := s.Stats(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("stats: %v", err)
|
||||
}
|
||||
|
||||
var p1, p2 string
|
||||
if err := s.pool.QueryRow(ctx,
|
||||
"INSERT INTO product (name, status) VALUES ($1,'active') RETURNING id",
|
||||
"批量测试1 "+randomHex(4)).Scan(&p1); err != nil {
|
||||
t.Fatalf("insert p1: %v", err)
|
||||
}
|
||||
if err := s.pool.QueryRow(ctx,
|
||||
"INSERT INTO product (name, status) VALUES ($1,'active') RETURNING id",
|
||||
"批量测试2 "+randomHex(4)).Scan(&p2); err != nil {
|
||||
t.Fatalf("insert p2: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _, _ = s.pool.Exec(ctx, "DELETE FROM product WHERE id = ANY($1)", []string{p1, p2}) })
|
||||
|
||||
after, err := s.Stats(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("stats after: %v", err)
|
||||
}
|
||||
if after.Products != base.Products+2 {
|
||||
t.Fatalf("product count: got %d want %d", after.Products, base.Products+2)
|
||||
}
|
||||
|
||||
// Bulk set status to deprecated.
|
||||
n, err := s.BulkSetStatus(ctx, "tester", []string{p1, p2}, "deprecated")
|
||||
if err != nil || n != 2 {
|
||||
t.Fatalf("bulk status: n=%d err=%v", n, err)
|
||||
}
|
||||
var deprecated int
|
||||
if err := s.pool.QueryRow(ctx,
|
||||
"SELECT count(*) FROM product WHERE id = ANY($1) AND status='deprecated'",
|
||||
[]string{p1, p2}).Scan(&deprecated); err != nil {
|
||||
t.Fatalf("verify: %v", err)
|
||||
}
|
||||
if deprecated != 2 {
|
||||
t.Fatalf("expected 2 deprecated, got %d", deprecated)
|
||||
}
|
||||
|
||||
// Invalid status rejected.
|
||||
if _, err := s.BulkSetStatus(ctx, "tester", []string{p1}, "nope"); err != ErrInvalidStatus {
|
||||
t.Fatalf("expected ErrInvalidStatus, got %v", err)
|
||||
}
|
||||
|
||||
// Empty selection rejected.
|
||||
if _, err := s.BulkSetStatus(ctx, "tester", nil, "active"); err != ErrNoTargets {
|
||||
t.Fatalf("expected ErrNoTargets, got %v", err)
|
||||
}
|
||||
|
||||
// Global audit log should contain the bulk_status entry.
|
||||
rows, total, err := s.ListAllAudit(ctx, 10, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("audit: %v", err)
|
||||
}
|
||||
if total == 0 || len(rows) == 0 {
|
||||
t.Fatalf("expected audit rows, got total=%d", total)
|
||||
}
|
||||
}
|
||||
@@ -481,6 +481,44 @@ type AuditEntry struct {
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
// AuditLogRow is one global audit-log row for the operations log view.
|
||||
type AuditLogRow struct {
|
||||
ID string `json:"id"`
|
||||
Actor string `json:"actor"`
|
||||
Action string `json:"action"`
|
||||
Entity string `json:"entity"`
|
||||
EntityID *string `json:"entity_id"`
|
||||
Fields []string `json:"fields"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
// ListAllAudit returns a page of the global audit log, newest first, along with
|
||||
// the total row count.
|
||||
func (s *Store) ListAllAudit(ctx context.Context, limit, offset int) ([]AuditLogRow, int, error) {
|
||||
var total int
|
||||
if err := s.pool.QueryRow(ctx, "SELECT count(*) FROM audit_log").Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT id, actor, action, entity, entity_id::text, fields, created_at::text
|
||||
FROM audit_log
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $1 OFFSET $2`, limit, offset)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []AuditLogRow{}
|
||||
for rows.Next() {
|
||||
var e AuditLogRow
|
||||
if err := rows.Scan(&e.ID, &e.Actor, &e.Action, &e.Entity, &e.EntityID, &e.Fields, &e.CreatedAt); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out = append(out, e)
|
||||
}
|
||||
return out, total, rows.Err()
|
||||
}
|
||||
|
||||
// ListAudit returns audit history for one product, newest first.
|
||||
func (s *Store) ListAudit(ctx context.Context, productID string, limit int) ([]AuditEntry, error) {
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
|
||||
@@ -26,6 +26,10 @@ var openAPISpec []byte
|
||||
// APIVersion is the current public API version prefix.
|
||||
const APIVersion = "v1"
|
||||
|
||||
// QualifiedMinScore is the quality_score threshold at or above which a product
|
||||
// record is considered "qualified" (合格) for public stats.
|
||||
const QualifiedMinScore = 0.6
|
||||
|
||||
const (
|
||||
defaultPageSize = 20
|
||||
maxPageSize = 100
|
||||
@@ -85,6 +89,7 @@ func (h *Handler) Router() http.Handler {
|
||||
r.Get("/brands", h.ListBrands)
|
||||
r.Get("/categories", h.ListCategories)
|
||||
r.Get("/sources/{id}", h.SourceByID)
|
||||
r.Get("/stats", h.Stats)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -122,6 +127,15 @@ func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
}
|
||||
|
||||
// Stats returns catalog totals and the count of qualified records.
|
||||
func (h *Handler) Stats(w http.ResponseWriter, r *http.Request) {
|
||||
st, err := h.store.Stats(r.Context(), QualifiedMinScore)
|
||||
if h.handleErr(w, r, err) {
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, st)
|
||||
}
|
||||
|
||||
// OpenAPI serves the embedded OpenAPI 3 specification for the public API.
|
||||
func (h *Handler) OpenAPI(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
@@ -29,6 +29,24 @@ func (s *Store) Ping(ctx context.Context) error {
|
||||
return s.pool.Ping(ctx)
|
||||
}
|
||||
|
||||
// PublicStats summarizes the public catalog for the homepage.
|
||||
type PublicStats struct {
|
||||
Total int `json:"total"`
|
||||
Qualified int `json:"qualified"`
|
||||
MinScore float64 `json:"min_score"`
|
||||
}
|
||||
|
||||
// Stats returns active-product totals and the number of qualified records whose
|
||||
// quality_score meets minScore.
|
||||
func (s *Store) Stats(ctx context.Context, minScore float64) (PublicStats, error) {
|
||||
st := PublicStats{MinScore: minScore}
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
SELECT count(*) FILTER (WHERE status = 'active'),
|
||||
count(*) FILTER (WHERE status = 'active' AND quality_score >= $1)
|
||||
FROM product`, minScore).Scan(&st.Total, &st.Qualified)
|
||||
return st, err
|
||||
}
|
||||
|
||||
// Barcode is one GS1 trade item number attached to a product.
|
||||
type Barcode struct {
|
||||
GTIN string `json:"gtin"`
|
||||
|
||||
@@ -30,7 +30,14 @@ export interface SearchFilters {
|
||||
country?: string;
|
||||
}
|
||||
|
||||
export interface Stats {
|
||||
total: number;
|
||||
qualified: number;
|
||||
min_score: number;
|
||||
}
|
||||
|
||||
export const api = {
|
||||
stats: () => req<Stats>(`/api/v1/stats`),
|
||||
search: (q: string, page = 1, size = 20, filters: SearchFilters = {}) => {
|
||||
const params = new URLSearchParams({
|
||||
q,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Search, PlusCircle, Code2 } from "lucide-react";
|
||||
import { api } from "../api";
|
||||
import type { ProductSummary } from "../types";
|
||||
@@ -20,6 +20,14 @@ export default function Home({
|
||||
const [searched, setSearched] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const [qualified, setQualified] = useState<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
api
|
||||
.stats()
|
||||
.then((s) => setQualified(s.qualified))
|
||||
.catch(() => setQualified(null));
|
||||
}, []);
|
||||
|
||||
async function run(e?: React.FormEvent) {
|
||||
e?.preventDefault();
|
||||
@@ -43,10 +51,19 @@ export default function Home({
|
||||
return (
|
||||
<div>
|
||||
<div className="text-center py-10">
|
||||
<h1 className="text-3xl font-bold text-gray-800">天工商品档案公共仓</h1>
|
||||
<h1 className="text-3xl font-bold text-gray-800">天工</h1>
|
||||
<p className="mt-2 text-gray-500">
|
||||
输入商品名称或条码,检索客观、可溯源的商品资料。人人可查,人人可贡献。
|
||||
</p>
|
||||
{qualified != null && (
|
||||
<p className="mt-3 text-sm text-gray-500">
|
||||
目前已收录
|
||||
<span className="mx-1 font-semibold text-emerald-600">
|
||||
{qualified.toLocaleString()}
|
||||
</span>
|
||||
条合格商品档案
|
||||
</p>
|
||||
)}
|
||||
<form onSubmit={run} className="mt-6 max-w-2xl mx-auto flex gap-2">
|
||||
<div className="flex-1 flex items-center gap-2 bg-white border rounded-lg px-3 shadow-sm focus-within:ring-2 focus-within:ring-emerald-400">
|
||||
<Search className="w-5 h-5 text-gray-400" />
|
||||
|
||||
Reference in New Issue
Block a user