import { request, type ApiResponse } from './request'
|
import type { BatteryGroupPayload, GroupStatus } from './batteryGroupPayload'
|
|
export type { GroupStatus } from './batteryGroupPayload'
|
|
export interface BatteryGroupListParams {
|
pageNum: number
|
pageSize: number
|
groupId?: number
|
groupName?: string
|
model?: string
|
status?: string
|
createBy?: string
|
beginCreateTime?: string
|
endCreateTime?: string
|
}
|
|
interface BatteryGroupBatteryDto {
|
batteryNo: string | null
|
}
|
|
interface BatteryGroupTemplateFieldDto {
|
templateId: number | null
|
batteryGroupFieldId: number | null
|
fieldName: string | null
|
fieldType: string | null
|
}
|
|
interface BatteryGroupDto {
|
groupId: number
|
groupName: string | null
|
model: string | null
|
purpose: string | null
|
status: string | null
|
createBy: string | null
|
createTime: string | null
|
batteryCount: number | null
|
remark?: string | null
|
z?: string | null
|
h?: string | null
|
f?: string | null
|
a?: string | null
|
b?: string | null
|
batteries?: BatteryGroupBatteryDto[] | null
|
templateFields?: BatteryGroupTemplateFieldDto[] | null
|
}
|
|
interface BatteryGroupListResponse extends ApiResponse {
|
total: number
|
rows: BatteryGroupDto[]
|
}
|
|
export interface BatteryGroupListItem {
|
id: number
|
name: string
|
model: string
|
purpose: string
|
batteryCount: number
|
createdAt: string
|
status: GroupStatus
|
creator: string
|
}
|
|
export interface BatteryGroupListResult {
|
total: number
|
rows: BatteryGroupListItem[]
|
}
|
|
interface BatteryGroupCreatorResponse extends ApiResponse {
|
data: BatteryGroupCreatorDto[]
|
}
|
|
interface BatteryGroupDetailResponse extends ApiResponse {
|
data: BatteryGroupDto
|
}
|
|
interface BatteryGroupCreatorDto {
|
userName: string | null
|
nickName: string | null
|
}
|
|
export interface BatteryGroupCreatorOption {
|
label: string
|
value: string
|
}
|
|
export interface BatteryGroupTemplateField {
|
templateId: number
|
batteryGroupFieldId: number
|
fieldName: string
|
fieldType: string
|
}
|
|
export interface BatteryGroupDetail {
|
id: number
|
name: string
|
model: string
|
purpose: string
|
remark: string
|
status: GroupStatus
|
creator: string
|
specs: Record<'z' | 'h' | 'f' | 'a' | 'b', string>
|
batteries: string[]
|
templateFields: BatteryGroupTemplateField[]
|
}
|
|
function mapBatteryGroup(item: BatteryGroupDto): BatteryGroupListItem {
|
return {
|
id: item.groupId,
|
name: item.groupName || '-',
|
model: item.model || '-',
|
purpose: item.purpose || '-',
|
batteryCount: Number(item.batteryCount) || 0,
|
createdAt: item.createTime || '-',
|
status: item.status === '1' ? '禁用' : '启用',
|
creator: item.createBy || '-',
|
}
|
}
|
|
export async function getBatteryGroupList(params: BatteryGroupListParams): Promise<BatteryGroupListResult> {
|
const searchParams = new URLSearchParams()
|
Object.entries(params).forEach(([key, value]) => {
|
if (value === undefined || value === '') return
|
searchParams.set(key, String(value))
|
})
|
|
const result = await request<BatteryGroupListResponse>(`/system/batteryGroup/list?${searchParams.toString()}`)
|
return {
|
total: Number(result.total) || 0,
|
rows: (result.rows || []).map(mapBatteryGroup),
|
}
|
}
|
|
export async function getBatteryGroupCreatorOptions(): Promise<BatteryGroupCreatorOption[]> {
|
const result = await request<BatteryGroupCreatorResponse>('/system/batteryGroup/creatorOptions')
|
return (result.data || [])
|
.map(item => {
|
const value = item.userName?.trim() || ''
|
const name = item.nickName?.trim() || value
|
return { label: name === value ? name : `${name}(${value})`, value }
|
})
|
.filter(item => item.value)
|
}
|
|
export async function getBatteryGroupDetail(groupId: number): Promise<BatteryGroupDetail> {
|
const result = await request<BatteryGroupDetailResponse>(`/system/batteryGroup/${groupId}`)
|
const item = result.data
|
return {
|
id: item.groupId,
|
name: item.groupName || '',
|
model: item.model || '',
|
purpose: item.purpose || '',
|
remark: item.remark || '',
|
status: item.status === '1' ? '禁用' : '启用',
|
creator: item.createBy || '',
|
specs: { z: item.z || '', h: item.h || '', f: item.f || '', a: item.a || '', b: item.b || '' },
|
batteries: (item.batteries || []).map(battery => battery.batteryNo?.trim() || '').filter(Boolean),
|
templateFields: (item.templateFields || [])
|
.filter(field => field.templateId != null && field.batteryGroupFieldId != null)
|
.map(field => ({
|
templateId: Number(field.templateId),
|
batteryGroupFieldId: Number(field.batteryGroupFieldId),
|
fieldName: field.fieldName || '',
|
fieldType: field.fieldType || '文本',
|
})),
|
}
|
}
|
|
export function createBatteryGroup(payload: BatteryGroupPayload): Promise<ApiResponse> {
|
return request<ApiResponse>('/system/batteryGroup', {
|
method: 'POST',
|
body: JSON.stringify(payload),
|
})
|
}
|
|
export function updateBatteryGroup(groupId: number, payload: BatteryGroupPayload): Promise<ApiResponse> {
|
return request<ApiResponse>('/system/batteryGroup', {
|
method: 'PUT',
|
body: JSON.stringify({ groupId, ...payload }),
|
})
|
}
|