@pisell/pisellos 2.1.28 → 2.1.30

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.
@@ -323,48 +323,35 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
323
323
  totalAmount: params.total_amount
324
324
  });
325
325
  try {
326
- const existingOrder = await this.dbManager.get("order", params.order_id);
327
- this.logInfo("createPaymentOrderAsync existingOrder", {
328
- existingOrder
326
+ const newOrder = {
327
+ uuid: (0, import_utils.getUniqueId)("pay_order_"),
328
+ id: params.order_id,
329
+ order_id: params.order_id,
330
+ order_info: params.order_info,
331
+ payment_status: import_types.PaymentStatus.Processing,
332
+ payment: [],
333
+ adjust_offline_payments: [],
334
+ total_amount: params.total_amount,
335
+ expect_amount: params.total_amount,
336
+ tax_fee: "0.00",
337
+ is_deposit: params.is_deposit || 0,
338
+ deposit_amount: params.deposit_amount || "0.00"
339
+ };
340
+ const dbAddStartTime = Date.now();
341
+ await this.dbManager.add("order", newOrder);
342
+ const dbAddDuration = Date.now() - dbAddStartTime;
343
+ this.logInfo("Database add operation completed", {
344
+ operation: "dbManager.add",
345
+ table: "order",
346
+ orderUuid: newOrder.uuid,
347
+ orderId: newOrder.order_id,
348
+ duration: `${dbAddDuration}ms`,
349
+ performance: dbAddDuration > 100 ? "slow" : dbAddDuration > 50 ? "medium" : "fast"
329
350
  });
330
- if (existingOrder) {
331
- this.logInfo(
332
- `createPaymentOrderAsync found duplicate order ID: ${params.order_id}, updating existing payment order`
333
- );
334
- existingOrder.order_info = params.order_info;
335
- existingOrder.total_amount = params.total_amount;
336
- existingOrder.is_deposit = params.is_deposit || 0;
337
- existingOrder.deposit_amount = params.deposit_amount || "0.00";
338
- this.recalculateOrderAmount(existingOrder);
339
- await this.dbManager.update("order", existingOrder);
340
- await this.core.effects.emit(
341
- import_types.PaymentHooks.OnOrderUpdated,
342
- existingOrder
343
- );
344
- return existingOrder;
345
- } else {
346
- const newOrder = {
347
- uuid: (0, import_utils.getUniqueId)("pay_order_"),
348
- id: params.order_id,
349
- order_id: params.order_id,
350
- order_info: params.order_info,
351
- payment_status: import_types.PaymentStatus.Processing,
352
- payment: [],
353
- adjust_offline_payments: [],
354
- total_amount: params.total_amount,
355
- expect_amount: params.total_amount,
356
- tax_fee: "0.00",
357
- is_deposit: params.is_deposit || 0,
358
- deposit_amount: params.deposit_amount || "0.00"
359
- };
360
- await this.dbManager.add("order", newOrder);
361
- this.core.effects.emit(import_types.PaymentHooks.OnOrderAdded, newOrder);
362
- this.logInfo("createPaymentOrderAsync completed - new payment order created", {
363
- orderUuid: newOrder.uuid,
364
- orderId: newOrder.id
365
- });
366
- return newOrder;
367
- }
351
+ setTimeout(() => {
352
+ this.core.effects.emit(`${this.name}:onOrderAdded`, newOrder);
353
+ }, 0);
354
+ return newOrder;
368
355
  } catch (error) {
369
356
  console.error("[PaymentModule] 创建支付订单失败", error);
370
357
  this.logError("createPaymentOrderAsync failed", error, {
@@ -913,7 +900,7 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
913
900
  }
914
901
  const paymentAmount = new import_decimal.Decimal(payment.amount || 0);
915
902
  const roundingAmount = new import_decimal.Decimal(payment.rounding_amount || 0);
916
- const effectiveAmount = paymentAmount.plus(roundingAmount.abs());
903
+ const effectiveAmount = paymentAmount.plus(roundingAmount.isNegative() ? roundingAmount.abs() : 0);
917
904
  return sum.plus(effectiveAmount);
918
905
  } catch (error) {
919
906
  console.warn(`[PaymentModule] 无效的支付金额: amount=${payment.amount}, rounding_amount=${payment.rounding_amount},跳过计算`);
@@ -935,7 +922,7 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
935
922
  code: p.code,
936
923
  amount: p.amount,
937
924
  rounding_amount: p.rounding_amount || "0.00",
938
- effective_amount: new import_decimal.Decimal(p.amount || 0).plus(new import_decimal.Decimal(p.rounding_amount || 0).abs()).toFixed(2)
925
+ effective_amount: new import_decimal.Decimal(p.amount || 0).plus(new import_decimal.Decimal(Number(p.rounding_amount) > 0 ? 0 : p.rounding_amount || 0).abs()).toFixed(2)
939
926
  })),
940
927
  说明: "有效支付金额包含抹零计算(amount + |rounding_amount|)"
941
928
  });
