yeg
2026-08-27 de70520fa0ce1b13e4b535f9b710f27074ff29d1
src/api/tests.ts
@@ -1,5 +1,8 @@
import type { TestRecord, TestStatus } from '@/stores/tests'
import type { MonitoringStatus, TestRecord, TestStatus } from '@/stores/tests'
import { request, type ApiResponse } from './request'
export type TestStatusCode = 'WAITING' | 'RUNNING' | 'PAUSED' | 'RUNNING_ABNORMAL' | 'COMPLETION_PENDING' | 'COMPLETED' | 'TERMINATED'
export type MonitoringStatusCode = 'NOT_MONITORED' | 'WAITING_DEVICE_START' | 'MONITORING' | 'BACKFILLING' | 'MONITORING_ENDED' | 'CANCELLED'
export interface TestListParams {
  pageNum: number
@@ -7,55 +10,110 @@
  testName?: string
  testTypeId?: number
  batteryNo?: string
  status?: string
  testStatus?: TestStatusCode
  monitoringStatus?: MonitoringStatusCode
  beginCreateTime?: string
  endCreateTime?: string
}
export interface TestPlanStepPayload {
  planStepId?: number
  cycleNo: number
  stepNo: number
  stepType: string
  stepName: string
  parameters: Record<string, unknown>
  sortOrder: number
}
export interface TestBatteryPayload {
  relationId?: number
  groupId: number
  batteryNo: string
  collectorId: number | null
  binding: { bindingId?: number; channelId: number; expectedBarcode: string | null; claimStatus?: string }
  fields: Array<{ fieldId?: number; fieldName: string; fieldValue: string }>
}
export interface TestThresholdPayload {
  thresholdId?: number
  metricCode: string
  minValue: number | null
  maxValue: number | null
  unit: 'A' | 'V' | 'Ah' | 'Wh' | '℃'
  severity: 'WARNING'
}
export interface TestConfigurationPayload {
  testId?: number
  testName: string
  principalId: number
  testTypeId: number
  testPurpose: string
  monitorTimeoutMinutes: number
  remark: string | null
  batteries: TestBatteryPayload[]
  planSteps: TestPlanStepPayload[]
  thresholds: TestThresholdPayload[]
}
export interface TestConfigurationDetail extends TestConfigurationPayload {
  testId: number
  creatorId: number
  creatorName: string | null
  principalName: string | null
  testTypeName: string | null
  testStatus: TestStatusCode
  monitoringStatus: MonitoringStatusCode
}
export interface TestConfigurationOptions {
  users: Array<{ userId: number; userName: string; nickName: string | null }>
  testTypes: Array<{ testTypeId: number; testTypeName: string }>
  batteries: Array<{ relationId: number; groupId: number; groupName: string; batteryNo: string }>
  channels: Array<{ channelId: number; cabinetId: number; cabinetCode: string; cabinetName: string; adapterCode: string; devtype: number; devid: number; subdevid: number; chlid: number; displayNo: string | null }>
  collectors: Array<{ collectorId: number; collectorCode: string; collectorName: string }>
}
interface TestDto {
  testId: number
  testName: string | null
  testTypeName: string | null
  leaderName: string | null
  purpose: string | null
  status: string | null
  principalName: string | null
  testPurpose: string | null
  testStatus: TestStatusCode | null
  monitoringStatus: MonitoringStatusCode | null
  createTime: string | null
  startedAtUtc: string | null
  endedAtUtc: string | null
  remark: string | null
  batteryCount: number | null
}
interface TestListResponse extends ApiResponse {
  total: number
  rows: TestDto[]
}
interface TestListResponse extends ApiResponse { total: number; rows: TestDto[] }
interface TestDetailResponse extends ApiResponse { data: TestConfigurationDetail }
interface TestOptionsResponse extends ApiResponse { data: TestConfigurationOptions }
interface TestIdentifierResponse extends ApiResponse { data: number }
export interface TestListResult { total: number; rows: TestRecord[] }
export interface TestListResult {
  total: number
  rows: TestRecord[]
const statusMap: Record<TestStatusCode, TestStatus> = {
  WAITING: '待测试', RUNNING: '检测中', PAUSED: '暂停', RUNNING_ABNORMAL: '检测中(异常)',
  COMPLETION_PENDING: '完成确认中', COMPLETED: '检测完成', TERMINATED: '已终止',
}
const statusMap: Record<string, TestStatus> = {
  '0': '待测试',
  '1': '检测中',
  '2': '检测中(异常)',
  '3': '检测完成',
const monitoringStatusMap: Record<MonitoringStatusCode, MonitoringStatus> = {
  NOT_MONITORED: '未监测', WAITING_DEVICE_START: '等待设备启动', MONITORING: '监测中',
  BACKFILLING: '补采中', MONITORING_ENDED: '监测结束', CANCELLED: '已取消',
}
function mapTest(item: TestDto): TestRecord {
  const testStatus = item.testStatus || 'WAITING'
  const monitoringStatus = item.monitoringStatus || 'NOT_MONITORED'
  return {
    id: item.testId,
    name: item.testName || '-',
    type: item.testTypeName || '-',
    batteryNo: '-',
    batteryGroup: '-',
    batteryCount: Number(item.batteryCount) || 0,
    createdAt: item.createTime || '-',
    startedAt: '-',
    endedAt: '-',
    purpose: item.purpose || '-',
    owner: item.leaderName || '-',
    remark: item.remark || '-',
    status: statusMap[item.status || ''] || '待测试',
    id: item.testId, name: item.testName || '-', type: item.testTypeName || '-', batteryNo: '-', batteryGroup: '-',
    batteryCount: Number(item.batteryCount) || 0, createdAt: item.createTime || '-', startedAt: item.startedAtUtc || '-',
    endedAt: item.endedAtUtc || '-', purpose: item.testPurpose || '-', owner: item.principalName || '-',
    remark: item.remark || '-', status: statusMap[testStatus], monitoringStatus: monitoringStatusMap[monitoringStatus],
    editable: monitoringStatus === 'NOT_MONITORED',
  }
}
@@ -63,15 +121,21 @@
  const searchParams = new URLSearchParams()
  Object.entries(params).forEach(([key, value]) => {
    if (value === undefined || value === '') return
    const queryKey = key === 'beginCreateTime' || key === 'endCreateTime'
      ? `params[${key}]`
      : key
    searchParams.set(queryKey, String(value))
    searchParams.set(key, String(value))
  })
  const result = await request<TestListResponse>(`/system/test/list?${searchParams.toString()}`)
  return {
    total: Number(result.total) || 0,
    rows: (result.rows || []).map(mapTest),
  }
  return { total: Number(result.total) || 0, rows: (result.rows || []).map(mapTest) }
}
export async function getTestConfigurationOptions(): Promise<TestConfigurationOptions> {
  return (await request<TestOptionsResponse>('/system/test/options')).data
}
export async function getTestDetail(testId: number): Promise<TestConfigurationDetail> {
  return (await request<TestDetailResponse>(`/system/test/${testId}`)).data
}
export async function createTest(payload: TestConfigurationPayload): Promise<number> {
  return (await request<TestIdentifierResponse>('/system/test', { method: 'POST', body: JSON.stringify(payload) })).data
}
export async function updateTest(payload: TestConfigurationPayload & { testId: number }): Promise<void> {
  await request<ApiResponse>('/system/test', { method: 'PUT', body: JSON.stringify(payload) })
}