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

  const cache = new Map<{ id: string; name: string; }, App.Health.Index.Chart[]>();
  for (const item of data) {
    if (skip && item.isAuto) 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, cache.get(_)?.length));
    } else {
      cache.set(_, [transform(model)]);
    }
  }
  return [...cache].map(gather => ({ ...gather[0], options: gather[1] }))
}

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: (+inputPrecision!) || 1,
    range, scope,
    values: patientQuotaRecordDTOS?.map(item => {
      return { value: +item.quotaVal, abnormal: item.abnormal, description: item.abnormalDesc, date: item.time2 }
    })?.filter(item => !!item.value),
  } as App.Health.Index.Model
}

export function healthIndex2Chart(model: App.Health.Index.Model, index = 0): AnyObject {
  const data = model.values?.map(t => [t.date, t.value]) ?? [];
  const yAxis = {
    name: model.unit,
    nameTextStyle: { color: '#ccc' },
    type: 'value', scale: true,
    min: model.range[0],
    max: Math.min(
      model.range[1],
      Math.floor(Math.max(model.scope[1], ...data.map((item: AnyArray) => item[1])) * 2)
    ),
    minInterval: model.precision >= 1 ? model.precision : void 0,
    axisLine: { show: true }
  }
  const series = {
    name: model.name,
    yAxisIndex: 0,
    type: 'line', smooth: true,
    data,
    markLine: {
      data: model.scope.map(value => { return { yAxis: value } })
    }
  }
  const visualMap = {
    show: false,
    type: 'piecewise',
    pieces: [
      { gt: model.scope[1] },
      { gte: model.scope[0], lte: model.scope[1] },
      { lt: model.scope[0] }
    ]
  }

  return { yAxis, series, visualMap }
}

export function transformHealthIndex2Chart(data: App.Health.Index.Data[]): AnyObject[] {
  const list = gather(data, healthIndex2Chart)
  console.log(list, 'transformHealthIndex2Chart-->list');

  const charts = [];
  for (const item of list) {
    const xAxis = { type: 'category', axisLabel: { overflow: 'breakAll' } }
    const yAxis = [], series = [], visualMap = [];
    for (const option of item.options) {
      yAxis.push(option.yAxis);
      visualMap.push(option.visualMap);
      if (option.series.data?.length) series.push(option.series);
    }
    if (!series.length) continue;
    charts.push({
      id: item.id,
      title: { text: `${item.name}` },
      tooltip: { trigger: 'axis' },
      legend: { show: series.length > 1 },
      grid: { bottom: 25 },
      xAxis, yAxis: yAxis[0],
      series, visualMap,
    })
  }
  return charts;
}