import request from '@/utils/request';
import { baseURL } from '@/config';

export function getList(data) {
  let param = '?page=' + data.page + '&size=' + data.size;
  if (data.projectId != '' && data.projectId != undefined) {
    param += '&projectId=' + data.projectId;
  }
  if (data.code != '' && data.code != undefined) {
    param += '&code=' + data.code;
  }
  if (data.name != '' && data.name != undefined) {
    param += '&name=' + data.name;
  }
  if (data.community != '' && data.community != undefined) {
    param += '&community=' + data.community;
  }
  if (data.townCode != '' && data.townCode != undefined) {
    param += '&townCode=' + data.townCode;
  }
  if (data.commPro != '' && data.commPro != undefined) {
    param += '&commPro=' + data.commPro;
  }
  if (data.status != 3) {
    param += '&status=' + data.status;
  }
  return request({
    url: '/deviceReport/list' + param,
    method: 'get',
    data,
  });
}
export function getDeviceData(data) {
  let param = '?page=' + data.page + '&size=' + data.size;
  if (data.projectId != '' && data.projectId != undefined) {
    param += '&projectId=' + data.projectId;
  }
  if (data.serial != '' && data.serial != undefined) {
    param += '&serial=' + data.serial;
  }
  if (data.name != '' && data.name != undefined) {
    param += '&name=' + data.name;
  }
  if (data.community != '' && data.community != undefined) {
    param += '&community=' + data.community;
  }
  if (data.townCode != '' && data.townCode != undefined) {
    param += '&townCode=' + data.townCode;
  }
  if (
    data.commPro != 'Entire agreement' &&
    data.commPro != undefined &&
    data.commPro != 'all'
  ) {
    param += '&commPro=' + data.commPro;
  }
  if (data.status != 3 && data.status != undefined) {
    param += '&status=' + data.status;
  }
  return request({
    url: '/iot/device/select' + param,
    method: 'get',
    data,
  });
}
export function doEdit(data) {
  return request({
    url: '/iot/device/update',
    method: 'post',
    data,
  });
}

export function doDelete(data) {
  return request({
    url: '/iot/device/remove',
    method: 'post',
    data,
  });
}

export function doAdd(data) {
  return request({
    url: '/iot/device/insert',
    method: 'post',
    data,
  });
}

export function selectDeviceInfo(data) {
  return request({
    url: '/iot/device/selectDeviceInfo?deviceId=' + data.deviceId,
    method: 'get',
    data,
  });
}

export function getDictData(data) {
  return request({
    url: '/iot/device/dict',
    method: 'get',
    data,
  });
}

export function selectAllProject(data) {
  return request({
    url: '/iot/device/selectAllProject',
    method: 'get',
    data,
  });
}

export function selectAllGroup(data) {
  return request({
    url: '/iot/device/selectAllGroup?projectId=' + data.projectId,
    method: 'get',
    data,
  });
}

export function selectAllAlarmUser(data) {
  return request({
    url: '/iot/device/selectAllAlarmUser',
    method: 'get',
    data,
  });
}

export function copy(data) {
  return request({
    url:
      '/iot/device/copy?deviceId=' +
      data.deviceId +
      '&name=' +
      data.name +
      '&code=' +
      data.code +
      '&serial=' +
      data.serial,
    method: 'post',
    data,
  });
}

export function getLatestDataByDevice(data) {
  let param =
    '?deviceCode=' +
    data.deviceCode +
    '&page=' +
    data.page +
    '&size=' +
    data.size;
  if (data.sensorName != '') {
    param += '&sensorName=' + data.sensorName;
  }
  return request({
    url: '/deviceReport/latestData' + param,
    method: 'get',
    data,
  });
}

export function getLatestData(data) {
  const param =
    '?deviceCode=' +
    data.deviceCode +
    '&page=' +
    data.page +
    '&size=' +
    data.size;
  return request({
    url: '/deviceData/latestData' + param,
    method: 'get',
    data,
  });
}

export function getLatestDataByDevices(data) {
  const param = '?deviceCode=' + data.deviceCode + '&dataKey=' + data.dataKey;
  return request({
    url: '/deviceData/getLatestDataByDevice' + param,
    method: 'get',
    data,
  });
}

export function getHistoryData(data) {
  return request({
    url:
      '/deviceData/getHistoryData?dataKey=' +
      data.dataKey +
      '&start=' +
      data.start +
      '&deviceCode=' +
      data.deviceCode +
      '&end=' +
      data.end,
    method: 'get',
    data,
  });
}

export function getReportHistoryData(data) {
  return request({
    url:
      '/deviceData/getHistoryChartData?dataKey=' +
      data.dataKey +
      '&start=' +
      data.start +
      '&deviceCode=' +
      data.deviceCode +
      '&end=' +
      data.end,
    method: 'get',
    data,
  });
}
//根据项目查询项目下的乡镇街道列表
export function getTownsByProject(data) {
  return request({
    url: '/iot/device/getTownsByProject?projectId=' + data.projectId,
    method: 'get',
    data,
  });
}

//查询乡镇街道下的社区列表
export function getCommunitiesByTown(data) {
  return request({
    url: '/iot/device/getCommunitiesByTown?townCode=' + data.townCode,
    method: 'get',
    data,
  });
}

//站点导出(运维)
export function exportDevice(data) {
  var param = '?projectId=' + data.projectId;
  return baseURL + '/deviceReport/exportDevice' + param;
}

//站点导出(物联平台)
export function exportIotDevice(data) {
  var param = '?projectId=' + data.projectId;
  return baseURL + '/iot/device/exportDevice' + param;
}
//最新数据导出
export function exportData(data) {
  var param = '?deviceCode=' + data.deviceCode;
  return baseURL + '/deviceReport/exportData' + param;
}
//最新数据导出(物联平台)
export function exportIotData(data) {
  var param = '?deviceCode=' + data.deviceCode;
  return baseURL + '/iot/device/exportData' + param;
}
//根据站点code查询传感器列表
export function sensors(data) {
  return request({
    url: '/deviceData/sensors?deviceCode=' + data.deviceCode,
    method: 'get',
    data,
  });
}
//获取盒子列表
export function boxList(data) {
  return request({
    url: '/fbox/boxList',
    method: 'get',
    data,
  });
}
//盒子同步
export function fboxSync(data, projectId) {
  return request({
    url: '/fbox/sync?projectId=' + projectId,
    method: 'post',
    data,
  });
}
//根据站点编号和传输id查询传感器信息
export function sensorInfo(data) {
  var param = '?deviceCode=' + data.deviceCode + '&dataKey=' + data.dataKey;
  return request({
    url: '/deviceData/sensorInfo' + param,
    method: 'get',
    data,
  });
}
