with([ 'category', 'consumeGroup.consumeItems', 'rewardGroup.rewardItems', 'activePurchaseLimits' ]) ->where('is_active', 1) ->where(function ($query) { // 检查上架时间 $query->whereNull('start_time') ->orWhere('start_time', '<=', now()); }) ->where(function ($query) { // 检查下架时间 $query->whereNull('end_time') ->orWhere('end_time', '>=', now()); }) // ->orderBy('sort_order', 'desc') ->orderBy('id', 'asc') ->get() ->map(function (ShopItem $item) { // 构建商品数据 $itemData = [ 'id' => $item->id, 'name' => $item->name, 'description' => $item->description, 'category_name' => $item->category_name, 'max_single_buy' => $item->max_single_buy, 'sort_order' => $item->sort_order, 'display_attributes' => $item->display_attributes, 'start_time' => $item->start_time ? $item->start_time->toDateTimeString() : null, 'end_time' => $item->end_time ? $item->end_time->toDateTimeString() : null, ]; // 添加消耗组信息 if ($item->consumeGroup) { $consume = ConsumeService::getConsumeGroupAsDeduct($item->consume_group_id); $itemData['consume_group'] = json_decode($consume->serializeToJsonString(),true); } // 添加奖励组信息 if ($item->rewardGroup) { $reward = RewardService::getRewardGroupAsReward($item->reward_group_id); $itemData['reward_group'] = json_decode($reward->serializeToJsonString(),true); } // 添加限购信息 if ($item->activePurchaseLimits->isNotEmpty()) { $itemData['purchase_limits'] = $item->activePurchaseLimits->map(function ($limit) { return [ 'id' => $limit->id, 'type' => $limit->type, 'period' => $limit->period, 'max_quantity' => $limit->max_quantity, 'description' => $limit->description, ]; })->toArray(); } return $itemData; }) ->toArray(); // dd($shopItems); // 准备完整数据,包含生成时间 $data = [ 'generated_ts' => time(), 'generated_at' => now()->toDateTimeString(), 'shop_items' => $shopItems ]; return $data; } catch (\Exception $e) { Log::error('Generate shop_items.json failed: ' . $e->getMessage()); return false; } } public function handle() { if (ShopItemsJsonConfig::getData([], true)) { $this->info('Successfully generated shop_items.json with timestamp'); } else { $this->error('Failed to generate shop_items.json'); } } }