@tbox.cn/app-module-parking 0.1.0 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +16 -0
- package/README.md +58 -11
- package/dist/chunk-KEUD5YHH.js +1087 -0
- package/dist/chunk-QHLX6WXM.js +2667 -0
- package/dist/client/index.d.ts +17 -3
- package/dist/client/index.js +4 -2
- package/dist/index.d.ts +4 -3
- package/dist/index.js +14 -4
- package/dist/parking-2S2UPCOF.css +1306 -0
- package/dist/parking-guide-hero-pea-v2-X2O44LPQ.jpg +0 -0
- package/dist/server/index.d.ts +1216 -72
- package/dist/server/index.js +11 -3
- package/package.json +24 -16
- package/skills/parking/SKILL.md +165 -0
- package/skills/parking/references/payment-safety.md +25 -0
- package/src/client/assets/parking-guide-hero-pea-v2.jpg +0 -0
- package/src/client/assets/parking-guide-hero.jpg +0 -0
- package/src/client/assets.d.ts +5 -0
- package/src/client/cards/index.ts +8 -0
- package/src/client/cards/parking-info/index.tsx +41 -0
- package/src/client/cards/parking-payment/benefit-selection.ts +79 -0
- package/src/client/cards/parking-payment/index.tsx +442 -0
- package/src/client/cards/parking-payment-records/index.tsx +259 -0
- package/src/client/cards/parking-payment-result/index.tsx +83 -0
- package/src/client/cards/parking-vehicle-management/PlateKeyboard.tsx +106 -0
- package/src/client/cards/parking-vehicle-management/index.tsx +344 -0
- package/src/client/cards/parking-vehicle-management/plate-keyboard.ts +113 -0
- package/src/client/cards/view.ts +83 -0
- package/src/client/index.ts +19 -4
- package/src/client/styles/parking.css +1306 -0
- package/src/client/types/css.d.ts +2 -0
- package/src/server/actions.ts +975 -0
- package/src/server/cards/index.ts +14 -0
- package/src/server/cards/parking-info/meta.ts +19 -0
- package/src/server/cards/parking-info/samples.ts +50 -0
- package/src/server/cards/parking-payment/meta.ts +29 -0
- package/src/server/cards/parking-payment/samples.ts +311 -0
- package/src/server/cards/parking-payment-records/meta.ts +32 -0
- package/src/server/cards/parking-payment-records/samples.ts +86 -0
- package/src/server/cards/parking-payment-result/meta.ts +21 -0
- package/src/server/cards/parking-payment-result/samples.ts +84 -0
- package/src/server/cards/parking-vehicle-management/meta.ts +20 -0
- package/src/server/cards/parking-vehicle-management/samples.ts +73 -0
- package/src/server/domain.ts +279 -0
- package/src/server/handler.ts +47 -16
- package/src/server/index.ts +97 -17
- package/src/server/intent-context.ts +14 -0
- package/src/server/ports.ts +59 -0
- package/src/server/service.ts +474 -45
- package/src/server/tools.ts +846 -0
- package/tbox.module.json +107 -0
- package/tests/benefit-selection.test.ts +70 -0
- package/tests/cards-render.test.tsx +643 -0
- package/tests/dispatcher-flow.test.ts +256 -0
- package/tests/payment-flow.test.ts +420 -0
- package/tests/plate-keyboard.test.ts +158 -0
- package/tests/ports-resolve.test.ts +113 -0
- package/tests/pretool-parking-fee.test.ts +104 -0
- package/tests/samples.test.ts +44 -0
- package/tests/skills.test.ts +37 -0
- package/tests/tools-gate.test.ts +237 -0
- package/tests/tools-membership.test.ts +91 -0
- package/tests/view.test.ts +61 -0
- package/tsup.config.ts +9 -1
- package/dist/chunk-BYD3C6NE.js +0 -140
- package/dist/chunk-QQLBZY6D.js +0 -55
- package/src/client/cards/parking-fee/index.tsx +0 -59
- package/src/server/cards/parking-fee/meta.ts +0 -26
- package/src/server/tool.ts +0 -36
- package/tbox.component.json +0 -20
- package/tests/service.test.ts +0 -49
package/src/server/service.ts
CHANGED
|
@@ -1,59 +1,488 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* InMemory 停车服务(015 S5:新三 Port 形状;演示/单测/未装配 provider 降级)。
|
|
3
|
+
* mock 语义:
|
|
4
|
+
* - queryFee 按 plateNo 生成报价(3 小时 15 元演示费率;已在场记忆)
|
|
5
|
+
* - reprice 权益重选(member-95 折 / points-5 元抵扣 2 项演示权益),revision 递增
|
|
6
|
+
* - 支付三步闭环:createOrder → getPaymentSign(mock 签名串)→ uploadResult/reconcile
|
|
7
|
+
* (reconcile 以 uploadResult 上报为准——InMemory 模拟厂商回查;真实厂商语义见 provider 包 adapter)
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from 'node:crypto';
|
|
10
|
+
import type {
|
|
11
|
+
ParkingQueryPort,
|
|
12
|
+
ParkingPaymentPort,
|
|
13
|
+
ParkingVehiclePort,
|
|
14
|
+
QuoteSnapshot,
|
|
15
|
+
ParkingVehicle,
|
|
16
|
+
ParkingBenefit,
|
|
17
|
+
ParkingLocation,
|
|
18
|
+
ParkingRecordPage,
|
|
19
|
+
ParkingPaymentRecord,
|
|
20
|
+
ParkingInfo,
|
|
21
|
+
} from '@tbox.cn/app-contracts-mall';
|
|
22
|
+
import type { ParkingQueryContext } from '@tbox.cn/app-contracts-mall';
|
|
3
23
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
24
|
+
export interface ParkingService
|
|
25
|
+
extends ParkingQueryPort,
|
|
26
|
+
ParkingPaymentPort,
|
|
27
|
+
ParkingVehiclePort {
|
|
28
|
+
/** 测试观测用(InMemory 恒有;provider 请求级壳无此语义) */
|
|
29
|
+
readonly quoteCount?: number;
|
|
7
30
|
}
|
|
8
31
|
|
|
9
|
-
|
|
32
|
+
const DEMO_FEE_ITEMS = [{ name: '停车费(3 小时)', amountCents: 1500 }];
|
|
33
|
+
|
|
34
|
+
const DEMO_BENEFITS: ParkingBenefit[] = [
|
|
35
|
+
{
|
|
36
|
+
benefitRef: 'member-discount',
|
|
37
|
+
title: '会员 95 折',
|
|
38
|
+
description: '会员停车费 95 折',
|
|
39
|
+
autoSelected: true,
|
|
40
|
+
estimatedDiscountCents: 75,
|
|
41
|
+
kind: 'discount',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
benefitRef: 'points-offset',
|
|
45
|
+
title: '积分抵 5 元',
|
|
46
|
+
description: '500 积分抵 5 元停车费',
|
|
47
|
+
autoSelected: false,
|
|
48
|
+
estimatedDiscountCents: 500,
|
|
49
|
+
kind: 'points',
|
|
50
|
+
requiredPoints: 500,
|
|
51
|
+
previewPayableCents: 925,
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
|
|
10
55
|
export class InMemoryParkingService implements ParkingService {
|
|
11
|
-
|
|
12
|
-
private
|
|
56
|
+
private quotes = new Map<string, QuoteSnapshot>();
|
|
57
|
+
private quoteOwners = new Map<string, string>();
|
|
58
|
+
private paymentToQuote = new Map<
|
|
59
|
+
string,
|
|
60
|
+
{ quoteRef: string; ownerKey: string }
|
|
61
|
+
>();
|
|
62
|
+
private records: ParkingPaymentRecord[] = [];
|
|
63
|
+
private recordOwners = new Map<string, string>();
|
|
64
|
+
private boundVehicles: ParkingVehicle[] = [];
|
|
65
|
+
private vehicleOwners = new Map<string, string>();
|
|
66
|
+
private paymentResults = new Map<
|
|
67
|
+
string,
|
|
68
|
+
'success' | 'cancel' | 'failure' | 'unavailable'
|
|
69
|
+
>();
|
|
70
|
+
private paymentRefSeq = 0;
|
|
71
|
+
|
|
72
|
+
constructor(mallId = 'mall-demo') {
|
|
73
|
+
this.mallId = mallId;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private mallId: string;
|
|
77
|
+
|
|
78
|
+
get quoteCount(): number {
|
|
79
|
+
return this.quotes.size;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ===== ParkingQueryPort =====
|
|
83
|
+
|
|
84
|
+
async queryFee(
|
|
85
|
+
q: ParkingQueryContext,
|
|
86
|
+
input: { plateNo?: string; vehicleRef?: string; barcode?: string },
|
|
87
|
+
): Promise<readonly QuoteSnapshot[]> {
|
|
88
|
+
const ownerKey = this.ownerKey(q);
|
|
89
|
+
const plateNo =
|
|
90
|
+
input.plateNo ??
|
|
91
|
+
this.plateOfVehicleRef(q, input.vehicleRef) ??
|
|
92
|
+
input.barcode ??
|
|
93
|
+
'浙A12345';
|
|
94
|
+
const existing = [...this.quotes.values()].find(
|
|
95
|
+
(x) =>
|
|
96
|
+
this.quoteOwners.get(x.quoteRef) === ownerKey &&
|
|
97
|
+
x.plateNo === plateNo &&
|
|
98
|
+
x.paymentState === 'UNPAID',
|
|
99
|
+
);
|
|
100
|
+
if (existing) return [existing];
|
|
101
|
+
const total = DEMO_FEE_ITEMS.reduce(
|
|
102
|
+
(sum, item) => sum + item.amountCents,
|
|
103
|
+
0,
|
|
104
|
+
);
|
|
105
|
+
const autoBenefits = DEMO_BENEFITS.filter((b) => b.autoSelected);
|
|
106
|
+
const discount = autoBenefits.reduce(
|
|
107
|
+
(sum, b) => sum + (b.estimatedDiscountCents ?? 0),
|
|
108
|
+
0,
|
|
109
|
+
);
|
|
110
|
+
const quote: QuoteSnapshot = {
|
|
111
|
+
quoteRef: `q_${createHash('sha256').update(`${ownerKey}:${plateNo}`).digest('hex').slice(0, 12)}`,
|
|
112
|
+
plateNo,
|
|
113
|
+
mallId: q.scope?.mallId || this.mallId,
|
|
114
|
+
entryAt: new Date(Date.now() - 3 * 3600 * 1000).toISOString(),
|
|
115
|
+
feeItems: DEMO_FEE_ITEMS.map((i) => ({ ...i })),
|
|
116
|
+
totalCents: total,
|
|
117
|
+
discountCents: discount,
|
|
118
|
+
payableCents: Math.max(0, total - discount),
|
|
119
|
+
appliedBenefits: autoBenefits.map((b) => ({
|
|
120
|
+
benefitRef: b.benefitRef,
|
|
121
|
+
title: b.title,
|
|
122
|
+
discountCents: b.estimatedDiscountCents ?? 0,
|
|
123
|
+
})),
|
|
124
|
+
revision: 1,
|
|
125
|
+
paymentState: 'UNPAID',
|
|
126
|
+
};
|
|
127
|
+
this.quotes.set(quote.quoteRef, quote);
|
|
128
|
+
this.quoteOwners.set(quote.quoteRef, ownerKey);
|
|
129
|
+
return [quote];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async listVehicles(q: ParkingQueryContext): Promise<{
|
|
133
|
+
bound: readonly ParkingVehicle[];
|
|
134
|
+
}> {
|
|
135
|
+
const ownerKey = this.ownerKey(q);
|
|
136
|
+
return {
|
|
137
|
+
bound: this.boundVehicles.filter(
|
|
138
|
+
(vehicle) => this.vehicleOwners.get(vehicle.vehicleRef) === ownerKey,
|
|
139
|
+
),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async listPaymentRecords(
|
|
144
|
+
q: ParkingQueryContext,
|
|
145
|
+
page: number,
|
|
146
|
+
pageSize: number,
|
|
147
|
+
): Promise<ParkingRecordPage> {
|
|
148
|
+
return this.paginate(this.ownedRecords(q), page, pageSize);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async getPaymentRecordDetail(
|
|
152
|
+
q: ParkingQueryContext,
|
|
153
|
+
recordRef: string,
|
|
154
|
+
): Promise<ParkingPaymentRecord> {
|
|
155
|
+
const ownerKey = this.ownerKey(q);
|
|
156
|
+
const record = this.records.find(
|
|
157
|
+
(item) =>
|
|
158
|
+
item.recordRef === recordRef &&
|
|
159
|
+
this.recordOwners.get(item.recordRef) === ownerKey,
|
|
160
|
+
);
|
|
161
|
+
if (!record) throw new Error('Parking record reference is unavailable');
|
|
162
|
+
return { ...record };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async getInvoiceSettings(
|
|
166
|
+
_q: ParkingQueryContext,
|
|
167
|
+
): Promise<{ parkingInvoiceEnabled: boolean }> {
|
|
168
|
+
return { parkingInvoiceEnabled: true };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async listInvoiceableOrders(
|
|
172
|
+
q: ParkingQueryContext,
|
|
173
|
+
page: number,
|
|
174
|
+
pageSize: number,
|
|
175
|
+
): Promise<ParkingRecordPage> {
|
|
176
|
+
// InMemory:已支付记录均可开票(真实口径 = 厂商 IsCheckInvoice,见 provider 包 adapter)
|
|
177
|
+
return this.paginate(
|
|
178
|
+
this.ownedRecords(q).filter(
|
|
179
|
+
(record) => record.invoiceEligible && !record.invoiceIssued,
|
|
180
|
+
),
|
|
181
|
+
page,
|
|
182
|
+
pageSize,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async queryParkingInfo(
|
|
187
|
+
_q: ParkingQueryContext,
|
|
188
|
+
kind: 'chargeRules' | 'benefitPolicy' | 'vehicleLocation',
|
|
189
|
+
): Promise<ParkingInfo> {
|
|
190
|
+
switch (kind) {
|
|
191
|
+
case 'chargeRules':
|
|
192
|
+
return {
|
|
193
|
+
kind,
|
|
194
|
+
title: '收费规则',
|
|
195
|
+
entries: [
|
|
196
|
+
{ label: '首小时', value: '5.00 元' },
|
|
197
|
+
{ label: '后续每小时', value: '5.00 元' },
|
|
198
|
+
{ label: '每日封顶', value: '60.00 元' },
|
|
199
|
+
],
|
|
200
|
+
};
|
|
201
|
+
case 'benefitPolicy':
|
|
202
|
+
return {
|
|
203
|
+
kind,
|
|
204
|
+
title: '会员停车权益',
|
|
205
|
+
entries: [
|
|
206
|
+
{ label: '会员', value: '停车费 95 折' },
|
|
207
|
+
{ label: '积分', value: '500 积分抵 5 元' },
|
|
208
|
+
],
|
|
209
|
+
};
|
|
210
|
+
case 'vehicleLocation':
|
|
211
|
+
// 寻车定位由 locateVehicle 工具直接出卡,内建服务无位置源
|
|
212
|
+
return { kind, title: '寻车定位', entries: [] };
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async queryApplicableBenefits(
|
|
217
|
+
_q: ParkingQueryContext,
|
|
218
|
+
_plateNo: string,
|
|
219
|
+
): Promise<readonly ParkingBenefit[]> {
|
|
220
|
+
return DEMO_BENEFITS.map((b) => ({ ...b }));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async locateVehicle(
|
|
224
|
+
_q: ParkingQueryContext,
|
|
225
|
+
_plateNo: string,
|
|
226
|
+
): Promise<ParkingLocation | null> {
|
|
227
|
+
return {
|
|
228
|
+
floorName: 'P2',
|
|
229
|
+
zoneName: 'B 区',
|
|
230
|
+
spaceNo: '128',
|
|
231
|
+
walkingRoute: '入口右转 50 米,乘 B 电梯至 P2',
|
|
232
|
+
};
|
|
233
|
+
}
|
|
13
234
|
|
|
14
|
-
|
|
15
|
-
|
|
235
|
+
// ===== ParkingPaymentPort =====
|
|
236
|
+
|
|
237
|
+
async createOrder(
|
|
238
|
+
q: ParkingQueryContext,
|
|
239
|
+
quoteRef: string,
|
|
240
|
+
_detail?: Parameters<ParkingPaymentPort['createOrder']>[2],
|
|
241
|
+
): Promise<{ payOrderId?: string; paymentRef: string }> {
|
|
242
|
+
const ownerKey = this.ownerKey(q);
|
|
243
|
+
const quote = this.quotes.get(quoteRef);
|
|
244
|
+
if (!quote || this.quoteOwners.get(quoteRef) !== ownerKey)
|
|
245
|
+
throw new Error(`报价不存在:${quoteRef}`);
|
|
246
|
+
if (quote.payableCents === 0) {
|
|
247
|
+
// 零元单:无厂商支付单(confirm 直接 reconcile 收口)
|
|
248
|
+
const paymentRef = `free_${quoteRef}`;
|
|
249
|
+
this.paymentToQuote.set(paymentRef, { quoteRef, ownerKey });
|
|
250
|
+
await this.markPaid(q, quote, paymentRef);
|
|
251
|
+
return { paymentRef };
|
|
252
|
+
}
|
|
253
|
+
this.paymentRefSeq += 1;
|
|
254
|
+
const payOrderId = `PO${String(Date.now()).slice(-8)}${this.paymentRefSeq}`;
|
|
255
|
+
const paymentRef = payOrderId;
|
|
256
|
+
this.paymentToQuote.set(paymentRef, { quoteRef, ownerKey });
|
|
257
|
+
this.quotes.set(quoteRef, { ...quote, paymentState: 'PAYING' });
|
|
258
|
+
return { payOrderId, paymentRef };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async getOrderInfo(
|
|
262
|
+
q: ParkingQueryContext,
|
|
263
|
+
paymentRef: string,
|
|
264
|
+
): Promise<{
|
|
265
|
+
orderInfo?: string;
|
|
266
|
+
channel: string;
|
|
267
|
+
payableFeeCents: number | undefined;
|
|
268
|
+
}> {
|
|
269
|
+
const quote = this.quoteOfPayment(q, paymentRef);
|
|
270
|
+
if (!quote) throw new Error(`支付单不存在:${paymentRef}`);
|
|
271
|
+
return { channel: '50', payableFeeCents: quote.payableCents };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async getPaymentSign(
|
|
275
|
+
q: ParkingQueryContext,
|
|
276
|
+
paymentRef: string,
|
|
277
|
+
): Promise<{ orderInfo: string }> {
|
|
278
|
+
const quote = this.quoteOfPayment(q, paymentRef);
|
|
279
|
+
if (!quote) throw new Error(`支付单不存在:${paymentRef}`);
|
|
280
|
+
return { orderInfo: `mock-order-info:${paymentRef}:${quote.payableCents}` };
|
|
16
281
|
}
|
|
17
282
|
|
|
18
|
-
|
|
19
|
-
|
|
283
|
+
async uploadResult(
|
|
284
|
+
q: ParkingQueryContext,
|
|
285
|
+
paymentRef: string,
|
|
286
|
+
status: 'success' | 'cancel' | 'failure' | 'unavailable',
|
|
287
|
+
): Promise<void> {
|
|
288
|
+
if (!this.quoteOfPayment(q, paymentRef))
|
|
289
|
+
throw new Error(`支付单不存在:${paymentRef}`);
|
|
290
|
+
this.paymentResults.set(paymentRef, status);
|
|
291
|
+
}
|
|
20
292
|
|
|
21
|
-
async
|
|
22
|
-
|
|
23
|
-
|
|
293
|
+
async reconcile(
|
|
294
|
+
q: ParkingQueryContext,
|
|
295
|
+
paymentRef: string,
|
|
296
|
+
): Promise<'PAID' | 'PAYING' | 'CANCELLED' | 'UNKNOWN'> {
|
|
297
|
+
const quote = this.quoteOfPayment(q, paymentRef);
|
|
298
|
+
if (!quote) throw new Error(`支付单不存在:${paymentRef}`);
|
|
299
|
+
// 零元单恒 PAID
|
|
300
|
+
if (paymentRef.startsWith('free_')) return 'PAID';
|
|
301
|
+
const reported = this.paymentResults.get(paymentRef);
|
|
302
|
+
if (reported === 'success') {
|
|
303
|
+
await this.markPaid(q, quote, paymentRef);
|
|
304
|
+
return 'PAID';
|
|
305
|
+
}
|
|
306
|
+
if (reported === 'cancel') return 'CANCELLED';
|
|
307
|
+
if (reported === 'failure' || reported === 'unavailable') return 'PAYING'; // 结果未知 → 悬挂(厂商语义)
|
|
308
|
+
return 'PAYING';
|
|
24
309
|
}
|
|
25
310
|
|
|
26
|
-
async
|
|
27
|
-
|
|
311
|
+
async reprice(
|
|
312
|
+
q: ParkingQueryContext,
|
|
313
|
+
quoteRef: string,
|
|
314
|
+
benefitRefs: readonly string[],
|
|
315
|
+
): Promise<QuoteSnapshot> {
|
|
316
|
+
const quote = this.quotes.get(quoteRef);
|
|
317
|
+
if (!quote || this.quoteOwners.get(quoteRef) !== this.ownerKey(q))
|
|
318
|
+
throw new Error(`报价不存在:${quoteRef}`);
|
|
319
|
+
if (quote.paymentState !== 'UNPAID')
|
|
320
|
+
throw new Error('订单已进入支付流程,不可重选权益');
|
|
321
|
+
const selected = DEMO_BENEFITS.filter((b) =>
|
|
322
|
+
benefitRefs.includes(b.benefitRef),
|
|
323
|
+
);
|
|
324
|
+
const discount = selected.reduce(
|
|
325
|
+
(sum, b) => sum + (b.estimatedDiscountCents ?? 0),
|
|
326
|
+
0,
|
|
327
|
+
);
|
|
328
|
+
const next: QuoteSnapshot = {
|
|
329
|
+
...quote,
|
|
330
|
+
discountCents: discount,
|
|
331
|
+
payableCents: Math.max(0, quote.totalCents - discount),
|
|
332
|
+
appliedBenefits: selected.map((b) => ({
|
|
333
|
+
benefitRef: b.benefitRef,
|
|
334
|
+
title: b.title,
|
|
335
|
+
discountCents: b.estimatedDiscountCents ?? 0,
|
|
336
|
+
})),
|
|
337
|
+
revision: quote.revision + 1,
|
|
338
|
+
};
|
|
339
|
+
this.quotes.set(quoteRef, next);
|
|
340
|
+
return next;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
async applyInvoice(q: ParkingQueryContext, recordRef: string): Promise<void> {
|
|
344
|
+
const ownerKey = this.ownerKey(q);
|
|
345
|
+
const index = this.records.findIndex(
|
|
346
|
+
(item) =>
|
|
347
|
+
item.recordRef === recordRef &&
|
|
348
|
+
this.recordOwners.get(item.recordRef) === ownerKey,
|
|
349
|
+
);
|
|
350
|
+
if (index < 0) throw new Error('Parking record reference is unavailable');
|
|
351
|
+
const record = this.records[index]!;
|
|
352
|
+
if (!record.orderCompleted || record.invoiceIssued) {
|
|
353
|
+
throw new Error(
|
|
354
|
+
record.invoiceIssued ? '该停车订单已开票' : '该停车订单暂不可开票',
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
this.records[index] = {
|
|
358
|
+
...record,
|
|
359
|
+
invoiceEligible: false,
|
|
360
|
+
invoiceIssued: true,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// ===== ParkingVehiclePort =====
|
|
365
|
+
|
|
366
|
+
async bindVehicle(
|
|
367
|
+
q: ParkingQueryContext,
|
|
368
|
+
plateNo: string,
|
|
369
|
+
): Promise<ParkingVehicle> {
|
|
370
|
+
const ownerKey = this.ownerKey(q);
|
|
371
|
+
const existing = this.boundVehicles.find(
|
|
372
|
+
(vehicle) =>
|
|
373
|
+
vehicle.plateNo === plateNo &&
|
|
374
|
+
this.vehicleOwners.get(vehicle.vehicleRef) === ownerKey,
|
|
375
|
+
);
|
|
28
376
|
if (existing) return existing;
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
377
|
+
const vehicle: ParkingVehicle = {
|
|
378
|
+
vehicleRef: `v_${createHash('sha256').update(`${ownerKey}:${plateNo}`).digest('hex').slice(0, 12)}`,
|
|
379
|
+
plateNo,
|
|
380
|
+
boundAt: new Date().toISOString(),
|
|
381
|
+
};
|
|
382
|
+
this.boundVehicles.push(vehicle);
|
|
383
|
+
this.vehicleOwners.set(vehicle.vehicleRef, ownerKey);
|
|
384
|
+
return vehicle;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
async unbindVehicle(
|
|
388
|
+
q: ParkingQueryContext,
|
|
389
|
+
input: { plateNo?: string; vehicleRef?: string },
|
|
390
|
+
): Promise<void> {
|
|
391
|
+
const ownerKey = this.ownerKey(q);
|
|
392
|
+
const removed = this.boundVehicles.filter(
|
|
393
|
+
(vehicle) =>
|
|
394
|
+
this.vehicleOwners.get(vehicle.vehicleRef) === ownerKey &&
|
|
395
|
+
(vehicle.plateNo === input.plateNo ||
|
|
396
|
+
vehicle.vehicleRef === input.vehicleRef),
|
|
397
|
+
);
|
|
398
|
+
this.boundVehicles = this.boundVehicles.filter(
|
|
399
|
+
(vehicle) => !removed.includes(vehicle),
|
|
400
|
+
);
|
|
401
|
+
for (const vehicle of removed)
|
|
402
|
+
this.vehicleOwners.delete(vehicle.vehicleRef);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// ===== 内部 =====
|
|
406
|
+
|
|
407
|
+
private plateOfVehicleRef(
|
|
408
|
+
q: ParkingQueryContext,
|
|
409
|
+
vehicleRef?: string,
|
|
410
|
+
): string | undefined {
|
|
411
|
+
return vehicleRef
|
|
412
|
+
? this.boundVehicles.find(
|
|
413
|
+
(vehicle) =>
|
|
414
|
+
vehicle.vehicleRef === vehicleRef &&
|
|
415
|
+
this.vehicleOwners.get(vehicle.vehicleRef) === this.ownerKey(q),
|
|
416
|
+
)?.plateNo
|
|
417
|
+
: undefined;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private quoteOfPayment(
|
|
421
|
+
q: ParkingQueryContext,
|
|
422
|
+
paymentRef: string,
|
|
423
|
+
): QuoteSnapshot | undefined {
|
|
424
|
+
const link = this.paymentToQuote.get(paymentRef);
|
|
425
|
+
return link?.ownerKey === this.ownerKey(q)
|
|
426
|
+
? this.quotes.get(link.quoteRef)
|
|
427
|
+
: undefined;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private async markPaid(
|
|
431
|
+
q: ParkingQueryContext,
|
|
432
|
+
quote: QuoteSnapshot,
|
|
433
|
+
paymentRef: string,
|
|
434
|
+
): Promise<void> {
|
|
435
|
+
this.quotes.set(quote.quoteRef, { ...quote, paymentState: 'PAID' });
|
|
436
|
+
const ownerKey = this.ownerKey(q);
|
|
437
|
+
const recordRef = `record-${createHash('sha256').update(`${this.mallId}:${ownerKey}:${paymentRef}`).digest('hex').slice(0, 24)}`;
|
|
438
|
+
const already = this.records.find(
|
|
439
|
+
(record) => record.recordRef === recordRef,
|
|
440
|
+
);
|
|
441
|
+
if (!already) {
|
|
442
|
+
const record: ParkingPaymentRecord = {
|
|
443
|
+
recordRef,
|
|
444
|
+
plateNo: quote.plateNo,
|
|
445
|
+
amountCents: quote.payableCents,
|
|
446
|
+
paidAt: new Date().toISOString(),
|
|
447
|
+
status: 'PAID',
|
|
448
|
+
parkName: '演示停车场',
|
|
449
|
+
entryAt: quote.entryAt,
|
|
450
|
+
payOrderNoMasked:
|
|
451
|
+
paymentRef.length > 8
|
|
452
|
+
? `${paymentRef.slice(0, 4)}****${paymentRef.slice(-4)}`
|
|
453
|
+
: paymentRef,
|
|
454
|
+
orderCompleted: true,
|
|
455
|
+
invoiceEligible: true,
|
|
456
|
+
invoiceIssued: false,
|
|
457
|
+
};
|
|
458
|
+
this.records.unshift(record);
|
|
459
|
+
this.recordOwners.set(recordRef, ownerKey);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
private ownerKey(q: ParkingQueryContext): string {
|
|
464
|
+
return `${q.subjectId ?? 'anonymous'}\u0000${q.scope?.mallId || this.mallId}`;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
private ownedRecords(q: ParkingQueryContext): ParkingPaymentRecord[] {
|
|
468
|
+
const ownerKey = this.ownerKey(q);
|
|
469
|
+
return this.records.filter(
|
|
470
|
+
(record) => this.recordOwners.get(record.recordRef) === ownerKey,
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
private paginate(
|
|
475
|
+
records: ParkingPaymentRecord[],
|
|
476
|
+
page: number,
|
|
477
|
+
pageSize: number,
|
|
478
|
+
): ParkingRecordPage {
|
|
479
|
+
const start = (page - 1) * pageSize;
|
|
480
|
+
return {
|
|
481
|
+
items: records.slice(start, start + pageSize),
|
|
482
|
+
total: records.length,
|
|
483
|
+
page,
|
|
484
|
+
pageSize,
|
|
485
|
+
hasMore: start + pageSize < records.length,
|
|
35
486
|
};
|
|
36
|
-
this.orders.set(orderId, order);
|
|
37
|
-
return order;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async useCoupon(_userId: string, orderId: string, couponId: string): Promise<boolean> {
|
|
41
|
-
const order = this.orders.get(orderId);
|
|
42
|
-
if (!order) return false;
|
|
43
|
-
if (this.couponsUsed.has(couponId)) return false; // 一张券只核销一次
|
|
44
|
-
this.couponsUsed.add(couponId);
|
|
45
|
-
this.onCouponUsed?.({ userId: _userId, couponId, amount: 5 }); // PoC 固定抵扣 5 元
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async settlePayment(_userId: string, orderId: string, amount: number): Promise<boolean> {
|
|
50
|
-
const order = this.orders.get(orderId);
|
|
51
|
-
if (!order) return false;
|
|
52
|
-
// 幂等:已结算订单重复结算视为成功(scenario 重放时避免误判失败触发补偿退款)
|
|
53
|
-
if (order.status === 'PAID') return true;
|
|
54
|
-
if (order.status !== 'PENDING') return false;
|
|
55
|
-
order.status = 'PAID';
|
|
56
|
-
order.paidAmount = amount;
|
|
57
|
-
return true;
|
|
58
487
|
}
|
|
59
488
|
}
|