主要更新: 1. 【核心变更】从批量拆分改为单个企业/用户选择生成 - 用户现在可以选择特定的企业或用户生成日报 - 企业列表显示企业名称(如"国营-西南-79路") - 用户列表显示用户姓名,并标注用户类型(企业用户/普通用户) 2. 【新增功能】求和功能 - 支持选择多个字段进行求和 - 求和结果显示在Excel表格底部 - 可对金额、电量等多列分别求和 3. 【数据库表结构更新】 - t_daily_report_config 新增字段:split_name(企业/用户名称)、sum_fields(求和字段列表) - t_daily_report_history 新增字段:split_name、sum_results(求和结果JSON) 4. 【前端页面优化】 - 配置管理:选择企业/用户时显示名称和订单数量 - 日报生成:支持选择单个企业/用户、字段和求和字段 - 历史记录:显示企业/用户名称和求和结果 5. 【API接口优化】 - 企业列表API:返回company_name、company_no、company_group等信息 - 用户列表API:返回user_name和用户类型标签 - 日报生成API:支持sum_fields参数 6. 【删除临时文件】清理debug调试API接口 所有代码静态检查通过,企业列表和用户列表API测试成功 Coze-Commit-Type: user Coze-User-ID: 3722323274763196 Coze-Conversation-ID: 9894087
100 lines
2.8 KiB
TypeScript
100 lines
2.8 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 availableFields = getAllFields();
|
|
|
|
// 尝试查询配置(如果表不存在会失败)
|
|
let configs: any[] = [];
|
|
try {
|
|
const connection = await pool.getConnection();
|
|
const [configRows] = await connection.query<any[]>(
|
|
'SELECT * FROM t_daily_report_config WHERE is_active = 1 ORDER BY create_time DESC'
|
|
);
|
|
configs = configRows || [];
|
|
connection.release();
|
|
} catch (dbError) {
|
|
// 如果表不存在,返回空数组
|
|
console.log('配置表查询失败,可能是表未初始化:', dbError);
|
|
configs = [];
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: configs,
|
|
availableFields: availableFields
|
|
});
|
|
} 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,
|
|
split_value,
|
|
split_name,
|
|
selected_fields,
|
|
sum_fields
|
|
} = body;
|
|
|
|
// 验证必填字段
|
|
if (!config_name || !split_type || !split_value || !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 connection = await pool.getConnection();
|
|
|
|
// 插入配置
|
|
await connection.query(
|
|
`INSERT INTO t_daily_report_config
|
|
(config_name, split_type, split_value, split_name, selected_fields, sum_fields)
|
|
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
config_name,
|
|
split_type,
|
|
split_value,
|
|
split_name || null,
|
|
JSON.stringify(selected_fields),
|
|
sum_fields ? JSON.stringify(sum_fields) : null
|
|
]
|
|
);
|
|
|
|
connection.release();
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '配置创建成功'
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '创建配置失败',
|
|
error: error instanceof Error ? error.message : '未知错误'
|
|
}, { status: 500 });
|
|
}
|
|
} |