feat: 更新管理员导航,添加结算和奖池模块,并优化活动匹配逻辑

This commit is contained in:
2026-05-11 15:35:41 +08:00
parent 0103d25426
commit f083b28fc6
19 changed files with 1226 additions and 5 deletions

View File

@@ -0,0 +1,4 @@
export const settlementModuleMeta = {
title: "结算批次",
description: "按期查看结算批次与注单结算明细",
} as const;

View File

@@ -0,0 +1,190 @@
"use client";
import Link from "next/link";
import { useCallback, useEffect, useState } from "react";
import { getAdminSettlementBatch, getAdminSettlementBatchDetails } from "@/api/admin-settlement";
import { AdminListPaginationFooter } from "@/components/admin/admin-list-pagination-footer";
import { ModuleScaffold } from "@/components/admin/module-scaffold";
import { Button, buttonVariants } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { useAdminDateTimeFormatter } from "@/hooks/use-admin-datetime-formatter";
import { formatAdminMinorUnits } from "@/lib/money";
import { cn } from "@/lib/utils";
import { LotteryApiBizError } from "@/types/api/errors";
import type {
AdminSettlementBatchDetailsData,
AdminSettlementBatchShowData,
} from "@/types/api/admin-settlement";
type Props = {
batchId: number;
};
export function SettlementBatchDetailsConsole({ batchId }: Props) {
const formatDt = useAdminDateTimeFormatter();
const [summary, setSummary] = useState<AdminSettlementBatchShowData | null>(null);
const [details, setDetails] = useState<AdminSettlementBatchDetailsData | null>(null);
const [loading, setLoading] = useState(true);
const [err, setErr] = useState<string | null>(null);
const [page, setPage] = useState(1);
const [perPage, setPerPage] = useState(25);
const load = useCallback(async () => {
setLoading(true);
setErr(null);
try {
const [s, d] = await Promise.all([
getAdminSettlementBatch(batchId),
getAdminSettlementBatchDetails(batchId, { page, per_page: perPage }),
]);
setSummary(s);
setDetails(d);
} catch (e) {
setErr(e instanceof LotteryApiBizError ? e.message : "加载失败");
setSummary(null);
setDetails(null);
} finally {
setLoading(false);
}
}, [batchId, page, perPage]);
useEffect(() => {
const t = window.setTimeout(() => void load(), 0);
return () => window.clearTimeout(t);
}, [load]);
return (
<ModuleScaffold>
<div className="mb-4">
<Link href="/admin/settlement-batches" className={cn(buttonVariants({ variant: "ghost", size: "sm" }), "px-0")}>
</Link>
</div>
{err ? (
<Card className="border-destructive/40">
<CardHeader>
<CardTitle className="text-base"></CardTitle>
<CardDescription>{err}</CardDescription>
</CardHeader>
<CardContent>
<Button type="button" size="sm" variant="secondary" onClick={() => void load()}>
</Button>
</CardContent>
</Card>
) : null}
{summary ? (
<Card className="mb-6">
<CardHeader>
<CardTitle className="font-mono text-base"> #{summary.id}</CardTitle>
<CardDescription>
{summary.draw_no ?? "—"} · {summary.draw_status ?? "—"} · v
{summary.result_batch_version ?? "—"}
</CardDescription>
</CardHeader>
<CardContent className="grid gap-2 text-sm sm:grid-cols-2">
<p>
<span className="text-muted-foreground"></span>{" "}
<span className="font-mono">{summary.status}</span>
</p>
<p>
<span className="text-muted-foreground"></span>{" "}
<span className="tabular-nums">{summary.total_ticket_count}</span>
</p>
<p>
<span className="text-muted-foreground"></span>{" "}
<span className="tabular-nums">{summary.total_win_count}</span>
</p>
<p>
<span className="text-muted-foreground"></span>{" "}
<span className="font-mono tabular-nums">{formatAdminMinorUnits(summary.total_payout_amount)}</span>
</p>
<p>
<span className="text-muted-foreground">Jackpot </span>{" "}
<span className="font-mono tabular-nums">
{formatAdminMinorUnits(summary.total_jackpot_payout_amount)}
</span>
</p>
<p>
<span className="text-muted-foreground"></span> {formatDt(summary.started_at)}
</p>
<p>
<span className="text-muted-foreground"></span> {formatDt(summary.finished_at)}
</p>
</CardContent>
</Card>
) : loading ? (
<p className="text-muted-foreground text-sm"></p>
) : null}
<Card>
<CardHeader>
<CardTitle className="text-base"></CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent>
{details ? (
<>
<Table>
<TableHeader>
<TableRow>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right">Jackpot</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{details.items.map((r) => (
<TableRow key={r.id}>
<TableCell className="font-mono text-xs">{r.ticket_no ?? "—"}</TableCell>
<TableCell className="font-mono text-xs">{r.play_code ?? "—"}</TableCell>
<TableCell className="max-w-[10rem] truncate text-xs">
{r.player_username ?? r.site_player_id ?? r.player_id ?? "—"}
</TableCell>
<TableCell className="text-xs">{r.matched_prize_tier ?? "—"}</TableCell>
<TableCell className="text-right font-mono text-xs tabular-nums">
{formatAdminMinorUnits(r.win_amount)}
</TableCell>
<TableCell className="text-right font-mono text-xs tabular-nums">
{formatAdminMinorUnits(r.jackpot_allocation_amount)}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
<AdminListPaginationFooter
selectId="settlement-details-per-page"
total={details.meta.total}
page={details.meta.current_page}
lastPage={details.meta.last_page}
perPage={details.meta.per_page}
loading={loading}
onPerPageChange={(n) => {
setPerPage(n);
setPage(1);
}}
onPageChange={setPage}
/>
</>
) : (
<p className="text-muted-foreground text-sm">{loading ? "加载明细…" : "无数据"}</p>
)}
</CardContent>
</Card>
</ModuleScaffold>
);
}

