1. 场景类别数量父子关系联动: - 选择店铺后,场景数量只显示该店铺下的数量 - 修改 /api/scenes API 支持 category 参数 - 前端动态更新场景数量统计 2. 用户部门编辑功能: - 新增 /api/admin/users/update-department API - 用户管理页面添加"编辑部门"按钮 - 支持为用户分配/修改所属部门 - 部门列表从系统设置的部门管理获取
209 lines
4.7 KiB
TypeScript
209 lines
4.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
// 获取所有场景类别
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(req.url)
|
|
const categoryName = searchParams.get('category')
|
|
|
|
const scenes = await prisma.scene.findMany({
|
|
orderBy: {
|
|
name: 'asc',
|
|
},
|
|
})
|
|
|
|
// 获取每个场景类别下的素材数量
|
|
// 如果指定了店铺分类,只统计该店铺下的场景数量
|
|
const scenesWithCount = await Promise.all(
|
|
scenes.map(async (scene) => {
|
|
const whereCondition: any = {
|
|
sceneName: scene.name,
|
|
}
|
|
|
|
// 如果选择了店铺,只统计该店铺下的场景数量
|
|
if (categoryName) {
|
|
whereCondition.categoryName = categoryName
|
|
}
|
|
|
|
const count = await prisma.material.count({
|
|
where: whereCondition,
|
|
})
|
|
|
|
return {
|
|
...scene,
|
|
count,
|
|
}
|
|
})
|
|
)
|
|
|
|
return NextResponse.json(scenesWithCount)
|
|
} catch (error) {
|
|
console.error('Error fetching scenes:', error)
|
|
return NextResponse.json(
|
|
{ error: '获取场景类别列表失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// 添加新场景类别
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session) {
|
|
return NextResponse.json(
|
|
{ error: '请先登录' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const { name } = await req.json()
|
|
|
|
if (!name || !name.trim()) {
|
|
return NextResponse.json(
|
|
{ error: '场景类别名称不能为空' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 检查场景类别是否已存在
|
|
const existing = await prisma.scene.findUnique({
|
|
where: { name: name.trim() },
|
|
})
|
|
|
|
if (existing) {
|
|
return NextResponse.json(
|
|
{ error: '该场景类别已存在' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 创建新场景类别
|
|
const scene = await prisma.scene.create({
|
|
data: {
|
|
name: name.trim(),
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(scene, { status: 201 })
|
|
} catch (error) {
|
|
console.error('Error creating scene:', error)
|
|
return NextResponse.json(
|
|
{ error: '创建场景类别失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// 更新场景类别名称
|
|
export async function PATCH(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '权限不足' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { id, name } = await req.json()
|
|
|
|
if (!id || !name || !name.trim()) {
|
|
return NextResponse.json(
|
|
{ error: '参数错误' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 检查新名称是否已存在(排除当前场景)
|
|
const existing = await prisma.scene.findFirst({
|
|
where: {
|
|
name: name.trim(),
|
|
NOT: { id },
|
|
},
|
|
})
|
|
|
|
if (existing) {
|
|
return NextResponse.json(
|
|
{ error: '该场景类别已存在' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 获取旧名称
|
|
const oldScene = await prisma.scene.findUnique({
|
|
where: { id },
|
|
})
|
|
|
|
if (!oldScene) {
|
|
return NextResponse.json(
|
|
{ error: '分类不存在' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
// 更新场景类别名称
|
|
const scene = await prisma.scene.update({
|
|
where: { id },
|
|
data: { name: name.trim() },
|
|
})
|
|
|
|
// 同步更新所有使用该场景的素材
|
|
await prisma.material.updateMany({
|
|
where: { sceneName: oldScene.name },
|
|
data: { sceneName: name.trim() },
|
|
})
|
|
|
|
return NextResponse.json(scene)
|
|
} catch (error) {
|
|
console.error('Error updating scene:', error)
|
|
return NextResponse.json(
|
|
{ error: '更新失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// 删除场景类别
|
|
export async function DELETE(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '权限不足' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { searchParams } = new URL(req.url)
|
|
const id = searchParams.get('id')
|
|
|
|
if (!id) {
|
|
return NextResponse.json(
|
|
{ error: '缺少ID参数' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 删除场景类别
|
|
await prisma.scene.delete({
|
|
where: { id },
|
|
})
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error('Error deleting scene:', error)
|
|
return NextResponse.json(
|
|
{ error: '删除失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|