UserActionDto.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Module\User\Dto;
  3. use App\Module\User\Enums\ACTION_STATUS;
  4. use App\Module\User\Enums\ACTION_TYPE;
  5. use App\Module\User\Models\UserAction;
  6. /**
  7. * 用户操作记录数据传输对象
  8. *
  9. * 用于在服务层返回用户操作记录,避免直接暴露模型对象
  10. */
  11. class UserActionDto
  12. {
  13. /**
  14. * @var int ID
  15. */
  16. public int $id;
  17. /**
  18. * @var int 管理员ID
  19. */
  20. public int $adminId;
  21. /**
  22. * @var int 用户ID
  23. */
  24. public int $userId;
  25. /**
  26. * @var ACTION_TYPE 操作类型
  27. */
  28. public ACTION_TYPE $type;
  29. /**
  30. * @var string 描述
  31. */
  32. public string $desc;
  33. /**
  34. * @var int 过期时间
  35. */
  36. public int $expTime;
  37. /**
  38. * @var int 关联ID
  39. */
  40. public int $reId;
  41. /**
  42. * @var string 关联类型
  43. */
  44. public string $reType;
  45. /**
  46. * @var ACTION_STATUS 状态
  47. */
  48. public ACTION_STATUS $status;
  49. /**
  50. * @var string 创建时间
  51. */
  52. public string $createdAt;
  53. /**
  54. * @var string 更新时间
  55. */
  56. public string $updatedAt;
  57. /**
  58. * 从模型创建DTO
  59. *
  60. * @param UserAction $model 用户操作记录模型
  61. * @return self
  62. */
  63. public static function fromModel(UserAction $model): self
  64. {
  65. $dto = new self();
  66. $dto->id = $model->id;
  67. $dto->adminId = $model->admin_id;
  68. $dto->userId = $model->user_id;
  69. $dto->type = $model->type;
  70. $dto->desc = $model->desc;
  71. $dto->expTime = $model->exp_time;
  72. $dto->reId = $model->re_id;
  73. $dto->reType = $model->re_type;
  74. $dto->status = $model->status;
  75. $dto->createdAt = $model->created_at ? $model->created_at->toDateTimeString() : '';
  76. $dto->updatedAt = $model->updated_at ? $model->updated_at->toDateTimeString() : '';
  77. return $dto;
  78. }
  79. /**
  80. * 转换为数组
  81. *
  82. * @return array
  83. */
  84. public function toArray(): array
  85. {
  86. return [
  87. 'id' => $this->id,
  88. 'admin_id' => $this->adminId,
  89. 'user_id' => $this->userId,
  90. 'type' => $this->type->value,
  91. 'type_name' => $this->type->name,
  92. 'desc' => $this->desc,
  93. 'exp_time' => $this->expTime,
  94. 're_id' => $this->reId,
  95. 're_type' => $this->reType,
  96. 'status' => $this->status->value,
  97. 'status_name' => $this->status->name,
  98. 'created_at' => $this->createdAt,
  99. 'updated_at' => $this->updatedAt,
  100. ];
  101. }
  102. }