| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace App\Module\AppGame\Handler\Shop;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\Shop\Services\ShopService;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Common\DataShopCategory;
- use Uraus\Kku\Common\DataShopItem;
- use Uraus\Kku\Common\LastData;
- use Uraus\Kku\Request\RequestShopQuery;
- use Uraus\Kku\Response\ResponseShopQuery;
- use UCore\Exception\LogicException;
- /**
- * 处理商店查询请求
- */
- class QueryHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理商店查询请求
- *
- * @param RequestShopQuery $data 商店查询请求数据
- * @return ResponseShopQuery 商店查询响应
- */
- public function handle(Message $data): Message
- {
- // 创建响应对象
- $response = new ResponseShopQuery();
- try {
- // 获取请求参数
- $categoryId = $data->getCategoryId() ?? 0;
- $promotionId = $data->getPromotionId() ?? 0;
- $onlyPromotion = $data->getOnlyPromotion() ?? false;
- $userId = $this->user_id;
- // 构建过滤条件
- $filters = [];
- if ($categoryId > 0) {
- $filters['category_id'] = $categoryId;
- }
- if ($promotionId > 0) {
- $filters['promotion_id'] = $promotionId;
- }
- if ($onlyPromotion) {
- $filters['only_promotion'] = true;
- }
- // 获取商店商品列表
- $shopItems = ShopService::getShopItems($filters);
- // 获取商店分类列表
- $shopCategories = ShopService::getShopCategories();
- // 创建LastData对象,用于返回商店信息
- $lastData = new LastData();
- // 添加商品数据
- $itemList = [];
- foreach ($shopItems as $shopItem) {
- $dataItem = new DataShopItem();
- $dataItem->setId($shopItem->id);
- $dataItem->setName($shopItem->name);
- $dataItem->setDescription($shopItem->description);
- $dataItem->setCategoryId($shopItem->category_id);
- $dataItem->setItemId($shopItem->item_id);
- $dataItem->setItemQuantity($shopItem->item_quantity);
- $dataItem->setPrice($shopItem->price);
- $dataItem->setCurrencyId($shopItem->currency_id);
- $dataItem->setMaxBuy($shopItem->max_buy);
- $dataItem->setImage($shopItem->image);
- // 设置折扣价格信息
- $dataItem->setOriginalPrice($shopItem->original_price ?? $shopItem->price);
- $dataItem->setDiscountedPrice($shopItem->discounted_price ?? $shopItem->price);
- $dataItem->setHasDiscount($shopItem->has_discount ?? false);
- $dataItem->setDiscountPercentage($shopItem->discount_percentage ?? 0);
- // 如果有促销活动,设置促销信息
- $promotion = $shopItem->getActivePromotion();
- if ($promotion) {
- $dataItem->setPromotionId($promotion->id);
- $dataItem->setPromotionName($promotion->name);
- $dataItem->setPromotionEndTime($promotion->end_time ? $promotion->end_time->timestamp : 0);
- }
- // 如果有购买限制,获取用户已购买数量
- if ($shopItem->max_buy > 0) {
- $boughtCount = $shopItem->getUserBoughtCount($userId);
- $dataItem->setBoughtCount($boughtCount);
- }
- $itemList[] = $dataItem;
- }
- // 添加分类数据
- $categoryList = [];
- foreach ($shopCategories as $category) {
- $dataCategory = new DataShopCategory();
- $dataCategory->setId($category->id);
- $dataCategory->setName($category->name);
- $dataCategory->setCode($category->code);
- $dataCategory->setIcon($category->icon);
- $dataCategory->setParentId($category->parent_id);
- $categoryList[] = $dataCategory;
- }
- // 设置商品列表和分类列表到LastData
- $lastData->setShopItems($itemList);
- $lastData->setShopCategories($categoryList);
- // 设置LastData到响应
- $this->response->setLastData($lastData);
- // 设置响应状态
- $this->response->setCode(0);
- $this->response->setMsg('查询成功');
- // 记录日志
- Log::info('用户查询商店成功', [
- 'user_id' => $userId,
- 'category_id' => $categoryId,
- 'item_count' => count($itemList),
- 'category_count' => count($categoryList)
- ]);
- } catch (LogicException $e) {
- // 设置错误响应
- $this->response->setCode(400);
- $this->response->setMsg($e->getMessage());
- Log::warning('用户查询商店失败', [
- 'user_id' => $this->user_id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- } catch (\Exception $e) {
- // 设置错误响应
- $this->response->setCode(500);
- $this->response->setMsg('系统错误,请稍后再试');
- Log::error('查询商店操作异常', [
- 'user_id' => $this->user_id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- return $response;
- }
- }
|