67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from 'next/server';
|
||
|
|
import pool from '@/lib/db';
|
||
|
|
import { getAllFields } from '@/lib/field-mapping';
|
||
|
|
|
||
|
|
// GET - 获取所有配置
|
||
|
|
export async function GET() {
|
||
|
|
try {
|
||
|
|
const [configs] = await pool.execute<any[]>(
|
||
|
|
'SELECT * FROM t_daily_report_config WHERE is_active = 1 ORDER BY create_time DESC'
|
||
|
|
);
|
||
|
|
|
||
|
|
return NextResponse.json({
|
||
|
|
success: true,
|
||
|
|
data: configs,
|
||
|
|
availableFields: getAllFields()
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
return NextResponse.json({
|
||
|
|
success: false,
|
||
|
|
message: '获取配置失败',
|
||
|
|
error: error instanceof Error ? error.message : '未知错误'
|
||
|
|
}, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// POST - 创建新配置
|
||
|
|
export async function POST(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const body = await request.json();
|
||
|
|
const { config_name, split_type, selected_fields } = body;
|
||
|
|
|
||
|
|
// 验证必填字段
|
||
|
|
if (!config_name || !split_type || !selected_fields || selected_fields.length === 0) {
|
||
|
|
return NextResponse.json({
|
||
|
|
success: false,
|
||
|
|
message: '缺少必填字段'
|
||
|
|
}, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
// 验证拆分方式
|
||
|
|
if (split_type !== 'company_id' && split_type !== 'user_id') {
|
||
|
|
return NextResponse.json({
|
||
|
|
success: false,
|
||
|
|
message: '拆分方式必须是 company_id 或 user_id'
|
||
|
|
}, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
// 插入配置
|
||
|
|
const [result] = await pool.execute(
|
||
|
|
`INSERT INTO t_daily_report_config (config_name, split_type, selected_fields)
|
||
|
|
VALUES (?, ?, ?)`,
|
||
|
|
[config_name, split_type, JSON.stringify(selected_fields)]
|
||
|
|
);
|
||
|
|
|
||
|
|
return NextResponse.json({
|
||
|
|
success: true,
|
||
|
|
message: '配置创建成功',
|
||
|
|
data: result
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
return NextResponse.json({
|
||
|
|
success: false,
|
||
|
|
message: '创建配置失败',
|
||
|
|
error: error instanceof Error ? error.message : '未知错误'
|
||
|
|
}, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|