View File

@@ -0,0 +1,218 @@
"use client";
import Link from "next/link";
import { useCallback, useEffect, useState } from "react";
import { getAdminSettlementBatches } from "@/api/admin-settlement";
import { AdminListPaginationFooter } from "@/components/admin/admin-list-pagination-footer";
import { ModuleScaffold } from "@/components/admin/module-scaffold";
import { Button, buttonVariants } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { useAdminDateTimeFormatter } from "@/hooks/use-admin-datetime-formatter";
import { formatAdminMinorUnits } from "@/lib/money";
import { cn } from "@/lib/utils";
import { LotteryApiBizError } from "@/types/api/errors";
import type { AdminSettlementBatchListData, AdminSettlementBatchRow } from "@/types/api/admin-settlement";
import { settlementModuleMeta } from "@/modules/settlement/meta";
const STATUS_ALL = "__all__";
const STATUS_OPTIONS: { value: string; label: string }[] = [
{ value: STATUS_ALL, label: "不限" },
{ value: "running", label: "进行中" },
{ value: "completed", label: "已完成" },
{ value: "failed", label: "失败" },
];
export function SettlementBatchesConsole() {
const formatDt = useAdminDateTimeFormatter();
const [data, setData] = useState<AdminSettlementBatchListData | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [draftDrawNo, setDraftDrawNo] = useState("");
const [appliedDrawNo, setAppliedDrawNo] = useState("");
const [draftStatus, setDraftStatus] = useState(STATUS_ALL);
const [appliedStatus, setAppliedStatus] = useState(STATUS_ALL);
const [page, setPage] = useState(1);
const [perPage, setPerPage] = useState(20);
const load = useCallback(async () => {
setLoading(true);
setError(null);
try {
const d = await getAdminSettlementBatches({
page,
per_page: perPage,
draw_no: appliedDrawNo.trim() || undefined,
status:
appliedStatus === STATUS_ALL || appliedStatus.trim() === ""
? undefined
: appliedStatus.trim(),
});
setData(d);
} catch (e) {
setError(e instanceof LotteryApiBizError ? e.message : "加载失败");
setData(null);
} finally {
setLoading(false);
}
}, [page, perPage, appliedDrawNo, appliedStatus]);
useEffect(() => {
const t = window.setTimeout(() => void load(), 0);
return () => window.clearTimeout(t);
}, [load]);
const applyFilters = () => {
setAppliedDrawNo(draftDrawNo);
setAppliedStatus(draftStatus);
setPage(1);
};
return (
<ModuleScaffold>
<div className="mb-6">
<h1 className="text-lg font-semibold tracking-tight">{settlementModuleMeta.title}</h1>
<p className="text-muted-foreground mt-1 text-sm">{settlementModuleMeta.description}</p>
</div>
<Card className="mb-6">
<CardHeader className="pb-3">
<CardTitle className="text-base"></CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-4 sm:flex-row sm:flex-wrap sm:items-end">
<div className="flex min-w-[12rem] flex-1 flex-col gap-1.5">
<Label htmlFor="sb-draw-no"></Label>
<Input
id="sb-draw-no"
value={draftDrawNo}
onChange={(e) => setDraftDrawNo(e.target.value)}
placeholder="如 20260511-001"
className="font-mono"
/>
</div>
<div className="flex min-w-[10rem] flex-col gap-1.5">
<Label></Label>
<Select value={draftStatus} onValueChange={(v) => setDraftStatus(v ?? STATUS_ALL)}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
{STATUS_OPTIONS.map((o) => (
<SelectItem key={o.value} value={o.value}>
{o.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<Button type="button" onClick={applyFilters}>
</Button>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-base"></CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent>
{error ? <p className="text-destructive text-sm">{error}</p> : null}
{loading && !data ? (
<p className="text-muted-foreground text-sm"></p>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead>ID</TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right">Jackpot</TableHead>
<TableHead></TableHead>
<TableHead />
</TableRow>
</TableHeader>
<TableBody>
{(data?.items ?? []).map((row: AdminSettlementBatchRow) => (
<TableRow key={row.id}>
<TableCell className="font-mono text-xs">{row.id}</TableCell>
<TableCell className="font-mono text-sm">{row.draw_no ?? "—"}</TableCell>
<TableCell className="font-mono text-xs">v{row.settle_version}</TableCell>
<TableCell>
<span
className={cn(
"rounded px-1.5 py-0.5 text-xs font-medium",
row.status === "completed" && "bg-emerald-500/15 text-emerald-800",
row.status === "running" && "bg-amber-500/15 text-amber-900",
row.status === "failed" && "bg-destructive/15 text-destructive",
)}
>
{row.status}
</span>
</TableCell>
<TableCell className="text-right tabular-nums">{row.total_ticket_count}</TableCell>
<TableCell className="text-right tabular-nums">{row.total_win_count}</TableCell>
<TableCell className="text-right font-mono text-xs tabular-nums">
{formatAdminMinorUnits(row.total_payout_amount)}
</TableCell>
<TableCell className="text-right font-mono text-xs tabular-nums">
{formatAdminMinorUnits(row.total_jackpot_payout_amount)}
</TableCell>
<TableCell className="text-xs text-muted-foreground whitespace-nowrap">
{formatDt(row.finished_at ?? row.started_at)}
</TableCell>
<TableCell>
<Link
href={`/admin/settlement-batches/${row.id}/details`}
className={cn(buttonVariants({ variant: "link", size: "sm" }), "px-0")}
>
</Link>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
{data ? (
<AdminListPaginationFooter
selectId="settlement-batches-per-page"
total={data.meta.total}
page={data.meta.current_page}
lastPage={data.meta.last_page}
perPage={data.meta.per_page}
loading={loading}
onPerPageChange={(n) => {
setPerPage(n);
setPage(1);
}}
onPageChange={setPage}
/>
) : null}
</CardContent>
</Card>
</ModuleScaffold>
);
}