import { apiPost } from "@/lib/api"; export type OrderStatus = 1 | 2 | 3 | 4 | 5; export type OrderRecord = { id: string; serial: string; amount: number; status: OrderStatus; addTime: string; payTime: string; details: string; }; export type OrderPage = { current: number; row: number; total: number; }; export type OrderListResult = { list: OrderRecord[]; page: OrderPage; }; function pickList(raw: unknown): unknown[] { if (!raw || typeof raw !== "object") return []; const o = raw as Record; const inner = o.data ?? o.list ?? o.rows ?? o.records; if (Array.isArray(inner)) return inner; if (inner && typeof inner === "object") { const x = inner as Record; if (Array.isArray(x.list)) return x.list; if (Array.isArray(x.records)) return x.records; if (Array.isArray(x.rows)) return x.rows; } return []; } function normalizeDetails(value: unknown): string { if (typeof value === "string" && value.trim()) return value.trim(); if (Array.isArray(value)) { return value .map((v) => (typeof v === "string" ? v.trim() : "")) .filter(Boolean) .join("、"); } if (value && typeof value === "object") { const o = value as Record; const name = o.title ?? o.name ?? o.goodsName ?? o.courseName; if (typeof name === "string" && name.trim()) return name.trim(); } return "-"; } function pickPage(raw: unknown, fallback: { current: number; row: number }): OrderPage { if (!raw || typeof raw !== "object") { return { current: fallback.current, row: fallback.row, total: 0 }; } const o = raw as Record; const fromData = o.data && typeof o.data === "object" && o.data !== null ? (o.data as Record) : null; const page = (fromData?.page && typeof fromData.page === "object" ? fromData.page : null) ?? (o.page && typeof o.page === "object" ? o.page : null); const current = Number( (page as Record | null)?.current ?? (page as Record | null)?.pageNum ?? fallback.current, ); const row = Number( (page as Record | null)?.row ?? (page as Record | null)?.size ?? fallback.row, ); const total = Number((page as Record | null)?.total ?? 0); return { current: Number.isFinite(current) && current > 0 ? current : fallback.current, row: Number.isFinite(row) && row > 0 ? row : fallback.row, total: Number.isFinite(total) && total >= 0 ? total : 0, }; } export function getOrderStatusLabel(status: number): string { const map: Record = { 1: "未支付", 2: "已支付", 3: "支付失败", 4: "已过期", 5: "已取消", }; return map[status] ?? `状态${status}`; } export async function fetchOrderList(page: { current: number; row: number; } = { current: 1, row: 10, }): Promise { const current = Number.isFinite(page.current) ? Math.max(1, page.current) : 1; const row = Number.isFinite(page.row) ? Math.max(1, page.row) : 10; const raw = await apiPost< unknown, { page: { current: number; row: number }; } >("/order/search/list", { page: { current, row, }, }); const list = pickList(raw); const out: OrderRecord[] = []; for (const item of list) { if (!item || typeof item !== "object") continue; const o = item as Record; const id = String(o.id ?? o.orderId ?? o.tradeId ?? o.serial ?? "").trim(); const serial = String(o.serial ?? o.orderNo ?? o.tradeNo ?? o.id ?? "").trim(); if (!serial || !id) continue; const amountRaw = o.amount ?? o.payAmount ?? o.totalAmount ?? 0; const amount = typeof amountRaw === "number" ? amountRaw : Number(amountRaw) || 0; const statusRaw = Number(o.status ?? o.payStatus ?? 1); const status: OrderStatus = statusRaw === 1 || statusRaw === 2 || statusRaw === 3 || statusRaw === 4 || statusRaw === 5 ? statusRaw : 1; const addTime = String(o.addTime ?? o.createTime ?? o.createdAt ?? "").trim(); const payTime = String(o.payTime ?? o.paidAt ?? "").trim(); const details = normalizeDetails(o.details ?? o.detail ?? o.goodsName ?? o.courseName); out.push({ id, serial, amount, status, addTime, payTime, details, }); } return { list: out, page: pickPage(raw, { current, row }), }; } export async function cancelOrder(id: string): Promise { await apiPost("/order/cancel", { id }); } export async function generateOrderByGoodId(goodsId: string): Promise { await apiPost("/order/generated", { goodsId }); }