242 lines
7.7 KiB
JavaScript
242 lines
7.7 KiB
JavaScript
import { getDbConnection } from './index.js';
|
||
import { getSystemDbPath } from './path.js';
|
||
import { executeWithRetry } from './utils.js';
|
||
|
||
/**
|
||
* 查询dict_items连接dict_types后的记录列表
|
||
* @returns {Promise<Array>} 记录列表
|
||
*/
|
||
export async function getDictItemsWithTypes() {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = `
|
||
SELECT di.*, dt.type_name
|
||
FROM dict_items di
|
||
JOIN dict_types dt ON di.type_code = dt.type_code
|
||
ORDER BY dt.type_code, di.item_code
|
||
`;
|
||
return await db.allAsync(sql);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 查询dict_types列表
|
||
* @returns {Promise<Array>} 类型列表
|
||
*/
|
||
export async function getDictTypes() {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT * FROM dict_types ORDER BY type_code';
|
||
return await db.allAsync(sql);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 添加dict_types
|
||
* @param {Object} typeData 类型数据
|
||
* @returns {Promise<Object>} 添加的类型
|
||
*/
|
||
export async function addDictType(typeData) {
|
||
const { type_code, type_name, description } = typeData;
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'INSERT INTO dict_types (type_code, type_name, description) VALUES (?, ?, ?)';
|
||
const result = await db.runAsync(sql, [type_code, type_name, description || null]);
|
||
return { id: result.lastID, ...typeData };
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 添加dict_items
|
||
* @param {Object} itemData 字典项数据
|
||
* @returns {Promise<Object>} 添加的字典项
|
||
*/
|
||
export async function addDictItem(itemData) {
|
||
const { type_code, item_code, item_name, item_description, parent_code, is_active } = itemData;
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'INSERT INTO dict_items (type_code, item_code, item_name, item_description, parent_code, is_active) VALUES (?, ?, ?, ?, ?, ?)';
|
||
const result = await db.runAsync(sql, [
|
||
type_code,
|
||
item_code,
|
||
item_name,
|
||
item_description || null,
|
||
parent_code || null,
|
||
is_active !== undefined ? is_active : 1
|
||
]);
|
||
|
||
// 检查result是否存在,如果不存在则获取最后插入的ID
|
||
let lastId;
|
||
if (result && result.lastID) {
|
||
lastId = result.lastID;
|
||
} else {
|
||
// 使用另一种方式获取最后插入的ID
|
||
const idResult = await db.getAsync('SELECT last_insert_rowid() as id');
|
||
lastId = idResult ? idResult.id : null;
|
||
}
|
||
|
||
if (!lastId) {
|
||
throw new Error('无法获取插入的字典项ID');
|
||
}
|
||
|
||
return { id: lastId, ...itemData };
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 查询一条dict_types
|
||
* @param {number} id 类型ID
|
||
* @returns {Promise<Object|null>} 类型数据或null
|
||
*/
|
||
export async function getDictTypeById(id) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT * FROM dict_types WHERE id = ?';
|
||
return await db.getAsync(sql, [id]);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 查询一条dict_items
|
||
* @param {number} id 字典项ID
|
||
* @returns {Promise<Object|null>} 字典项数据或null
|
||
*/
|
||
export async function getDictItemById(id) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT * FROM dict_items WHERE id = ?';
|
||
return await db.getAsync(sql, [id]);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 根据dict_types的type_code查询dict_items列表
|
||
* @param {string} typeCode 类型编码
|
||
* @param {number} isActive 是否激活(1=激活, 0=未激活, 不传=全部)
|
||
* @returns {Promise<Array>} 字典项列表
|
||
*/
|
||
export async function getDictItemsByTypeCode(typeCode, isActive = undefined) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
let sql = 'SELECT * FROM dict_items WHERE type_code = ?';
|
||
const params = [typeCode];
|
||
|
||
if (isActive !== undefined) {
|
||
sql += ' AND is_active = ?';
|
||
params.push(isActive);
|
||
}
|
||
|
||
sql += ' ORDER BY item_code';
|
||
return await db.allAsync(sql, params);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 更新一条dict_types
|
||
* @param {number} id 类型ID
|
||
* @param {Object} typeData 更新的数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function updateDictType(id, typeData) {
|
||
const { type_code, type_name, description } = typeData;
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'UPDATE dict_types SET type_code = ?, type_name = ?, description = ? WHERE id = ?';
|
||
const result = await db.runAsync(sql, [type_code, type_name, description || null, id]);
|
||
return result.changes > 0;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 更新一条dict_items
|
||
* @param {number} id 字典项ID
|
||
* @param {Object} itemData 更新的数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function updateDictItem(id, itemData) {
|
||
const { type_code, item_code, item_name, item_description, parent_code, is_active } = itemData;
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'UPDATE dict_items SET type_code = ?, item_code = ?, item_name = ?, item_description = ?, parent_code = ?, is_active = ? WHERE id = ?';
|
||
const result = await db.runAsync(sql, [
|
||
type_code,
|
||
item_code,
|
||
item_name,
|
||
item_description || null,
|
||
parent_code || null,
|
||
is_active !== undefined ? is_active : 1,
|
||
id
|
||
]);
|
||
|
||
// 检查result是否存在以及是否有changes属性
|
||
if (!result || result.changes === undefined) {
|
||
return false;
|
||
}
|
||
|
||
return result.changes > 0;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 删除一条dict_types
|
||
* @param {number} id 类型ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
export async function deleteDictType(id) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
// 先检查是否有关联的字典项
|
||
const checkSql = 'SELECT COUNT(*) as count FROM dict_items WHERE type_code = (SELECT type_code FROM dict_types WHERE id = ?)';
|
||
const result = await db.getAsync(checkSql, [id]);
|
||
if (result.count > 0) {
|
||
throw new Error('该字典类型下有关联的字典项,不允许删除');
|
||
}
|
||
|
||
// 删除字典类型
|
||
const deleteSql = 'DELETE FROM dict_types WHERE id = ?';
|
||
const deleteResult = await db.runAsync(deleteSql, [id]);
|
||
return deleteResult.changes > 0;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 删除一条dict_items
|
||
* @param {number} id 字典项ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
export async function deleteDictItem(id) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'DELETE FROM dict_items WHERE id = ?';
|
||
const result = await db.runAsync(sql, [id]);
|
||
|
||
// 检查result是否存在以及是否有changes属性
|
||
if (!result || result.changes === undefined) {
|
||
return false;
|
||
}
|
||
|
||
return result.changes > 0;
|
||
});
|
||
}
|
||
|
||
// 检查parent_code是否存在
|
||
export async function checkParentCodeExists(parentCode) {
|
||
if (!parentCode) return true; // 允许空的parent_code
|
||
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT COUNT(*) as count FROM dict_items WHERE item_code = ?';
|
||
const result = await db.getAsync(sql, [parentCode]);
|
||
return result.count > 0;
|
||
});
|
||
}
|
||
|
||
// 检查是否有其他记录引用了该item_code作为parent_code
|
||
export async function hasChildReferences(itemCode) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT COUNT(*) as count FROM dict_items WHERE parent_code = ?';
|
||
const result = await db.getAsync(sql, [itemCode]);
|
||
return result.count > 0;
|
||
});
|
||
} |