XNSim/XNSimHtml/utils/data-interface-utils.js

407 lines
11 KiB
JavaScript

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;
}
}
/**
* 删除指定构型ID的整个接口表
* @param {number} confID - 构型ID
* @returns {Object} 删除结果
*/
function deleteDataInterfaceTable(confID) {
try {
if (!confID) {
throw new Error('ConfID 是必填字段');
}
const db = getDBConnection();
const tableName = `DataInterface_${confID}`;
// 删除整个表
const result = db.prepare(`DROP TABLE IF EXISTS '${tableName}'`).run();
return {
success: true,
message: '接口表删除成功'
};
} catch (error) {
console.error('删除接口表失败:', error);
throw error;
}
}
/**
* 创建指定构型ID的接口表
* @param {number} confID - 构型ID
* @returns {Object} 创建结果
*/
function createDataInterfaceTable(confID) {
try {
if (!confID) {
throw new Error('ConfID 是必填字段');
}
const db = getDBConnection();
const tableName = `DataInterface_${confID}`;
// 创建表
const result = db.prepare(`
CREATE TABLE IF NOT EXISTS '${tableName}' (
SystemName TEXT NOT NULL,
PlaneName TEXT NOT NULL,
ATAName TEXT NOT NULL,
ModelStructName TEXT NOT NULL,
InterfaceName TEXT NOT NULL PRIMARY KEY,
InterfaceType TEXT NOT NULL,
InterfaceOption TEXT NOT NULL,
InterfaceIsArray INTEGER NOT NULL,
InterfaceArraySize_1 INTEGER NOT NULL,
InterfaceArraySize_2 INTEGER NOT NULL,
InterfaceNotes TEXT
)
`).run();
return {
success: true,
message: '接口表创建成功'
};
} catch (error) {
console.error('创建接口表失败:', error);
throw error;
}
}
/**
* 复制接口表
* @param {number} sourceConfID - 源构型ID
* @param {number} targetConfID - 目标构型ID
* @returns {Object} 复制结果
*/
function copyDataInterfaceTable(sourceConfID, targetConfID) {
try {
if (!sourceConfID || !targetConfID) {
throw new Error('sourceConfID 和 targetConfID 都是必填字段');
}
if (sourceConfID === targetConfID) {
throw new Error('源构型ID和目标构型ID不能相同');
}
const db = getDBConnection();
const sourceTableName = `DataInterface_${sourceConfID}`;
const targetTableName = `DataInterface_${targetConfID}`;
// 检查源表是否存在
const sourceTableExists = db.prepare(`
SELECT name FROM sqlite_master
WHERE type='table' AND name=?
`).get(sourceTableName);
if (!sourceTableExists) {
throw new Error('源接口表不存在');
}
// 检查目标表是否存在
const targetTableExists = db.prepare(`
SELECT name FROM sqlite_master
WHERE type='table' AND name=?
`).get(targetTableName);
if (targetTableExists) {
throw new Error('目标接口表已存在,请新建新的构型或联系管理员处理');
}
// 创建新表并复制数据
const result = db.prepare(`
CREATE TABLE '${targetTableName}' AS
SELECT * FROM '${sourceTableName}'
`).run();
return {
success: true,
message: '接口表复制成功'
};
} catch (error) {
console.error('复制接口表失败:', error);
throw error;
}
}
module.exports = {
getDataInterfaces,
addDataInterface,
updateDataInterface,
deleteDataInterface,
getDataInterfaceStructs,
deleteDataInterfaceTable,
createDataInterfaceTable,
copyDataInterfaceTable
};