@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.
@@ -131,6 +131,10 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
131
131
  return this.ensurePaymentTables();
132
132
  case 14:
133
133
  this.registerNetworkHandlers();
134
+
135
+ // // 预连接数据库
136
+ // this.dbManager.connect()
137
+
134
138
  console.log('[PaymentModule] 初始化完成');
135
139
  this.logInfo('PaymentModule initialized successfully');
136
140
  case 17:
@@ -657,7 +661,8 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
657
661
  key: "createPaymentOrderAsync",
658
662
  value: (function () {
659
663
  var _createPaymentOrderAsync = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(params) {
660
- var existingOrder, newOrder;
664
+ var _this3 = this;
665
+ var newOrder, dbAddStartTime, dbAddDuration;
661
666
  return _regeneratorRuntime().wrap(function _callee7$(_context7) {
662
667
  while (1) switch (_context7.prev = _context7.next) {
663
668
  case 0:
@@ -666,41 +671,41 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
666
671
  totalAmount: params.total_amount
667
672
  });
668
673
  _context7.prev = 1;
669
- _context7.next = 4;
670
- return this.dbManager.get('order', params.order_id);
671
- case 4:
672
- existingOrder = _context7.sent;
673
- this.logInfo('createPaymentOrderAsync existingOrder', {
674
- existingOrder: existingOrder
675
- });
674
+ // 检测是否有重复的 order_id
675
+ // console.time('createLocalOrderAsync: getExistingOrder')
676
+ // const existingOrder = await this.dbManager.get('order', params.order_id);
677
+ // const existingOrders = await this.dbManager.getAll('order');
676
678
  // const existingOrder = existingOrders.find(
677
679
  // (order: PaymentOrder) => String(order.id) === String(params.order_id),
678
680
  // );
679
- if (!existingOrder) {
680
- _context7.next = 20;
681
- break;
682
- }
683
- // 如果存在相同 order_id 的订单,更新该订单信息
684
- this.logInfo("createPaymentOrderAsync found duplicate order ID: ".concat(params.order_id, ", updating existing payment order"));
685
-
686
- // const originalOrder = { ...existingOrder };
687
- existingOrder.order_info = params.order_info;
688
- existingOrder.total_amount = params.total_amount;
689
- existingOrder.is_deposit = params.is_deposit || 0;
690
- existingOrder.deposit_amount = params.deposit_amount || '0.00';
691
-
692
- // 重新计算待付金额
693
- this.recalculateOrderAmount(existingOrder);
694
-
695
- // 更新到数据库
696
- _context7.next = 15;
697
- return this.dbManager.update('order', existingOrder);
698
- case 15:
699
- _context7.next = 17;
700
- return this.core.effects.emit(PaymentHooks.OnOrderUpdated, existingOrder);
701
- case 17:
702
- return _context7.abrupt("return", existingOrder);
703
- case 20:
681
+ // this.logInfo('createPaymentOrderAsync existingOrder', {
682
+ // existingOrder
683
+ // });
684
+ // console.timeEnd('createLocalOrderAsync: getExistingOrder')
685
+ // const existingOrder = existingOrders.find(
686
+ // (order: PaymentOrder) => String(order.id) === String(params.order_id),
687
+ // );
688
+ // if (existingOrder) {
689
+ // // 如果存在相同 order_id 的订单,更新该订单信息
690
+ // this.logInfo(
691
+ // `createPaymentOrderAsync found duplicate order ID: ${params.order_id}, updating existing payment order`,
692
+ // );
693
+ // // const originalOrder = { ...existingOrder };
694
+ // existingOrder.order_info = params.order_info;
695
+ // existingOrder.total_amount = params.total_amount;
696
+ // existingOrder.is_deposit = params.is_deposit || 0;
697
+ // existingOrder.deposit_amount = params.deposit_amount || '0.00';
698
+ // // 重新计算待付金额
699
+ // this.recalculateOrderAmount(existingOrder);
700
+ // // 更新到数据库
701
+ // await this.dbManager.update('order', existingOrder);
702
+ // // 触发更新事件
703
+ // this.core.effects.emit(
704
+ // `${this.name}:onOrderUpdated`,
705
+ // existingOrder,
706
+ // );
707
+ // return existingOrder;
708
+ // } else {
704
709
  // 创建新的支付订单
705
710
  newOrder = {
706
711
  uuid: getUniqueId('pay_order_'),
@@ -715,32 +720,37 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
715
720
  tax_fee: '0.00',
716
721
  is_deposit: params.is_deposit || 0,
717
722
  deposit_amount: params.deposit_amount || '0.00'
718
- };
719
- _context7.next = 23;
723
+ }; // 🚀 性能监控:记录数据库添加操作耗时
724
+ dbAddStartTime = Date.now();
725
+ _context7.next = 6;
720
726
  return this.dbManager.add('order', newOrder);
721
- case 23:
722
- this.core.effects.emit(PaymentHooks.OnOrderAdded, newOrder);
723
- this.logInfo('createPaymentOrderAsync completed - new payment order created', {
727
+ case 6:
728
+ dbAddDuration = Date.now() - dbAddStartTime;
729
+ this.logInfo('Database add operation completed', {
730
+ operation: 'dbManager.add',
731
+ table: 'order',
724
732
  orderUuid: newOrder.uuid,
725
- orderId: newOrder.id
733
+ orderId: newOrder.order_id,
734
+ duration: "".concat(dbAddDuration, "ms"),
735
+ performance: dbAddDuration > 100 ? 'slow' : dbAddDuration > 50 ? 'medium' : 'fast'
726
736
  });
737
+ setTimeout(function () {
738
+ _this3.core.effects.emit("".concat(_this3.name, ":onOrderAdded"), newOrder);
739
+ }, 0);
727
740
  return _context7.abrupt("return", newOrder);
728
- case 26:
729
- _context7.next = 33;
730
- break;
731
- case 28:
732
- _context7.prev = 28;
741
+ case 12:
742
+ _context7.prev = 12;
733
743
  _context7.t0 = _context7["catch"](1);
734
744
  console.error('[PaymentModule] 创建支付订单失败', _context7.t0);
735
745
  this.logError('createPaymentOrderAsync failed', _context7.t0, {
736
746
  orderId: params.order_id
737
747
  });
738
748
  throw _context7.t0;
739
- case 33:
749
+ case 17:
740
750
  case "end":
741
751
  return _context7.stop();
742
752
  }
743
- }, _callee7, this, [[1, 28]]);
753
+ }, _callee7, this, [[1, 12]]);
744
754
  }));
