/**
 *  性能查询服务
 */
import HelperUtil from '../../utils/HelperUtil'
import PerformanceDataQuaryAPI from '../../api/restApis/PerformanceDataQuaryAPI'
import {
  PERFORMANCE_KEY,
  PERFORMANCE_DOWNLOAD_FILE_NAME,
  TOPN_PERFORMANCE_DOWNLOAD_FILE_NAME,
  PERFORMANCE_DOWNLOAD_FILE_NAME_CSV
} from '../../utils/constantCollection'
import AlarmManagerAPI from "../../api/restApis/AlarmManagerAPI";

// 静态方法
let staticMethods = {
  // 性能数据按条件查询：总条数
  queryPerformDataTotalNumber: function (selectList) {
    return PerformanceDataQuaryAPI.putPerformanceCounts(selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 性能数据按条件查询：分页性能
  queryPerformDataSplitNumber: function (beginItem, endItem, selectList) {
    return PerformanceDataQuaryAPI.putPerformanceQueryterms(beginItem, endItem, selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, PERFORMANCE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : top查询
   * @author       : ls
   * @date         : 2020/6/29 10:44
   * @param        :
   * @return       :
   */
  queryPerformDataNumber: function (num, selectList) {
    return PerformanceDataQuaryAPI.putPerformanceQuery(num, selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : topN性能查询总数
   * @author       : ls
   * @date         : 2020/6/29 9:51
   * @param        :
   * @return       :
   */
  queryTopNPerformDataTotalNumber: function (num, selectList) {
    return PerformanceDataQuaryAPI.putTopNPerformanceCounts(num, selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : topN性能查询
   * @author       : ls
   * @date         : 2020/6/29 9:45
   * @param        :
   * @return       :
   */
  queryTopNPerformDataSplitNumber: function (beginItem, endItem, selectList) {
    return PerformanceDataQuaryAPI.putPerformanceQueryterms(beginItem, endItem, selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, PERFORMANCE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : topNExportToExcel
   * @author       : ls
   * @date         : 2020/12/21 17:39
   * @param        :
   * @return       :
   */
  topNExportToExcel: function (selectList) {
    return PerformanceDataQuaryAPI.putDownloadPerformance(selectList).then(result => {
      return HelperUtil.downFile(result, TOPN_PERFORMANCE_DOWNLOAD_FILE_NAME)
    })
  },

  // 导出excel
  exportToExcel: function (selectList) {
    return PerformanceDataQuaryAPI.putDownloadPerformance(selectList).then(result => {
      return HelperUtil.downFile(result, PERFORMANCE_DOWNLOAD_FILE_NAME)
    })
  },
  /**
   * @Description  : 导出csv
   * @author       : ls
   * @date         : 2021/3/1 17:37
   * @param        :
   * @return       :
   */
  downloadPerformCsv: function (conditionSet) {
    return PerformanceDataQuaryAPI.putDownloadPerformanceCsv(conditionSet).then(result => {
      return HelperUtil.downFile(result, PERFORMANCE_DOWNLOAD_FILE_NAME_CSV)
    })
  },
  queryPerformTaskData: function (val) {
    return PerformanceDataQuaryAPI.putQueryPerformTaskData(val).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, PERFORMANCE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  }
}

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