<%@ page contentType="application/xml;charset=UTF-8" language="java" pageEncoding="UTF-8"%><%@
page import="com.zhitaotech.util.*" %><%@
page import="java.sql.*" %><%@
page import="java.text.SimpleDateFormat" %><%@
page import="java.util.Date" %><%
    // 设置响应头
    response.setContentType("application/xml; charset=UTF-8");
    response.setHeader("Cache-Control", "max-age=3600");
    
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
    
    // 获取配置中的网站URL
    ResultSet urlRs = DBUtil.queryone("SELECT config_value FROM cms_config WHERE config_key = 'site_url'");
    String siteUrl = "http://localhost:8080";
    if (urlRs != null && urlRs.next()) {
        siteUrl = urlRs.getString("config_value");
        if (siteUrl != null && !siteUrl.endsWith("/")) {
            siteUrl += "/";
        }
    }
    urlRs.close();
%><?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <%-- 首页 --%>
    <url>
        <loc><%= siteUrl %></loc>
        <lastmod><%= sdfDate.format(new Date()) %></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <%-- 文章列表 --%>
    <url>
        <loc><%= siteUrl %>article_list.jsp</loc>
        <lastmod><%= sdfDate.format(new Date()) %></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <%-- 关于我们 --%>
    <url>
        <loc><%= siteUrl %>about.jsp</loc>
        <lastmod><%= sdfDate.format(new Date()) %></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <%-- 文章详情 --%>
    <%
        ResultSet articleRs = DBUtil.query("SELECT id, publish_time, update_time FROM cms_article WHERE is_enabled=1 ORDER BY publish_time DESC");
        while (articleRs.next()) {
            Date pubTime = articleRs.getTimestamp("publish_time");
            Date updTime = articleRs.getTimestamp("update_time");
            Date useTime = (updTime != null) ? updTime : (pubTime != null ? pubTime : new Date());
    %>
    <url>
        <loc><%= siteUrl %>article_detail.jsp?id=<%= articleRs.getInt("id") %></loc>
        <lastmod><%= sdfDate.format(useTime) %></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <% 
        }
        if (articleRs != null) articleRs.close();
    %>
</urlset>