108 lines
3.6 KiB
JavaScript
108 lines
3.6 KiB
JavaScript
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
// 部门树构建工具:/api/org/dept 返回按 sort asc, id asc 排序的扁平列表,
|
||
// 这里按 parent_id 组装成 Semi Tree/TreeSelect 的 treeData。
|
||
|
||
/**
|
||
* buildDeptTreeData 扁平部门列表 → Semi treeData。
|
||
*
|
||
* @param {Array} depts 后端返回的部门列表(保持返回顺序即层内排序)
|
||
* @param {Object} options
|
||
* - disableNonLeaf 有子部门的节点不可选(成员主归属只允许挂末级部门)
|
||
* - excludeSubtreeOf 排除某部门自身及其整棵子树(编辑部门选上级时防环),
|
||
* 传含 id/path 的部门对象
|
||
* - disabledLabel 停用部门的后缀文案(外部传入以走 i18n)
|
||
*/
|
||
export function buildDeptTreeData(depts, options = {}) {
|
||
const {
|
||
disableNonLeaf = true,
|
||
excludeSubtreeOf = null,
|
||
disabledLabel = '已停用',
|
||
} = options;
|
||
const excluded = (d) =>
|
||
excludeSubtreeOf &&
|
||
(d.id === excludeSubtreeOf.id ||
|
||
(excludeSubtreeOf.path &&
|
||
d.path &&
|
||
d.path.startsWith(excludeSubtreeOf.path)));
|
||
const visible = (depts || []).filter((d) => !excluded(d));
|
||
|
||
const childCount = {};
|
||
visible.forEach((d) => {
|
||
childCount[d.parent_id] = (childCount[d.parent_id] || 0) + 1;
|
||
});
|
||
|
||
const idSet = new Set(visible.map((d) => d.id));
|
||
const nodeMap = {};
|
||
visible.forEach((d) => {
|
||
const stopped = d.status !== 1;
|
||
const nonLeafBlocked = disableNonLeaf && (childCount[d.id] || 0) > 0;
|
||
nodeMap[d.id] = {
|
||
label: stopped ? `${d.name}(${disabledLabel})` : d.name,
|
||
value: d.id,
|
||
key: String(d.id),
|
||
disabled: stopped || nonLeafBlocked,
|
||
children: [],
|
||
};
|
||
});
|
||
|
||
const roots = [];
|
||
visible.forEach((d) => {
|
||
const node = nodeMap[d.id];
|
||
if (d.parent_id && idSet.has(d.parent_id)) {
|
||
nodeMap[d.parent_id].children.push(node);
|
||
} else {
|
||
// 顶级部门;父节点缺失/被排除的孤儿也挂根兜底,避免整棵子树消失
|
||
roots.push(node);
|
||
}
|
||
});
|
||
// 叶子节点去掉空 children,避免渲染多余的展开箭头
|
||
Object.values(nodeMap).forEach((n) => {
|
||
if (n.children.length === 0) delete n.children;
|
||
});
|
||
return roots;
|
||
}
|
||
|
||
/**
|
||
* buildDeptFullNameMap 部门 id → 全路径名(如「华南事业部 / 研发中心 / 平台组」)。
|
||
* 基于 path 字段("/1/4/9/")解析祖先链;path 异常时退回自身名称。
|
||
*/
|
||
export function buildDeptFullNameMap(depts) {
|
||
const nameById = {};
|
||
(depts || []).forEach((d) => {
|
||
nameById[d.id] = d.name;
|
||
});
|
||
const map = {};
|
||
(depts || []).forEach((d) => {
|
||
const names = (d.path || '')
|
||
.split('/')
|
||
.filter(Boolean)
|
||
.map((seg) => nameById[Number(seg)])
|
||
.filter(Boolean);
|
||
map[d.id] = names.length ? names.join(' / ') : d.name;
|
||
});
|
||
return map;
|
||
}
|
||
|
||
/** hasChildDept 某部门当前是否存在子部门 */
|
||
export function hasChildDept(depts, deptId) {
|
||
return (depts || []).some((d) => d.parent_id === deptId);
|
||
}
|