QueryHandler.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Module\AppGame\Handler\Shop;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\Shop\Services\ShopService;
  5. use Google\Protobuf\Internal\Message;
  6. use Illuminate\Support\Facades\Log;
  7. use Uraus\Kku\Common\DataShopCategory;
  8. use Uraus\Kku\Common\DataShopItem;
  9. use Uraus\Kku\Common\LastData;
  10. use Uraus\Kku\Request\RequestShopQuery;
  11. use Uraus\Kku\Response\ResponseShopQuery;
  12. use UCore\Exception\LogicException;
  13. /**
  14. * 处理商店查询请求
  15. */
  16. class QueryHandler extends BaseHandler
  17. {
  18. /**
  19. * 是否需要登录
  20. * @var bool
  21. */
  22. protected bool $need_login = true;
  23. /**
  24. * 处理商店查询请求
  25. *
  26. * @param RequestShopQuery $data 商店查询请求数据
  27. * @return ResponseShopQuery 商店查询响应
  28. */
  29. public function handle(Message $data): Message
  30. {
  31. // 创建响应对象
  32. $response = new ResponseShopQuery();
  33. try {
  34. // 获取请求参数
  35. $categoryId = $data->getCategoryId() ?? 0;
  36. $promotionId = $data->getPromotionId() ?? 0;
  37. $onlyPromotion = $data->getOnlyPromotion() ?? false;
  38. $userId = $this->user_id;
  39. // 构建过滤条件
  40. $filters = [];
  41. if ($categoryId > 0) {
  42. $filters['category_id'] = $categoryId;
  43. }
  44. if ($promotionId > 0) {
  45. $filters['promotion_id'] = $promotionId;
  46. }
  47. if ($onlyPromotion) {
  48. $filters['only_promotion'] = true;
  49. }
  50. // 获取商店商品列表
  51. $shopItems = ShopService::getShopItems($filters);
  52. // 获取商店分类列表
  53. $shopCategories = ShopService::getShopCategories();
  54. // 创建LastData对象,用于返回商店信息
  55. $lastData = new LastData();
  56. // 添加商品数据
  57. $itemList = [];
  58. foreach ($shopItems as $shopItem) {
  59. $dataItem = new DataShopItem();
  60. $dataItem->setId($shopItem->id);
  61. $dataItem->setName($shopItem->name);
  62. $dataItem->setDescription($shopItem->description);
  63. $dataItem->setCategoryId($shopItem->category_id);
  64. $dataItem->setItemId($shopItem->item_id);
  65. $dataItem->setItemQuantity($shopItem->item_quantity);
  66. $dataItem->setPrice($shopItem->price);
  67. $dataItem->setCurrencyId($shopItem->currency_id);
  68. $dataItem->setMaxBuy($shopItem->max_buy);
  69. $dataItem->setImage($shopItem->image);
  70. // 设置折扣价格信息
  71. $dataItem->setOriginalPrice($shopItem->original_price ?? $shopItem->price);
  72. $dataItem->setDiscountedPrice($shopItem->discounted_price ?? $shopItem->price);
  73. $dataItem->setHasDiscount($shopItem->has_discount ?? false);
  74. $dataItem->setDiscountPercentage($shopItem->discount_percentage ?? 0);
  75. // 如果有促销活动,设置促销信息
  76. $promotion = $shopItem->getActivePromotion();
  77. if ($promotion) {
  78. $dataItem->setPromotionId($promotion->id);
  79. $dataItem->setPromotionName($promotion->name);
  80. $dataItem->setPromotionEndTime($promotion->end_time ? $promotion->end_time->timestamp : 0);
  81. }
  82. // 如果有购买限制,获取用户已购买数量
  83. if ($shopItem->max_buy > 0) {
  84. $boughtCount = $shopItem->getUserBoughtCount($userId);
  85. $dataItem->setBoughtCount($boughtCount);
  86. }
  87. $itemList[] = $dataItem;
  88. }
  89. // 添加分类数据
  90. $categoryList = [];
  91. foreach ($shopCategories as $category) {
  92. $dataCategory = new DataShopCategory();
  93. $dataCategory->setId($category->id);
  94. $dataCategory->setName($category->name);
  95. $dataCategory->setCode($category->code);
  96. $dataCategory->setIcon($category->icon);
  97. $dataCategory->setParentId($category->parent_id);
  98. $categoryList[] = $dataCategory;
  99. }
  100. // 设置商品列表和分类列表到LastData
  101. $lastData->setShopItems($itemList);
  102. $lastData->setShopCategories($categoryList);
  103. // 设置LastData到响应
  104. $this->response->setLastData($lastData);
  105. // 设置响应状态
  106. $this->response->setCode(0);
  107. $this->response->setMsg('查询成功');
  108. // 记录日志
  109. Log::info('用户查询商店成功', [
  110. 'user_id' => $userId,
  111. 'category_id' => $categoryId,
  112. 'item_count' => count($itemList),
  113. 'category_count' => count($categoryList)
  114. ]);
  115. } catch (LogicException $e) {
  116. // 设置错误响应
  117. $this->response->setCode(400);
  118. $this->response->setMsg($e->getMessage());
  119. Log::warning('用户查询商店失败', [
  120. 'user_id' => $this->user_id,
  121. 'error' => $e->getMessage(),
  122. 'trace' => $e->getTraceAsString()
  123. ]);
  124. } catch (\Exception $e) {
  125. // 设置错误响应
  126. $this->response->setCode(500);
  127. $this->response->setMsg('系统错误,请稍后再试');
  128. Log::error('查询商店操作异常', [
  129. 'user_id' => $this->user_id,
  130. 'error' => $e->getMessage(),
  131. 'trace' => $e->getTraceAsString()
  132. ]);
  133. }
  134. return $response;
  135. }
  136. }