//
export function traverseArr(arr, traverseObj, saveOld = false) {
  const newArr = [];
  if (arr.length !== 0) {
    arr.forEach((item) => {
      let obj = {};
      if (saveOld) {
        obj = {
          ...item,
        };
      }
      if (Object.keys(traverseObj).length !== 0) {
        for (const key in traverseObj) {
          if (typeof traverseObj[key] === "string") {
            obj[key] = item[traverseObj[key]];
          } else {
            if (item[key] && item[key].length !== 0) {
              obj[key] = traverseArr(item[key], traverseObj);
            }
          }
        }
      }
      newArr.push(obj);
    });
  }
  // console.log('newArr', newArr)
  return newArr;
}

export function deepClone(obj) {
  let result = typeof obj.splice === "function" ? [] : {};
  if (obj && typeof obj === "object") {
    for (let key in obj) {
      if (obj[key] && typeof obj[key] === "object") {
        result[key] = deepClone(obj[key]);
      } else {
        result[key] = obj[key];
      }
    }
    return result;
  }
  return obj;
}

export function deepCloneFilterString(obj, fillterKeys) {
  let result = typeof obj.splice === "function" ? [] : {};
  if (obj && typeof obj === "object") {
    for (let key in obj) {
      if (obj[key] && typeof obj[key] === "object") {
        result[key] = deepCloneFilterString(obj[key], fillterKeys);
      } else {
        result[key] = fillterKeys.includes(key) ? obj[key] : String(obj[key]);
      }
    }
    return result;
  }
  return obj;
}

export function sortBy(arr, key) {
  return arr.sort((x, y) => {
    const valueX = parseInt(x[key]);
    const valueY = parseInt(y[key]);
    if (valueX > valueY) {
      return 1;
    }
    if (valueX < valueY) {
      return -1;
    }
  });
}

export function filterCommon(arr, key) {
  let newData = {};
  arr.forEach((item) => {
    //新建属性名
    if (Object.keys(newData).indexOf("" + item[key]) === -1) {
      newData[item[key]] = [];
    }
    //对应插入属性值
    newData["" + item[key]].push(item);
  });
  return newData;
}

export function getdateToTime(date) {
  let now = new Date(parseInt(date)),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate(),
    hh = now.getHours(),
    mm = now.getMinutes();
  return `${y}-${m < 10 ? "0" + m : m}-${d < 10 ? "0" + d : d} ${hh}:${mm}`;
}

export function getdateToDate(date) {
  let now = new Date(parseInt(date)),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
  return `${y}${m < 10 ? "0" + m : m}${d < 10 ? "0" + d : d}`;
}

//计算两个时间之间的时间差 多少天时分秒
export function intervalTime(startTime) {
  const curTime = new Date().getTime(); //计算当前时间戳
  const date3 = curTime - startTime; //时间差的毫秒数
  //计算出相差天数
  const days = Math.floor(date3 / (24 * 3600 * 1000));
  //计算出小时数
  const leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  const hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  const leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  const minutes = Math.floor(leave2 / (60 * 1000));
  //计算相差秒数
  const leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  const seconds = Math.round(leave3 / 1000);
  return {
    days,
    hours,
    minutes,
    seconds,
  };
}

// 分片处理
export function batchLoadList(
  sourceData,
  dataTotal,
  splitNumber = 10,
  callback,
  complete
) {
  if (sourceData.length <= splitNumber) {
    typeof callback === "function" && callback(sourceData);
    typeof callback === "function" && complete();
    return;
  }
  let first = 0;
  const intTime = parseInt(String(dataTotal / splitNumber));
  const floatTime = sourceData.length % splitNumber;
  const total = intTime + (floatTime > 0 ? 1 : 0);
  for (let i = 0; i < total; i += 1) {
    const end = first + splitNumber;
    const splitArray = sourceData.slice(first, end);
    setTimeout(() => {
      typeof callback === "function" && callback(splitArray);
      if (i === total - 1) {
        // 分片处理完成
        typeof callback === "function" && complete();
      }
    });
    first = end;
  }
}

export function newFile(code) {
  const blob = new Blob([code], {
    type: "application/vnd.ms-excel",
  });
  const linkNode = document.createElement("a");
  linkNode.style.display = "none";
  linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
  document.body.appendChild(linkNode);
  linkNode.click(); //模拟在按钮上的一次鼠标单击
  URL.revokeObjectURL(linkNode.href); // 释放URL 对象
  document.body.removeChild(linkNode);
}
