function createDeptSearchState() {
|
return {
|
condition: ''
|
}
|
}
|
|
function createDeptFormState() {
|
return {
|
id: null,
|
parentId: 0,
|
name: '',
|
fullName: '',
|
leader: '',
|
sort: 0,
|
status: 1,
|
memo: ''
|
}
|
}
|
|
function normalizeDeptTreeRows(records = []) {
|
if (!Array.isArray(records)) {
|
return []
|
}
|
|
return records.map((item) => {
|
const id = normalizeDeptId(item?.id)
|
const children = normalizeDeptTreeRows(item?.children || [])
|
return {
|
...item,
|
id,
|
parentId: normalizeDeptId(item?.parentId),
|
name: item?.name || '',
|
fullName: item?.fullName || '-',
|
leader: item?.leader || '-',
|
sort: normalizeDeptNumber(item?.sort, 0),
|
status: normalizeDeptNumber(item?.status, 1),
|
statusBool: normalizeDeptNumber(item?.status, 1) === 1,
|
statusTextKey: normalizeDeptNumber(item?.status, 1) === 1 ? 'common.status.normal' : 'common.status.disabled',
|
statusText: normalizeDeptNumber(item?.status, 1) === 1 ? 'Normal' : 'Disabled',
|
statusType: normalizeDeptNumber(item?.status, 1) === 1 ? 'success' : 'danger',
|
updateTimeText: item?.updateTime$ || item?.updateTime || '-',
|
createTimeText: item?.createTime$ || item?.createTime || '-',
|
memo: item?.memo || '-',
|
children
|
}
|
})
|
}
|
|
function buildDeptTreeOptions(records = [], currentId = null) {
|
const normalizedCurrentId = normalizeDeptId(currentId)
|
|
return normalizeDeptTreeRows(records)
|
.filter((item) => item.id !== normalizedCurrentId)
|
.map((item) => mapDeptTreeOption(item, normalizedCurrentId))
|
.filter(Boolean)
|
}
|
|
function mapDeptTreeOption(item, currentId) {
|
if (!item || item.id === currentId) {
|
return null
|
}
|
|
const children = Array.isArray(item.children)
|
? item.children.map((child) => mapDeptTreeOption(child, currentId)).filter(Boolean)
|
: []
|
|
return {
|
value: item.id,
|
label: item.name || '-',
|
children
|
}
|
}
|
|
function buildDeptSearchParams(params = {}) {
|
return {
|
...(params.condition !== undefined ? { condition: String(params.condition || '').trim() } : {})
|
}
|
}
|
|
function buildDeptDialogModel(record = {}) {
|
return {
|
id: normalizeDeptNullableId(record?.id),
|
parentId: normalizeDeptId(record?.parentId),
|
name: record?.name || '',
|
fullName: record?.fullName || '',
|
leader: record?.leader || '',
|
sort: normalizeDeptNumber(record?.sort, 0),
|
status: normalizeDeptNumber(record?.status, 1),
|
memo: record?.memo || ''
|
}
|
}
|
|
function buildDeptSavePayload(form = {}) {
|
return {
|
...(form.id ? { id: normalizeDeptId(form.id) } : {}),
|
parentId: normalizeDeptId(form.parentId),
|
name: String(form.name || '').trim(),
|
fullName: String(form.fullName || '').trim(),
|
leader: String(form.leader || '').trim(),
|
sort: normalizeDeptNumber(form.sort, 0),
|
status: normalizeDeptNumber(form.status, 1),
|
memo: String(form.memo || '').trim()
|
}
|
}
|
|
function normalizeDeptId(value) {
|
if (value === null || value === undefined || value === '') {
|
return 0
|
}
|
const normalized = Number(value)
|
return Number.isFinite(normalized) ? normalized : 0
|
}
|
|
function normalizeDeptNullableId(value) {
|
if (value === null || value === undefined || value === '') {
|
return null
|
}
|
const normalized = Number(value)
|
return Number.isFinite(normalized) ? normalized : null
|
}
|
|
function normalizeDeptNumber(value, fallback = 0) {
|
if (value === null || value === undefined || value === '') {
|
return fallback
|
}
|
const normalized = Number(value)
|
return Number.isFinite(normalized) ? normalized : fallback
|
}
|
|
export {
|
buildDeptDialogModel,
|
buildDeptSavePayload,
|
buildDeptSearchParams,
|
buildDeptTreeOptions,
|
createDeptFormState,
|
createDeptSearchState,
|
normalizeDeptTreeRows
|
}
|