Files
tms-erp-web/src/utils/dept.js
T

40 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 业务部门下拉规范化。
* rawLabel 保留原始部门名供落库;展示名优先追加所属公司,缺省时用上级组织。
*
* @param {Array} list 业务部门原始列表
* @param {{ alwaysShowCompany?: boolean }} [options]
* alwaysShowCompany=true 时始终展示「部门(公司)」;默认仅同名时追加
*/
export function normalizeBusinessDeptOptions(list, options = {}) {
const alwaysShowCompany = !!options.alwaysShowCompany;
const source = Array.isArray(list) ? list : [];
const nameCount = {};
source.forEach(item => {
const name = String(item?.deptName || item?.fullName || '').trim();
if (!name) return;
nameCount[name] = (nameCount[name] || 0) + 1;
});
return source
.map(item => {
const id = item?.id == null || item?.id === '' ? '' : String(item.id);
if (!id) return null;
const rawLabel = item.deptName || item.fullName || id;
const companyName = String(item.belongCompanyName || item.parentName || '').trim();
const label =
companyName && (alwaysShowCompany || nameCount[rawLabel] > 1)
? `${rawLabel}(${companyName})`
: rawLabel;
return {
id,
value: id,
label,
rawLabel,
parentName: String(item.parentName || '').trim(),
belongCompanyName: String(item.belongCompanyName || '').trim(),
kingdeeDeptCode: item.kingdeeDeptCode || '',
};
})
.filter(Boolean);
}