import request from '@/config/axios'

// 物联设备 VO
export interface DeviceVO {
  id: number // ID
  deviceCode: string // 设备编号
  deviceName: string // 设备名称
  deviceType: string // 设备类型
  status: string // 状态
  isConnect?: string | number
  operatingStatus?: string | number
  readTopic: string // 读主题
  writeTopic: string // 写主题
  gatewayId: number // 网关id
  deviceBrandId: number // 设备品牌id
  offLineDuration: number // 离线间隔
  lastOnlineTime: Date // 最后上线时间
  remark: string // 备注
  isEnable: boolean // 是否启用
  deviceModelId: number // 关联设备模型
  protocol: string  // 通讯协议
  sampleCycle: number  // 采集周期
  url: string  //  端点url
  username: string  // 用户名
  password: string // 密码
  certificate?: string // 证书
  secretKey?: string // 秘钥
  collectionTime?: string | number
}

export interface DeviceConnectParams {
  id: string | number
  isConnect: string | number
}

export interface LineDeviceVO {
  id?: string | number
  lineNode?: string
  lineName?: string
  deviceCode?: string
  deviceName?: string
  status?: string | number
  collectionTime?: string | number
}

export interface LineDevicePageParams {
  pageNo: number
  pageSize: number
  id?: string | number
  lineNode?: string
  lineName?: string
  deviceCode?: string
  deviceName?: string
  status?: string | number
  collectionTime?: string | number
  collectionTimeStart?: string
  collectionTimeEnd?: string
}

export interface SingleDeviceParams {
  deviceId: string | number
}

export interface HistoryRecordParams {
  deviceId: string | number
  collectionStartTime?: string
  collectionEndTime?: string
}

export interface DeviceContactModelVO {
  id: number
  attributeCode?: string
  attributeName?: string
  attributeType?: string
  dataType?: string
  dataUnit?: string
}


// 物联设备 API
export const DeviceApi = {
  // 查询物联设备分页
  getDevicePage: async (params: any) => {
    return await request.get({ url: `/iot/device/page`, params })
  },
// 查询物联设备
  getDeviceList: async () => {
    return await request.get({ url: `/iot/device/deviceList` })
  },
  // 查询物联设备详情
  getDevice: async (id: number) => {
    return await request.get({ url: `/iot/device/get?id=` + id })
  },
  getDeviceListByNoUsed: async () => {
    return await request.get({ url: `/iot/device/noUsedlist` })
  },
  getDeviceList2ByNoUsed: async (id: number) => {
    return await request.get({ url: `/iot/device/noUsedlist2?id=` + id })
  },
  // 新增物联设备
  createDevice: async (data: DeviceVO) => {
    return await request.post({ url: `/iot/device/create`, data })
  },

  // 批量获取设备属性列表
  getDeviceAttributeBatchList: async (params: { goviewId: number | string; orgId?: number | string }) => {
    return await request.get({ url: `/iot/device/device-attribute/batchList`, params })
  },
  // 修改物联设备
  updateDevice: async (data: DeviceVO) => {
    return await request.put({ url: `/iot/device/update`, data })
  },

  // 删除物联设备
  deleteDevice: async (ids: string) => {
    return await request.delete({ url: `/iot/device/delete?ids=` + ids })
  },

  // 复制物联设备
  copyDevice: async (id: number) => {
    return await request.post({ url: `/iot/device/copy`, params: { id } })
  },

  connectDevice: async (params: DeviceConnectParams) => {
    return await request.post({ url: `/iot/device/connect`, data: params })
  },

  // 导出物联设备 Excel
  exportDevice: async (params) => {
    return await request.download({ url: `/iot/device/export-excel`, params })
  },

  exportLineDevice: async (params) => {
    return await request.download({ url: `/iot/device/export-line-device`, params })
  },

  getLineDevicePage: async (params: LineDevicePageParams) => {
    return await request.get({ url: `/iot/device/lineDevicePage`, params })
  },

  getSingleDevice: async (params: SingleDeviceParams) => {
    return await request.get({ url: `/iot/device/singleDevice`, params })
  },

  getHistoryRecord: async (params: HistoryRecordParams) => {
    return await request.get({ url: `/iot/device/historyRecord`, params })
  },

  devicePointList: async () => {
    return await request.get({ url: `/iot/device/devicePointList` })
  },

  updateDeviceEnabled: async (id: number | string, enabled: string) => {
    const data = { id, enabled }
    return await request.put({ url: `/iot/device/update-enabled`, data })
  },

// ==================== 子表（设备属性） ====================

  // 获得设备属性分页
  getDeviceAttributePage: async (params) => {
    return await request.get({ url: `/iot/device/device-attribute/page`, params })
  },
  // 获得设备属性列表
  getDeviceAttributeList: async (deviceId: number) => {
    return await request.get({ url: `/iot/device/device-attribute/list?deviceId=` + deviceId })
  },

  getDeviceContactModelPage: async () => {
    return await request.get({ url: `/iot/device-contact-model/page` })
  },
  // 新增设备属性
  createDeviceAttribute: async (data) => {
    return await request.post({ url: `/iot/device-contact-model/create`, data })
  },

  // 修改设备属性
  updateDeviceAttribute: async (data) => {
    return await request.put({ url: `/iot/device-contact-model/update`, data })
  },

  // 删除设备属性
  deleteDeviceAttribute: async (ids: string) => {
    return await request.delete({ url: `/iot/device-contact-model/delete?ids=` + ids })
  },

  // 获得设备属性
  getDeviceAttribute: async (id: number) => {
    return await request.get({ url: `/iot/device-contact-model/get?id=` + id })
  }
}
