function gather(
  data: App.Health.Index.Data[],
  transform: (model: App.Health.Index.Model) => any,
  skip = false,
  link = { 血压: ['收缩压', '舒张压'], }
) {
  const ref = ((link) => {
    const ref: Record<string, { id: string; name: string; }> = {};
    Object.entries(link).forEach(([name, keys]) => {
      const _ = { id: name, name };
      keys.forEach(key => { ref[key] = _; })
    })
    return ref;
  })(link);

  const cache = new Map<{ id: string; name: string; }, App.Health.Index.Ruler[]>();
  for (const item of data) {
    if (skip && item.isAuto === 'Y') continue;
    const model = createHealthIndex(item);
    const _ = ref[model.name] ?? { id: model.id, name: model.name };
    if (ref[model.id]) model.name = model.name.replace(_.name, '');
    if (cache.has(_)) {
      cache.get(_)?.push(transform(model))
    } else {
      cache.set(_, [transform(model)])
    }
  }
  return [...cache].map(gather => ({ ...gather[0], options: gather[1] }))
}

export function healthIndex2Ruler(model: App.Health.Index.Model): App.Health.Index.Ruler {
  let { scope, range, precision, values, ...ruler } = model;
  const min = range?.[0] ?? Math.floor(scope[0] * 0.5);
  const max = range?.[1] ?? Math.floor(scope[1] * 1.5);
  return {
    ...ruler, min, max, precision,
    markLine: [
      { value: scope[0], style: {} },
      { value: scope[1], style: {} },
    ],
    value: values?.slice(-1)[0]?.value,
  }
}

export function healthIndex2Progress(model: App.Health.Index.Model[]): AnyObject[] {
  return model.filter((item) => item.values?.length).map(item => {
    const { scope, range, values } = item;
    const { value, abnormal, description = '' } = values?.slice(-1)[0]!
    // 修订 range[1] 值
    range[1] = Math.min(range[1], Math.floor(Math.max(value, scope[1]) * 1.3));

    const length = range[1] - range[0];

    const scopeOffsetLeft = `${(scope[0] - range[0]) * 100 / length}%`;
    const scopeOffsetRight = `${100 - (scope[1] - range[0]) * 100 / length}%`;

    let valueOffset = Math.floor((value - range[0]) * 100 / length);
    let valueOffsetLeft = '0';
    let valueOffsetRight = '0';
    let valueType;

    if (value > scope[1]) {
      valueType = 'big';
      valueOffsetLeft = scopeOffsetLeft;
      valueOffsetRight = `${100 - valueOffset}%`;
    } else if (value < scope[0]) {
      valueType = 'small';
      valueOffsetLeft = `${valueOffset}%`;
      valueOffsetRight = scopeOffsetRight;
    } else {
      valueType = 'normal';
      valueOffsetLeft = scopeOffsetLeft;
      valueOffsetRight = scopeOffsetRight;
    }

    const a = {
      name: item.name, unit: item.unit,
      value, min: scope[0], max: scope[1],
      abnormal, description: description?.replace(item.name, ''),
      scopeOffsetLeft, scopeOffsetRight,
      valueType, valueOffset: `${valueOffset}%`,
      valueOffsetLeft, valueOffsetRight,
    }
    console.log(a, '12345-->', length);
    
    return a;
  })
}



export function createHealthIndex(data: App.Health.Index.Data): App.Health.Index.Model {
  const { quotaId: id, minVal, maxVal, inputMin, inputMax, inputPrecision, patientQuotaRecordDTOS, ...model } = data;
  const scope = [+minVal, +maxVal] as const;
  const range = [
    +inputMin!/*! || Math.floor(scope[0] * 0.75)*/,
    +inputMax!/*! || Math.floor(scope[1] * 1.25)*/,
  ]
  return {
    ...model, id,
    precision: ({ 0: 0.01, 1: 0.1, 2: 2, 3: 1 } as any)[inputPrecision ?? 3],
    range, scope,
    values: patientQuotaRecordDTOS?.map(item => {
      return { value: item.quotaVal, abnormal: item.abnormal, description: item.abnormalDesc, date: item.time2 }
    })?.filter(item => !!item.value),
    defaultValue: 12
  } as App.Health.Index.Model
}


export function transformHealthIndex2Ruler(data: App.Health.Index.Data[]): { id: string, name: string, options: App.Health.Index.Ruler[] }[] {
  return gather(data, healthIndex2Ruler, true);
}

export function healthIndex2Chart(model: App.Health.Index.Model): AnyObject {
  return {
    name: model.name,
    type: 'line', smooth: true,
    data: model.values?.map(t => [t.date, t.value]),
    markLine: {
      data: model.scope.map(value => { return { yAxis: value } })
    }
  }
}

export function transformHealthIndex2Chart(data: App.Health.Index.Data[]): AnyObject[] {
  const list = gather(data, healthIndex2Chart)
  const charts = [];
  for (const item of list) {
    const xAxis = { type: 'category', axisLabel: { overflow: 'breakAll' } }
    const yAxis = { type: 'value', scale: true, }
    const series = item.options.filter((item: any) => item.data?.length)
    if (!series.length) continue;
    charts.push({
      id: item.id,
      title: { text: `${item.name}` },
      tooltip: { trigger: 'axis' },
      legend: { show: series.length > 1 },
      xAxis, yAxis,
      series,
    })
  }
  return charts;
}