// 静态方法
import HelperUtil from '../../utils/HelperUtil'
import {
  ALARM_KEY,
  ALARM_QUERY_FILE_NAME_EXCEL,
  ALARM_QUERY_FILE_NAME_CSV,
  ALARMCFGLIST_KEY, ALARMTYPELIST_KEY
} from '../../utils/constantCollection'
import AlarmManagerAPI from '../../api/restApis/AlarmManagerAPI'
import {ALARM_UPLOAD_URL} from '../../utils/RequestUrls'
let staticMethods = {
  // 获取告警列表 cxf/nms/alarm/typelist
  getAlarmTypeList: function () {
    return AlarmManagerAPI.getAlarmTypeList().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, ALARMTYPELIST_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 获取告警配置 cxf/nms/alarmconfig/
  getAllAlarmCfgList: function () {
    return AlarmManagerAPI.getAllAlarmCfgList().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, ALARMCFGLIST_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 告警数据按条件查询：总条数cxf/nms/alarm/totalcounts/
  getTotalCounts: function (selectList) {
    return AlarmManagerAPI.getTotalCounts(selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 性能数据按条件查询：分页 cxf/nms/alarm/queryterms/
  queryAlarmPaging: function (beginItem, endItem, selectList) {
    return AlarmManagerAPI.queryAlarmPaging(beginItem, endItem, selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, ALARM_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 性能数据按条件查询：统计图数据 cxf/nms/alarm/alarmstatistic/
  queryAlarmStatistic: function (statisticsList) {
    return AlarmManagerAPI.queryAlarmStatistic(statisticsList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // topN
  queryAlarmReport: function (valueTopN, statisticsList) {
    return AlarmManagerAPI.queryAlarmReport(valueTopN, statisticsList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 告警分布统计报表
   * @author       : ls
   * @date         : 2020/5/12 9:41
   * @param        :
   * @return       :
   */
  queryAlarmDistributionReport: function (statisticsList) {
    return AlarmManagerAPI.queryAlarmDistributionReportApi(statisticsList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 统计报表service
   * @author       : ls
   * @date         : 2020/5/29 16:14
   * @param        :
   * @return       :
   */
  queryReportService: function (val) {
    return AlarmManagerAPI.queryReport(val).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 更新告警等级信息 cxf/nms/alarm/alarmlevelconfigbatch
  updateAlarmLevel: function (alarmLevelListUpdate) {
    return AlarmManagerAPI.updateAlarmLevel(alarmLevelListUpdate).then(result => {
      if (result.data.result) {

      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 更新告警配置 cxf/nms/alarmconfig/batch
  updateAlarmConfig: function (alarmCfgListUpdate) {
    return AlarmManagerAPI.updateAlarmConfig(alarmCfgListUpdate).then(result => {
      if (result.data.result) {

      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 告警查询导出excel
  downloadAlarmConfig: function (conditionSet) {
    return AlarmManagerAPI.putDownloadAlarmQuery(conditionSet).then(result => {
      return HelperUtil.downFile(result, ALARM_QUERY_FILE_NAME_EXCEL)
    })
  },
  // 告警查询导出csv
  downloadAlarmConfigCsv: function (conditionSet) {
    return AlarmManagerAPI.putDownloadAlarmQueryCsv(conditionSet).then(result => {
      return HelperUtil.downFile(result, ALARM_QUERY_FILE_NAME_CSV)
    })
  },
  // 告警上传
  getUplaodUrl: function () {
    return ALARM_UPLOAD_URL
  }
}

function AlarmManagerService (websocketService = null) {
  return Object.freeze(Object.assign(
    {
      // object methods comes here

    },
    staticMethods
  ))
}
// inject static methods
Object.assign(AlarmManagerService, staticMethods)
export default AlarmManagerService
