From 46da805cb41cdb777dbd9627a53f431e204904e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=9C=E4=BA=91?= Date: Mon, 1 Dec 2025 14:04:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(InvoiceRepo):=20=E4=BF=AE=E5=A4=8DInvoicePr?= =?UTF-8?q?oduct=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=E7=BC=BA=E5=B0=91pric?= =?UTF-8?q?es=E5=8F=82=E6=95=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加PointPrice数组参数到InvoiceProduct构造函数调用中,确保API响应中的价格数据被正确处理。同时添加必要的类导入声明。 --- ...InvoiceProduct构造函数缺少prices参数的问题.md | 66 +++++++++++++++++++ src/Infrastructure/Repository/InvoiceRepo.php | 14 +++- .../Account/InitialAccountBalanceTest.php | 2 +- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 .trae/documents/修复InvoiceRepo中InvoiceProduct构造函数缺少prices参数的问题.md diff --git a/.trae/documents/修复InvoiceRepo中InvoiceProduct构造函数缺少prices参数的问题.md b/.trae/documents/修复InvoiceRepo中InvoiceProduct构造函数缺少prices参数的问题.md new file mode 100644 index 0000000..90ebcbb --- /dev/null +++ b/.trae/documents/修复InvoiceRepo中InvoiceProduct构造函数缺少prices参数的问题.md @@ -0,0 +1,66 @@ +## 修复计划 + +### 问题分析 +在 `/Users/weili/Projects/HDK/Pay/src/Infrastructure/Repository/InvoiceRepo.php` 第131-137行,创建 `InvoiceProduct` 实例时缺少 `prices` 参数,导致构造函数调用失败。 + +### 错误原因 +1. `InvoiceProduct` 类构造函数需要6个参数:`caseId`, `uid`, `name`, `sku`, `description`, `prices` +2. 当前代码只传递了5个参数,缺少 `prices` 参数 +3. `prices` 参数是 `PointPrice[]` 类型的数组,不能为空 + +### 修复步骤 + +1. **添加必要的导入** + - 在 `InvoiceRepo.php` 中添加 `Money` 和 `Currency` 类的导入 + - 添加 `PointPrice` 类的导入 + +2. **处理API响应中的prices数据** + - 从API响应中提取 `prices` 数据(路径:`$result['product']['prices']`) + - 遍历 `prices` 数据,为每个价格创建 `PointPrice` 实例 + - 使用 `array_map` 函数简化处理过程 + +3. **完善InvoiceProduct实例创建** + - 将处理好的 `prices` 数组传递给 `InvoiceProduct` 构造函数 + +### 预期结果 +- 修复后,`InvoiceRepo::getInvoiceInfo()` 方法能够成功创建 `InvoiceProduct` 实例 +- 不再出现构造函数参数缺失的错误 +- 代码能够正常运行,返回完整的 `InvoiceInfo` 对象 + +### 修复代码示例 + +```php +// 1. 在InvoiceRepo.php顶部添加必要的导入 +use Money\Currency; +use Money\Money; +use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\ValueObject\PointPrice; + +// 2. 在getInvoiceInfo()方法中完善InvoiceProduct实例创建 +product: new InvoiceProduct( + caseId: $result['case_id'], + uid: $result['uid'], + name: $result['product']['name'], + sku: $result['product']['sku'], + description: $result['product']['description'], + prices: array_map( + callback: fn(array $price) => new PointPrice( + price: new Money( + amount: bcmul((string)$price['amount'], '100', 2), + currency: new Currency($price['currency']), + ), + currencySymbol: $price['symbol'], + ), + array: $result['product']['prices'], + ), +), +``` + +### 检查结果 +- 已检查所有使用 `InvoiceProduct` 的地方,只有 `InvoiceRepo.php` 中存在参数缺失问题 +- `InvoiceProductRepo.php` 中的实现是正确的,可以作为参考 +- 修复后,所有 `InvoiceProduct` 实例创建都会符合构造函数要求 + +### 测试建议 +- 修复后,运行相关测试用例验证修复效果 +- 可以使用 `./run-in-docker.sh APP_STATUS=false vendor/bin/pest --coroutine --prepend=tests/bootstrap.php` 运行测试 +- 检查是否还有其他类似的构造函数参数问题 \ No newline at end of file diff --git a/src/Infrastructure/Repository/InvoiceRepo.php b/src/Infrastructure/Repository/InvoiceRepo.php index 3543080..48e7e35 100644 --- a/src/Infrastructure/Repository/InvoiceRepo.php +++ b/src/Infrastructure/Repository/InvoiceRepo.php @@ -7,6 +7,7 @@ * Powered by PhpStorm * Created on 2025/8/29 */ + declare(strict_types=1); namespace Singularity\HDK\Pay\Infrastructure\Repository; @@ -23,6 +24,7 @@ use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\Invoice; use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\InvoiceInfo; use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\InvoiceProduct; use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\ValueObject\PointBalance; +use Singularity\HDK\Pay\Domain\Invoice\Aggregate\Invoice\ValueObject\PointPrice; use Singularity\HDK\Pay\Domain\Invoice\Repository\InvoiceRepoInterface; final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface @@ -134,6 +136,16 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface name: $result['product']['name'], sku: $result['product']['sku'], description: $result['product']['description'], + prices: array_map( + callback: fn(array $price) => new PointPrice( + price: new Money( + amount: bcmul((string)$price['amount'], '100', 2), + currency: new Currency($price['currency']), + ), + currencySymbol: $price['symbol'], + ), + array: $result['product']['prices'] ?? [], + ), ), balance: new PointBalance( total: $result['balance']['total'], @@ -146,4 +158,4 @@ final class InvoiceRepo extends AbstractRepo implements InvoiceRepoInterface patientName: $result['patient_name'], ); } -} \ No newline at end of file +} diff --git a/tests/Feature/Account/InitialAccountBalanceTest.php b/tests/Feature/Account/InitialAccountBalanceTest.php index 5a35b93..c17ff22 100644 --- a/tests/Feature/Account/InitialAccountBalanceTest.php +++ b/tests/Feature/Account/InitialAccountBalanceTest.php @@ -41,7 +41,7 @@ it('should initial account balance', function () { type: PointType::from($point_balance['type']), basic: $point_balance['basic'], bonus: $point_balance['bonus'], - expiredAt: $point_balance['expired_at'], + expiredAt:$point_balance['expired_at'], version: $point_balance['version'], ); }