745
755
  function createPaymentOrderAsync(_x6) {
746
756
  return _createPaymentOrderAsync.apply(this, arguments);
@@ -1808,7 +1818,7 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1808
1818
  // 例如:amount=15, rounding_amount=-0.2,有效支付=15.2(抹掉0.2元零头)
1809
1819
  var paymentAmount = new Decimal(payment.amount || 0);
1810
1820
  var roundingAmount = new Decimal(payment.rounding_amount || 0);
1811
- var effectiveAmount = paymentAmount.plus(roundingAmount.abs());
1821
+ var effectiveAmount = paymentAmount.plus(roundingAmount.isNegative() ? roundingAmount.abs() : 0);
1812
1822
  return sum.plus(effectiveAmount);
1813
1823
  } catch (error) {
1814
1824
  console.warn("[PaymentModule] \u65E0\u6548\u7684\u652F\u4ED8\u91D1\u989D: amount=".concat(payment.amount, ", rounding_amount=").concat(payment.rounding_amount, "\uFF0C\u8DF3\u8FC7\u8BA1\u7B97"));
@@ -1836,7 +1846,7 @@ export var PaymentModule = /*#__PURE__*/function (_BaseModule) {
1836
1846
  code: p.code,
1837
1847
  amount: p.amount,
1838
1848
  rounding_amount: p.rounding_amount || '0.00',
1839
- effective_amount: new Decimal(p.amount || 0).plus(new Decimal(p.rounding_amount || 0).abs()).toFixed(2)
1849
+ effective_amount: new Decimal(p.amount || 0).plus(new Decimal(Number(p.rounding_amount) > 0 ? 0 : p.rounding_amount || 0).abs()).toFixed(2)
1840
1850
  };
1841
1851
  }),
1842
1852
  说明: '有效支付金额包含抹零计算(amount + |rounding_amount|)'
@@ -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
  }