wangrong
2026-09-26 3c7f5d8d9cc64f1d459f5e199f556a5d1e68ce49
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
import type { ScreenData, TimeRange } from './screen';
import { FieldTracker, resolveDataMode, type ScreenDataMode } from './screen-field-meta';
import { structureScreen } from './screen-structure';
 
export type { ScreenDataMode };
 
export interface ResolvedScreenData {
    data: ScreenData;
    mode: ScreenDataMode;
    demoFields: string[];
    liveFields: string[];
}
 
function num(v: unknown, fallback = 0) {
    const n = Number(v);
    return Number.isFinite(n) ? n : fallback;
}
 
function alignSeries(source: number[] | undefined, len: number) {
    const values = (source || []).map(v => num(v));
    if (!values.length) return Array.from({ length: len }, () => 0);
    if (values.length === len) return values;
    if (values.length > len) return values.slice(values.length - len);
    return [...Array.from({ length: len - values.length }, () => 0), ...values];
}
 
/** 用接口结果覆盖零值结构。缺字段保持 0 或空列表,不再回落到演示数据。 */
export function resolveScreenData(live: ScreenData | null | undefined, range: TimeRange): ResolvedScreenData {
    const base = structureScreen(range);
    const ctx = new FieldTracker();
 
    if (!live) {
        return {
            data: base,
            mode: 'live',
            demoFields: [],
            liveFields: []
        };
    }
 
    const axis = live.axis?.length ? live.axis : base.axis;
    ctx.markLive('axis');
 
    const kpis = { ...base.kpis };
    Object.keys(kpis).forEach(key => {
        if (live.kpis && live.kpis[key] != null) {
            kpis[key] = num(live.kpis[key]);
        }
        ctx.markLive(`kpis.${key}`);
    });
 
    const wasteMix = live.wasteMix?.length
        ? live.wasteMix.map((item, i) => ({
                ...base.wasteMix[i],
                ...item,
                weight: num(item.weight),
                value: num(item.value),
                count: num(item.count),
                color: item.color || base.wasteMix[i]?.color
            }))
        : base.wasteMix;
    ctx.markLive('wasteMix');
 
    const recycleItems = live.recycleItems?.length
        ? live.recycleItems.map((item, i) => ({
                ...base.recycleItems[i],
                ...item,
                weight: num(item.weight),
                value: num(item.value),
                carbon: num(item.carbon),
                color: item.color || base.recycleItems[i]?.color
            }))
        : base.recycleItems;
    ctx.markLive('recycleItems');
 
    const trend = {
        weight: alignSeries(live.trend?.weight, axis.length),
        value: alignSeries(live.trend?.value, axis.length),
        carbon: alignSeries(live.trend?.carbon, axis.length)
    };
    ctx.markLive('trend');
 
    const yearCarbon = live.yearCarbon?.length
        ? alignSeries(live.yearCarbon, 12)
        : base.yearCarbon;
    ctx.markLive('yearCarbon');
 
    const deptRank = Array.isArray(live.deptRank) ? live.deptRank : [];
    const classRank = Array.isArray(live.classRank) ? live.classRank : [];
    const majorRank = Array.isArray(live.majorRank) ? live.majorRank : [];
    ctx.markLive('deptRank');
    ctx.markLive('classRank');
    ctx.markLive('majorRank');
 
    const pointFlow = {
        issued: num(live.pointFlow?.issued),
        transfer: num(live.pointFlow?.transfer),
        donate: num(live.pointFlow?.donate),
        mall: num(live.pointFlow?.mall),
        balance: num(live.pointFlow?.balance)
    };
    ctx.markLive('pointFlow');
 
    const participateTrend = alignSeries(live.participateTrend, axis.length);
    const deliverTrend = alignSeries(live.deliverTrend, axis.length);
    ctx.markLive('participateTrend');
    ctx.markLive('deliverTrend');
 
    const ordersByType = live.ordersByType?.axis?.length
        ? {
                axis: live.ordersByType.axis,
                现场回收: alignSeries(live.ordersByType.现场回收, live.ordersByType.axis.length),
                预约上门: alignSeries(live.ordersByType.预约上门, live.ordersByType.axis.length),
                大屏投递: alignSeries(live.ordersByType.大屏投递, live.ordersByType.axis.length),
                活动回收: alignSeries(live.ordersByType.活动回收, live.ordersByType.axis.length)
            }
        : base.ordersByType;
    ctx.markLive('ordersByType');
 
    const appointmentFlow = live.appointmentFlow?.length ? live.appointmentFlow : base.appointmentFlow;
    const sorterStatus = live.sorterStatus?.length ? live.sorterStatus : base.sorterStatus;
    const vehicles = Array.isArray(live.vehicles) ? live.vehicles : [];
    const sites = Array.isArray(live.sites) ? live.sites : [];
    ctx.markLive('appointmentFlow');
    ctx.markLive('sorterStatus');
    ctx.markLive('vehicles');
    ctx.markLive('sites');
 
    const data: ScreenData = {
        range,
        axis,
        kpis,
        wasteMix,
        recycleItems,
        trend,
        yearCarbon,
        deptRank,
        classRank,
        majorRank,
        pointFlow,
        participateTrend,
        deliverTrend,
        ordersByType,
        appointmentFlow,
        sorterStatus,
        vehicles,
        sites,
        unavailable: live.unavailable || []
    };
 
    return {
        data,
        mode: resolveDataMode(live, ctx),
        demoFields: [],
        liveFields: ctx.listLive()
    };
}
 
/** 接口尚未返回时先展示零值,避免先闪出演示数字 */
export function initialScreenData(range: TimeRange = 'month') {
    return resolveScreenData(null, range);
}
 
export { structureScreen, buildScreenAxis } from './screen-structure';