import type {
|
CollectorOnlineStatus,
|
CollectorStatus,
|
CollectorTestStatus,
|
DeviceCollectorRecord,
|
} from '@/stores/deviceCollectors'
|
import { request, type ApiResponse } from './request'
|
|
export interface DeviceCollectorListParams {
|
pageNum: number
|
pageSize: number
|
device?: string
|
beginCreateTime?: string
|
endCreateTime?: string
|
onlineStatus?: string
|
testStatus?: string
|
status?: string
|
}
|
|
interface CollectorDto {
|
collectorId: number
|
collectorCode: string | null
|
collectorName: string | null
|
ipAddress: string | null
|
macAddress: string | null
|
manufacturer: string | null
|
model: string | null
|
channelCount: number | null
|
onlineStatus: string | null
|
testStatus: string | null
|
currentTest: string | null
|
status: string | null
|
remark: string | null
|
createTime: string | null
|
}
|
|
interface CollectorListResponse extends ApiResponse {
|
total: number
|
rows: CollectorDto[]
|
}
|
|
interface CollectorDetailResponse extends ApiResponse {
|
data: CollectorDto
|
}
|
|
export interface DeviceCollectorInput {
|
deviceCode: string
|
ip: string
|
mac: string
|
name: string
|
manufacturer: string
|
model: string
|
channelCount: number
|
status: CollectorStatus
|
remark: string
|
}
|
|
interface CollectorSaveDto {
|
collectorId?: number
|
collectorCode: string
|
ipAddress: string
|
macAddress: string
|
collectorName: string
|
manufacturer: string
|
model: string
|
channelCount: number
|
status: string
|
remark: string
|
}
|
|
export interface DeviceCollectorListResult {
|
total: number
|
rows: DeviceCollectorRecord[]
|
}
|
|
const onlineStatusMap: Record<string, CollectorOnlineStatus> = { '0': '离线', '1': '在线' }
|
const testStatusMap: Record<string, CollectorTestStatus> = { '0': '待检测', '1': '测试中' }
|
const statusMap: Record<string, CollectorStatus> = { '0': '启用', '1': '禁用' }
|
|
function mapCollector(item: CollectorDto): DeviceCollectorRecord {
|
return {
|
id: item.collectorId,
|
deviceCode: item.collectorCode || '-',
|
ip: item.ipAddress || '-',
|
mac: item.macAddress || '-',
|
name: item.collectorName || '-',
|
manufacturer: item.manufacturer || '',
|
model: item.model || '',
|
channelCount: item.channelCount ?? 0,
|
remark: item.remark || '',
|
onlineStatus: onlineStatusMap[item.onlineStatus || ''] || '离线',
|
testStatus: testStatusMap[item.testStatus || ''] || '待检测',
|
currentTest: item.currentTest || '',
|
status: statusMap[item.status || ''] || '禁用',
|
createdAt: item.createTime || '',
|
}
|
}
|
|
function toSaveDto(input: DeviceCollectorInput, collectorId?: number): CollectorSaveDto {
|
return {
|
collectorId,
|
collectorCode: input.deviceCode.trim(),
|
ipAddress: input.ip.trim(),
|
macAddress: input.mac.trim(),
|
collectorName: input.name.trim(),
|
manufacturer: input.manufacturer.trim(),
|
model: input.model.trim(),
|
channelCount: input.channelCount,
|
status: input.status === '启用' ? '0' : '1',
|
remark: input.remark.trim(),
|
}
|
}
|
|
export async function getDeviceCollectorList(params: DeviceCollectorListParams): Promise<DeviceCollectorListResult> {
|
const searchParams = new URLSearchParams()
|
Object.entries(params).forEach(([key, value]) => {
|
if (value !== undefined && value !== '') searchParams.set(key, String(value))
|
})
|
|
const result = await request<CollectorListResponse>(`/system/collector/list?${searchParams.toString()}`)
|
return {
|
total: Number(result.total) || 0,
|
rows: (result.rows || []).map(mapCollector),
|
}
|
}
|
|
export async function getDeviceCollector(collectorId: number): Promise<DeviceCollectorRecord> {
|
const result = await request<CollectorDetailResponse>(`/system/collector/${collectorId}`)
|
return mapCollector(result.data)
|
}
|
|
export function createDeviceCollector(input: DeviceCollectorInput): Promise<ApiResponse> {
|
return request<ApiResponse>('/system/collector', {
|
method: 'POST',
|
body: JSON.stringify(toSaveDto(input)),
|
})
|
}
|
|
export function updateDeviceCollector(collectorId: number, input: DeviceCollectorInput): Promise<ApiResponse> {
|
return request<ApiResponse>('/system/collector', {
|
method: 'PUT',
|
body: JSON.stringify(toSaveDto(input, collectorId)),
|
})
|
}
|
|
export async function changeDeviceCollectorStatus(collectorId: number, status: CollectorStatus): Promise<void> {
|
await request<ApiResponse>('/system/collector/changeStatus', {
|
method: 'PUT',
|
body: JSON.stringify({
|
collectorId,
|
status: status === '启用' ? '0' : '1',
|
}),
|
})
|
}
|
|
export async function deleteDeviceCollectors(collectorIds: number | number[]): Promise<void> {
|
const ids = Array.isArray(collectorIds) ? collectorIds : [collectorIds]
|
await request<ApiResponse>(`/system/collector/${ids.join(',')}`, {
|
method: 'DELETE',
|
})
|
}
|