ItemUserDto.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Module\GameItems\Dtos;
  3. use App\Module\GameItems\Models\ItemUser;
  4. use UCore\Dto\BaseDto;
  5. /**
  6. * 用户物品数据传输对象
  7. *
  8. * 用于在服务层和控制器层之间传递用户物品数据,避免直接暴露模型
  9. */
  10. class ItemUserDto extends BaseDto
  11. {
  12. /**
  13. * 用户物品ID
  14. *
  15. * @var int
  16. */
  17. public int $id;
  18. /**
  19. * 用户ID
  20. *
  21. * @var int
  22. */
  23. public int $userId;
  24. /**
  25. * 物品ID
  26. *
  27. * @var int
  28. */
  29. public int $itemId;
  30. /**
  31. * 物品名称
  32. *
  33. * @var string|null
  34. */
  35. public ?string $itemName;
  36. /**
  37. * 物品类型
  38. *
  39. * @var int|null
  40. */
  41. public ?int $itemType;
  42. /**
  43. * 物品类型名称
  44. *
  45. * @var string|null
  46. */
  47. public ?string $itemTypeName;
  48. /**
  49. * 物品实例ID
  50. *
  51. * @var int|null
  52. */
  53. public ?int $instanceId;
  54. /**
  55. * 数量
  56. *
  57. * @var int
  58. */
  59. public int $quantity;
  60. /**
  61. * 过期时间
  62. *
  63. * @var string|null
  64. */
  65. public ?string $expireAt;
  66. /**
  67. * 是否冻结
  68. *
  69. * @var bool
  70. */
  71. public bool $isFrozen;
  72. /**
  73. * 物品数据
  74. *
  75. * @var ItemDto|null
  76. */
  77. public ?ItemDto $item;
  78. /**
  79. * 创建时间
  80. *
  81. * @var string
  82. */
  83. public string $createdAt;
  84. /**
  85. * 更新时间
  86. *
  87. * @var string
  88. */
  89. public string $updatedAt;
  90. /**
  91. * 从模型创建DTO
  92. *
  93. * @param ItemUser $itemUser 用户物品模型
  94. * @param bool $includeItem 是否包含物品信息
  95. * @return self
  96. */
  97. public static function fromModel(ItemUser $itemUser, bool $includeItem = true): self
  98. {
  99. $dto = new self();
  100. $dto->id = $itemUser->id;
  101. $dto->userId = $itemUser->user_id;
  102. $dto->itemId = $itemUser->item_id;
  103. $dto->itemName = $itemUser->item->name ?? null;
  104. $dto->itemType = $itemUser->item->type->value ?? null;
  105. $dto->itemTypeName = $itemUser->item->type ? \App\Module\GameItems\Enums\ITEM_TYPE::getName($itemUser->item->type->value) : null;
  106. $dto->instanceId = $itemUser->instance_id;
  107. $dto->quantity = $itemUser->quantity;
  108. $dto->expireAt = $itemUser->expire_at ? $itemUser->expire_at->format('Y-m-d H:i:s') : null;
  109. $dto->isFrozen = $itemUser->is_frozen;
  110. $dto->createdAt = $itemUser->created_at->format('Y-m-d H:i:s');
  111. $dto->updatedAt = $itemUser->updated_at->format('Y-m-d H:i:s');
  112. // 如果需要包含物品信息
  113. if ($includeItem && $itemUser->item) {
  114. $dto->item = ItemDto::fromModel($itemUser->item);
  115. } else {
  116. $dto->item = null;
  117. }
  118. return $dto;
  119. }
  120. /**
  121. * 转换为数组
  122. *
  123. * @return array
  124. */
  125. public function toArray(): array
  126. {
  127. return [
  128. 'id' => $this->id,
  129. 'user_id' => $this->userId,
  130. 'item_id' => $this->itemId,
  131. 'item_name' => $this->itemName,
  132. 'item_type' => $this->itemType,
  133. 'item_type_name' => $this->itemTypeName,
  134. 'instance_id' => $this->instanceId,
  135. 'quantity' => $this->quantity,
  136. 'expire_at' => $this->expireAt,
  137. 'is_frozen' => $this->isFrozen,
  138. 'item' => $this->item ? $this->item->toArray() : null,
  139. 'created_at' => $this->createdAt,
  140. 'updated_at' => $this->updatedAt,
  141. ];
  142. }
  143. }