200 lines
4.9 KiB
JavaScript
200 lines
4.9 KiB
JavaScript
// 1. 首先,在导入部分添加新函数
|
||
import {
|
||
getDictItemsWithTypes,
|
||
getDictTypes,
|
||
addDictType,
|
||
addDictItem,
|
||
getDictTypeById,
|
||
getDictItemById,
|
||
getDictItemsByTypeCode,
|
||
updateDictType,
|
||
updateDictItem,
|
||
deleteDictType,
|
||
deleteDictItem,
|
||
checkParentCodeExists, // 添加这一行
|
||
hasChildReferences // 添加这一行
|
||
} from '../db/dict.js';
|
||
|
||
/**
|
||
* 服务层:查询字典项及其类型
|
||
* @returns {Promise<Array>} 字典项列表
|
||
*/
|
||
export async function fetchDictItemsWithTypes() {
|
||
try {
|
||
return await getDictItemsWithTypes();
|
||
} catch (error) {
|
||
console.error('服务层: 查询字典项及其类型失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:查询字典类型列表
|
||
* @returns {Promise<Array>} 字典类型列表
|
||
*/
|
||
export async function fetchDictTypes() {
|
||
try {
|
||
return await getDictTypes();
|
||
} catch (error) {
|
||
console.error('服务层: 查询字典类型列表失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:添加字典类型
|
||
* @param {Object} typeData 字典类型数据
|
||
* @returns {Promise<Object>} 添加的字典类型
|
||
*/
|
||
export async function createDictType(typeData) {
|
||
try {
|
||
return await addDictType(typeData);
|
||
} catch (error) {
|
||
console.error('服务层: 添加字典类型失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:添加字典项
|
||
* @param {Object} itemData 字典项数据
|
||
* @returns {Promise<Object>} 添加的字典项
|
||
*/
|
||
export async function createDictItem(itemData) {
|
||
try {
|
||
return await addDictItem(itemData);
|
||
} catch (error) {
|
||
console.error('服务层: 添加字典项失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:根据ID查询字典类型
|
||
* @param {number} id 字典类型ID
|
||
* @returns {Promise<Object|null>} 字典类型数据
|
||
*/
|
||
export async function fetchDictTypeById(id) {
|
||
try {
|
||
return await getDictTypeById(id);
|
||
} catch (error) {
|
||
console.error('服务层: 根据ID查询字典类型失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:根据ID查询字典项
|
||
* @param {number} id 字典项ID
|
||
* @returns {Promise<Object|null>} 字典项数据
|
||
*/
|
||
export async function fetchDictItemById(id) {
|
||
try {
|
||
return await getDictItemById(id);
|
||
} catch (error) {
|
||
console.error('服务层: 根据ID查询字典项失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:根据类型编码查询字典项
|
||
* @param {string} typeCode 类型编码
|
||
* @param {number} isActive 是否激活(1=激活, 0=未激活, 不传=全部)
|
||
* @returns {Promise<Array>} 字典项列表
|
||
*/
|
||
export async function fetchDictItemsByTypeCode(typeCode, isActive = undefined) {
|
||
try {
|
||
return await getDictItemsByTypeCode(typeCode, isActive);
|
||
} catch (error) {
|
||
console.error('服务层: 根据类型编码查询字典项失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:更新字典类型
|
||
* @param {number} id 字典类型ID
|
||
* @param {Object} typeData 更新的数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function modifyDictType(id, typeData) {
|
||
try {
|
||
return await updateDictType(id, typeData);
|
||
} catch (error) {
|
||
console.error('服务层: 更新字典类型失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:更新字典项
|
||
* @param {number} id 字典项ID
|
||
* @param {Object} itemData 更新的数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function modifyDictItem(id, itemData) {
|
||
try {
|
||
return await updateDictItem(id, itemData);
|
||
} catch (error) {
|
||
console.error('服务层: 更新字典项失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:删除字典类型
|
||
* @param {number} id 字典类型ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
export async function removeDictType(id) {
|
||
try {
|
||
return await deleteDictType(id);
|
||
} catch (error) {
|
||
console.error('服务层: 删除字典类型失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:删除字典项
|
||
* @param {number} id 字典项ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
export async function removeDictItem(id) {
|
||
try {
|
||
return await deleteDictItem(id);
|
||
} catch (error) {
|
||
console.error('服务层: 删除字典项失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 2. 在文件末尾添加新函数的服务层封装
|
||
/**
|
||
* 服务层:检查父级编码是否存在
|
||
* @param {string} parentCode 父级编码
|
||
* @returns {Promise<boolean>} 是否存在
|
||
*/
|
||
export async function checkDictParentCode(parentCode) {
|
||
try {
|
||
return await checkParentCodeExists(parentCode);
|
||
} catch (error) {
|
||
console.error('服务层: 检查父级编码失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:检查是否有子引用
|
||
* @param {string} itemCode 字典项编码
|
||
* @returns {Promise<boolean>} 是否有子引用
|
||
*/
|
||
export async function checkDictChildReferences(itemCode) {
|
||
try {
|
||
return await hasChildReferences(itemCode);
|
||
} catch (error) {
|
||
console.error('服务层: 检查子引用失败', error);
|
||
throw error;
|
||
}
|
||
} |