zhou zhou
1 天以前 c579731e0c206d6062f8442ec9df70ca781b26f6
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import request from '@/utils/http'
 
function normalizeText(value) {
  return typeof value === 'string' ? value.trim() : value
}
 
function normalizeIds(ids) {
  if (Array.isArray(ids)) {
    return ids
      .map((id) => String(id).trim())
      .filter(Boolean)
      .join(',')
  }
  if (ids === null || ids === undefined) {
    return ''
  }
  return String(ids).trim()
}
 
function filterParams(params = {}, ignoredKeys = []) {
  return Object.fromEntries(
    Object.entries(params)
      .filter(([key, value]) => {
        if (ignoredKeys.includes(key)) return false
        if (value === undefined || value === null) return false
        if (typeof value === 'string' && value.trim() === '') return false
        return true
      })
      .map(([key, value]) => [key, normalizeText(value)])
  )
}
 
export function buildBasContainerPageParams(params = {}) {
  return {
    current: params.current || 1,
    pageSize: params.pageSize || params.size || 20,
    ...filterParams(params, ['current', 'pageSize', 'size'])
  }
}
 
export function buildBasContainerSavePayload(formData = {}) {
  const areas = normalizeBasContainerAreas(formData.areas)
  return {
    ...(formData.id !== undefined && formData.id !== null && formData.id !== ''
      ? { id: Number(formData.id) }
      : {}),
    code: normalizeText(formData.code) || '',
    containerType:
      formData.containerType !== undefined && formData.containerType !== null && formData.containerType !== ''
        ? Number(formData.containerType)
        : void 0,
    codeType: normalizeText(formData.codeType) || '',
    ...(areas.length ? { areas } : {}),
    status:
      formData.status !== undefined && formData.status !== null && formData.status !== ''
        ? Number(formData.status)
        : 1,
    memo: normalizeText(formData.memo) || ''
  }
}
 
export function normalizeBasContainerAreas(areas = []) {
  if (!Array.isArray(areas)) {
    return []
  }
 
  return areas
    .map((item, index) => {
      if (item === null || item === undefined) {
        return null
      }
 
      if (typeof item === 'object') {
        const id = normalizeNumberValue(item.id ?? item.areaId ?? item.value)
        if (id === void 0) {
          return null
        }
        const sort = normalizeSortValue(item.sort, index + 1)
        return { id, sort }
      }
 
      const id = normalizeNumberValue(item)
      if (id === void 0) {
        return null
      }
      return { id, sort: index + 1 }
    })
    .filter(Boolean)
    .sort((left, right) => {
      const leftSort = normalizeSortValue(left.sort, Number.MAX_SAFE_INTEGER)
      const rightSort = normalizeSortValue(right.sort, Number.MAX_SAFE_INTEGER)
      return leftSort - rightSort
    })
    .map((item, index) => ({
      id: item.id,
      sort: index + 1
    }))
}
 
function normalizeNumberValue(value) {
  if (value === '' || value === null || value === undefined) {
    return void 0
  }
  const numberValue = Number(value)
  return Number.isNaN(numberValue) ? void 0 : numberValue
}
 
function normalizeSortValue(value, fallback = 1) {
  const numberValue = normalizeNumberValue(value)
  return numberValue === void 0 ? fallback : numberValue
}
 
export function buildBasContainerSearchParams(params = {}) {
  const searchParams = {
    condition: normalizeText(params.condition),
    code: normalizeText(params.code),
    containerType:
      params.containerType !== undefined && params.containerType !== null && params.containerType !== ''
        ? Number(params.containerType)
        : void 0,
    codeType: normalizeText(params.codeType),
    areas: normalizeText(params.areas),
    status:
      params.status !== undefined && params.status !== null && params.status !== ''
        ? Number(params.status)
        : void 0,
    memo: normalizeText(params.memo),
    timeStart: normalizeText(params.timeStart),
    timeEnd: normalizeText(params.timeEnd)
  }
 
  return Object.fromEntries(
    Object.entries(searchParams).filter(([, value]) => value !== '' && value !== void 0 && value !== null)
  )
}
 
export function fetchBasContainerPage(params = {}) {
  return request.post({
    url: '/basContainer/page',
    params: buildBasContainerPageParams(params)
  })
}
 
export function fetchGetBasContainerDetail(id) {
  return request.get({
    url: `/basContainer/${id}`
  })
}
 
export function fetchGetBasContainerMany(ids) {
  return request.post({
    url: `/basContainer/many/${normalizeIds(ids)}`
  })
}
 
export function fetchSaveBasContainer(params = {}) {
  return request.post({
    url: '/basContainer/save',
    params: buildBasContainerSavePayload(params)
  })
}
 
export function fetchUpdateBasContainer(params = {}) {
  return request.post({
    url: '/basContainer/update',
    params: buildBasContainerSavePayload(params)
  })
}
 
export function fetchDeleteBasContainer(ids) {
  return request.post({
    url: `/basContainer/remove/${normalizeIds(ids)}`
  })
}
 
export function fetchBasContainerQuery(condition = '') {
  return request.post({
    url: '/basContainer/query',
    params: { condition: normalizeText(condition) }
  })
}
 
export async function fetchExportBasContainerReport(payload = {}, options = {}) {
  return fetch(`${import.meta.env.VITE_API_URL}/basContainer/export`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      ...(options.headers || {})
    },
    body: JSON.stringify(payload)
  })
}
 
export function fetchWarehouseAreasList() {
  return request.post({
    url: '/warehouseAreas/list',
    data: {}
  })
}