156 lines
3.6 KiB
TypeScript
156 lines
3.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { verifyToken } from '@/lib/auth'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const authHeader = request.headers.get('authorization')
|
|
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
return NextResponse.json(
|
|
{ message: '请先登录' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const token = authHeader.substring(7)
|
|
const decoded = verifyToken(token)
|
|
if (!decoded) {
|
|
return NextResponse.json(
|
|
{ message: '登录已过期' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const { items } = await request.json()
|
|
if (!items || items.length === 0) {
|
|
return NextResponse.json(
|
|
{ message: '订单商品不能为空' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 生成订单号
|
|
const orderNumber = `ORD${Date.now()}`
|
|
|
|
// 计算总金额
|
|
let totalAmount = 0
|
|
const orderItems = []
|
|
|
|
for (const item of items) {
|
|
const component = await prisma.component.findUnique({
|
|
where: { id: item.componentId }
|
|
})
|
|
|
|
if (!component) {
|
|
return NextResponse.json(
|
|
{ message: `配件不存在: ${item.componentId}` },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (component.stock < item.quantity) {
|
|
return NextResponse.json(
|
|
{ message: `配件库存不足: ${component.name}` },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const itemTotal = component.price * item.quantity
|
|
totalAmount += itemTotal
|
|
|
|
orderItems.push({
|
|
componentId: component.id,
|
|
quantity: item.quantity,
|
|
price: component.price
|
|
})
|
|
}
|
|
|
|
// 创建订单
|
|
const order = await prisma.order.create({
|
|
data: {
|
|
orderNumber,
|
|
totalAmount,
|
|
userId: decoded.userId,
|
|
orderItems: {
|
|
create: orderItems
|
|
}
|
|
}, include: {
|
|
orderItems: {
|
|
include: {
|
|
component: {
|
|
include: {
|
|
componentType: true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
// 更新库存
|
|
for (const item of items) {
|
|
await prisma.component.update({
|
|
where: { id: item.componentId },
|
|
data: {
|
|
stock: {
|
|
decrement: item.quantity
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
return NextResponse.json(order, { status: 201 })
|
|
} catch (error) {
|
|
console.error('创建订单失败:', error)
|
|
return NextResponse.json(
|
|
{ message: '创建订单失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const authHeader = request.headers.get('authorization')
|
|
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
return NextResponse.json(
|
|
{ message: '请先登录' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const token = authHeader.substring(7)
|
|
const decoded = verifyToken(token)
|
|
if (!decoded) {
|
|
return NextResponse.json(
|
|
{ message: '登录已过期' },
|
|
{ status: 401 }
|
|
)
|
|
} const orders = await prisma.order.findMany({
|
|
where: { userId: decoded.userId },
|
|
include: {
|
|
orderItems: {
|
|
include: {
|
|
component: {
|
|
include: {
|
|
componentType: true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc'
|
|
}
|
|
})
|
|
|
|
return NextResponse.json(orders)
|
|
} catch (error) {
|
|
console.error('获取订单列表失败:', error)
|
|
return NextResponse.json(
|
|
{ message: '获取订单列表失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|