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; } }