| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace App\Module\Shop\Dtos;
- use App\Module\Shop\Models\ShopItem;
- use UCore\Dto\BaseDto;
- /**
- * 商店商品数据传输对象
- *
- * 用于在服务层和控制器层之间传递商店商品数据,避免直接暴露模型
- */
- class ShopItemDto extends BaseDto
- {
- /**
- * 商品ID
- *
- * @var int
- */
- public int $id;
- /**
- * 商品名称
- *
- * @var string
- */
- public string $name;
- /**
- * 商品描述
- *
- * @var string|null
- */
- public ?string $description;
- /**
- * 分类ID
- *
- * @var int|null
- */
- public ?int $categoryId;
- /**
- * 分类名称(字符串格式)
- *
- * @var string|null
- */
- public ?string $categoryName;
- /**
- * 消耗组ID
- *
- * @var int|null
- */
- public ?int $consumeGroupId;
- /**
- * 消耗组名称
- *
- * @var string|null
- */
- public ?string $consumeGroupName;
- /**
- * 消耗组描述
- *
- * @var string|null
- */
- public ?string $consumeGroupDescription;
- /**
- * 奖励组ID
- *
- * @var int|null
- */
- public ?int $rewardGroupId;
- /**
- * 奖励组名称
- *
- * @var string|null
- */
- public ?string $rewardGroupName;
- /**
- * 奖励组描述
- *
- * @var string|null
- */
- public ?string $rewardGroupDescription;
- /**
- * 最大购买数量(0表示无限制)
- *
- * @var int
- */
- public int $maxBuy;
- /**
- * 是否激活
- *
- * @var bool
- */
- public bool $isActive;
- /**
- * 排序权重
- *
- * @var int
- */
- public int $sortOrder;
- /**
- * 展示属性
- *
- * @var array|null
- */
- public ?array $displayAttributes;
- /**
- * 上架时间(时间戳)
- *
- * @var int|null
- */
- public ?int $startTime;
- /**
- * 下架时间(时间戳)
- *
- * @var int|null
- */
- public ?int $endTime;
- /**
- * 创建时间
- *
- * @var string
- */
- public string $createdAt;
- /**
- * 更新时间
- *
- * @var string
- */
- public string $updatedAt;
- /**
- * 用户已购买数量(可选,需要传入用户ID时才设置)
- *
- * @var int|null
- */
- public ?int $userBoughtCount = null;
- /**
- * 当前有效的促销活动(可选)
- *
- * @var ShopPromotionDto|null
- */
- public ?ShopPromotionDto $activePromotion = null;
- /**
- * 从模型创建DTO
- *
- * @param ShopItem $shopItem 商店商品模型
- * @param int|null $userId 用户ID(可选,用于获取用户购买数量)
- * @return self
- */
- public static function fromModel(ShopItem $shopItem, ?int $userId = null): self
- {
- $dto = new self();
- $dto->id = $shopItem->id;
- $dto->name = $shopItem->name;
- $dto->description = $shopItem->description;
- $dto->categoryId = $shopItem->category_id;
- $dto->categoryName = $shopItem->category_name ?? ($shopItem->category->name ?? null);
- $dto->consumeGroupId = $shopItem->consume_group_id;
- $dto->consumeGroupName = $shopItem->consumeGroup->name ?? null;
- $dto->consumeGroupDescription = $shopItem->consumeGroup->description ?? null;
- $dto->rewardGroupId = $shopItem->reward_group_id;
- $dto->rewardGroupName = $shopItem->rewardGroup->name ?? null;
- $dto->rewardGroupDescription = $shopItem->rewardGroup->description ?? null;
- $dto->maxBuy = $shopItem->max_single_buy;
- $dto->isActive = $shopItem->is_active;
- $dto->sortOrder = $shopItem->sort_order;
- $dto->displayAttributes = $shopItem->display_attributes ? $shopItem->display_attributes->toArray() : null;
- $dto->startTime = $shopItem->start_time ? $shopItem->start_time->timestamp : null;
- $dto->endTime = $shopItem->end_time ? $shopItem->end_time->timestamp : null;
- $dto->createdAt = $shopItem->created_at->toDateTimeString();
- $dto->updatedAt = $shopItem->updated_at->toDateTimeString();
- // 如果提供了用户ID,获取用户已购买数量
- if ($userId !== null) {
- $dto->userBoughtCount = $shopItem->getUserBoughtCount($userId);
- }
- // 获取当前有效的促销活动
- $activePromotion = $shopItem->getActivePromotion();
- if ($activePromotion) {
- $dto->activePromotion = ShopPromotionDto::fromModel($activePromotion);
- }
- return $dto;
- }
- /**
- * 从模型创建DTO(优化版本,避免N+1查询)
- *
- * @param ShopItem $shopItem 商店商品模型
- * @param int|null $userId 用户ID(可选,用于获取用户购买数量)
- * @param array $userBoughtCounts 用户购买数量数组(预先查询)
- * @return self
- */
- public static function fromModelOptimized(ShopItem $shopItem, ?int $userId = null, array $userBoughtCounts = []): self
- {
- $dto = new self();
- $dto->id = $shopItem->id;
- $dto->name = $shopItem->name;
- $dto->description = $shopItem->description;
- $dto->categoryId = $shopItem->category_id;
- $dto->categoryName = $shopItem->category_name ?? ($shopItem->category->name ?? null);
- $dto->consumeGroupId = $shopItem->consume_group_id;
- $dto->consumeGroupName = $shopItem->consumeGroup->name ?? null;
- $dto->consumeGroupDescription = $shopItem->consumeGroup->description ?? null;
- $dto->rewardGroupId = $shopItem->reward_group_id;
- $dto->rewardGroupName = $shopItem->rewardGroup->name ?? null;
- $dto->rewardGroupDescription = $shopItem->rewardGroup->description ?? null;
- $dto->maxBuy = $shopItem->max_single_buy;
- $dto->isActive = $shopItem->is_active;
- $dto->sortOrder = $shopItem->sort_order;
- $dto->displayAttributes = $shopItem->display_attributes ? $shopItem->display_attributes->toArray() : null;
- $dto->startTime = $shopItem->start_time ? $shopItem->start_time->timestamp : null;
- $dto->endTime = $shopItem->end_time ? $shopItem->end_time->timestamp : null;
- $dto->createdAt = $shopItem->created_at->toDateTimeString();
- $dto->updatedAt = $shopItem->updated_at->toDateTimeString();
- // 使用预先查询的用户购买数量,避免N+1查询
- if ($userId !== null) {
- $dto->userBoughtCount = $userBoughtCounts[$shopItem->id] ?? 0;
- }
- // 获取当前有效的促销活动(已通过with预加载)
- $activePromotion = $shopItem->promotions->first();
- if ($activePromotion) {
- $dto->activePromotion = ShopPromotionDto::fromModel($activePromotion);
- }
- return $dto;
- }
- }
|