61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
import type { InventoryScopeEntity } from '@/types'
|
||
|
||
type InventoryScopeLike = {
|
||
scope_departments?: InventoryScopeEntity[] | null
|
||
scope_asset_types?: InventoryScopeEntity[] | null
|
||
scope_storage_locations?: InventoryScopeEntity[] | null
|
||
scope_department_name?: string | null
|
||
scope_asset_type_name?: string | null
|
||
}
|
||
|
||
const normalizeScopeEntities = (
|
||
values?: InventoryScopeEntity[] | null,
|
||
fallbackName?: string | null
|
||
) => {
|
||
if (Array.isArray(values) && values.length > 0) {
|
||
return values
|
||
}
|
||
|
||
if (fallbackName) {
|
||
return [
|
||
{
|
||
id: 0,
|
||
name: fallbackName
|
||
}
|
||
]
|
||
}
|
||
|
||
return []
|
||
}
|
||
|
||
const formatScopeSegment = (label: string, values: InventoryScopeEntity[]) => {
|
||
if (values.length === 0) {
|
||
return ''
|
||
}
|
||
|
||
if (values.length === 1) {
|
||
return `${label}:${values[0].name}`
|
||
}
|
||
|
||
return `${label}:已选 ${values.length} 项`
|
||
}
|
||
|
||
export const buildInventoryScopeLabel = (scope: InventoryScopeLike) => {
|
||
const segments = [
|
||
formatScopeSegment(
|
||
'部门',
|
||
normalizeScopeEntities(scope.scope_departments, scope.scope_department_name)
|
||
),
|
||
formatScopeSegment(
|
||
'资产类型',
|
||
normalizeScopeEntities(scope.scope_asset_types, scope.scope_asset_type_name)
|
||
),
|
||
formatScopeSegment(
|
||
'地区',
|
||
normalizeScopeEntities(scope.scope_storage_locations)
|
||
)
|
||
].filter(Boolean)
|
||
|
||
return segments.length > 0 ? segments.join(' / ') : '全量资产范围'
|
||
}
|