import HelperUtil from '@/utils/HelperUtil'
import {PERIOD_REPORT_DOWNLOAD, PERIOD_REPORT_KEY} from '../../utils/constantCollection'
import PeriodReportAPI from '../../api/restApis/PeriodReportAPI'

// 静态方法
let staticMethods = {
  /**
   * @Description  : 删除
   * @author       : ls
   * @date         : 2020/5/2 14:19
   * @param        :
   * @return       :
   */
  delatePeriodReport: function (keyList) {
    return PeriodReportAPI.deleteDeletePeriodReport(keyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 分页查询所有
   * @author       : ls
   * @date         : 2020/5/6 16:13
   * @param        :
   * @return       :
   */
  queryPeriodReportDataSplitNumber: function (beginItem, endItem, selectList) {
    return PeriodReportAPI.putPeriodReportListQueryterms(beginItem, endItem, selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, PERIOD_REPORT_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 查询总数service
   * @author       : ls
   * @date         : 2020/5/6 16:12
   * @param        :
   * @return       :
   */
  getPeriodReportTotalCounts: function (selectList) {
    return PeriodReportAPI.putPeriodReportListCounts(selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 下载
   * @author       : ls
   * @date         : 2020/6/15 14:16
   * @param        :
   * @return       :
   */
  downloadPeriodReport: function (deviceKey) {
    return PeriodReportAPI.putDownloadPeriodReport(deviceKey).then(result => {
      return HelperUtil.downFile(result, JSON.parse(result.config.data).reportName)
    })
  }
}

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