255 lines
6.3 KiB
TypeScript
255 lines
6.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
||
import { getServerSession } from 'next-auth';
|
||
import { authOptions } from '@/lib/auth';
|
||
import { prisma } from '@/lib/prisma';
|
||
import { isSystemAdmin } from '@/lib/department-permission';
|
||
|
||
// GET - 获取所有平台分类
|
||
export async function GET(request: NextRequest) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
if (!session?.user) {
|
||
return NextResponse.json({ error: '未登录' }, { status: 401 });
|
||
}
|
||
|
||
const platforms = await prisma.platform.findMany({
|
||
orderBy: { createdAt: 'asc' },
|
||
});
|
||
|
||
// 统计每个平台的素材数量(只统计未删除的素材)
|
||
const platformsWithCount = await Promise.all(
|
||
platforms.map(async (platform) => {
|
||
let count: number;
|
||
try {
|
||
count = await prisma.material.count({
|
||
where: {
|
||
platformName: platform.name,
|
||
deletedAt: null,
|
||
} as any,
|
||
});
|
||
} catch (e: any) {
|
||
// 兼容旧版本数据库(没有deletedAt字段)
|
||
if (e?.meta?.column?.includes('deletedAt')) {
|
||
count = await prisma.material.count({
|
||
where: { platformName: platform.name },
|
||
});
|
||
} else {
|
||
throw e;
|
||
}
|
||
}
|
||
|
||
return {
|
||
id: platform.id,
|
||
name: platform.name,
|
||
count,
|
||
};
|
||
})
|
||
);
|
||
|
||
return NextResponse.json(platformsWithCount);
|
||
} catch (error) {
|
||
console.error('Error fetching platforms:', error);
|
||
return NextResponse.json(
|
||
{ error: '获取平台分类失败' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
// POST - 创建新的平台分类
|
||
export async function POST(request: NextRequest) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
|
||
if (!session?.user || !(session.user as any).id) {
|
||
return NextResponse.json(
|
||
{ error: '未登录' },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
const userRole = (session.user as any).role;
|
||
if (!isSystemAdmin(userRole)) {
|
||
return NextResponse.json(
|
||
{ error: '需要管理员权限' },
|
||
{ status: 403 }
|
||
);
|
||
}
|
||
|
||
const { name } = await request.json();
|
||
|
||
if (!name || !name.trim()) {
|
||
return NextResponse.json(
|
||
{ error: '平台分类名称不能为空' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 检查是否已存在
|
||
const existing = await prisma.platform.findUnique({
|
||
where: { name: name.trim() },
|
||
});
|
||
|
||
if (existing) {
|
||
return NextResponse.json(
|
||
{ error: '该平台分类已存在' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
const platform = await prisma.platform.create({
|
||
data: { name: name.trim() },
|
||
});
|
||
|
||
return NextResponse.json(platform, { status: 201 });
|
||
} catch (error) {
|
||
console.error('Error creating platform:', error);
|
||
return NextResponse.json(
|
||
{ error: '创建平台分类失败' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
// PUT - 更新平台分类
|
||
export async function PUT(request: NextRequest) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
|
||
if (!session?.user || !(session.user as any).id) {
|
||
return NextResponse.json(
|
||
{ error: '未登录' },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
const userRole = (session.user as any).role;
|
||
if (!isSystemAdmin(userRole)) {
|
||
return NextResponse.json(
|
||
{ error: '需要管理员权限' },
|
||
{ status: 403 }
|
||
);
|
||
}
|
||
|
||
const { id, name } = await request.json();
|
||
|
||
if (!id || !name || !name.trim()) {
|
||
return NextResponse.json(
|
||
{ error: '平台分类ID和名称不能为空' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 检查新名称是否已被其他分类使用
|
||
const existing = await prisma.platform.findFirst({
|
||
where: {
|
||
name: name.trim(),
|
||
NOT: { id },
|
||
},
|
||
});
|
||
|
||
if (existing) {
|
||
return NextResponse.json(
|
||
{ error: '该平台分类名称已被使用' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 获取旧名称
|
||
const oldPlatform = await prisma.platform.findUnique({
|
||
where: { id },
|
||
});
|
||
|
||
if (!oldPlatform) {
|
||
return NextResponse.json(
|
||
{ error: '平台分类不存在' },
|
||
{ status: 404 }
|
||
);
|
||
}
|
||
|
||
// 更新分类
|
||
const updated = await prisma.platform.update({
|
||
where: { id },
|
||
data: { name: name.trim() },
|
||
});
|
||
|
||
// 更新所有使用该分类的素材
|
||
await prisma.material.updateMany({
|
||
where: { platformName: oldPlatform.name },
|
||
data: { platformName: name.trim() },
|
||
});
|
||
|
||
return NextResponse.json(updated);
|
||
} catch (error) {
|
||
console.error('Error updating platform:', error);
|
||
return NextResponse.json(
|
||
{ error: '更新平台分类失败' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
// DELETE - 删除平台分类
|
||
export async function DELETE(request: NextRequest) {
|
||
try {
|
||
const session = await getServerSession(authOptions);
|
||
|
||
if (!session?.user || !(session.user as any).id) {
|
||
return NextResponse.json(
|
||
{ error: '未登录' },
|
||
{ status: 401 }
|
||
);
|
||
}
|
||
const userRole = (session.user as any).role;
|
||
if (!isSystemAdmin(userRole)) {
|
||
return NextResponse.json(
|
||
{ error: '需要管理员权限' },
|
||
{ status: 403 }
|
||
);
|
||
}
|
||
|
||
const { searchParams } = new URL(request.url);
|
||
const id = searchParams.get('id');
|
||
|
||
if (!id) {
|
||
return NextResponse.json(
|
||
{ error: '平台分类ID不能为空' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
// 检查是否有素材使用该分类
|
||
const platform = await prisma.platform.findUnique({
|
||
where: { id },
|
||
});
|
||
|
||
if (!platform) {
|
||
return NextResponse.json(
|
||
{ error: '平台分类不存在' },
|
||
{ status: 404 }
|
||
);
|
||
}
|
||
|
||
const materialCount = await prisma.material.count({
|
||
where: { platformName: platform.name },
|
||
});
|
||
|
||
if (materialCount > 0) {
|
||
return NextResponse.json(
|
||
{ error: `无法删除:有 ${materialCount} 个素材正在使用该平台分类` },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
await prisma.platform.delete({
|
||
where: { id },
|
||
});
|
||
|
||
return NextResponse.json({ message: '平台分类已删除' });
|
||
} catch (error) {
|
||
console.error('Error deleting platform:', error);
|
||
return NextResponse.json(
|
||
{ error: '删除平台分类失败' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|