zhou zhou
1 天以前 0a1d91e42e6c5af96e1108e9ebcc37e99eb3b22c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
}