const { getDBConnection } = require('./file-utils'); // 获取接口列表 function getDataInterfaces(systemName = 'XNSim', confID) { try { if (!confID) { throw new Error('ConfID 是必填字段'); } const db = getDBConnection(true); const tableName = `DataInterface_${confID}`; // 查询所有接口 const query = ` SELECT SystemName, PlaneName, ATAName, ModelStructName, InterfaceName, InterfaceType, InterfaceOption, InterfaceIsArray, InterfaceArraySize_1, InterfaceArraySize_2, InterfaceNotes FROM '${tableName}' WHERE SystemName = ? ORDER BY PlaneName, ATAName, ModelStructName, InterfaceName `; const interfaces = db.prepare(query).all(systemName); return interfaces; } catch (error) { console.error('获取接口列表数据失败:', error.message); throw error; } } // 添加接口 function addDataInterface(interfaceData) { try { // 验证必填字段 const requiredFields = [ 'SystemName', 'PlaneName', 'ATAName', 'ModelStructName', 'InterfaceName', 'InterfaceType', 'InterfaceOption', 'InterfaceIsArray', 'InterfaceArraySize_1', 'InterfaceArraySize_2', 'ConfID' ]; for (const field of requiredFields) { if (interfaceData[field] === undefined || interfaceData[field] === null || interfaceData[field] === '') { throw new Error(`${field} 是必填字段`); } } const db = getDBConnection(); const tableName = `DataInterface_${interfaceData.ConfID}`; // 检查接口是否已存在 const existingInterface = db.prepare(` SELECT COUNT(*) as count FROM '${tableName}' WHERE InterfaceName = ? `).get(interfaceData.InterfaceName); if (existingInterface.count > 0) { throw new Error('接口名称已存在'); } // 插入新接口 const insertResult = db.prepare(` INSERT INTO '${tableName}' ( SystemName, PlaneName, ATAName, ModelStructName, InterfaceName, InterfaceType, InterfaceOption, InterfaceIsArray, InterfaceArraySize_1, InterfaceArraySize_2, InterfaceNotes ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `).run( interfaceData.SystemName, interfaceData.PlaneName, interfaceData.ATAName, interfaceData.ModelStructName, interfaceData.InterfaceName, interfaceData.InterfaceType, interfaceData.InterfaceOption, interfaceData.InterfaceIsArray, interfaceData.InterfaceArraySize_1, interfaceData.InterfaceArraySize_2, interfaceData.InterfaceNotes || null ); return { success: true, id: insertResult.lastInsertRowid, message: '接口添加成功' }; } catch (error) { console.error('添加接口失败:', error); throw error; } } // 更新接口 function updateDataInterface(interfaceData) { try { // 验证必填字段 const requiredFields = [ 'SystemName', 'PlaneName', 'ATAName', 'ModelStructName', 'InterfaceName', 'InterfaceType', 'InterfaceOption', 'InterfaceIsArray', 'InterfaceArraySize_1', 'InterfaceArraySize_2', 'ConfID' ]; // 检查数据结构 if (!interfaceData.currentData || !interfaceData.originalData) { throw new Error('数据格式错误:缺少 currentData 或 originalData'); } // 验证 currentData 中的必填字段 for (const field of requiredFields) { if (interfaceData.currentData[field] === undefined || interfaceData.currentData[field] === null || interfaceData.currentData[field] === '') { throw new Error(`${field} 是必填字段`); } } const db = getDBConnection(); const tableName = `DataInterface_${interfaceData.currentData.ConfID}`; // 首先检查记录是否存在 const existingRecord = db.prepare(` SELECT COUNT(*) as count FROM '${tableName}' WHERE InterfaceName = ? `).get(interfaceData.originalData.InterfaceName); if (existingRecord.count === 0) { throw new Error('要更新的记录不存在'); } // 更新接口,包括所有字段 const updateResult = db.prepare(` UPDATE '${tableName}' SET SystemName = ?, PlaneName = ?, ATAName = ?, ModelStructName = ?, InterfaceName = ?, InterfaceType = ?, InterfaceOption = ?, InterfaceIsArray = ?, InterfaceArraySize_1 = ?, InterfaceArraySize_2 = ?, InterfaceNotes = ? WHERE InterfaceName = ? `).run( interfaceData.currentData.SystemName, interfaceData.currentData.PlaneName, interfaceData.currentData.ATAName, interfaceData.currentData.ModelStructName, interfaceData.currentData.InterfaceName, interfaceData.currentData.InterfaceType, interfaceData.currentData.InterfaceOption, interfaceData.currentData.InterfaceIsArray, interfaceData.currentData.InterfaceArraySize_1, interfaceData.currentData.InterfaceArraySize_2, interfaceData.currentData.InterfaceNotes || null, interfaceData.originalData.InterfaceName ); if (updateResult.changes === 0) { throw new Error('更新失败:没有记录被修改'); } return { success: true, changes: updateResult.changes, message: '接口更新成功' }; } catch (error) { console.error('更新接口失败:', error); throw error; } } // 删除接口 function deleteDataInterface(interfaceName, confID) { try { if (!confID) { throw new Error('ConfID 是必填字段'); } if (!interfaceName) { throw new Error('InterfaceName 是必填字段'); } const db = getDBConnection(); const tableName = `DataInterface_${confID}`; // 删除接口 const deleteResult = db.prepare(` DELETE FROM '${tableName}' WHERE InterfaceName = ? `).run(interfaceName); return { success: true, changes: deleteResult.changes, message: '接口删除成功' }; } catch (error) { console.error('删除接口失败:', error); throw error; } } // 获取接口结构体列表 function getDataInterfaceStructs(systemName = 'XNSim', planeName, ataName, confID) { try { if (!confID) { throw new Error('ConfID 是必填字段'); } const db = getDBConnection(true); // 根据是否传入 planeName 构建不同的查询 let query; let params; const tableName = `DataInterface_${confID}`; if (!planeName || planeName === '') { query = ` SELECT SystemName, PlaneName, ATAName, ModelStructName FROM '${tableName}' WHERE SystemName = ? GROUP BY ModelStructName ORDER BY PlaneName, ATAName, ModelStructName `; params = [systemName]; } else { query = ` SELECT SystemName, PlaneName, ATAName, ModelStructName FROM '${tableName}' WHERE SystemName = ? AND PlaneName = ? GROUP BY ModelStructName ORDER BY ATAName, ModelStructName `; params = [systemName, planeName]; } if (ataName) { query = query.replace('GROUP BY', 'AND ATAName = ? GROUP BY'); params.push(ataName); } const structs = db.prepare(query).all(...params); return structs; } catch (error) { console.error('获取接口结构体列表数据失败:', error.message); throw error; } } module.exports = { getDataInterfaces, addDataInterface, updateDataInterface, deleteDataInterface, getDataInterfaceStructs };