Browse Source

部门

master
ma-zhongxu 10 months ago
parent
commit
c5f48e1317
  1. 2
      frontend/src/locales/zh/common.js
  2. 11
      frontend/src/models/admin.js
  3. 66
      frontend/src/pages/Admin/Section/index.jsx
  4. 4
      frontend/src/pages/WorkspaceSettings/Members/AddMemberModal/index.jsx
  5. 2
      server/endpoints/admin.js
  6. 64
      server/endpoints/dept.js
  7. 26
      server/endpoints/deptDocument.js
  8. 38
      server/endpoints/deptUsers.js
  9. 2
      server/endpoints/workspaces.js
  10. 87
      server/models/dept.js
  11. 18
      server/models/deptDocument.js
  12. 12
      server/models/deptUsers.js

2
frontend/src/locales/zh/common.js

@ -71,7 +71,7 @@ const TRANSLATIONS = {
system: "系统",
invites: "邀请",
users: "用户",
section:"部门",
section:"组织机构",
workspaces: "文件分析区",
"workspace-chats": "对话历史记录",
customization: "外观",

11
frontend/src/models/admin.js

@ -282,6 +282,17 @@ const Admin = {
return { depts: null, error: e.message };
});
},
deleteDept: async (deptId) => {
return await fetch(`${API_BASE}/dept/${deptId}`, {
method: "DELETE",
headers: baseHeaders(),
})
.then((res) => res.json())
.catch((e) => {
console.error(e);
return { success: false, error: e.message };
});
},
};
export default Admin;

66
frontend/src/pages/Admin/Section/index.jsx

