yeg
4 天以前 f4d7007b89cf565746af4623623a294b82666ff1
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
import type {
  CabinetOnlineStatus,
  CabinetPayload,
  CabinetStatus,
  DeviceCabinetRecord,
} from '@/stores/deviceCabinets'
import { request, type ApiResponse } from './request'
import { formatBusinessTime } from '@/utils/businessTime'
import type { DeviceRelatedTest } from '@/stores/deviceRelatedTests'
 
export interface DeviceCabinetListParams {
  pageNum: number
  pageSize: number
  device?: string
  beginCreateTime?: string
  endCreateTime?: string
  onlineStatus?: string
  status?: string
}
 
interface DetectionCabinetDto {
  cabinetId: number
  cabinetCode: string | null
  cabinetName: string | null
  adapterCode: string | null
  adapterName: string | null
  ipAddress: string | null
  macAddress: string | null
  manufacturer: string | null
  model: string | null
  capacity: number | string | null
  channelCount: number | null
  onlineStatus: string | null
  relatedTests: DeviceRelatedTest[]
  status: string | null
  remark: string | null
  createTime: number | null
}
 
interface DetectionCabinetListResponse extends ApiResponse {
  total: number
  rows: DetectionCabinetDto[]
}
 
interface DetectionCabinetDetailResponse extends ApiResponse {
  data: DetectionCabinetDto
}
 
interface DetectionCabinetPayload {
  cabinetId?: number
  cabinetCode: string
  cabinetName: string
  ipAddress: string
  macAddress: string | null
  manufacturer: string
  model: string
  capacity: number
  channelCount: number
  onlineStatus: string
  status: string
  remark: string
}
 
export interface DeviceCabinetListResult {
  total: number
  rows: DeviceCabinetRecord[]
}
 
export interface DiscoveredCabinet {
  deviceCode: string
  ip: string
  mac: string | null
  name: string | null
  manufacturer: string | null
  model: string | null
  channelCount: number
  onlineStatus: CabinetOnlineStatus
  /** 来源里最近一次采样行的设备时间;没有采样行或来源不提供时为空。 */
  /** 最近数据时间:后端 Instant 属性输出 ISO 文本,手写快照输出毫秒数;没有采样行时为空。 */
  lastDataAt: string | number | null
}
 
export interface DiscoveredCabinetListResult {
  total: number
  rows: DiscoveredCabinet[]
}
 
interface DiscoveredCabinetListResponse extends ApiResponse {
  total: number
  rows: DiscoveredCabinet[]
}
 
const onlineStatusMap: Record<string, CabinetOnlineStatus> = { '0': '在线', '1': '离线' }
const statusMap: Record<string, CabinetStatus> = { '0': '启用', '1': '禁用' }
 
function mapCabinet(item: DetectionCabinetDto): DeviceCabinetRecord {
  return {
    id: item.cabinetId,
    deviceCode: item.cabinetCode || '-',
    ip: item.ipAddress || '-',
    mac: item.macAddress || '-',
    name: item.cabinetName || '-',
    // 数据来源只用于区分跨机器的同名柜号;代码装配实例没有数据库记录时退回展示编码。
    dataSource: item.adapterName || item.adapterCode || '-',
    manufacturer: item.manufacturer || '-',
    model: item.model || '-',
    channelCount: item.channelCount ?? 0,
    specs: Number(item.capacity) || 0,
    remark: item.remark || '',
    onlineStatus: onlineStatusMap[item.onlineStatus || ''] || '离线',
    relatedTests: item.relatedTests || [],
    status: statusMap[item.status || ''] || '禁用',
    createdAt: formatBusinessTime(item.createTime),
  }
}
 
function mapPayload(payload: CabinetPayload, cabinetId?: number): DetectionCabinetPayload {
  return {
    ...(cabinetId === undefined ? {} : { cabinetId }),
    cabinetCode: payload.deviceCode,
    cabinetName: payload.name,
    ipAddress: payload.ip,
    macAddress: payload.mac || null,
    manufacturer: payload.manufacturer,
    model: payload.model,
    capacity: payload.specs,
    channelCount: payload.channelCount,
    onlineStatus: payload.onlineStatus === '在线' ? '0' : '1',
    status: payload.status === '启用' ? '0' : '1',
    remark: payload.remark,
  }
}
 
export async function getDeviceCabinetList(params: DeviceCabinetListParams): Promise<DeviceCabinetListResult> {
  const searchParams = new URLSearchParams()
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined && value !== '') searchParams.set(key, String(value))
  })
 
  const result = await request<DetectionCabinetListResponse>(`/system/detectionCabinet/list?${searchParams.toString()}`)
  return {
    total: Number(result.total) || 0,
    rows: (result.rows || []).map(mapCabinet),
  }
}
 
export async function getDiscoveredCabinetList(pageNum: number, pageSize: number): Promise<DiscoveredCabinetListResult> {
  const result = await request<DiscoveredCabinetListResponse>(
    `/system/detectionCabinet/discovered/list?pageNum=${pageNum}&pageSize=${pageSize}`,
  )
  return {
    total: Number(result.total) || 0,
    rows: result.rows || [],
  }
}
 
export async function getDeviceCabinetDetail(cabinetId: number): Promise<DeviceCabinetRecord> {
  const result = await request<DetectionCabinetDetailResponse>(`/system/detectionCabinet/${cabinetId}`)
  return mapCabinet(result.data)
}
 
export async function createDeviceCabinet(payload: CabinetPayload): Promise<void> {
  await request<ApiResponse>('/system/detectionCabinet', {
    method: 'POST',
    body: JSON.stringify(mapPayload(payload)),
  })
}
 
export async function updateDeviceCabinet(cabinetId: number, payload: CabinetPayload): Promise<void> {
  await request<ApiResponse>('/system/detectionCabinet', {
    method: 'PUT',
    body: JSON.stringify(mapPayload(payload, cabinetId)),
  })
}
 
export async function changeDeviceCabinetStatus(cabinetId: number, status: CabinetStatus): Promise<void> {
  await request<ApiResponse>('/system/detectionCabinet/changeStatus', {
    method: 'PUT',
    body: JSON.stringify({
      cabinetId,
      status: status === '启用' ? '0' : '1',
    }),
  })
}
 
export async function deleteDeviceCabinets(cabinetIds: number | number[]): Promise<void> {
  const ids = Array.isArray(cabinetIds) ? cabinetIds : [cabinetIds]
  await request<ApiResponse>(`/system/detectionCabinet/${ids.join(',')}`, {
    method: 'DELETE',
  })
}