yeg
2026-08-27 de70520fa0ce1b13e4b535f9b710f27074ff29d1
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
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
  pageSize: number
  testName?: string
  testTypeId?: number
  batteryNo?: 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
  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 TestDetailResponse extends ApiResponse { data: TestConfigurationDetail }
interface TestOptionsResponse extends ApiResponse { data: TestConfigurationOptions }
interface TestIdentifierResponse extends ApiResponse { data: number }
export interface TestListResult { total: number; rows: TestRecord[] }
 
const statusMap: Record<TestStatusCode, TestStatus> = {
  WAITING: '待测试', RUNNING: '检测中', PAUSED: '暂停', RUNNING_ABNORMAL: '检测中(异常)',
  COMPLETION_PENDING: '完成确认中', COMPLETED: '检测完成', TERMINATED: '已终止',
}
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: item.startedAtUtc || '-',
    endedAt: item.endedAtUtc || '-', purpose: item.testPurpose || '-', owner: item.principalName || '-',
    remark: item.remark || '-', status: statusMap[testStatus], monitoringStatus: monitoringStatusMap[monitoringStatus],
    editable: monitoringStatus === 'NOT_MONITORED',
  }
}
 
export async function getTestList(params: TestListParams): Promise<TestListResult> {
  const searchParams = new URLSearchParams()
  Object.entries(params).forEach(([key, value]) => {
    if (value === undefined || value === '') return
    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) }
}
 
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) })
}