刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import type { EChartsOption } from 'echarts';
 
export interface WaterTrendItem {
  id: string;
  name: string;
  trend_enabled: boolean;
  unit: string;
}
 
export interface WaterTrendRecord {
  ceshi_jieguo: null | number;
  jianyan_xiang_id: string;
  jianyan_riqi: string;
  jianyan_xiangmu_id: string;
  quyangdian_id: string;
  quyangdian_mingcheng: string;
  renwu_jianyan_xiang_kuaizhao_id: string;
}
 
export interface WaterTrendStandard {
  biaozhun_banben?: string;
  biaozhun_bianhao?: string;
  biaozhun_shangxian?: null | number;
  biaozhun_xiaxian?: null | number;
  jianyan_xiangmu_id: string;
  renwu_jianyan_xiang_kuaizhao_id: string;
  trend_enabled: boolean;
  unit: string;
}
 
export interface WaterTrendSeries {
  data: Array<null | number>;
  id: string;
  kind: 'limit' | 'record';
  name: string;
}
 
export type WaterTrendChartType = 'bar' | 'line';
 
export interface WaterTrendModel {
  categories: string[];
  item: WaterTrendItem;
  key: string;
  series: WaterTrendSeries[];
  standard: WaterTrendStandard;
  unit: string;
}
 
interface BuildWaterTrendModelsOptions {
  items: WaterTrendItem[];
  records: WaterTrendRecord[];
  standards: WaterTrendStandard[];
}
 
export function buildWaterTrendModels({ items, records, standards }: BuildWaterTrendModelsOptions): WaterTrendModel[] {
  const standardsBySnapshotId = new Map(
    standards
      .filter((standard) => standard.trend_enabled && standard.renwu_jianyan_xiang_kuaizhao_id)
      .map((standard) => [standard.renwu_jianyan_xiang_kuaizhao_id, standard]),
  );
 
  return items.flatMap((item) => {
    if (!item.trend_enabled) {
      return [];
    }
 
    const recordsByStandard = new Map<string, { records: WaterTrendRecord[]; standard: WaterTrendStandard }>();
    for (const record of records) {
      if (record.jianyan_xiangmu_id !== item.id || !record.jianyan_riqi) {
        continue;
      }
      const standard = standardsBySnapshotId.get(record.renwu_jianyan_xiang_kuaizhao_id);
      if (!standard || standard.jianyan_xiangmu_id !== item.id) {
        continue;
      }
      const signature = buildStandardSignature(standard);
      const group = recordsByStandard.get(signature);
      if (group) {
        group.records.push(record);
      } else {
        recordsByStandard.set(signature, { records: [record], standard });
      }
    }
 
    return [...recordsByStandard.entries()].map(([signature, group]) => buildWaterTrendModel(item, signature, group.standard, group.records));
  });
}
 
function buildStandardSignature(standard: WaterTrendStandard) {
  return [
    standard.biaozhun_bianhao || '',
    standard.biaozhun_banben || '',
    standard.biaozhun_xiaxian ?? '',
    standard.biaozhun_shangxian ?? '',
    standard.unit || '',
  ].join('|');
}
 
function buildWaterTrendModel(
  item: WaterTrendItem,
  standardSignature: string,
  standard: WaterTrendStandard,
  records: WaterTrendRecord[],
): WaterTrendModel {
  const orderedRecords = [...records].sort(
    (left, right) =>
      left.jianyan_riqi.localeCompare(right.jianyan_riqi) || left.jianyan_xiang_id.localeCompare(right.jianyan_xiang_id),
  );
  const categories = orderedRecords.map((record) => record.jianyan_riqi);
  const series: WaterTrendSeries[] = [];
 
  const addLimitSeries = (value: null | number | undefined, id: string, name: string) => {
    if (typeof value === 'number' && Number.isFinite(value)) {
      series.push({ data: categories.map(() => value), id, kind: 'limit', name });
    }
  };
 
  addLimitSeries(standard.biaozhun_xiaxian, 'lower-limit', '最低限');
  addLimitSeries(standard.biaozhun_shangxian, 'upper-limit', '最高限');
 
  const recordsByPoint = new Map<string, Map<string, WaterTrendRecord>>();
  for (const record of orderedRecords) {
    const pointRecords = recordsByPoint.get(record.quyangdian_id) || new Map<string, WaterTrendRecord>();
    pointRecords.set(record.jianyan_xiang_id, record);
    recordsByPoint.set(record.quyangdian_id, pointRecords);
  }
 
  for (const [pointId, pointRecords] of recordsByPoint) {
    series.push({
      data: orderedRecords.map((record) => {
        const value = pointRecords.get(record.jianyan_xiang_id)?.ceshi_jieguo;
        return typeof value === 'number' && Number.isFinite(value) ? value : null;
      }),
      id: `point-${pointId}`,
      kind: 'record',
      name: pointRecords.values().next().value?.quyangdian_mingcheng || pointId,
    });
  }
 
  return {
    categories,
    item,
    key: `${item.id}:${standardSignature}`,
    series,
    standard,
    unit: standard.unit || item.unit,
  };
}
 
export function createWaterTrendOption(model: WaterTrendModel, title: string, chartType: WaterTrendChartType = 'line'): EChartsOption {
  const recordNameCounts = model.series.reduce((counts, series) => {
    if (series.kind === 'record') {
      counts.set(series.name, (counts.get(series.name) || 0) + 1);
    }
    return counts;
  }, new Map<string, number>());
  const chartSeries = model.series.map((series) => ({
    ...series,
    name: series.kind === 'record' && (recordNameCounts.get(series.name) || 0) > 1 ? `${series.name}(${series.id.replace('point-', '')})` : series.name,
  }));
 
  return {
    color: ['#d94a4a', '#356fc5', '#12a594', '#ed9b23', '#7658ab', '#5c7080'],
    graphic: {
      bottom: 4,
      left: 'center',
      style: {
        fill: '#666',
        font: '13px sans-serif',
        text: `标准编号:${model.standard.biaozhun_bianhao || '-'};版本:${model.standard.biaozhun_banben || '-'}`,
      },
      type: 'text',
    },
    grid: { bottom: 104, containLabel: true, left: 70, right: 40, top: 86 },
    legend: {
      bottom: 34,
      data: chartSeries.map((series) => series.name),
      type: 'scroll',
    },
    series: chartSeries.map((series) => {
      const type = series.kind === 'limit' ? 'line' : chartType;
      return {
        ...(type === 'bar' ? { barMaxWidth: 36 } : {}),
        data: series.data,
        id: series.id,
        lineStyle: series.kind === 'limit' ? { type: 'dashed', width: 2 } : { width: 2 },
        name: series.name,
        symbol: series.kind === 'limit' ? 'none' : 'circle',
        type,
      };
    }),
    title: {
      left: 'center',
      text: title,
      textStyle: { fontSize: 20, fontWeight: 700 },
    },
    toolbox: { feature: { saveAsImage: {} }, right: 16, top: 14 },
    tooltip: { trigger: 'axis' },
    xAxis: {
      axisLabel: {
        interval: 0,
        rotate: model.categories.length > 8 ? 35 : 0,
      },
      data: model.categories,
      name: '时间',
      type: 'category',
    },
    yAxis: {
      name: model.unit || '检测值',
      nameGap: 18,
      type: 'value',
    },
  };
}