<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;
use App\Models\DistProduct;
use App\Models\SitePage as DistPages;
use App\Models\DistVideo;

class SitemapController extends Controller
{
    public function index()
    {
        // 定义缓存键和时长
        $cacheKey = "sitemap::sitemap_index_" . getDistId();
        $cacheDuration = 60 * 60; // 缓存时间(秒):60 分钟

        // 获取缓存数据或重新生成
        $sitemapData = Cache::remember($cacheKey, $cacheDuration, function () {
            // 查询数据
            $dist_products = DistProduct::where('enabled', 1)->where('dist_id', getDistId())->get();
            $dist_pages = DistPages::where('status', 1)->where('dist_id', getDistId())->get();
            $dist_videos = DistVideo::where('enabled', 1)->where('dist_id', getDistId())->get();

            // 返回查询结果
            return [
                'dist_products' => $dist_products,
                'dist_pages' => $dist_pages,
                'dist_videos' => $dist_videos,
            ];
        });

        // 创建视图并返回响应
        return response()->view('sitemap.index', $sitemapData)->header('Content-Type', 'application/xml');
    }
}