/** * 代码生成数据转换工具 * 确保三种模式输出统一的数据格式 */ export interface FieldType { name: string; type: string; type_length: string; default: string; comment: string; not_null: boolean; formShow: boolean; tableShow: boolean; formType: string; search: boolean; searchValue: string; } export interface CodeGenData { class_name: string; class_comment: string; icon: string; sort: number; pid: number; field: FieldType[]; } /** * 默认字段结构 */ const createDefaultField = (): FieldType => ({ name: '', type: 'varchar', type_length: '', default: '', comment: '', not_null: true, formShow: true, tableShow: true, formType: 'VbenInput', search: true, searchValue: '=', }); /** * JSON模式数据转换 */ export function transformJsonToStandard(json: any): CodeGenData { // 如果JSON包含完整结构,直接使用 if (json.class_name && json.field) { return { class_name: json.class_name || '', class_comment: json.class_comment || '', icon: json.icon || '', sort: json.sort ?? 9999, pid: json.pid ?? 0, field: Array.isArray(json.field) ? json.field.map((f: any) => ({ name: f.name || '', type: f.type || 'varchar', type_length: f.type_length || '', default: f.default || '', comment: f.comment || '', not_null: f.not_null !== undefined ? f.not_null : true, formShow: f.formShow !== undefined ? f.formShow : true, tableShow: f.tableShow !== undefined ? f.tableShow : true, formType: f.formType || 'VbenInput', search: f.search !== undefined ? f.search : true, searchValue: f.searchValue || '=', })) : [], }; } // 如果只有字段数组,需要用户补充基础信息 if (Array.isArray(json)) { return { class_name: '', class_comment: '', icon: '', sort: 9999, pid: 0, field: json.map((f: any) => ({ name: f.name || '', type: f.type || 'varchar', type_length: f.type_length || '', default: f.default || '', comment: f.comment || '', not_null: f.not_null !== undefined ? f.not_null : true, formShow: f.formShow !== undefined ? f.formShow : true, tableShow: f.tableShow !== undefined ? f.tableShow : true, formType: f.formType || 'VbenInput', search: f.search !== undefined ? f.search : true, searchValue: f.searchValue || '=', })), }; } // 默认返回空结构 return { class_name: '', class_comment: '', icon: '', sort: 9999, pid: 0, field: [createDefaultField()], }; } /** * 手动模式数据转换 */ export function transformManualToStandard( formData: Record, fields: FieldType[], ): CodeGenData { return { class_name: formData.class_name || '', class_comment: formData.class_comment || '', icon: formData.icon || '', sort: formData.sort ?? 9999, pid: formData.pid ?? 0, field: Array.isArray(fields) ? fields : [], }; } /** * AI模式数据转换 */ export function transformAiToStandard(aiData: any): CodeGenData { // AI返回完整表单数据 if (aiData && aiData.class_name) { return { class_name: aiData.class_name || '', class_comment: aiData.class_comment || '', icon: aiData.icon || '', sort: aiData.sort ?? 9999, pid: aiData.pid ?? 0, field: Array.isArray(aiData.field) ? aiData.field.map((f: any) => ({ name: f.name || '', type: f.type || 'varchar', type_length: f.type_length || '', default: f.default || '', comment: f.comment || '', not_null: f.not_null !== undefined ? f.not_null : true, formShow: f.formShow !== undefined ? f.formShow : true, tableShow: f.tableShow !== undefined ? f.tableShow : true, formType: f.formType || 'VbenInput', search: f.search !== undefined ? f.search : true, searchValue: f.searchValue || '=', })) : [], }; } // 默认返回空结构 return { class_name: '', class_comment: '', icon: '', sort: 9999, pid: 0, field: [createDefaultField()], }; } /** * 数据验证 */ export function validateData(data: CodeGenData): { valid: boolean; message?: string; } { if (!data.class_name || !data.class_name.trim()) { return { valid: false, message: '类名不能为空' }; } if (!data.class_comment || !data.class_comment.trim()) { return { valid: false, message: '中文名称不能为空' }; } if (!data.icon || !data.icon.trim()) { return { valid: false, message: '菜单图标不能为空' }; } if (!Array.isArray(data.field) || data.field.length === 0) { return { valid: false, message: '至少需要一个字段' }; } // 验证字段 for (const field of data.field) { if (!field.name || !field.name.trim()) { return { valid: false, message: '字段名不能为空' }; } } return { valid: true }; }