121 lines
4.0 KiB
TypeScript
121 lines
4.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { db } from "@/lib/db";
|
|
import { user } from "@/lib/db/schema";
|
|
import { eq, isNull, or, sql } from "drizzle-orm";
|
|
import { auth } from "@/app/(auth)/auth";
|
|
|
|
// 获取所有部门列表
|
|
export async function GET() {
|
|
try {
|
|
const session = await auth();
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
// 检查是否为管理员
|
|
const currentUser = await db.query.user.findFirst({
|
|
where: eq(user.id, session.user.id),
|
|
});
|
|
|
|
if (currentUser?.role !== "admin") {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
// 获取所有不重复的部门列表
|
|
const departments = await db
|
|
.selectDistinct({ department: user.department })
|
|
.from(user)
|
|
.where(sql`${user.department} IS NOT NULL AND ${user.department} != ''`)
|
|
.orderBy(user.department);
|
|
|
|
return NextResponse.json({
|
|
departments: departments.map(d => d.department).filter(Boolean),
|
|
});
|
|
} catch (error) {
|
|
console.error("[Batch Limit API] Error:", error);
|
|
return NextResponse.json({ error: "获取部门列表失败" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// 批量设置出图限制
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const session = await auth();
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
// 检查是否为管理员
|
|
const currentUser = await db.query.user.findFirst({
|
|
where: eq(user.id, session.user.id),
|
|
});
|
|
|
|
if (currentUser?.role !== "admin") {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { type, limit, department } = body;
|
|
|
|
// limit 为 null 表示无限制,为数字表示限制张数
|
|
const limitValue = limit === null || limit === "" || limit === undefined ? null : parseInt(limit, 10);
|
|
|
|
let updatedCount = 0;
|
|
|
|
if (type === "all") {
|
|
// 批量设置所有用户
|
|
const result = await db
|
|
.update(user)
|
|
.set({ monthlyImageLimit: limitValue });
|
|
|
|
// 获取更新的用户数
|
|
const allUsers = await db.select({ id: user.id }).from(user);
|
|
updatedCount = allUsers.length;
|
|
|
|
console.log(`[Batch Limit] 批量设置所有用户限额: ${limitValue ?? "无限制"}, 影响 ${updatedCount} 个用户`);
|
|
} else if (type === "department" && department) {
|
|
// 按部门批量设置
|
|
await db
|
|
.update(user)
|
|
.set({ monthlyImageLimit: limitValue })
|
|
.where(eq(user.department, department));
|
|
|
|
// 获取更新的用户数
|
|
const deptUsers = await db.select({ id: user.id }).from(user).where(eq(user.department, department));
|
|
updatedCount = deptUsers.length;
|
|
|
|
console.log(`[Batch Limit] 批量设置部门 "${department}" 限额: ${limitValue ?? "无限制"}, 影响 ${updatedCount} 个用户`);
|
|
} else if (type === "no_department") {
|
|
// 设置无部门用户
|
|
await db
|
|
.update(user)
|
|
.set({ monthlyImageLimit: limitValue })
|
|
.where(or(isNull(user.department), eq(user.department, "")));
|
|
|
|
// 获取更新的用户数
|
|
const noDeptUsers = await db
|
|
.select({ id: user.id })
|
|
.from(user)
|
|
.where(or(isNull(user.department), eq(user.department, "")));
|
|
updatedCount = noDeptUsers.length;
|
|
|
|
console.log(`[Batch Limit] 批量设置无部门用户限额: ${limitValue ?? "无限制"}, 影响 ${updatedCount} 个用户`);
|
|
} else {
|
|
return NextResponse.json({ error: "无效的操作类型" }, { status: 400 });
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: `成功更新 ${updatedCount} 个用户的出图限额`,
|
|
updatedCount,
|
|
limit: limitValue,
|
|
});
|
|
} catch (error) {
|
|
console.error("[Batch Limit API] Error:", error);
|
|
return NextResponse.json({ error: "批量设置失败" }, { status: 500 });
|
|
}
|
|
}
|
|
|