//设备列表导出  list=>json数据   arr => 表头和key的对应关系 {name:'中文表头',  value:'key'}     name => 导出的文件名
export function batchDownload(list, arr, name) {
  let string = ''
  if (list.length > 0) {
    string = '序号\t';
  }
  for (let item of arr) {
    string += item.name + '\t'
  }
  string += '\n';
  for (let i = 0; i < list.length; i++) {
    string += (i + 1).toString() + '\t';
    for (let item of arr) {
      // string += list[i][item.value]+'\t'
      string += getValue(list[i], item.value) + '\t'
    }
    string += '\n'
  }
  let blob = new Blob([string], { type: "text/plain;charset=utf-8" });
  let excel_url = window.URL.createObjectURL(blob);
  let link = document.createElement("a");
  link.href = excel_url;
  link.download = name + ".xls"
  document.body.appendChild(link);
  link.click()
  document.body.removeChild(link);
}
//获得嵌套对象的值
function getValue(obj, stringKey) {
  let arr = stringKey.split('.');
  let index = 0;
  function doGetValue(item) {
    if (item[arr[index]] && index <= arr.length - 2) {
      return doGetValue(item[arr[index++]])
    } else {
      return item[arr[index]] === null || item[arr[index]] === undefined ? '' : item[arr[index]]
    }
  }
  return doGetValue(obj)
}
