| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\Module\User\Dto;
- use App\Module\User\Enums\ACTION_STATUS;
- use App\Module\User\Enums\ACTION_TYPE;
- use App\Module\User\Models\UserAction;
- /**
- * 用户操作记录数据传输对象
- *
- * 用于在服务层返回用户操作记录,避免直接暴露模型对象
- */
- class UserActionDto
- {
- /**
- * @var int ID
- */
- public int $id;
- /**
- * @var int 管理员ID
- */
- public int $adminId;
- /**
- * @var int 用户ID
- */
- public int $userId;
- /**
- * @var ACTION_TYPE 操作类型
- */
- public ACTION_TYPE $type;
- /**
- * @var string 描述
- */
- public string $desc;
- /**
- * @var int 过期时间
- */
- public int $expTime;
- /**
- * @var int 关联ID
- */
- public int $reId;
- /**
- * @var string 关联类型
- */
- public string $reType;
- /**
- * @var ACTION_STATUS 状态
- */
- public ACTION_STATUS $status;
- /**
- * @var string 创建时间
- */
- public string $createdAt;
- /**
- * @var string 更新时间
- */
- public string $updatedAt;
- /**
- * 从模型创建DTO
- *
- * @param UserAction $model 用户操作记录模型
- * @return self
- */
- public static function fromModel(UserAction $model): self
- {
- $dto = new self();
- $dto->id = $model->id;
- $dto->adminId = $model->admin_id;
- $dto->userId = $model->user_id;
- $dto->type = $model->type;
- $dto->desc = $model->desc;
- $dto->expTime = $model->exp_time;
- $dto->reId = $model->re_id;
- $dto->reType = $model->re_type;
- $dto->status = $model->status;
- $dto->createdAt = $model->created_at ? $model->created_at->toDateTimeString() : '';
- $dto->updatedAt = $model->updated_at ? $model->updated_at->toDateTimeString() : '';
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'id' => $this->id,
- 'admin_id' => $this->adminId,
- 'user_id' => $this->userId,
- 'type' => $this->type->value,
- 'type_name' => $this->type->name,
- 'desc' => $this->desc,
- 'exp_time' => $this->expTime,
- 're_id' => $this->reId,
- 're_type' => $this->reType,
- 'status' => $this->status->value,
- 'status_name' => $this->status->name,
- 'created_at' => $this->createdAt,
- 'updated_at' => $this->updatedAt,
- ];
- }
- }
|