<?php
header('Content-Type:text/html;charset=utf-8');
// 数据库配置（已按你宝塔信息填好）
$db_host = 'localhost';
$db_user = 'gdwkjx';
$db_pwd = 'gdwkjx1234';
$db_name = 'gdwkjx';
$conn = mysqli_connect($db_host,$db_user,$db_pwd,$db_name);
mysqli_set_charset($conn,'utf8');
$domain = 'https://www.gdwkjx.cn';

// 初始化原有6条栏目页面XML内容
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- 原有固定栏目页 -->
    <url>
        <loc>'.$domain.'/index.php</loc>
        <lastmod>2026-06-14</lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc>'.$domain.'/product.php</loc>
        <lastmod>2026-06-14</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc>'.$domain.'/business.php</loc>
        <lastmod>2026-06-14</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc>'.$domain.'/about.php</loc>
        <lastmod>2026-06-14</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>'.$domain.'/news.php</loc>
        <lastmod>2026-06-14</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc>'.$domain.'/contact.php</loc>
        <lastmod>2026-06-14</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
';

// 自动读取所有已发布产品详情
$pro_sql = "SELECT id,update_time FROM product WHERE status=1";
$pro_res = mysqli_query($conn,$pro_sql);
while($row = mysqli_fetch_assoc($pro_res)){
    $url = $domain.'/product_detail.php?id='.$row['id'];
    $time = date('Y-m-d',strtotime($row['update_time']));
    $xml .= '
    <url>
        <loc>'.htmlspecialchars($url).'</loc>
        <lastmod>'.$time.'</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.85</priority>
    </url>';
}

// 自动读取所有新闻详情
$news_sql = "SELECT id,update_time FROM news WHERE status=1";
$news_res = mysqli_query($conn,$news_sql);
while($row = mysqli_fetch_assoc($news_res)){
    $url = $domain.'/news_detail.php?id='.$row['id'];
    $time = date('Y-m-d',strtotime($row['update_time']));
    $xml .= '
    <url>
        <loc>'.htmlspecialchars($url).'</loc>
        <lastmod>'.$time.'</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.75</priority>
    </url>';
}

$xml .= '</urlset>';

// 自动覆盖根目录原有sitemap.xml
file_put_contents('./sitemap.xml',$xml);
echo "完整站点地图生成完成，已包含全部产品、新闻详情页面";
mysqli_close($conn);
?>