| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace App\Module\Fund\Dto;
- use App\Module\Fund\Enums\FUND_TYPE;
- /**
- * 资金流转数据传输对象
- *
- * 用于在服务层返回资金流转信息,避免直接暴露模型对象
- */
- class CirculationDto
- {
- /**
- * @var int 流转ID
- */
- public int $id;
- /**
- * @var int 用户ID
- */
- public int $userId;
- /**
- * @var int|FUND_TYPE 源资金类型ID
- */
- public $fromFundId;
- /**
- * @var int|FUND_TYPE 目标资金类型ID
- */
- public $toFundId;
- /**
- * @var float 金额(小数形式)
- */
- public float $amount;
- /**
- * @var int 关联ID
- */
- public int $relatedId;
- /**
- * @var string 关联类型
- */
- public string $relatedType;
- /**
- * @var string 备注
- */
- public string $remark;
- /**
- * @var int 创建时间
- */
- public int $createTime;
- /**
- * @var string|null 源资金类型名称(可选)
- */
- public ?string $fromFundName = null;
- /**
- * @var string|null 目标资金类型名称(可选)
- */
- public ?string $toFundName = null;
- /**
- * 从模型创建DTO
- *
- * @param mixed $model 流转模型
- * @param array $fundNames 资金类型名称映射(可选)
- * @return self
- */
- public static function fromModel($model, array $fundNames = []): self
- {
- $dto = new self();
- $dto->id = $model->id;
- $dto->userId = $model->user_id;
- $dto->fromFundId = $model->fund_id; // 修正字段映射
- $dto->toFundId = $model->to_fund_id;
- // 直接使用数据库中的小数值
- $dto->amount = (float)$model->total_fee;
- $dto->relatedId = $model->re_id; // 修正字段映射
- $dto->relatedType = $model->re_type; // 修正字段映射
- $dto->remark = $model->remark;
- $dto->createTime = $model->create_time;
- // 如果提供了资金类型名称映射,则设置资金类型名称
- if (!empty($fundNames)) {
- $fromFundIdValue = $model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id; // 修正字段映射
- $toFundIdValue = $model->to_fund_id instanceof FUND_TYPE ? $model->to_fund_id->value : $model->to_fund_id;
- if (isset($fundNames[$fromFundIdValue])) {
- $dto->fromFundName = $fundNames[$fromFundIdValue];
- }
- if (isset($fundNames[$toFundIdValue])) {
- $dto->toFundName = $fundNames[$toFundIdValue];
- }
- }
- return $dto;
- }
- /**
- * 获取源资金类型ID的整数值
- *
- * @return int
- */
- public function getFromFundId(): int
- {
- if ($this->fromFundId instanceof FUND_TYPE) {
- return $this->fromFundId->value;
- }
- return (int)$this->fromFundId;
- }
- /**
- * 获取目标资金类型ID的整数值
- *
- * @return int
- */
- public function getToFundId(): int
- {
- if ($this->toFundId instanceof FUND_TYPE) {
- return $this->toFundId->value;
- }
- return (int)$this->toFundId;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- $result = [
- 'id' => $this->id,
- 'user_id' => $this->userId,
- 'from_fund_id' => $this->fromFundId instanceof FUND_TYPE ? $this->fromFundId->value : $this->fromFundId,
- 'to_fund_id' => $this->toFundId instanceof FUND_TYPE ? $this->toFundId->value : $this->toFundId,
- 'amount' => $this->amount,
- 'related_id' => $this->relatedId,
- 'related_type' => $this->relatedType,
- 'remark' => $this->remark,
- 'create_time' => $this->createTime,
- ];
- if ($this->fromFundName !== null) {
- $result['from_fund_name'] = $this->fromFundName;
- }
- if ($this->toFundName !== null) {
- $result['to_fund_name'] = $this->toFundName;
- }
- return $result;
- }
- }
|