mirror of
http://124.126.16.154:8888/singularity/hdk-pay.git
synced 2026-01-15 05:55:08 +08:00
feat(account): 添加初始化账户余额功能
- 新增 InitialAccountCmd 类用于处理初始账户命令 - 在 AccountRepoInterface 接口中添加 initial 方法 - 实现 AccountBalanceRepo 类中的 initial 方法 - 添加单元测试 InitialAccountBalanceTest 以验证初始化功能
This commit is contained in:
40
src/Application/Command/InitialAccountCmd.php
Normal file
40
src/Application/Command/InitialAccountCmd.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* InitialAccountCmd.php@Pay
|
||||||
|
*
|
||||||
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||||
|
* Powered by PhpStorm
|
||||||
|
* Created on 2025/9/17
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Singularity\HDK\Pay\Application\Command;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Singularity\HDK\Pay\Domain\Account\Enum\PointType;
|
||||||
|
|
||||||
|
final class InitialAccountCmd {
|
||||||
|
public array $pointsBalances;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public readonly string $uid,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function addPointsBalance(
|
||||||
|
PointType $type,
|
||||||
|
float $basic,
|
||||||
|
float $bonus,
|
||||||
|
?Carbon $expiredAt = null,
|
||||||
|
?string $version = null,
|
||||||
|
): array
|
||||||
|
{
|
||||||
|
return $this->pointsBalances[] = [
|
||||||
|
'type' => $type,
|
||||||
|
'basic' => $basic,
|
||||||
|
'bonus' => $bonus,
|
||||||
|
'expired_at' => $expiredAt,
|
||||||
|
'version' => $version,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,13 +11,29 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Singularity\HDK\Pay\Domain\Account\Repository;
|
namespace Singularity\HDK\Pay\Domain\Account\Repository;
|
||||||
|
|
||||||
|
use Singularity\HDK\Pay\Application\Command\InitialAccountCmd;
|
||||||
use Singularity\HDK\Pay\Domain\Account\Aggregate\Account\AccountBalance;
|
use Singularity\HDK\Pay\Domain\Account\Aggregate\Account\AccountBalance;
|
||||||
use Singularity\HDK\Pay\Domain\Account\Aggregate\Account\PointsBalance;
|
use Singularity\HDK\Pay\Domain\Account\Aggregate\Account\PointsBalance;
|
||||||
use Singularity\HDK\Pay\Domain\Account\Enum\PointType;
|
use Singularity\HDK\Pay\Domain\Account\Enum\PointType;
|
||||||
|
|
||||||
interface AccountRepoInterface
|
interface AccountRepoInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param string $uid
|
||||||
|
* @return AccountBalance
|
||||||
|
*/
|
||||||
public function getAccount(string $uid): AccountBalance;
|
public function getAccount(string $uid): AccountBalance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $uid
|
||||||
|
* @param PointType $pointType
|
||||||
|
* @return PointsBalance
|
||||||
|
*/
|
||||||
public function getPointBalance(string $uid, PointType $pointType): PointsBalance;
|
public function getPointBalance(string $uid, PointType $pointType): PointsBalance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param InitialAccountCmd $cmd
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function initial(InitialAccountCmd $cmd): void;
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,7 @@ namespace Singularity\HDK\Pay\Infrastructure\Repository;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Hyperf\Codec\Json;
|
use Hyperf\Codec\Json;
|
||||||
|
use Singularity\HDK\Pay\Application\Command\InitialAccountCmd;
|
||||||
use Singularity\HDK\Pay\Domain\Account\Aggregate\Account\AccountBalance;
|
use Singularity\HDK\Pay\Domain\Account\Aggregate\Account\AccountBalance;
|
||||||
use Singularity\HDK\Pay\Domain\Account\Aggregate\Account\PointsBalance;
|
use Singularity\HDK\Pay\Domain\Account\Aggregate\Account\PointsBalance;
|
||||||
use Singularity\HDK\Pay\Domain\Account\Enum\PointType;
|
use Singularity\HDK\Pay\Domain\Account\Enum\PointType;
|
||||||
@@ -67,4 +68,17 @@ final class AccountBalanceRepo extends AbstractRepo implements AccountRepoInterf
|
|||||||
expiredAt: isset($result['expired_at']) ? new Carbon($result['expired_at']): null,
|
expiredAt: isset($result['expired_at']) ? new Carbon($result['expired_at']): null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function initial(InitialAccountCmd $cmd): void
|
||||||
|
{
|
||||||
|
$uid = $cmd->uid;
|
||||||
|
|
||||||
|
$this->requestService->requestPost(
|
||||||
|
url: "/rpc/v2/account/$uid/balance",
|
||||||
|
data: $cmd->pointsBalances
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
53
tests/Feature/Account/InitialAccountBalanceTest.php
Normal file
53
tests/Feature/Account/InitialAccountBalanceTest.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* InitialAccountBalanceTest.php@Pay
|
||||||
|
*
|
||||||
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||||
|
* Powered by PhpStorm
|
||||||
|
* Created on 2025/9/17
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Singularity\HDK\Pay\Application\Command\InitialAccountCmd;
|
||||||
|
use Singularity\HDK\Pay\Domain\Account\Enum\PointType;
|
||||||
|
|
||||||
|
use Singularity\HDK\Pay\Infrastructure\Repository\AccountBalanceRepo;
|
||||||
|
|
||||||
|
use function Hyperf\Support\make;
|
||||||
|
|
||||||
|
it('should initial account balance', function () {
|
||||||
|
$uid = uniqid('TDD');
|
||||||
|
$data = [
|
||||||
|
[
|
||||||
|
'type' => 'aligner',
|
||||||
|
'basic' => 0,
|
||||||
|
'bonus' => 40,
|
||||||
|
'expired_at' => Carbon::now()->addYear()->toDateTimeString(),
|
||||||
|
'version' => 'Trial',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'ema',
|
||||||
|
'basic' => 0,
|
||||||
|
'bonus' => 1,
|
||||||
|
'expired_at' => Carbon::now()->addYear()->toDateTimeString(),
|
||||||
|
'version' => 'trial',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$cmd = new InitialAccountCmd($uid);
|
||||||
|
foreach ($data as $point_balance) {
|
||||||
|
$cmd->addPointsBalance(
|
||||||
|
type: PointType::from($point_balance['type']),
|
||||||
|
basic: $point_balance['basic'] ?? 0,
|
||||||
|
bonus: $point_balance['bonus'] ?? 0,
|
||||||
|
expiredAt: isset($point_balance['expired_at']) ? new Carbon($point_balance['expired_at']) : null,
|
||||||
|
version: $point_balance['version'] ?? 0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$repo = make(AccountBalanceRepo::class);
|
||||||
|
$repo->initial($cmd);
|
||||||
|
|
||||||
|
expect(true)->toBeTrue();
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user