Files
wzdj/old/lib/db/admin-queries.ts
2026-04-20 14:56:28 +08:00

145 lines
4.4 KiB
TypeScript

/**
* 管理后台统一数据查询 - 与 IndexedDB 打通
* 聚合多表订单、用户等供管理端使用
*/
import { db } from "./schema"
export type AdminOrderType = "product" | "service" | "recharge" | "hotel" | "cafe" | "course" | "pawn" | "pointCard"
export interface AdminOrder {
id: number
orderNo: string
type: AdminOrderType
itemName: string
amount: number
status: string
userId?: number
createdAt: string
sourceTable: "productOrders" | "companionOrders" | "pointCardOrders" | "rechargeOrders" | "venueBookings" | "accountPawns"
}
/** 获取管理端统一订单列表(多表聚合) */
export async function getAllOrdersForAdmin(): Promise<AdminOrder[]> {
const result: AdminOrder[] = []
const now = new Date().toISOString()
try {
const [productOrders, companionOrders, pointCardOrders, rechargeOrders, venueBookings] = await Promise.all([
db.productOrders.orderBy("createdAt").reverse().toArray(),
db.companionOrders.orderBy("createdAt").reverse().toArray(),
db.pointCardOrders.orderBy("createdAt").reverse().toArray(),
db.rechargeOrders.orderBy("createdAt").reverse().toArray(),
db.venueBookings.orderBy("createdAt").reverse().toArray(),
])
productOrders.forEach((o) => {
if (o.id == null) return
const title = (o.items && o.items[0]?.productName) ? o.items[0].productName : "商品订单"
result.push({
id: o.id,
orderNo: o.orderNo,
type: "product",
itemName: title,
amount: o.finalAmount ?? o.totalAmount ?? 0,
status: o.status,
userId: o.userId,
createdAt: o.createdAt ?? now,
sourceTable: "productOrders",
})
})
companionOrders.forEach((o) => {
if (o.id == null) return
result.push({
id: o.id,
orderNo: o.orderNo,
type: "service",
itemName: `${o.type === "cp" ? "CP" : "陪练"} - ${o.game} ${o.hours}小时`,
amount: o.totalAmount ?? 0,
status: o.status,
userId: o.userId,
createdAt: o.createdAt ?? now,
sourceTable: "companionOrders",
})
})
pointCardOrders.forEach((o) => {
if (o.id == null) return
result.push({
id: o.id,
orderNo: o.orderNo,
type: "pointCard",
itemName: `点卡 ${o.game} 面值${o.faceValue} x${o.quantity}`,
amount: o.totalAmount ?? 0,
status: o.status,
userId: o.userId,
createdAt: o.createdAt ?? now,
sourceTable: "pointCardOrders",
})
})
rechargeOrders.forEach((o) => {
if (o.id == null) return
result.push({
id: o.id,
orderNo: o.orderNo,
type: "recharge",
itemName: `充值 ¥${o.amount}`,
amount: o.totalAmount ?? o.amount,
status: o.status,
userId: o.userId,
createdAt: o.createdAt ?? now,
sourceTable: "rechargeOrders",
})
})
venueBookings.forEach((o) => {
if (o.id == null) return
result.push({
id: o.id,
orderNo: o.orderNo,
type: o.type === "hotel" ? "hotel" : "cafe",
itemName: `${o.type === "hotel" ? "酒店" : "网咖"}预订`,
amount: o.totalAmount ?? 0,
status: o.status,
userId: o.userId,
createdAt: o.createdAt ?? now,
sourceTable: "venueBookings",
})
})
result.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
} catch (e) {
console.error("getAllOrdersForAdmin error:", e)
}
return result
}
/** 更新订单状态(根据来源表) */
export async function updateOrderStatusInSource(
sourceTable: AdminOrder["sourceTable"],
id: number,
status: string
): Promise<void> {
switch (sourceTable) {
case "productOrders":
await db.productOrders.update(id, { status: status as any, updatedAt: new Date().toISOString() })
break
case "companionOrders":
await db.companionOrders.update(id, { status: status as any, updatedAt: new Date().toISOString() })
break
case "pointCardOrders":
await db.pointCardOrders.update(id, { status: status as any })
break
case "rechargeOrders":
await db.rechargeOrders.update(id, { status: status as any })
break
case "venueBookings":
await db.venueBookings.update(id, { status: status as any })
break
default:
throw new Error(`Unknown sourceTable: ${sourceTable}`)
}
}