249 lines
7.6 KiB
JavaScript
249 lines
7.6 KiB
JavaScript
const { getDBConnection } = require('./file-utils');
|
||
|
||
// 查询XNServices表中的所有服务
|
||
function getServices() {
|
||
try {
|
||
const db = getDBConnection(true);
|
||
|
||
// 查询所有服务
|
||
const services = db.prepare(`
|
||
SELECT ServiceName, ServiceName_CN, Description, ClassName
|
||
FROM 'XNServices'
|
||
ORDER BY ServiceName
|
||
`).all();
|
||
|
||
return services;
|
||
} catch (error) {
|
||
console.error('获取服务列表数据失败:', error.message);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 根据ClassName查询XNServiceVersion表中的服务版本
|
||
function getServiceVersionsByClassName(className) {
|
||
try {
|
||
const db = getDBConnection(true);
|
||
|
||
// 查询该类名下的所有版本
|
||
const versions = db.prepare(`
|
||
SELECT ClassName, Name, Version, CodePath, Author, Description,
|
||
CreatTime, ChangeTime, CmdList, OtherParam
|
||
FROM 'XNServiceVersion'
|
||
WHERE ClassName = ?
|
||
ORDER BY Version DESC
|
||
`).all(className);
|
||
|
||
return versions;
|
||
} catch (error) {
|
||
console.error(`获取服务${className}的版本数据失败:`, error.message);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 保存或更新服务版本信息
|
||
function saveServiceVersion(versionData) {
|
||
try {
|
||
// 验证必填字段
|
||
const requiredFields = ['ClassName', 'Name', 'Version', 'Author'];
|
||
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 'XNServiceVersion'
|
||
WHERE ClassName = ? AND Version = ?
|
||
`).get(versionData.ClassName, versionData.originalVersion || versionData.Version);
|
||
|
||
if (existingVersion.count === 0) {
|
||
// 不存在要更新的版本,创建新版本
|
||
return saveNewServiceVersion(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 'XNServiceVersion'
|
||
SET
|
||
Name = ?,
|
||
Version = ?,
|
||
Author = ?,
|
||
Description = ?,
|
||
CodePath = ?,
|
||
ChangeTime = ?,
|
||
CmdList = ?,
|
||
OtherParam = ?
|
||
WHERE ClassName = ? AND Version = ?
|
||
`).run(
|
||
versionData.Name,
|
||
versionData.Version,
|
||
versionData.Author,
|
||
versionData.Description || '',
|
||
versionData.CodePath || '',
|
||
changeTime, // 使用前端传来的时间或生成的当前时间
|
||
versionData.CmdList || '[]',
|
||
versionData.OtherParam || '{}',
|
||
versionData.ClassName,
|
||
versionData.originalVersion || versionData.Version
|
||
);
|
||
|
||
return {
|
||
success: true,
|
||
isNew: false,
|
||
changes: updateResult.changes,
|
||
message: '服务版本更新成功'
|
||
};
|
||
} else {
|
||
// 创建新版本
|
||
return saveNewServiceVersion(db, versionData);
|
||
}
|
||
} catch (error) {
|
||
console.error('保存服务版本时出错:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 内部函数:保存新的服务版本
|
||
function saveNewServiceVersion(db, versionData) {
|
||
try {
|
||
// 检查版本是否已存在
|
||
const existingVersion = db.prepare(`
|
||
SELECT COUNT(*) as count FROM 'XNServiceVersion'
|
||
WHERE ClassName = ? AND Version = ?
|
||
`).get(versionData.ClassName, versionData.Version);
|
||
|
||
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 'XNServiceVersion' (
|
||
ClassName, Name, Version, CodePath, Author, Description,
|
||
CreatTime, ChangeTime, CmdList, OtherParam
|
||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||
`).run(
|
||
versionData.ClassName,
|
||
versionData.Name,
|
||
versionData.Version,
|
||
versionData.CodePath || '',
|
||
versionData.Author,
|
||
versionData.Description || '',
|
||
createTime, // 使用前端传来的创建时间或生成的当前时间
|
||
changeTime, // 使用前端传来的修改时间或生成的当前时间
|
||
versionData.CmdList || '[]',
|
||
versionData.OtherParam || '{}'
|
||
);
|
||
|
||
return {
|
||
success: true,
|
||
isNew: true,
|
||
id: insertResult.lastInsertRowid,
|
||
message: '服务版本创建成功'
|
||
};
|
||
} catch (error) {
|
||
console.error('创建服务版本失败:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 创建新服务
|
||
function createService(serviceData) {
|
||
try {
|
||
// 验证必填字段
|
||
const requiredFields = ['ClassName', 'ServiceName', 'ServiceName_CN'];
|
||
for (const field of requiredFields) {
|
||
if (!serviceData[field]) {
|
||
throw new Error(`${field} 是必填字段`);
|
||
}
|
||
}
|
||
|
||
// 验证类名是否以XN开头
|
||
if (!serviceData.ClassName.startsWith('XN')) {
|
||
throw new Error('服务类名必须以XN开头');
|
||
}
|
||
|
||
const db = getDBConnection();
|
||
|
||
// 检查服务类名是否已存在
|
||
const existingService = db.prepare(`
|
||
SELECT COUNT(*) as count FROM 'XNServices'
|
||
WHERE ClassName = ?
|
||
`).get(serviceData.ClassName);
|
||
|
||
if (existingService.count > 0) {
|
||
throw new Error(`服务类名 ${serviceData.ClassName} 已存在,请使用其他类名`);
|
||
}
|
||
|
||
// 检查服务名称是否已存在
|
||
const existingServiceName = db.prepare(`
|
||
SELECT COUNT(*) as count FROM 'XNServices'
|
||
WHERE ServiceName = ?
|
||
`).get(serviceData.ServiceName);
|
||
|
||
if (existingServiceName.count > 0) {
|
||
throw new Error(`服务名称 ${serviceData.ServiceName} 已存在,请使用其他名称`);
|
||
}
|
||
|
||
// 插入新服务
|
||
const insertResult = db.prepare(`
|
||
INSERT INTO 'XNServices' (
|
||
ClassName, ServiceName, ServiceName_CN, Description
|
||
) VALUES (?, ?, ?, ?)
|
||
`).run(
|
||
serviceData.ClassName,
|
||
serviceData.ServiceName,
|
||
serviceData.ServiceName_CN,
|
||
serviceData.Description || ''
|
||
);
|
||
|
||
return {
|
||
success: true,
|
||
id: insertResult.lastInsertRowid,
|
||
message: '服务创建成功'
|
||
};
|
||
} catch (error) {
|
||
console.error('创建服务失败:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
module.exports = {
|
||
getServices,
|
||
getServiceVersionsByClassName,
|
||
saveServiceVersion,
|
||
createService
|
||
};
|