<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.boyue.hasfj.mapper.HtmlPagesMapper">

    <resultMap type="HtmlPages" id="HtmlPagesResult">
        <result property="id" column="id" />
        <result property="formatId" column="format_id" />
        <result property="title" column="title" />
        <result property="content" column="content" />
        <result property="pageType" column="page_type" />
        <result property="pageUrl" column="page_url" />
        <result property="createTime" column="create_time" />
        <result property="updateTime" column="update_time" />
        <result property="status" column="status" />
        <result property="sortOrder" column="sort_order" />
        <result property="viewCount" column="view_count" />
        <result property="author" column="author" />
        <result property="keywords" column="keywords" />
        <result property="description" column="description" />
        <result property="multiAttachments" column="multi_attachments" />
    </resultMap>

    <sql id="selectHtmlPagesVo">
        select
            hp.id,
            hp.format_id,
            hp.title,
            hp.content,
            hp.page_type,
            hp.page_url,
            hp.create_time,
            hp.update_time,
            hp.status,
            hp.sort_order,
            hp.view_count,
            hp.author,
            hp.keywords,
            hp.description,
            hp.multi_attachments
        from html_pages hp
    </sql>

    <select id="selectHtmlPagesList" parameterType="HtmlPages" resultMap="HtmlPagesResult">
        <include refid="selectHtmlPagesVo"/>
        <where>
            <if test="formatId != null and formatId != ''"> and hp.format_id = #{formatId}</if>
            <if test="title != null and title != ''"> and hp.title like concat('%', #{title}, '%')</if>
            <if test="pageType != null and pageType != ''"> and hp.page_type = #{pageType}</if>
            <if test="status != null"> and hp.status = #{status}</if>
            <if test="author != null and author != ''"> and hp.author like concat('%', #{author}, '%')</if>
            <if test="keywords != null and keywords != ''"> and hp.keywords like concat('%', #{keywords}, '%')</if>
        </where>
        order by hp.sort_order asc, hp.create_time desc
    </select>

    <select id="selectHtmlPagesById" parameterType="Long" resultMap="HtmlPagesResult">
        <include refid="selectHtmlPagesVo"/>
        where hp.id = #{id}
    </select>
    
    <select id="selectHtmlPagesByFormatId" parameterType="String" resultMap="HtmlPagesResult">
        <include refid="selectHtmlPagesVo"/>
        where hp.format_id = #{formatId}
    </select>
    
    <select id="selectHtmlPagesListByType" parameterType="String" resultMap="HtmlPagesResult">
        <include refid="selectHtmlPagesVo"/>
        where hp.page_type = #{pageType} and hp.status = 1
        order by hp.sort_order asc, hp.create_time desc
    </select>

    <insert id="insertHtmlPages" parameterType="HtmlPages" useGeneratedKeys="true" keyProperty="id">
        insert into html_pages
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="formatId != null and formatId != ''">format_id,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="pageType != null and pageType != ''">page_type,</if>
            <if test="pageUrl != null and pageUrl != ''">page_url,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="status != null">status,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="viewCount != null">view_count,</if>
            <if test="author != null">author,</if>
            <if test="keywords != null">keywords,</if>
            <if test="description != null">description,</if>
            <if test="multiAttachments != null">multi_attachments,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="formatId != null and formatId != ''">#{formatId},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="pageType != null and pageType != ''">#{pageType},</if>
            <if test="pageUrl != null and pageUrl != ''">#{pageUrl},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="status != null">#{status},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="viewCount != null">#{viewCount},</if>
            <if test="author != null">#{author},</if>
            <if test="keywords != null">#{keywords},</if>
            <if test="description != null">#{description},</if>
            <if test="multiAttachments != null">#{multiAttachments},</if>
        </trim>
    </insert>

    <update id="updateHtmlPages" parameterType="HtmlPages">
        update html_pages
        <trim prefix="SET" suffixOverrides=",">
            <if test="formatId != null and formatId != ''">format_id = #{formatId},</if>
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="content != null and content != ''">content = #{content},</if>
            <if test="pageType != null and pageType != ''">page_type = #{pageType},</if>
            <if test="pageUrl != null and pageUrl != ''">page_url = #{pageUrl},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="viewCount != null">view_count = #{viewCount},</if>
            <if test="author != null">author = #{author},</if>
            <if test="keywords != null">keywords = #{keywords},</if>
            <if test="description != null">description = #{description},</if>
            <if test="multiAttachments != null">multi_attachments = #{multiAttachments},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteHtmlPagesById" parameterType="Long">
        delete from html_pages where id = #{id}
    </delete>

    <delete id="deleteHtmlPagesByIds" parameterType="String">
        delete from html_pages where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 更新浏览次数（直接在数据库中累加） -->
    <update id="updateHtmlPagesViewCount" parameterType="Long">
        update html_pages
        set view_count = IFNULL(view_count, 0) + 1,
            update_time = sysdate()
        where id = #{id}
    </update>
</mapper>