ListingChangeHistory.tsx
9.2 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import type { StoreChangeLog } from '@/api';
import { STORE_STATUS, type StoreStatus } from '@/config/storeStatus';
import type { StoreDictionaries } from './dictionaries';
import { dictionaryLabel, formatMoney } from './formatters';
import './listingHistory.css';
const EMPTY = '—';
const FIELD_LABELS: Record<string, string> = {
storeName: '门店名称',
merchantNo: '门店商户号',
status: '上架状态',
businessScene: '业务场景',
productCategory: '产品分类',
productCategories: '产品分类',
categoryCount: '产品分类数',
costDecoration: '装修、设备总投入',
costMonthly: '当月产生费用',
costLabor: '人工成本',
costTotal: '运营成本合计',
};
const COST_FIELDS = new Set(['costDecoration', 'costMonthly', 'costLabor', 'costTotal']);
const COST_LABELS = new Set([
'运营成本',
'装修、设备总投入',
'当月产生费用',
'人工成本',
'运营成本合计',
]);
const POLICY_LABELS: Record<string, string> = {
marketFee1: '管理层市场费 1',
marketFee2: '管理层市场费 2',
sunshine1: '一线阳光激励费 1',
sunshine2: '一线阳光激励费 2',
};
const POLICY_SUFFIX_LABELS: Record<string, string> = {
CalcMode: '计算模式',
CalcWay: '计算方式',
Value: '数值',
Unit: '单位',
OnlinePoint: '开启线上积分',
};
interface HistoryGroup {
key: string;
version?: number;
changes: StoreChangeLog[];
effectiveTime?: string;
}
function policyField(field: string) {
return field.match(/^(marketFee[12]|sunshine[12])(CalcMode|CalcWay|Value|Unit|OnlinePoint)$/);
}
function fieldLabel(field: string): string {
if (Object.hasOwn(FIELD_LABELS, field)) return FIELD_LABELS[field];
const policy = policyField(field);
if (policy) return `${POLICY_LABELS[policy[1]]} ${POLICY_SUFFIX_LABELS[policy[2]]}`;
return field || '变更字段未提供';
}
function changeCategory(field: string): string {
if (COST_FIELDS.has(field) || COST_LABELS.has(field)) return '运营成本';
if (policyField(field) || /市场费|阳光激励|激励计算|激励政策/.test(field)) return '激励政策';
if (field === 'businessScene' || field === '业务场景') return '业务场景';
if (
['productCategory', 'productCategories', 'categoryCount', '产品分类', '产品分类数'].includes(
field,
)
) {
return '产品分类';
}
return fieldLabel(field);
}
function isFirstListing(group: HistoryGroup): boolean {
return (
group.version === 1 &&
group.changes.some(
(change) =>
change.changeField === 'status' &&
change.afterValue === 'APPROVED' &&
change.beforeValue !== 'APPROVED',
)
);
}
function groupTitle(group: HistoryGroup): string {
if (isFirstListing(group)) return '首次上架';
const categories = [
...new Set(group.changes.map((change) => changeCategory(change.changeField || ''))),
];
if (
categories.length === 2 &&
categories.includes('激励政策') &&
categories.includes('运营成本')
) {
return '激励政策与运营成本调整';
}
if (
categories.length === 2 &&
categories.includes('业务场景') &&
categories.includes('产品分类')
) {
return '业务场景与产品分类调整';
}
if (categories.every((category) => category === '变更字段未提供')) return '门店信息变更';
return `${categories.join('与')}调整`;
}
function dateText(value?: string, includeTime = false): string {
const match = value?.trim().match(/^(\d{4}-\d{2}-\d{2})(?:[T ](\d{2}:\d{2}))?/);
if (!match) return EMPTY;
return includeTime && match[2] ? `${match[1]} ${match[2]}` : match[1];
}
function groupHistory(history: StoreChangeLog[]): HistoryGroup[] {
const groups: HistoryGroup[] = [];
const versions = new Map<number, HistoryGroup>();
history.forEach((change, index) => {
const version =
typeof change.versionNo === 'number' &&
Number.isSafeInteger(change.versionNo) &&
change.versionNo > 0
? change.versionNo
: undefined;
let group = version === undefined ? undefined : versions.get(version);
if (!group) {
group = {
key: version === undefined ? `unknown-${index}` : `version-${version}`,
version,
changes: [],
};
groups.push(group);
if (version !== undefined) versions.set(version, group);
}
group.changes.push(change);
const effectiveTime = change.effectiveTime?.trim();
if (
effectiveTime &&
dateText(effectiveTime) !== EMPTY &&
(!group.effectiveTime ||
effectiveTime.replace('T', ' ') > group.effectiveTime.replace('T', ' '))
) {
group.effectiveTime = effectiveTime;
}
});
return groups.sort((a, b) => {
if (a.version !== undefined && b.version !== undefined) return a.version > b.version ? -1 : 1;
if (a.version !== undefined) return -1;
if (b.version !== undefined) return 1;
return (b.effectiveTime || '').localeCompare(a.effectiveTime || '');
});
}
function categoryValue(value: string, spec?: StoreDictionaries): string {
// Multi-category audit values may be serialized arrays; preserve other API text verbatim.
if (value.trim().startsWith('[')) {
try {
const categories: unknown = JSON.parse(value);
if (
Array.isArray(categories) &&
categories.every((category) => typeof category === 'string')
) {
return categories.length
? categories
.map((category) => dictionaryLabel(spec?.productCategories, category))
.join(' / ')
: EMPTY;
}
} catch {
return value;
}
}
return dictionaryLabel(spec?.productCategories, value);
}
function changeValue(field: string, value: unknown, spec?: StoreDictionaries): string {
if (value === null || value === undefined || String(value).trim() === '') return EMPTY;
const text = String(value);
if (COST_FIELDS.has(field)) {
const formatted = formatMoney(value);
return formatted === EMPTY ? text : formatted;
}
if (field === 'status') {
return Object.hasOwn(STORE_STATUS, text) ? STORE_STATUS[text as StoreStatus].label : text;
}
if (field === 'businessScene') return dictionaryLabel(spec?.businessScenes, text);
if (field === 'productCategory' || field === 'productCategories')
return categoryValue(text, spec);
const policy = policyField(field);
if (policy?.[2] === 'CalcMode') return dictionaryLabel(spec?.calcModes, text);
if (policy?.[2] === 'CalcWay') return dictionaryLabel(spec?.calcWays, text);
if (policy?.[2] === 'OnlinePoint') return dictionaryLabel(spec?.onlinePointFlags, text);
if (policy?.[2] === 'Unit') return text === 'AMOUNT' ? '金额' : text === 'RATIO' ? '比例' : text;
// Policy values carry no unit in the audit DTO; do not infer money or percentages.
return text;
}
export function ListingChangeHistory({
history,
spec,
}: {
history: StoreChangeLog[] | null | undefined;
spec?: StoreDictionaries;
}) {
const groups = groupHistory(history || []);
return (
<section className="listing-change-history" aria-label="变更历史(只读)">
<h3 className="listing-change-history__heading">变更历史(只读)</h3>
{groups.length ? (
groups.map((group) => {
const versionLabel = group.version === undefined ? '版本未提供' : `v${group.version}`;
const users = [
...new Set(
group.changes.map((change) => change.changeUserName?.trim()).filter(Boolean),
),
];
return (
<article
key={group.key}
className="listing-change-history__version"
aria-label={group.version === undefined ? versionLabel : `版本 ${versionLabel}`}
>
<div className="listing-change-history__version-meta">
<strong>{versionLabel}</strong>
<span>{dateText(group.effectiveTime)}</span>
</div>
<div className="listing-change-history__content">
<h4>{groupTitle(group)}</h4>
{isFirstListing(group) && (
<p className="listing-change-history__description">
门店在系统中完成首次上架并生效。
</p>
)}
<dl className="listing-change-history__changes">
{group.changes.map((change, index) => {
const field = change.changeField?.trim() || '';
return (
<div key={`${change.changeId ?? 'change'}-${index}`}>
<dt>{fieldLabel(field)}</dt>
<dd>
{changeValue(field, change.beforeValue, spec)} →{' '}
{changeValue(field, change.afterValue, spec)}
</dd>
</div>
);
})}
</dl>
<p className="listing-change-history__audit">
变更人:{users.join('、') || EMPTY} · 生效时间{' '}
{dateText(group.effectiveTime, true)}
</p>
</div>
</article>
);
})
) : (
<p className="listing-change-history__empty">暂无变更历史</p>
)}
</section>
);
}