import request from '@/config/axios'

// 能源设备 VO
export interface EnergyDeviceOperationRuleVO {
  deviceId: number
  pointId: number
  operator?: string
}

export interface EnergyDeviceVO {
  id: number // ID
  name: string // 名称
  code: string // 编码
  deviceTypeId: number // 设备类型id
  deviceTypeName: string // 设备类型名称
  info: string // 信息资料
  checkCron: string // 抄表周期cron
  lastCheckTime: Date // 最后抄表时间
  lastCheckValue: number // 最后抄表值
  unitName: string // 单位
  isEnable: boolean // 是否启用
  orgId: number // 所属区域id
  orgName: string  // 所属区域名称
  rules: string // 计算规则
  operationRulesVOList?: EnergyDeviceOperationRuleVO[]
}

export interface EnergyDeviceDataRecordVO {
  id: number
  name?: string
  deviceTypeName?: string
  orgId?: number | string
  orgName?: string
  operationRulesVOMap?: Record<string, string | number | null>
  energyConsumption?: string | number
  energyAllocationConsumption?: string | number
  energyActualConsumption?: string | number
  shift?: string
  startTime?: string
  endTime?: string
  earliestDataTime?: string
  latestDataTime?: string
}

// 能源设备 API
export const EnergyDeviceApi = {
  // 查询能源设备分页
  getEnergyDevicePage: async (params: any) => {
    return await request.get({ url: `/mes/energy-device/page`, params })
  },

  // 查询能源设备详情
  getEnergyDevice: async (id: number) => {
    return await request.get({ url: `/mes/energy-device/get?id=` + id })
  },
// 查询能源设备列表
  getList: async (params: any) => {
    return await request.get({ url: `/mes/energy-device/getList`, params })
  },

  getLatestSevenDaysStatistics: async (params: any) => {
    return await request.get({ url: `/mes/energy-device/latestSevenDaysStatistics`, params })
  },

  queryDataRecords: async (params: any) => {
    return await request.get({ url: `/mes/energy-device/queryDataRecords`, params })
  },

  exportQueryDataRecords: async (params: any) => {
    return await request.download({ url: `/mes/energy-device/record-export-excel`, params })
  },

  // 新增能源设备
  createEnergyDevice: async (data: EnergyDeviceVO) => {
    return await request.post({ url: `/mes/energy-device/create`, data })
  },

  // 修改能源设备
  updateEnergyDevice: async (data: EnergyDeviceVO) => {
    return await request.put({ url: `/mes/energy-device/update`, data })
  },

  // 删除能源设备
  deleteEnergyDevice: async (id: number) => {
    return await request.delete({ url: `/mes/energy-device/delete?id=` + id })
  },

  // 导出能源设备 Excel
  exportEnergyDevice: async (params) => {
    return await request.download({ url: `/mes/energy-device/export-excel`, params })
  },

// ==================== 子表（抄表记录） ====================

  // 获得抄表记录分页
  getEnergyDeviceCheckRecordPage: async (params) => {
    return await request.get({ url: `/mes/energy-device/energy-device-check-record/page`, params })
  },
  // 新增抄表记录
  createEnergyDeviceCheckRecord: async (data) => {
    return await request.post({ url: `/mes/energy-device/energy-device-check-record/create`, data })
  },

  // 修改抄表记录
  updateEnergyDeviceCheckRecord: async (data) => {
    return await request.put({ url: `/mes/energy-device/energy-device-check-record/update`, data })
  },

  // 删除抄表记录
  deleteEnergyDeviceCheckRecord: async (id: number) => {
    return await request.delete({ url: `/mes/energy-device/energy-device-check-record/delete?id=` + id })
  },

  // 获得抄表记录
  getEnergyDeviceCheckRecord: async (id: number) => {
    return await request.get({ url: `/mes/energy-device/energy-device-check-record/get?id=` + id })
  },

  // 获取最后一次能耗统计
  getLastEnergyStatistics: async (params: any) => {
    return await request.get({ url: `/mes/energy-device/lastEnergyStatistics`, params })
  }
}