@@ -19,6 +19,7 @@ export declare class CheckoutImpl extends BaseModule implements Module, Checkout
19
19
  private store;
20
20
  private otherParams;
21
21
  private logger;
22
+ private calculationCache;
22
23
  order: OrderModule;
23
24
  payment: PaymentModule;
24
25
  constructor(name?: string, version?: string);
@@ -302,26 +303,72 @@ export declare class CheckoutImpl extends BaseModule implements Module, Checkout
302
303
  * 删除本地 IndexDB 中超过指定天数且已同步到后端的订单数据
303
304
  */
304
305
  private cleanupExpiredOrdersAsync;
306
+ /**
307
+ * 清除计算缓存
308
+ */
309
+ private clearCalculationCache;
310
+ /**
311
+ * 批量获取订单数据(用于性能优化)
312
+ * 一次性获取所有需要的数据,避免重复查询
313
+ */
314
+ private fetchOrderDataBatch;
315
+ /**
316
+ * 从支付项数组计算已支付金额(纯计算,不查询数据库)
317
+ */
318
+ private calculatePaidAmountFromItems;
305
319
  /**
306
320
  * 计算已支付金额(从 Payment 模块获取最新数据)
321
+ *
322
+ * 注意:此方法保持独立性,可以单独调用。
323
+ * 在 updateStateAmountToRemaining 等批量操作中会使用缓存优化。
307
324
  */
308
325
  private calculatePaidAmountAsync;
326
+ /**
327
+ * 从订单和支付项计算剩余金额(纯计算,不查询数据库)
328
+ */
329
+ private calculateRemainingAmountFromData;
330
+ /**
331
+ * 从订单和支付项计算剩余总金额(纯计算,排除定金)
332
+ */
333
+ private calculateRemainingTotalAmountFromData;
309
334
  /**
310
335
  * 计算剩余未支付金额(从 Payment 模块获取最新数据)
336
+ *
337
+ * 注意:此方法保持独立性,可以单独调用。
338
+ * 在 updateStateAmountToRemaining 等批量操作中会使用缓存优化。
311
339
  */
312
340
  private calculateRemainingAmountAsync;
341
+ /**
342
+ * 计算剩余未支付金额(排除定金计算,始终使用订单总金额)
343
+ *
344
+ * 注意:此方法保持独立性,可以单独调用。
345
+ * 在 updateStateAmountToRemaining 等批量操作中会使用缓存优化。
346
+ */
347
+ private calculateRemainingTotalAmountAsync;
313
348
  /**
314
349
  * 更新 balanceDueAmount 为当前剩余未支付金额(系统内部计算)
315
350
  */
316
351
  private updateBalanceDueAmount;
317
352
  /**
318
353
  * 更新 stateAmount 为当前剩余未支付金额
354
+ *
355
+ * 优化版本:批量获取数据,避免重复查询数据库
319
356
  */
320
357
  private updateStateAmountToRemaining;
358
+ /**
359
+ * 检查订单支付是否完成(优化版,复用已获取的数据)
360
+ *
361
+ * @param paymentItems 已获取的支付项数据
362
+ * @param remainingAmount 已计算的剩余金额
363
+ */
364
+ private checkOrderPaymentCompletionOptimized;
321
365
  /**
322
366
  * 检查订单支付是否完成
323
367
  *
324
368
  * 当剩余待付款金额 <= 0 时,触发订单支付完成事件
369
+ *
370
+ * 注意:此方法保持独立性,可以单独调用。
371
+ * 在 updateStateAmountToRemaining 中会使用优化版本。
325
372
  */
326
373
  private checkOrderPaymentCompletion;
327
374
  /**
@@ -377,6 +424,8 @@ export declare class CheckoutImpl extends BaseModule implements Module, Checkout
377
424
  * 重置 store 状态
378
425
  *
379
426
  * 在创建新订单前调用,确保状态完全干净
427
+ *
428
+ * 🚀 性能优化:改为同步方法,事件发射不阻塞主流程
380
429
  */
381
- private resetStoreStateAsync;
430
+ private resetStoreState;
382
431
  }