| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace App\Module\GameItems\Dtos;
- use App\Module\GameItems\Models\ItemUser;
- use UCore\Dto\BaseDto;
- /**
- * 用户物品数据传输对象
- *
- * 用于在服务层和控制器层之间传递用户物品数据,避免直接暴露模型
- */
- class ItemUserDto extends BaseDto
- {
- /**
- * 用户物品ID
- *
- * @var int
- */
- public int $id;
- /**
- * 用户ID
- *
- * @var int
- */
- public int $userId;
- /**
- * 物品ID
- *
- * @var int
- */
- public int $itemId;
- /**
- * 物品名称
- *
- * @var string|null
- */
- public ?string $itemName;
- /**
- * 物品类型
- *
- * @var int|null
- */
- public ?int $itemType;
- /**
- * 物品类型名称
- *
- * @var string|null
- */
- public ?string $itemTypeName;
- /**
- * 物品实例ID
- *
- * @var int|null
- */
- public ?int $instanceId;
- /**
- * 数量
- *
- * @var int
- */
- public int $quantity;
- /**
- * 过期时间
- *
- * @var string|null
- */
- public ?string $expireAt;
- /**
- * 是否冻结
- *
- * @var bool
- */
- public bool $isFrozen;
- /**
- * 物品数据
- *
- * @var ItemDto|null
- */
- public ?ItemDto $item;
- /**
- * 创建时间
- *
- * @var string
- */
- public string $createdAt;
- /**
- * 更新时间
- *
- * @var string
- */
- public string $updatedAt;
- /**
- * 从模型创建DTO
- *
- * @param ItemUser $itemUser 用户物品模型
- * @param bool $includeItem 是否包含物品信息
- * @return self
- */
- public static function fromModel(ItemUser $itemUser, bool $includeItem = true): self
- {
- $dto = new self();
- $dto->id = $itemUser->id;
- $dto->userId = $itemUser->user_id;
- $dto->itemId = $itemUser->item_id;
- $dto->itemName = $itemUser->item->name ?? null;
- $dto->itemType = $itemUser->item->type->value ?? null;
- $dto->itemTypeName = $itemUser->item->type ? \App\Module\GameItems\Enums\ITEM_TYPE::getName($itemUser->item->type->value) : null;
- $dto->instanceId = $itemUser->instance_id;
- $dto->quantity = $itemUser->quantity;
- $dto->expireAt = $itemUser->expire_at ? $itemUser->expire_at->format('Y-m-d H:i:s') : null;
- $dto->isFrozen = $itemUser->is_frozen;
- $dto->createdAt = $itemUser->created_at->format('Y-m-d H:i:s');
- $dto->updatedAt = $itemUser->updated_at->format('Y-m-d H:i:s');
-
- // 如果需要包含物品信息
- if ($includeItem && $itemUser->item) {
- $dto->item = ItemDto::fromModel($itemUser->item);
- } else {
- $dto->item = null;
- }
-
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'id' => $this->id,
- 'user_id' => $this->userId,
- 'item_id' => $this->itemId,
- 'item_name' => $this->itemName,
- 'item_type' => $this->itemType,
- 'item_type_name' => $this->itemTypeName,
- 'instance_id' => $this->instanceId,
- 'quantity' => $this->quantity,
- 'expire_at' => $this->expireAt,
- 'is_frozen' => $this->isFrozen,
- 'item' => $this->item ? $this->item->toArray() : null,
- 'created_at' => $this->createdAt,
- 'updated_at' => $this->updatedAt,
- ];
- }
- }
|