@ -26,11 +26,11 @@ export default function AdminDepartments() {
<div className="w-full flex flex-col gap-y-1 pb-6 border-white/10 border-b-2">
<div className="items-center flex gap-x-4">
<p className="text-lg leading-6 font-bold text-theme-text-primary">
部门管理
组织机构管理
</p>
</div>
<p className="text-xs leading-[18px] font-base text-theme-text-secondary">
管理所有部门及其层级结构删除部门将同时删除其子部门
管理所有组织机构及其层级结构删除组织机构将同时删除其子组织机构
</p>
</div>
<div className="w-full justify-end flex">
@ -38,7 +38,7 @@ export default function AdminDepartments() {
onClick={openModal}
className="mt-3 mr-0 mb-4 md:-mb-6 z-10"
>
<FolderOpen className="h-4 w-4" weight="bold" /> 添加部门
<FolderOpen className="h-4 w-4" weight="bold" /> 添加组织机构
</CTAButton>
</div>
<div className="overflow-x-auto">
@ -62,7 +62,7 @@ function DepartmentsContainer() {
async function fetchDepartments() {
const _departments = await Admin.depts();
console.log(1111,_departments);
setDepartments(buildTree(_departments)); //
setDepartments(buildTree(_departments)); //
setLoading(false);
}
fetchDepartments();
@ -87,7 +87,7 @@ function DepartmentsContainer() {
<thead className="text-theme-text-secondary text-xs leading-[18px] font-bold uppercase border-white/10 border-b">
<tr>
<th scope="col" className="px-6 py-3 rounded-tl-lg">
部门名称
组织机构名称
</th>
<th scope="col" className="px-6 py-3">
排序
@ -126,10 +126,18 @@ function DepartmentRow({ dept }) {
};
const handleSuccess = () => {
//
//
window.location.reload(); //
};
const handleDelete = async (deptId) => {
const confirmDelete = window.confirm("确定要删除该组织机构吗?");
if (confirmDelete) {
await Admin.deleteDept(deptId); // API
window.location.reload(); //
}
};
return (
<>
<tr className="border-b border-white/10">
@ -199,32 +207,32 @@ function DepartmentRow({ dept }) {
function NewDepartmentModal({ closeModal }) {
const [formData, setFormData] = useState({
deptName: "",
parentId: null, // ID
parentId: null, // ID
orderNum: 0,
status: 0,
});
const [departments, setDepartments] = useState([]); //
const [departments, setDepartments] = useState([]); //
const [treeData, setTreeData] = useState([]); //
//
//
useEffect(() => {
async function fetchDepartments() {
const _departments = await Admin.depts();
setDepartments(_departments);
setTreeData(buildTree(_departments)); //
setTreeData(buildTree(_departments)); //
}
fetchDepartments();
}, []);
//
//
function buildTree(depts, parentId = 0) {
return depts
.filter((dept) => dept.parentId === parentId)
.map((dept) => ({
title: dept.deptName, //
value: dept.deptId, // ID
children: buildTree(depts, dept.deptId), //
title: dept.deptName, //
value: dept.deptId, // ID
children: buildTree(depts, dept.deptId), //
}));
}
@ -237,30 +245,30 @@ function NewDepartmentModal({ closeModal }) {
return (
<div className="p-6">
<h2 className="text-lg font-bold text-theme-text-primary mb-4">
添加部门
添加组织机构
</h2>
<div className="flex flex-col gap-y-4">
{/* 上级部门选择器 */}
{/* 上级组织机构选择器 */}
<div>
<label className="text-sm font-medium text-theme-text-secondary block mb-2">
上级部门
上级组织机构
</label>
<TreeSelect
treeData={treeData} //
value={formData.parentId} // ID
value={formData.parentId} // ID
onChange={(value) =>
setFormData({ ...formData, parentId: value })
}
placeholder="请选择上级部门"
placeholder="请选择上级组织机构"
className="w-full"
dropdownStyle={{ maxHeight: 400, overflow: "auto" }}
/>
</div>
{/* 部门名称输入框 */}
{/* 组织机构名称输入框 */}
<input
type="text"
placeholder="部门名称"
placeholder="组织机构名称"
value={formData.deptName}
onChange={(e) =>
setFormData({ ...formData, deptName: e.target.value })
@ -328,34 +336,34 @@ function EditDepartmentModal({ dept, closeModal, onSuccess }) {
return depts
.filter((dept) => dept.parentId === parentId)
.map((dept) => ({
title: dept.deptName, // title
value: dept.deptId, // ID
children: buildTree(depts, dept.deptId), //
title: dept.deptName, // title
value: dept.deptId, // ID
children: buildTree(depts, dept.deptId), //
}));
}
return (
<div className="p-6">
<h2 className="text-lg font-bold text-theme-text-primary mb-4">
编辑部门
编辑组织机构
</h2>
<div className="flex flex-col gap-y-4">
<div>
<label className="text-sm font-medium text-theme-text-secondary block mb-2">
上级部门
上级组织机构
</label>
<TreeSelect
treeData={treeData}
value={formData.parentId}
onChange={(value) => setFormData({ ...formData, parentId: value })}
placeholder="请选择上级部门"
placeholder="请选择上级组织机构"
className="w-full"
dropdownStyle={{ maxHeight: 400, overflow: "auto" }}
/>
</div>
<input
type="text"
placeholder="部门名称"
placeholder="组织机构名称"
value={formData.deptName}
onChange={(e) => setFormData({ ...formData, deptName: e.target.value })}
className="border-none bg-theme-settings-input-bg text-white placeholder:text-theme-settings-input-placeholder text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5"
@ -381,7 +389,7 @@ function EditDepartmentModal({ dept, closeModal, onSuccess }) {
);
}
//
//
function buildTree(departments, parentId = 0) {
return departments
.filter((dept) => dept.parentId === parentId)

4
frontend/src/pages/WorkspaceSettings/Members/AddMemberModal/index.jsx

@ -56,8 +56,8 @@ export default function AddMemberModal({ closeModal, workspace, users }) {
.filter((user) =>
user.username.toLowerCase().includes(searchTerm.toLowerCase())
)
.filter((user) => user.role !== "admin")
.filter((user) => user.role !== "manager");
.filter((user) => user.role !== "admin");
// .filter((user) => user.role !== "manager");
return (
<div className="relative w-full max-w-[550px] max-h-full">

2
server/endpoints/admin.js

@ -104,7 +104,7 @@ function adminEndpoints(app) {
// data: newUserParams,
// });
//
// // 2. 将用户和部门关联到 dept_users 表
// // 2. 将用户和组织机构关联到 dept_users 表
// if (newUserParams.deptId) {
// await prisma.dept_users.create({
// data: {

64
server/endpoints/dept.js

@ -13,7 +13,7 @@ function deptEndpoints(app) {
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
async (_request, response) => {
try {
const depts = await Dept.where();
const depts = await Dept.where({ delFlag: 0 });
response.status(200).json({ depts });
} catch (e) {
console.error(e);
@ -21,7 +21,7 @@ function deptEndpoints(app) {
}
}
);
// 获取部门树状结构
// 获取组织机构树状结构
app.get(
"/dept/tree",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
@ -36,7 +36,7 @@ function deptEndpoints(app) {
}
);
// 懒加载子部门列表
// 懒加载子组织机构列表
app.get(
"/dept/children",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
@ -58,20 +58,20 @@ function deptEndpoints(app) {
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
async (request, response) => {
try {
const dept = reqBody(request); // 获取请求体中的部门数据
const dept = reqBody(request); // 获取请求体中的组织机构数据
console.log("dept 类型:", typeof dept);
// 检查部门名称是否唯一
// 检查组织机构名称是否唯一
const isUnique = await Dept.checkDeptNameUnique(dept);
if (!isUnique) {
return response.status(400).json({
success: false,
message: `新增部门 '${dept.deptName}' 失败,部门名称已存在`,
message: `新增组织机构 '${dept.deptName}' 失败,组织机构名称已存在`,
});
};
// 按照deptId查询父部门
// 按照deptId查询父组织机构
const parentDept = await Dept.get({ deptId: dept.parentId });
dept.ancestors = parentDept.dept.ancestors + ',' + dept.parentId;
// 插入部门数据
// 插入组织机构数据
const insertedDept = await Dept.insertDept(dept);
// 返回成功响应
response.status(200).json({
@ -80,10 +80,10 @@ function deptEndpoints(app) {
});
} catch (error) {
// 处理错误
console.error("添加部门失败:", error);
console.error("添加组织机构失败:", error);
response.status(500).json({
success: false,
message: "添加部门失败,服务器内部错误",
message: "添加组织机构失败,服务器内部错误",
});
}
});
@ -93,37 +93,37 @@ function deptEndpoints(app) {
async (request, response) => {
try {
const deptId = parseInt(request.params.deptId);
const dept = reqBody(request); // 获取请求体中的部门数据
const dept = reqBody(request); // 获取请求体中的组织机构数据
// 检查部门名称是否唯一
// 检查组织机构名称是否唯一
const isUnique = await Dept.checkDeptNameUnique(dept);
console.log("isUnique:", isUnique);
if (!isUnique) {
return response.status(400).json({
success: false,
message: `修改部门 '${dept.deptName}' 失败,部门名称已存在`,
message: `修改组织机构 '${dept.deptName}' 失败,组织机构名称已存在`,
});
}
// 检查上级部门是否是自己
// 检查上级组织机构是否是自己
if (dept.parentId === deptId) {
return response.status(400).json({
success: false,
message: `修改部门 '${dept.deptName}' 失败,上级部门不能是自己`,
message: `修改组织机构 '${dept.deptName}' 失败,上级组织机构不能是自己`,
});
}
// 检查部门是否包含未停用的子部门
// 检查组织机构是否包含未停用的子组织机构
if (dept.status === 1) {
const normalChildrenCount = await Dept.selectNormalChildrenDeptById(deptId);
if (normalChildrenCount > 0) {
return response.status(400).json({
success: false,
message: "该部门包含未停用的子部门!",
message: "该组织机构包含未停用的子组织机构!",
});
}
}
// 更新部门数据
// 更新组织机构数据
const updatedDept = await Dept.update(deptId, dept);
// 返回成功响应
response.status(200).json({
@ -132,50 +132,50 @@ function deptEndpoints(app) {
});
} catch (error) {
// 处理错误
console.error("修改部门失败:", error);
console.error("修改组织机构失败:", error);
response.status(500).json({
success: false,
message: "修改部门失败,服务器内部错误",
message: "修改组织机构失败,服务器内部错误",
});
}
});
// 删除部门的接口
// 删除组织机构的接口
app.delete("/dept/:deptId",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
async (request, response) => {
try {
const deptId = parseInt(request.params.deptId); // 获取部门 ID
const deptId = parseInt(request.params.deptId); // 获取组织机构 ID
// 检查部门是否有子部门
// 检查组织机构是否有子组织机构
const hasChild = await Dept.hasChildByDeptId(deptId);
if (hasChild) {
return response.status(400).json({
success: false,
message: "存在下级部门,不允许删除",
message: "存在下级组织机构,不允许删除",
});
}
// 检查部门是否存在用户
// 检查组织机构是否存在用户
const hasUser = await Dept.checkDeptExistUser(deptId);
if (hasUser) {
return response.status(400).json({
success: false,
message: "部门存在用户,不允许删除",
message: "组织机构存在用户,不允许删除",
});
}
// // 检查部门数据权限
// // 检查组织机构数据权限
// const hasDataScope = await Dept.checkDeptDataScope(deptId);
// if (!hasDataScope) {
// return response.status(403).json({
// success: false,
// message: "无权限删除该部门",
// message: "无权限删除该组织机构",
// });
// }
// 删除部门
const deletedDept = await Dept.deleteDeptById(deptId);
// 删除组织机构
const deletedDept = await Dept.softDelete(deptId);
// 返回成功响应
response.status(200).json({
@ -184,10 +184,10 @@ function deptEndpoints(app) {
});
} catch (error) {
// 处理错误
console.error("删除部门失败:", error);
console.error("删除组织机构失败:", error);
response.status(500).json({
success: false,
message: "删除部门失败,服务器内部错误",
message: "删除组织机构失败,服务器内部错误",
});
}
});

26
server/endpoints/deptDocument.js

@ -8,7 +8,7 @@ const {
function deptDocumentEndpoints(app) {
if (!app) return;
// 获取部门文档列表
// 获取组织机构文档列表
app.get(
"/deptDocument/list",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
@ -23,7 +23,7 @@ function deptDocumentEndpoints(app) {
}
);
// 添加部门文档
// 添加组织机构文档
app.post(
"/deptDocument/add",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
@ -47,7 +47,7 @@ function deptDocumentEndpoints(app) {
if (error) {
return response.status(500).json({
success: false,
message: "添加部门文档失败",
message: "添加组织机构文档失败",
error: error,
});
}
@ -58,16 +58,16 @@ function deptDocumentEndpoints(app) {
data: deptDocument,
});
} catch (error) {
console.error("添加部门文档失败:", error);
console.error("添加组织机构文档失败:", error);
response.status(500).json({
success: false,
message: "添加部门文档失败,服务器内部错误",
message: "添加组织机构文档失败,服务器内部错误",
});
}
}
);
// 编辑部门文档
// 编辑组织机构文档
app.post(
"/deptDocument/edit",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
@ -92,7 +92,7 @@ function deptDocumentEndpoints(app) {
if (!success) {
return response.status(500).json({
success: false,
message: "编辑部门文档失败",
message: "编辑组织机构文档失败",
error: error,
});
}
@ -103,16 +103,16 @@ function deptDocumentEndpoints(app) {
data: deptDocument,
});
} catch (error) {
console.error("编辑部门文档失败:", error);
console.error("编辑组织机构文档失败:", error);
response.status(500).json({
success: false,
message: "编辑部门文档失败,服务器内部错误",
message: "编辑组织机构文档失败,服务器内部错误",
});
}
}
);
// 删除部门文档
// 删除组织机构文档
app.delete(
"/deptDocument/:id",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
@ -134,7 +134,7 @@ function deptDocumentEndpoints(app) {
if (!success) {
return response.status(500).json({
success: false,
message: "删除部门文档失败",
message: "删除组织机构文档失败",
});
}
@ -144,10 +144,10 @@ function deptDocumentEndpoints(app) {
message: "文档删除成功",
});
} catch (error) {
console.error("删除部门文档失败:", error);
console.error("删除组织机构文档失败:", error);
response.status(500).json({
success: false,
message: "删除部门文档失败,服务器内部错误",
message: "删除组织机构文档失败,服务器内部错误",
});
}
}

38
server/endpoints/deptUsers.js

@ -8,7 +8,7 @@ const {
function deptUsersEndpoints(app) {
if (!app) return;
// 获取部门用户关联列表
// 获取组织机构用户关联列表
app.get(
"/deptUsers/list",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
@ -23,7 +23,7 @@ function deptUsersEndpoints(app) {
}
);
// 添加部门用户关联
// 添加组织机构用户关联
app.post(
"/deptUsers/add",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
@ -31,12 +31,12 @@ function deptUsersEndpoints(app) {
try {
const deptUserData = request.body; // 获取请求体中的数据
// 插入部门用户关联数据
// 插入组织机构用户关联数据
const { deptUser, error } = await DeptUsers.create(deptUserData);
if (error) {
return response.status(500).json({
success: false,
message: "添加部门用户关联失败",
message: "添加组织机构用户关联失败",
error: error,
});
}
@ -47,16 +47,16 @@ function deptUsersEndpoints(app) {
data: deptUser,
});
} catch (error) {
console.error("添加部门用户关联失败:", error);
console.error("添加组织机构用户关联失败:", error);
response.status(500).json({
success: false,
message: "添加部门用户关联失败,服务器内部错误",
message: "添加组织机构用户关联失败,服务器内部错误",
});
}
}
);
// 编辑部门用户关联
// 编辑组织机构用户关联
app.post(
"/deptUsers/edit",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
@ -69,11 +69,11 @@ function deptUsersEndpoints(app) {
if (!existingDeptUser) {
return response.status(404).json({
success: false,
message: "部门用户关联不存在",
message: "组织机构用户关联不存在",
});
}
// 更新部门用户关联
// 更新组织机构用户关联
const { success, error, deptUser } = await DeptUsers.update(
deptUserData.id,
deptUserData
@ -81,7 +81,7 @@ function deptUsersEndpoints(app) {
if (!success) {
return response.status(500).json({
success: false,
message: "编辑部门用户关联失败",
message: "编辑组织机构用户关联失败",
error: error,
});
}
@ -92,16 +92,16 @@ function deptUsersEndpoints(app) {
data: deptUser,
});
} catch (error) {
console.error("编辑部门用户关联失败:", error);
console.error("编辑组织机构用户关联失败:", error);
response.status(500).json({
success: false,
message: "编辑部门用户关联失败,服务器内部错误",
message: "编辑组织机构用户关联失败,服务器内部错误",
});
}
}
);
// 删除部门用户关联
// 删除组织机构用户关联
app.delete(
"/deptUsers/:id",
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
@ -114,29 +114,29 @@ function deptUsersEndpoints(app) {
if (!existingDeptUser) {
return response.status(404).json({
success: false,
message: "部门用户关联不存在",
message: "组织机构用户关联不存在",
});
}
// 删除部门用户关联
// 删除组织机构用户关联
const success = await DeptUsers.delete({ id });
if (!success) {
return response.status(500).json({
success: false,
message: "删除部门用户关联失败",
message: "删除组织机构用户关联失败",
});
}
// 返回成功响应
response.status(200).json({
success: true,
message: "部门用户关联删除成功",
message: "组织机构用户关联删除成功",
});
} catch (error) {
console.error("删除部门用户关联失败:", error);
console.error("删除组织机构用户关联失败:", error);
response.status(500).json({
success: false,
message: "删除部门用户关联失败,服务器内部错误",
message: "删除组织机构用户关联失败,服务器内部错误",
});
}
}

2
server/endpoints/workspaces.js

@ -174,7 +174,7 @@ function workspaceEndpoints(app) {
const user = await userFromSession(request, response);
const deptUserRecord = await DeptUsers.get({ userId: user.id });
if (!deptUserRecord.deptUser) {
return response.status(500).json({ success: false, error: "没有发现用户部门" });
return response.status(500).json({ success: false, error: "没有发现用户组织机构" });
}
const Collector = new CollectorApi();
const { originalname } = request.file;

87
server/models/dept.js

@ -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;
}
},

18
server/models/deptDocument.js

@ -61,8 +61,8 @@ const DeptDocument = {
},
/**
* 创建部门文档
* @param {Object} data - 部门文档数据
* 创建组织机构文档
* @param {Object} data - 组织机构文档数据
* @returns {Promise<{ deptDocument: DeptDocument | null, error: string | null }>}
*/
create: async function (data) {
@ -94,7 +94,7 @@ const DeptDocument = {
},
/**
* 更新部门文档
* 更新组织机构文档
* @param {number} id - 文档 ID
* @param {Object} updates - 更新的字段
* @returns {Promise<{ success: boolean, error: string | null, deptDocument: DeptDocument | null }>}
@ -134,7 +134,7 @@ const DeptDocument = {
},
/**
* 获取部门文档
* 获取组织机构文档
* @param {Object} clause - 查询条件
* @returns {Promise<{ deptDocument: DeptDocument | null }>}
*/
@ -151,7 +151,7 @@ const DeptDocument = {
},
/**
* 删除部门文档
* 删除组织机构文档
* @param {Object} clause - 删除条件
* @returns {Promise<boolean>}
*/
@ -168,7 +168,7 @@ const DeptDocument = {
},
/**
* 查询部门文档列表
* 查询组织机构文档列表
* @param {Object} clause - 查询条件
* @param {number} limit - 限制数量
* @returns {Promise<DeptDocument[]>}
@ -204,9 +204,9 @@ const DeptDocument = {
},
/**
* 检查文档是否属于指定部门
* 检查文档是否属于指定组织机构
* @param {number} id - 文档 ID
* @param {number} deptId - 部门 ID
* @param {number} deptId - 组织机构 ID
* @returns {Promise<boolean>}
*/
checkDocumentBelongsToDept: async function (id, deptId) {
@ -216,7 +216,7 @@ const DeptDocument = {
});
return !!document;
} catch (error) {
console.error("检查文档所属部门失败:", error);
console.error("检查文档所属组织机构失败:", error);
throw error;
}
},

12
server/models/deptUsers.js

@ -38,8 +38,8 @@ const DeptUsers = {
},
/**
* 创建部门用户关联
* @param {Object} data - 部门用户数据
* 创建组织机构用户关联
* @param {Object} data - 组织机构用户数据
* @returns {Promise<{ deptUser: DeptUser | null, error: string | null }>}
*/
create: async function (data) {
@ -71,7 +71,7 @@ const DeptUsers = {
},
/**
* 更新部门用户关联
* 更新组织机构用户关联
* @param {number} id - 关联 ID
* @param {Object} updates - 更新的字段
* @returns {Promise<{ success: boolean, error: string | null, deptUser: DeptUser | null }>}
@ -111,7 +111,7 @@ const DeptUsers = {
},
/**
* 获取部门用户关联
* 获取组织机构用户关联
* @param {Object} clause - 查询条件
* @returns {Promise<{ deptUser: DeptUser | null }>}
*/
@ -128,7 +128,7 @@ const DeptUsers = {
},
/**
* 删除部门用户关联
* 删除组织机构用户关联
* @param {Object} clause - 删除条件
* @returns {Promise<boolean>}
*/
@ -145,7 +145,7 @@ const DeptUsers = {
},
/**
* 查询部门用户关联列表
* 查询组织机构用户关联列表
* @param {Object} clause - 查询条件
* @param {number} limit - 限制数量
* @returns {Promise<DeptUser[]>}

Loading…
Cancel
Save