259 lines
8.2 KiB
JavaScript
259 lines
8.2 KiB
JavaScript
const { getDBConnection } = require('./file-utils');
|
||
|
||
// 查询ATAChapters表
|
||
function getATAChapters() {
|
||
try {
|
||
const db = getDBConnection(true);
|
||
|
||
// 直接使用ATAChapters表名查询
|
||
const chapters = db.prepare("SELECT ID, Name, Name_CN FROM 'ATAChapters' ORDER BY ID").all();
|
||
|
||
return chapters;
|
||
} catch (error) {
|
||
console.error('获取ATA章节数据失败:', error.message);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 根据章节ID查询XNModels表中的模型
|
||
function getModelsByChapterId(chapterId, planeName) {
|
||
try {
|
||
// 验证必填参数
|
||
if (!chapterId) {
|
||
throw new Error('ChapterId 是必填参数');
|
||
}
|
||
if (!planeName) {
|
||
throw new Error('PlaneName 是必填参数');
|
||
}
|
||
|
||
const db = getDBConnection(true);
|
||
|
||
const query = `
|
||
SELECT PlaneName, Chapters_ID, ModelName, ModelName_CN, Description, ClassName
|
||
FROM 'XNModels'
|
||
WHERE Chapters_ID = ? AND PlaneName = ?
|
||
ORDER BY ModelName
|
||
`;
|
||
|
||
const models = db.prepare(query).all(chapterId, planeName);
|
||
|
||
return models;
|
||
} catch (error) {
|
||
console.error(`获取章节${chapterId}的模型数据失败:`, error.message);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 根据ClassName查询XNModelsVersion表中的模型版本
|
||
function getModelVersionsByClassName(className, planeName) {
|
||
try {
|
||
// 验证必填参数
|
||
if (!className) {
|
||
throw new Error('ClassName 是必填参数');
|
||
}
|
||
if (!planeName) {
|
||
throw new Error('PlaneName 是必填参数');
|
||
}
|
||
|
||
const db = getDBConnection(true);
|
||
|
||
const query = `
|
||
SELECT
|
||
PlaneName, ClassName, Name, Version, CodePath, Author, Description,
|
||
CreatTime, ChangeTime, RunFreqGroup, RunNode, Priority,
|
||
DataPackagePath, DataPackageHeaderPath, DataPackageEntryPoint, DataPackageInterfaceName,
|
||
ConfID, CmdList
|
||
FROM 'XNModelsVersion'
|
||
WHERE ClassName = ? AND PlaneName = ?
|
||
ORDER BY Version DESC
|
||
`;
|
||
|
||
const versions = db.prepare(query).all(className, planeName);
|
||
|
||
return versions;
|
||
} catch (error) {
|
||
console.error(`获取模型${className}的版本数据失败:`, error.message);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 保存或更新模型版本信息
|
||
function saveModelVersion(versionData) {
|
||
try {
|
||
// 验证必填字段
|
||
const requiredFields = ['ClassName', 'Name', 'Version', 'Author', 'PlaneName', 'ConfID'];
|
||
for (const field of requiredFields) {
|
||
if (!versionData[field]) {
|
||
throw new Error(`${field} 是必填字段`);
|
||
}
|
||
}
|
||
|
||
const db = getDBConnection();
|
||
|
||
// 检查是否为更新模式
|
||
if (versionData.isUpdate) {
|
||
// 查询是否存在要更新的版本
|
||
const existingVersion = db.prepare(`
|
||
SELECT COUNT(*) as count FROM 'XNModelsVersion'
|
||
WHERE ClassName = ? AND Version = ? AND PlaneName = ?
|
||
`).get(versionData.ClassName, versionData.originalVersion || versionData.Version, versionData.PlaneName);
|
||
|
||
if (existingVersion.count === 0) {
|
||
// 不存在要更新的版本,创建新版本
|
||
return saveNewVersion(db, versionData);
|
||
}
|
||
|
||
// 使用前端传来的修改时间,如果没有则生成当前时间
|
||
let changeTime = versionData.ChangeTime;
|
||
if (!changeTime) {
|
||
// 生成当前时间,格式为YYYY-MM-DD HH:MM:SS
|
||
const now = new Date();
|
||
const year = now.getFullYear();
|
||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||
const day = String(now.getDate()).padStart(2, '0');
|
||
const hours = String(now.getHours()).padStart(2, '0');
|
||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||
const seconds = String(now.getSeconds()).padStart(2, '0');
|
||
changeTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||
}
|
||
|
||
// 更新现有版本
|
||
const updateResult = db.prepare(`
|
||
UPDATE 'XNModelsVersion'
|
||
SET
|
||
PlaneName = ?,
|
||
ClassName = ?,
|
||
Name = ?,
|
||
Version = ?,
|
||
CodePath = ?,
|
||
Author = ?,
|
||
Description = ?,
|
||
CreatTime = ?,
|
||
ChangeTime = ?,
|
||
RunFreqGroup = ?,
|
||
RunNode = ?,
|
||
Priority = ?,
|
||
DataPackagePath = ?,
|
||
DataPackageHeaderPath = ?,
|
||
DataPackageEntryPoint = ?,
|
||
DataPackageInterfaceName = ?,
|
||
ConfID = ?,
|
||
CmdList = ?
|
||
WHERE ClassName = ? AND Version = ? AND PlaneName = ?
|
||
`).run(
|
||
versionData.PlaneName,
|
||
versionData.ClassName,
|
||
versionData.Name,
|
||
versionData.Version,
|
||
versionData.CodePath || '',
|
||
versionData.Author,
|
||
versionData.Description || '',
|
||
versionData.CreatTime || changeTime,
|
||
changeTime,
|
||
versionData.RunFreqGroup || '',
|
||
versionData.RunNode || '',
|
||
versionData.Priority || '0',
|
||
versionData.DataPackagePath || '',
|
||
versionData.DataPackageHeaderPath || '',
|
||
versionData.DataPackageEntryPoint || '',
|
||
versionData.DataPackageInterfaceName || '',
|
||
versionData.ConfID,
|
||
versionData.CmdList || '[]',
|
||
versionData.ClassName,
|
||
versionData.originalVersion || versionData.Version,
|
||
versionData.PlaneName
|
||
);
|
||
|
||
return {
|
||
success: true,
|
||
isNew: false,
|
||
changes: updateResult.changes,
|
||
message: '模型版本更新成功'
|
||
};
|
||
} else {
|
||
// 创建新版本
|
||
return saveNewVersion(db, versionData);
|
||
}
|
||
} catch (error) {
|
||
console.error('保存模型版本时出错:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 内部函数:保存新版本
|
||
function saveNewVersion(db, versionData) {
|
||
try {
|
||
// 检查版本是否已存在
|
||
const existingVersion = db.prepare(`
|
||
SELECT COUNT(*) as count FROM 'XNModelsVersion'
|
||
WHERE ClassName = ? AND Version = ? AND PlaneName = ?
|
||
`).get(versionData.ClassName, versionData.Version, versionData.PlaneName);
|
||
|
||
if (existingVersion.count > 0) {
|
||
throw new Error(`版本 ${versionData.Version} 已存在,请使用其他版本号`);
|
||
}
|
||
|
||
// 生成当前时间,格式为YYYY-MM-DD HH:MM:SS
|
||
const now = new Date();
|
||
const year = now.getFullYear();
|
||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||
const day = String(now.getDate()).padStart(2, '0');
|
||
const hours = String(now.getHours()).padStart(2, '0');
|
||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||
const seconds = String(now.getSeconds()).padStart(2, '0');
|
||
const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||
|
||
// 使用前端传来的时间或生成的时间
|
||
const createTime = versionData.CreatTime || formattedDateTime;
|
||
const changeTime = versionData.ChangeTime || formattedDateTime;
|
||
|
||
// 插入新版本
|
||
const insertResult = db.prepare(`
|
||
INSERT INTO 'XNModelsVersion' (
|
||
PlaneName, ClassName, Name, Version, CodePath, Author, Description,
|
||
CreatTime, ChangeTime, RunFreqGroup, RunNode, Priority,
|
||
DataPackagePath, DataPackageHeaderPath, DataPackageEntryPoint, DataPackageInterfaceName,
|
||
ConfID, CmdList
|
||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||
`).run(
|
||
versionData.PlaneName,
|
||
versionData.ClassName,
|
||
versionData.Name,
|
||
versionData.Version,
|
||
versionData.CodePath || '',
|
||
versionData.Author,
|
||
versionData.Description || '',
|
||
createTime,
|
||
changeTime,
|
||
versionData.RunFreqGroup || '',
|
||
versionData.RunNode || '',
|
||
versionData.Priority || '0',
|
||
versionData.DataPackagePath || '',
|
||
versionData.DataPackageHeaderPath || '',
|
||
versionData.DataPackageEntryPoint || '',
|
||
versionData.DataPackageInterfaceName || '',
|
||
versionData.ConfID,
|
||
versionData.CmdList || '[]',
|
||
versionData.ClassName,
|
||
versionData.originalVersion || versionData.Version,
|
||
versionData.PlaneName
|
||
);
|
||
|
||
return {
|
||
success: true,
|
||
isNew: true,
|
||
id: insertResult.lastInsertRowid,
|
||
message: '模型版本创建成功'
|
||
};
|
||
} catch (error) {
|
||
console.error('创建模型版本失败:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
module.exports = {
|
||
getATAChapters,
|
||
getModelsByChapterId,
|
||
getModelVersionsByClassName,
|
||
saveModelVersion
|
||
};
|