/**
 *  性能查询服务
 */
import HelperUtil from '../../utils/HelperUtil'
import RtPerformanceAPI from '../../api/restApis/RtPerformanceAPI'
import {RT_PERTYPE_KEY} from '../../utils/constantCollection'
// 静态方法
let staticMethods = {
  // 性能数据按条件查询：总条数
  rtPerformData: function (selectList) {
    return RtPerformanceAPI.rtPerformanceDatas(selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  rtPerformDatas: function (selectList, port, event) {
    return RtPerformanceAPI.rtPerformanceDatas(selectList, port, event).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 获取性能事件
  getRtEventType: function () {
    return RtPerformanceAPI.getAllRtEventType().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, RT_PERTYPE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  getRtPerformType: function (device) {
    return RtPerformanceAPI.getAllRtPerformType(device).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, RT_PERTYPE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  }
}

function RtPerformanceService (websocketService = null) {
  return Object.freeze(Object.assign(
    {
      // object methods comes here
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(RtPerformanceService, staticMethods)
export default RtPerformanceService
