| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace App\Module\Fund\Dto;
- use App\Module\Fund\Enums\LOG_TYPE;
- /**
- * 资金日志数据传输对象
- *
- * 用于在服务层返回资金日志信息,避免直接暴露模型对象
- */
- class FundLogDto
- {
- /**
- * @var int 日志ID
- */
- public int $id;
- /**
- * @var int 用户ID
- */
- public int $userId;
- /**
- * @var int 资金类型ID
- */
- public $fundId;
- /**
- * @var int 金额
- */
- public int $amount;
- /**
- * @var int 操作ID
- */
- public int $operateId;
- /**
- * @var int|LOG_TYPE 操作类型
- */
- public $operateType;
- /**
- * @var string 备注
- */
- public string $remark;
- /**
- * @var int 创建时间
- */
- public int $createTime;
- /**
- * @var string 创建IP
- */
- public string $createIp;
- /**
- * @var int 操作后余额
- */
- public int $laterBalance;
- /**
- * @var int 操作前余额
- */
- public int $beforeBalance;
- /**
- * @var string 日期键
- */
- public string $dateKey;
- /**
- * @var string 哈希值
- */
- public string $hash;
- /**
- * @var string 前一条记录的哈希值
- */
- public string $prevHash;
- /**
- * @var string|null 资金类型名称(可选)
- */
- public ?string $fundName = null;
- /**
- * @var string|null 操作类型名称(可选)
- */
- public ?string $operateTypeName = null;
- /**
- * 从模型创建DTO
- *
- * @param mixed $model 日志模型
- * @param array $fundNames 资金类型名称映射(可选)
- * @param array $operateTypeNames 操作类型名称映射(可选)
- * @return self
- */
- public static function fromModel($model, array $fundNames = [], array $operateTypeNames = []): self
- {
- $dto = new self();
- $dto->id = $model->id;
- $dto->userId = $model->user_id;
- $dto->fundId = $model->fund_id;
- $dto->amount = $model->amount;
- $dto->operateId = $model->operate_id;
- $dto->operateType = $model->operate_type;
- $dto->remark = $model->remark;
- $dto->createTime = $model->create_time;
- $dto->createIp = $model->create_ip;
- $dto->laterBalance = $model->later_balance;
- $dto->beforeBalance = $model->before_balance;
- $dto->dateKey = $model->date_key;
- $dto->hash = $model->hash;
- $dto->prevHash = $model->prev_hash;
- // 如果提供了资金类型名称映射,则设置资金类型名称
- if (!empty($fundNames) && isset($fundNames[$model->fund_id])) {
- $dto->fundName = $fundNames[$model->fund_id];
- }
- // 如果提供了操作类型名称映射,则设置操作类型名称
- if (!empty($operateTypeNames) && isset($operateTypeNames[$model->operate_type instanceof LOG_TYPE ? $model->operate_type->value : $model->operate_type])) {
- $dto->operateTypeName = $operateTypeNames[$model->operate_type instanceof LOG_TYPE ? $model->operate_type->value : $model->operate_type];
- }
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- $result = [
- 'id' => $this->id,
- 'user_id' => $this->userId,
- 'fund_id' => $this->fundId,
- 'amount' => $this->amount,
- 'operate_id' => $this->operateId,
- 'operate_type' => $this->operateType instanceof LOG_TYPE ? $this->operateType->value : $this->operateType,
- 'remark' => $this->remark,
- 'create_time' => $this->createTime,
- 'create_ip' => $this->createIp,
- 'later_balance' => $this->laterBalance,
- 'before_balance' => $this->beforeBalance,
- 'date_key' => $this->dateKey,
- 'hash' => $this->hash,
- 'prev_hash' => $this->prevHash,
- ];
- if ($this->fundName !== null) {
- $result['fund_name'] = $this->fundName;
- }
- if ($this->operateTypeName !== null) {
- $result['operate_type_name'] = $this->operateTypeName;
- }
- return $result;
- }
- }
|