You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
320 lines
9.7 KiB
320 lines
9.7 KiB
const prisma = require("../utils/prisma");
|
|
|
|
/**
|
|
* @typedef {Object} Dept
|
|
* @property {number} deptId
|
|
* @property {number} parentId
|
|
* @property {string} ancestors
|
|
* @property {string} deptName
|
|
* @property {number} orderNum
|
|
* @property {number} status
|
|
* @property {number} delFlag
|
|
* @property {Date} createdAt
|
|
* @property {Date} lastUpdatedAt
|
|
*/
|
|
|
|
const Dept = {
|
|
writable: [
|
|
'parentId', 'ancestors', 'deptName', 'orderNum', 'status', 'delFlag'
|
|
],
|
|
validations: {
|
|
deptName: (newValue = '') => {
|
|
if (typeof newValue !== 'string' || newValue.length > 255) {
|
|
throw new Error('Dept name must be a string and cannot be longer than 255 characters');
|
|
}
|
|
return newValue;
|
|
},
|
|
orderNum: (newValue = 0) => {
|
|
const num = Number(newValue);
|
|
if (isNaN(num)) {
|
|
throw new Error('Order num must be a number');
|
|
}
|
|
return num;
|
|
},
|
|
status: (newValue = 0) => {
|
|
const status = Number(newValue);
|
|
if (isNaN(status) || status < 0 || status > 1) {
|
|
throw new Error('Status must be a number between 0 and 1');
|
|
}
|
|
return status;
|
|
},
|
|
delFlag: (newValue = 0) => {
|
|
const flag = Number(newValue);
|
|
if (isNaN(flag) || flag < 0 || flag > 1) {
|
|
throw new Error('Del flag must be a number between 0 and 1');
|
|
}
|
|
return flag;
|
|
}
|
|
},
|
|
castColumnValue: function (key, value) {
|
|
switch (key) {
|
|
case 'status':
|
|
case 'delFlag':
|
|
return Number(value);
|
|
default:
|
|
return value;
|
|
}
|
|
},
|
|
create: async function ({ parentId, ancestors, deptName, orderNum, status = 0, delFlag = 0 }) {
|
|
try {
|
|
const validatedDeptName = this.validations.deptName(deptName);
|
|
const validatedOrderNum = this.validations.orderNum(orderNum);
|
|
const validatedStatus = this.validations.status(status);
|
|
const validatedDelFlag = this.validations.delFlag(delFlag);
|
|
|
|
const dept = await prisma.dept.create({
|
|
data: {
|
|
parentId,
|
|
ancestors,
|
|
deptName: validatedDeptName,
|
|
orderNum: validatedOrderNum,
|
|
status: validatedStatus,
|
|
delFlag: validatedDelFlag,
|
|
createdAt: new Date(),
|
|
lastUpdatedAt: new Date()
|
|
}
|
|
});
|
|
|
|
return { dept, error: null };
|
|
} catch (error) {
|
|
console.error('FAILED TO CREATE DEPT.', error.message);
|
|
return { dept: null, error: error.message };
|
|
}
|
|
},
|
|
// 插入组织机构数据
|
|
insertDept: async function (dept) {
|
|
try {
|
|
const insertedDept = await prisma.dept.create({
|
|
data: {
|
|
deptName: dept.deptName,
|
|
parentId: dept.parentId || null,
|
|
ancestors: dept.ancestors || null,
|
|
orderNum: dept.orderNum || 0,
|
|
status: dept.status || 0,
|
|
delFlag: dept.delFlag || 0,
|
|
createdAt: new Date(),
|
|
lastUpdatedAt: new Date(),
|
|
},
|
|
});
|
|
return insertedDept;
|
|
} catch (error) {
|
|
console.error("插入组织机构数据失败:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
update: async function (deptId, updates = {}) {
|
|
console.log("更新组织机构数据:", updates);
|
|
try {
|
|
// 检查 deptId 是否存在
|
|
if (!deptId) throw new Error('没有提供用于查询的deptId');
|
|
|
|
// 检查 updates 是否为空
|
|
if (Object.keys(updates).length === 0) throw new Error('没有提供更新字段');
|
|
|
|
// 查询当前组织机构
|
|
const currentDept = await prisma.dept.findUnique({ where: { deptId } });
|
|
if (!currentDept) throw new Error('不存在该组织机构');
|
|
|
|
// 更新 lastUpdatedAt 字段
|
|
updates.lastUpdatedAt = new Date();
|
|
|
|
// 如果 parentId 发生变化,更新 ancestors 字段
|
|
if (updates.parentId !== undefined && updates.parentId !== currentDept.parentId) {
|
|
const parentId = updates.parentId;
|
|
const parentDept = await prisma.dept.findUnique({ where: { deptId: parentId } });
|
|
updates.ancestors = parentDept ? `${parentDept.ancestors},${parentId}` : `${parentId}`;
|
|
}
|
|
|
|
console.log("更新前组织机构数据:", updates);
|
|
|
|
// 执行更新操作
|
|
const updatedDept = await prisma.dept.update({
|
|
where: { deptId }, // 使用 deptId 作为查询条件
|
|
data: updates, // 更新的字段
|
|
});
|
|
|
|
console.log("更新后组织机构数据:", updatedDept);
|
|
|
|
// 返回成功结果
|
|
return { success: true, error: null, dept: updatedDept };
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return { success: false, error: error.message, dept: null };
|
|
}
|
|
},
|
|
get: async function (clause = {}) {
|
|
try {
|
|
const dept = await prisma.dept.findFirst({ where: clause, select: {
|
|
deptId: true, parentId: true, ancestors: true, deptName: true, orderNum: true, status: true, delFlag: true, createdAt: true, lastUpdatedAt: true
|
|
} });
|
|
return dept ? { dept } : null;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return null;
|
|
}
|
|
},
|
|
delete: async function (clause = {}) {
|
|
try {
|
|
const affectedRows = await prisma.dept.deleteMany({ where: clause });
|
|
return affectedRows > 0;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
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({
|
|
where: clause,
|
|
take: limit !== null ? limit : undefined,
|
|
select: {
|
|
deptId: true, parentId: true, ancestors: true, deptName: true, orderNum: true, status: true, delFlag: true, createdAt: true, lastUpdatedAt: true
|
|
}
|
|
});
|
|
return depts;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return [];
|
|
}
|
|
},
|
|
checkDeptNameUnique: async function (dept){
|
|
try {
|
|
const existingDept = await prisma.dept.findFirst({
|
|
where: {
|
|
deptName: dept.deptName, // 根据组织机构名称查询
|
|
parentId: dept.parentId, // 排除父id
|
|
},
|
|
});
|
|
|
|
// 如果查询到记录,说明组织机构名称已存在
|
|
console.log("existingDept:", existingDept);
|
|
return !existingDept;
|
|
} catch (error) {
|
|
console.error('检查组织机构名称唯一性失败:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
// 检查组织机构是否包含未停用的子组织机构
|
|
selectNormalChildrenDeptById: async function (deptId) {
|
|
try {
|
|
// 查询所有祖先组织机构中包含当前组织机构的未停用组织机构
|
|
const childrenDepts = await prisma.$queryRaw`
|
|
SELECT COUNT(*) as count
|
|
FROM sys_dept
|
|
WHERE status = 0
|
|
AND del_flag = '0'
|
|
AND FIND_IN_SET(${deptId}, ancestors)
|
|
`;
|
|
|
|
// 返回未停用的子组织机构数量
|
|
return childrenDepts[0].count;
|
|
} catch (error) {
|
|
console.error("查询子组织机构失败:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
// 检查组织机构是否有子组织机构
|
|
hasChildByDeptId: async function (deptId) {
|
|
try {
|
|
const children = await prisma.dept.findMany({
|
|
where: {
|
|
parentId: deptId, // 查询当前组织机构的子组织机构
|
|
delFlag: 0,
|
|
},
|
|
});
|
|
|
|
// 如果有子组织机构,返回 true
|
|
return children.length > 0;
|
|
} catch (error) {
|
|
console.error("检查子组织机构失败:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
// 检查组织机构是否存在用户
|
|
checkDeptExistUser: async function (deptId) {
|
|
try {
|
|
const deptUsers = await prisma.dept_users.findMany({
|
|
where: {
|
|
deptId: deptId, // 查询当前组织机构的用户
|
|
},
|
|
});
|
|
|
|
// 如果存在用户,返回 true
|
|
return deptUsers.length > 0;
|
|
} catch (error) {
|
|
console.error("检查组织机构用户失败:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
/**
|
|
* 获取组织机构树状结构
|
|
* @returns {Promise<Array>}
|
|
*/
|
|
getDeptTree:async function () {
|
|
try {
|
|
// 查询所有组织机构
|
|
const allDepts = await prisma.dept.findMany();
|
|
|
|
// 构建树状结构
|
|
const buildTree = (parentId = null) => {
|
|
return allDepts
|
|
.filter((dept) => dept.parentId === parentId)
|
|
.map((dept) => ({
|
|
...dept,
|
|
children: buildTree(dept.deptId), // 递归获取子组织机构
|
|
}));
|
|
};
|
|
|
|
return buildTree();
|
|
} catch (error) {
|
|
console.error("获取组织机构树状结构失败:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
/**
|
|
* 根据父组织机构 ID 获取子组织机构列表
|
|
* @param {number} parentId - 父组织机构 ID
|
|
* @returns {Promise<Array>}
|
|
*/
|
|
getChildrenByParentId:async function (parentId = null) {
|
|
try {
|
|
const children = await prisma.dept.findMany({
|
|
where: { parentId },
|
|
});
|
|
return children;
|
|
} catch (error) {
|
|
console.error("获取子组织机构列表失败:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
};
|
|
|
|
module.exports = { Dept };
|