import { request } from '@/utils/request'

// 设备异常记录表 类型定义
export interface DeviceAbnormalRecord {
  id?: number

  abnormalCode?:  string

  deviceId?: number

  deviceCode?:  string

  deviceName?:  string

  stationId?: number

  stationName?:  string

  abnormalType?:  number

  abnormalTypeName?:  string

  abnormalLevel?:  string

  abnormalLevelName?:  string

  abnormalDesc?:  string

  abnormalTime?:  string

  status?:  string

  statusName?:  string

  handler?:  string

  handleTime?:  string

  handleResult?:  string

  affectDuration?: number

  abnormalFrequency?:  string

  remark?:  string

  createTime?:  string

  createBy?:  string

}

// 设备异常记录表 API
export const deviceAbnormalRecordApi = {
  // 分页查询
  page(params: { page: number; pageSize: number; deviceId?: number; abnormalCode?:  string,abnormalType?:number }) {
    return request({ url: '/biz/deviceAbnormalRecord/page', method: 'get', params })
  },

  // 获取详情
  detail(id: number) {
    return request({ url: `/biz/deviceAbnormalRecord/${id}`, method: 'get' })
  },

  // 新增
  create(data: DeviceAbnormalRecord) {
    return request({ url: '/biz/deviceAbnormalRecord', method: 'post', data })
  },

  // 修改
  update(data: DeviceAbnormalRecord) {
    return request({ url: '/biz/deviceAbnormalRecord', method: 'put', data })
  },

  // 删除
  delete(ids: number[]) {
    return request({ url: `/biz/deviceAbnormalRecord/${ids.join(',')}`, method: 'delete' })
  },

  // 导出
  export(params?: { ids?: number[]; id?: number; status?:  string }) {
    const p: Record<string, any> = {}
    if (params?.ids?.length) p.ids = params.ids.join(',')
    if (params?.id !== undefined && params?.id !== null) p.id = params.id
    if (params?.status !== undefined && params?.status !== null) p.status = params.status
    return request({ url: `/biz/deviceAbnormalRecord/export`, method: 'get', params: p, responseType: 'blob' })
  },

  // 导入
  importData(file: File) {
    const formData = new FormData()
    formData.append('file', file)
    return request<{ success: number; fail: number; errors: string[] }>({
      url: `/biz/deviceAbnormalRecord/import`,
      method: 'post',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

  // 下载导入模板
  downloadTemplate() {
    return request({ url: `/biz/deviceAbnormalRecord/template`, method: 'get', responseType: 'blob' })
  },
  //根据设备Id获取异常记录列表
  getDeviceErrorLogList(deviceId: number) {
    return request({
      url: `/biz/deviceAbnormalRecord/getDeviceErrorLogList/${deviceId}`,
      method: 'get'
    })
  }
}
