| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace App\Module\Fund\Dto;
- use App\Module\Fund\Enums\FUND_TYPE;
- /**
- * 贸易结果数据传输对象
- *
- * 用于在服务层返回贸易结果信息,避免直接暴露模型对象
- */
- class TradeResultDto
- {
- /**
- * @var int 发送用户ID
- */
- public int $fromUserId;
- /**
- * @var int 接收用户ID
- */
- public int $toUserId;
- /**
- * @var int|FUND_TYPE 资金类型ID
- */
- public $fundId;
- /**
- * @var float 金额(小数形式)
- */
- public float $amount;
- /**
- * @var string 关联类型
- */
- public string $transferType;
- /**
- * @var string 关联ID
- */
- public string $transferId;
- /**
- * @var string 备注
- */
- public string $remark;
- /**
- * @var string|null 资金类型名称(可选)
- */
- public ?string $fundName = null;
- /**
- * @var string|null 发送用户名称(可选)
- */
- public ?string $fromUserName = null;
- /**
- * @var string|null 接收用户名称(可选)
- */
- public ?string $toUserName = null;
- /**
- * 创建新的DTO
- *
- * @param int $fromUserId 发送用户ID
- * @param int $toUserId 接收用户ID
- * @param int|FUND_TYPE $fundId 资金类型ID
- * @param int $amount 金额
- * @param string $transferType 关联类型
- * @param string $transferId 关联ID
- * @param string $remark 备注
- * @param array $fundNames 资金类型名称映射(可选)
- * @param array $userNames 用户名称映射(可选)
- * @return self
- */
- public static function create(
- int $fromUserId,
- int $toUserId,
- $fundId,
- int $amount,
- string $transferType,
- string $transferId,
- string $remark,
- array $fundNames = [],
- array $userNames = []
- ): self {
- $dto = new self();
- $dto->fromUserId = $fromUserId;
- $dto->toUserId = $toUserId;
- $dto->fundId = $fundId;
- $dto->amount = $amount;
- $dto->transferType = $transferType;
- $dto->transferId = $transferId;
- $dto->remark = $remark;
- // 如果提供了资金类型名称映射,则设置资金类型名称
- if (!empty($fundNames)) {
- $fundIdValue = $fundId instanceof FUND_TYPE ? $fundId->value : $fundId;
- if (isset($fundNames[$fundIdValue])) {
- $dto->fundName = $fundNames[$fundIdValue];
- }
- }
- // 如果提供了用户名称映射,则设置用户名称
- if (!empty($userNames)) {
- if (isset($userNames[$fromUserId])) {
- $dto->fromUserName = $userNames[$fromUserId];
- }
-
- if (isset($userNames[$toUserId])) {
- $dto->toUserName = $userNames[$toUserId];
- }
- }
- return $dto;
- }
- /**
- * 获取资金类型ID的整数值
- *
- * @return int
- */
- public function getFundId(): int
- {
- if ($this->fundId instanceof FUND_TYPE) {
- return $this->fundId->value;
- }
- return (int)$this->fundId;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- $result = [
- 'from_user_id' => $this->fromUserId,
- 'to_user_id' => $this->toUserId,
- 'fund_id' => $this->fundId instanceof FUND_TYPE ? $this->fundId->value : $this->fundId,
- 'amount' => $this->amount,
- 'transfer_type' => $this->transferType,
- 'transfer_id' => $this->transferId,
- 'remark' => $this->remark,
- ];
- if ($this->fundName !== null) {
- $result['fund_name'] = $this->fundName;
- }
- if ($this->fromUserName !== null) {
- $result['from_user_name'] = $this->fromUserName;
- }
- if ($this->toUserName !== null) {
- $result['to_user_name'] = $this->toUserName;
- }
- return $result;
- }
- }
|