/**
 * 组装统计信息文字
 *
 * @param {*} total
 * @param {string} [keys=["num", "volume", "weight"]]
 * @return {*}
 */
function getTotlContent(total, keys = ['num', 'volume', 'weight']) {
  let content = [];
  for (const key of keys) {
    if (key === 'num') {
      content.push(`${total?.num ?? 0}箱`);
    }
    if (key === 'volume') {
      content.push(`${total?.volume ?? 0}m³`);
    }
    if (key === 'weight') {
      content.push(`${total?.weight ?? 0}kg`);
    }
  }
  return content.join(' ');
}

function sumStatistics(val) {
  let count = { num: 0, volume: 0, weight: 0 };
  if (Array.isArray(val)) {
    val.forEach((item) => {
      const { secStatistics } = item;
      if (secStatistics) {
        if (!Number.isNaN(Number(secStatistics.num))) {
          count.num = count.num + Number(secStatistics.num);
        }
        if (!Number.isNaN(Number(secStatistics.volume))) {
          count.volume = count.volume + Number(secStatistics.volume);
        }
        if (!Number.isNaN(Number(secStatistics.weight))) {
          count.weight = count.weight + Number(secStatistics.weight);
        }
      }
    });
  }
  return getTotlContent(count);
}

export { getTotlContent, sumStatistics };
