|
|
|
@ -81,7 +81,7 @@ const Dept = { |
|
|
|
return { dept: null, error: error.message }; |
|
|
|
} |
|
|
|
}, |
|
|
|
// 插入部门数据
|
|
|
|
// 插入组织机构数据
|
|
|
|
insertDept: async function (dept) { |
|
|
|
try { |
|
|
|
const insertedDept = await prisma.dept.create({ |
|
|
|
@ -98,12 +98,12 @@ const Dept = { |
|
|
|
}); |
|
|
|
return insertedDept; |
|
|
|
} catch (error) { |
|
|
|
console.error("插入部门数据失败:", error); |
|
|
|
console.error("插入组织机构数据失败:", error); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
update: async function (deptId, updates = {}) { |
|
|
|
console.log("更新部门数据:", updates); |
|
|
|
console.log("更新组织机构数据:", updates); |
|
|
|
try { |
|
|
|
// 检查 deptId 是否存在
|
|
|
|
if (!deptId) throw new Error('没有提供用于查询的deptId'); |
|
|
|
@ -111,9 +111,9 @@ const Dept = { |
|
|
|
// 检查 updates 是否为空
|
|
|
|
if (Object.keys(updates).length === 0) throw new Error('没有提供更新字段'); |
|
|
|
|
|
|
|
// 查询当前部门
|
|
|
|
// 查询当前组织机构
|
|
|
|
const currentDept = await prisma.dept.findUnique({ where: { deptId } }); |
|
|
|
if (!currentDept) throw new Error('不存在该部门'); |
|
|
|
if (!currentDept) throw new Error('不存在该组织机构'); |
|
|
|
|
|
|
|
// 更新 lastUpdatedAt 字段
|
|
|
|
updates.lastUpdatedAt = new Date(); |
|
|
|
@ -125,7 +125,7 @@ const Dept = { |
|
|
|
updates.ancestors = parentDept ? `${parentDept.ancestors},${parentId}` : `${parentId}`; |
|
|
|
} |
|
|
|
|
|
|
|
console.log("更新前部门数据:", updates); |
|
|
|
console.log("更新前组织机构数据:", updates); |
|
|
|
|
|
|
|
// 执行更新操作
|
|
|
|
const updatedDept = await prisma.dept.update({ |
|
|
|
@ -133,7 +133,7 @@ const Dept = { |
|
|
|
data: updates, // 更新的字段
|
|
|
|
}); |
|
|
|
|
|
|
|
console.log("更新后部门数据:", updatedDept); |
|
|
|
console.log("更新后组织机构数据:", updatedDept); |
|
|
|
|
|
|
|
// 返回成功结果
|
|
|
|
return { success: true, error: null, dept: updatedDept }; |
|
|
|
@ -162,6 +162,33 @@ const Dept = { |
|
|
|
return false; |
|
|
|
} |
|
|
|
}, |
|
|
|
softDelete: async function (deptId) { |
|
|
|
try { |
|
|
|
// 检查 deptId 是否存在
|
|
|
|
if (!deptId) throw new Error('没有提供用于查询的deptId'); |
|
|
|
|
|
|
|
// 查询当前组织机构
|
|
|
|
const currentDept = await prisma.dept.findUnique({ where: { deptId } }); |
|
|
|
if (!currentDept) throw new Error('不存在该组织机构'); |
|
|
|
|
|
|
|
// 执行逻辑删除操作
|
|
|
|
const updatedDept = await prisma.dept.update({ |
|
|
|
where: { deptId }, // 使用 deptId 作为查询条件
|
|
|
|
data: { |
|
|
|
delFlag: 1, // 标记为已删除
|
|
|
|
lastUpdatedAt: new Date(), // 更新最后修改时间
|
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
console.log("逻辑删除后的组织机构数据:", updatedDept); |
|
|
|
|
|
|
|
// 返回成功结果
|
|
|
|
return { success: true, error: null, dept: updatedDept }; |
|
|
|
} catch (error) { |
|
|
|
console.error(error.message); |
|
|
|
return { success: false, error: error.message, dept: null }; |
|
|
|
} |
|
|
|
}, |
|
|
|
where: async function (clause = {}, limit = null) { |
|
|
|
try { |
|
|
|
const depts = await prisma.dept.findMany({ |
|
|
|
@ -181,24 +208,24 @@ const Dept = { |
|
|
|
try { |
|
|
|
const existingDept = await prisma.dept.findFirst({ |
|
|
|
where: { |
|
|
|
deptName: dept.deptName, // 根据部门名称查询
|
|
|
|
deptName: dept.deptName, // 根据组织机构名称查询
|
|
|
|
parentId: dept.parentId, // 排除父id
|
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
// 如果查询到记录,说明部门名称已存在
|
|
|
|
// 如果查询到记录,说明组织机构名称已存在
|
|
|
|
console.log("existingDept:", existingDept); |
|
|
|
return !existingDept; |
|
|
|
} catch (error) { |
|
|
|
console.error('检查部门名称唯一性失败:', error); |
|
|
|
console.error('检查组织机构名称唯一性失败:', error); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
// 检查部门是否包含未停用的子部门
|
|
|
|
// 检查组织机构是否包含未停用的子组织机构
|
|
|
|
selectNormalChildrenDeptById: async function (deptId) { |
|
|
|
try { |
|
|
|
// 查询所有祖先部门中包含当前部门的未停用部门
|
|
|
|
// 查询所有祖先组织机构中包含当前组织机构的未停用组织机构
|
|
|
|
const childrenDepts = await prisma.$queryRaw`
|
|
|
|
SELECT COUNT(*) as count |
|
|
|
FROM sys_dept |
|
|
|
@ -207,53 +234,53 @@ const Dept = { |
|
|
|
AND FIND_IN_SET(${deptId}, ancestors) |
|
|
|
`;
|
|
|
|
|
|
|
|
// 返回未停用的子部门数量
|
|
|
|
// 返回未停用的子组织机构数量
|
|
|
|
return childrenDepts[0].count; |
|
|
|
} catch (error) { |
|
|
|
console.error("查询子部门失败:", error); |
|
|
|
console.error("查询子组织机构失败:", error); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
// 检查部门是否有子部门
|
|
|
|
// 检查组织机构是否有子组织机构
|
|
|
|
hasChildByDeptId: async function (deptId) { |
|
|
|
try { |
|
|
|
const children = await prisma.dept.findMany({ |
|
|
|
where: { |
|
|
|
parentId: deptId, // 查询当前部门的子部门
|
|
|
|
parentId: deptId, // 查询当前组织机构的子组织机构
|
|
|
|
delFlag: 0, |
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
// 如果有子部门,返回 true
|
|
|
|
// 如果有子组织机构,返回 true
|
|
|
|
return children.length > 0; |
|
|
|
} catch (error) { |
|
|
|
console.error("检查子部门失败:", error); |
|
|
|
console.error("检查子组织机构失败:", error); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
// 检查部门是否存在用户
|
|
|
|
// 检查组织机构是否存在用户
|
|
|
|
checkDeptExistUser: async function (deptId) { |
|
|
|
try { |
|
|
|
const users = await prisma.user.findMany({ |
|
|
|
const deptUsers = await prisma.dept_users.findMany({ |
|
|
|
where: { |
|
|
|
deptId: deptId, // 查询当前部门的用户
|
|
|
|
deptId: deptId, // 查询当前组织机构的用户
|
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
// 如果存在用户,返回 true
|
|
|
|
return users.length > 0; |
|
|
|
return deptUsers.length > 0; |
|
|
|
} catch (error) { |
|
|
|
console.error("检查部门用户失败:", error); |
|
|
|
console.error("检查组织机构用户失败:", error); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
/** |
|
|
|
* 获取部门树状结构 |
|
|
|
* 获取组织机构树状结构 |
|
|
|
* @returns {Promise<Array>} |
|
|
|
*/ |
|
|
|
getDeptTree:async function () { |
|
|
|
try { |
|
|
|
// 查询所有部门
|
|
|
|
// 查询所有组织机构
|
|
|
|
const allDepts = await prisma.dept.findMany(); |
|
|
|
|
|
|
|
// 构建树状结构
|
|
|
|
@ -262,19 +289,19 @@ const Dept = { |
|
|
|
.filter((dept) => dept.parentId === parentId) |
|
|
|
.map((dept) => ({ |
|
|
|
...dept, |
|
|
|
children: buildTree(dept.deptId), // 递归获取子部门
|
|
|
|
children: buildTree(dept.deptId), // 递归获取子组织机构
|
|
|
|
})); |
|
|
|
}; |
|
|
|
|
|
|
|
return buildTree(); |
|
|
|
} catch (error) { |
|
|
|
console.error("获取部门树状结构失败:", error); |
|
|
|
console.error("获取组织机构树状结构失败:", error); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
/** |
|
|
|
* 根据父部门 ID 获取子部门列表 |
|
|
|
* @param {number} parentId - 父部门 ID |
|
|
|
* 根据父组织机构 ID 获取子组织机构列表 |
|
|
|
* @param {number} parentId - 父组织机构 ID |
|
|
|
* @returns {Promise<Array>} |
|
|
|
*/ |
|
|
|
getChildrenByParentId:async function (parentId = null) { |
|
|
|
@ -284,7 +311,7 @@ const Dept = { |
|
|
|
}); |
|
|
|
return children; |
|
|
|
} catch (error) { |
|
|
|
console.error("获取子部门列表失败:", error); |
|
|
|
console.error("获取子组织机构列表失败:", error); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
}, |
|
|
|
|