import { NextRequest, NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { prisma } from '@/lib/prisma' // 批量删除素材(软删除或永久删除) export async function POST(request: NextRequest) { try { const session = await getServerSession(authOptions) if (!session || (session.user as any)?.role !== 'ADMIN') { return NextResponse.json({ error: '无权限' }, { status: 403 }) } const { ids, permanent } = await request.json() if (!Array.isArray(ids) || ids.length === 0) { return NextResponse.json({ error: '请提供要删除的素材ID列表' }, { status: 400 }) } if (permanent) { // 永久删除:从数据库中彻底删除 await prisma.material.deleteMany({ where: { id: { in: ids } } }) } else { // 软删除:设置deletedAt字段,同时将任务号作废 // 需要逐条处理,因为每条记录的任务号后缀不同 const materials = await prisma.material.findMany({ where: { id: { in: ids } }, select: { id: true, taskNo: true } }) const now = Date.now() await Promise.all(materials.map((m, index) => prisma.material.update({ where: { id: m.id }, data: { deletedAt: new Date(), // 如果有任务号,加上删除时间戳后缀使其作废 ...(m.taskNo ? { taskNo: `${m.taskNo}_DELETED_${now}_${index}` } : {}) } }) )) } return NextResponse.json({ success: true, message: permanent ? '批量永久删除成功' : '批量删除成功' }) } catch (error) { console.error('Batch delete materials error:', error) return NextResponse.json({ error: '批量删除失败' }, { status: 500 }) } }