v1
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { requireAdmin } from '@/lib/admin-auth'
|
||||
|
||||
// 获取所有组件
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
await requireAdmin(request)
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const page = parseInt(searchParams.get('page') || '1')
|
||||
const limit = parseInt(searchParams.get('limit') || '20')
|
||||
const type = searchParams.get('type')
|
||||
const search = searchParams.get('search')
|
||||
|
||||
const where: any = {}
|
||||
if (type) {
|
||||
where.componentType = {
|
||||
name: type
|
||||
}
|
||||
}
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ name: { contains: search, mode: 'insensitive' } },
|
||||
{ brand: { contains: search, mode: 'insensitive' } },
|
||||
]
|
||||
}
|
||||
|
||||
const [components, total] = await Promise.all([
|
||||
prisma.component.findMany({
|
||||
where,
|
||||
include: {
|
||||
componentType: true
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
},
|
||||
skip: (page - 1) * limit,
|
||||
take: limit
|
||||
}),
|
||||
prisma.component.count({ where })
|
||||
])
|
||||
|
||||
return NextResponse.json({
|
||||
components,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
pages: Math.ceil(total / limit)
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error: any) {
|
||||
return NextResponse.json(
|
||||
{ message: error.message || '获取组件列表失败' },
|
||||
{ status: error.message === '权限不足' ? 403 : 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 创建新组件
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
await requireAdmin(request)
|
||||
|
||||
const body = await request.json()
|
||||
const { name, brand, model, price, description, imageUrl, stock, specifications, componentTypeId } = body
|
||||
|
||||
if (!name || !brand || !model || !price || !componentTypeId) {
|
||||
return NextResponse.json(
|
||||
{ message: '请填写所有必填字段' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const component = await prisma.component.create({
|
||||
data: {
|
||||
name,
|
||||
brand,
|
||||
model,
|
||||
price: parseFloat(price),
|
||||
description,
|
||||
imageUrl,
|
||||
stock: parseInt(stock) || 0,
|
||||
specifications: specifications ? JSON.stringify(specifications) : null,
|
||||
componentTypeId
|
||||
},
|
||||
include: {
|
||||
componentType: true
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(component, { status: 201 })
|
||||
|
||||
} catch (error: any) {
|
||||
return NextResponse.json(
|
||||
{ message: error.message || '创建组件失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params
|
||||
const { status } = await request.json()
|
||||
|
||||
const order = await prisma.order.update({
|
||||
where: { id: id },
|
||||
data: { status },
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
username: true,
|
||||
name: true
|
||||
}
|
||||
},
|
||||
orderItems: {
|
||||
include: {
|
||||
component: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
brand: true,
|
||||
model: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(order)
|
||||
} catch (error) {
|
||||
console.error('更新订单状态失败:', error)
|
||||
return NextResponse.json(
|
||||
{ message: '更新订单状态失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const orders = await prisma.order.findMany({
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
username: true,
|
||||
name: true
|
||||
}
|
||||
}, orderItems: {
|
||||
include: {
|
||||
component: {
|
||||
include: {
|
||||
componentType: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(orders)
|
||||
} catch (error) {
|
||||
console.error('获取订单列表失败:', error)
|
||||
return NextResponse.json(
|
||||
{ message: '获取订单列表失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { requireAdmin } from '@/lib/admin-auth'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
await requireAdmin(request)
|
||||
|
||||
// 按消费金额倒排的前十用户
|
||||
const topUsersBySpending = await prisma.user.findMany({
|
||||
where: {
|
||||
isAdmin: false,
|
||||
orders: {
|
||||
some: {}
|
||||
}
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
name: true,
|
||||
email: true,
|
||||
orders: {
|
||||
select: {
|
||||
totalAmount: true
|
||||
}
|
||||
}
|
||||
},
|
||||
take: 10
|
||||
})
|
||||
|
||||
const usersWithSpending = topUsersBySpending.map(user => ({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
name: user.name || user.username,
|
||||
email: user.email,
|
||||
totalSpending: user.orders.reduce((sum, order) => sum + order.totalAmount, 0),
|
||||
orderCount: user.orders.length
|
||||
})).sort((a, b) => b.totalSpending - a.totalSpending)
|
||||
|
||||
// 按销售量倒排序的前十配件
|
||||
const topComponentsBySales = await prisma.component.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
brand: true,
|
||||
price: true,
|
||||
componentType: {
|
||||
select: {
|
||||
name: true
|
||||
}
|
||||
},
|
||||
orderItems: {
|
||||
select: {
|
||||
quantity: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const componentsWithSales = topComponentsBySales.map(component => ({
|
||||
id: component.id,
|
||||
name: component.name,
|
||||
brand: component.brand,
|
||||
price: component.price,
|
||||
typeName: component.componentType.name,
|
||||
totalSales: component.orderItems.reduce((sum, item) => sum + item.quantity, 0),
|
||||
totalRevenue: component.orderItems.reduce((sum, item) => sum + (item.quantity * component.price), 0)
|
||||
})).sort((a, b) => b.totalSales - a.totalSales).slice(0, 10)
|
||||
|
||||
// 总体统计
|
||||
const totalUsers = await prisma.user.count({ where: { isAdmin: false } })
|
||||
const totalOrders = await prisma.order.count()
|
||||
const totalComponents = await prisma.component.count()
|
||||
const totalRevenue = await prisma.order.aggregate({
|
||||
_sum: {
|
||||
totalAmount: true
|
||||
}
|
||||
})
|
||||
|
||||
const recentOrders = await prisma.order.findMany({
|
||||
take: 5,
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
username: true,
|
||||
name: true
|
||||
}
|
||||
},
|
||||
orderItems: {
|
||||
include: {
|
||||
component: {
|
||||
select: {
|
||||
name: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
overview: {
|
||||
totalUsers,
|
||||
totalOrders,
|
||||
totalComponents,
|
||||
totalRevenue: totalRevenue._sum.totalAmount || 0
|
||||
},
|
||||
topUsers: usersWithSpending,
|
||||
topComponents: componentsWithSales,
|
||||
recentOrders
|
||||
})
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('获取统计数据失败:', error)
|
||||
return NextResponse.json(
|
||||
{ message: error.message || '获取统计数据失败' },
|
||||
{ status: error.message === '权限不足' ? 403 : 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user