// 此文件为算法实验文件，用来验证算法的正确性与稳定性

// 合并数组
function mergeArray() {
  // 原始数组
  const arr = [
    { saleOrg: 'org1', saleZone: 'zone1', custom: 'custom1', creator: 'creator1', totalGoal: 100 },
    { saleOrg: 'org1', saleZone: 'zone1', custom: 'custom2', creator: 'creator1', totalGoal: 200 },
    { saleOrg: 'org2', saleZone: 'zone2', custom: 'custom3', creator: 'creator3', totalGoal: 300 },
    { saleOrg: 'org2', saleZone: 'zone2', custom: 'custom4', creator: 'creator3', totalGoal: 400 },
    { saleOrg: 'org3', saleZone: 'zone3', custom: 'custom5', creator: 'creator5', totalGoal: 500 }
  ];
// 根据saleOrg、saleZone和creator属性进行合并并相加totalGoal的函数
  const mergeAndSumTotalGoal = (array) => {
    return Array.from(array.reduce((map, obj) => {
      const key = `${obj.saleOrg}-${obj.saleZone}-${obj.creator}`;
      if (map.has(key)) {
        const existingObj = map.get(key);
        existingObj.totalGoal += obj.totalGoal;
      } else {
        map.set(key, { ...obj });
      }
      return map;
    }, new Map()).values());
  };
// 调用合并函数
  const mergedArray = mergeAndSumTotalGoal(arr);
  console.log(mergedArray);
}

// 计算子表小计
export function getSummary(param) {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = '小计';
      return;
    }
    const values = data.map(item => Number(item[column.property]));
    if (!values.every(value => isNaN(value))) {
      sums[index] = values.reduce((prev, curr) => {
        const value = Number(curr);
        if (!isNaN(value)) {
          return ((prev * 10000000 + curr * 10000000) / 10000000).toFixed(2);
        } else {
          return prev;
        }
      }, 0);
      sums[index] += '';
    } else {
      sums[index] = '*';
    }
  });

  return sums;
}
