import request from '@/config/axios'

/**
 * 设备数据 VO
 */
export interface EquipmentDataVO {
  id?: number
  // 进料泵
  feedPumpStatus: string        // 运行状态: running | stopped
  feedPumpSetLevel: number      // 设定液位
  feedPumpIsAuto: boolean       // 控制模式
  feedPumpFrequency: number     // 频率设置
  feedPumpEvaporationLevel: number // 蒸发液位

  // 蒸汽阀
  steamValveStatus: string
  steamValveSetTemperature: number
  steamValveIsAuto: boolean
  steamValveOpening: number
  steamValveAlcoholConcentration: number

  // 回料阀
  returnValveStatus: string
  returnValveSetFlow: number
  returnValveIsAuto: boolean
  returnValveOpening: number
  returnValveActualFlow: number

  // 浓液
  concentrateStatus: string
  concentrateSetFlow: number
  concentrateIsAuto: boolean
  concentrateOpening: number
  concentrateActualFlow: number

  // RO系统
  roSystemStatus: string
  roSystemInletFlow: number
  roSystemProductFlow: number
  roSystemRecoveryRate: number

  // 记录时间
  createTime?: Date
}

/**
 * 趋势数据 VO
 */
export interface TrendDataVO {
  times: string[]
  // 液位数据
  feedPumpEvaporationLevel: number[]
  // 流量数据
  returnValveActualFlow: number[]
  concentrateActualFlow: number[]
  roSystemInletFlow: number[]
  roSystemProductFlow: number[]
  // 其他数据
  steamValveAlcoholConcentration: number[]
  roSystemRecoveryRate: number[]
}

/**
 * 获取最新的设备数据（一行数据包含五个卡片的所有字段）
 */
export const getLatestEquipmentData = () => {
  return request.get<EquipmentDataVO>({ url: '/mes/collect/latest' })
}

/**
 * 获取设备数据分页列表
 */
export const getEquipmentDataPage = (params: any) => {
  return request.get({ url: '/mes/collect/page', params })
}

/**
 * 获取趋势数据（用于折线图展示）
 * @param params - 查询参数，包含时间范围
 */
export const getTrendData = (params?: { beginTime?: string; endTime?: string }) => {
  return request.get<TrendDataVO>({ url: '/mes/collect/trend', params })
}

/**
 * 更新设备控制模式
 * @param equipment - 设备名称: feedPump | steamValve | returnValve | concentrate
 * @param isAuto - 是否自动模式
 */
export const updateControlMode = (equipment: string, isAuto: boolean) => {
  return request.put({ url: '/mes/collect/control-mode', data: { equipment, isAuto } })
}
