<?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.unilife.mapper.PostMapper">
    <resultMap id="postResultMap" type="com.unilife.model.entity.Post">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="category_id" property="categoryId"/>
        <result column="view_count" property="viewCount"/>
        <result column="like_count" property="likeCount"/>
        <result column="comment_count" property="commentCount"/>
        <result column="status" property="status"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
    </resultMap>
    
    <insert id="insert" parameterType="com.unilife.model.entity.Post" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO posts (
            user_id, title, content, category_id, view_count, like_count, comment_count, status, created_at, updated_at
        ) VALUES (
            #{userId}, #{title}, #{content}, #{categoryId}, #{viewCount}, #{likeCount}, #{commentCount}, #{status}, NOW(), NOW()
        )
    </insert>
    
    <select id="getById" resultMap="postResultMap">
        SELECT id, user_id, title, content, category_id, view_count, like_count, comment_count, status, created_at, updated_at
        FROM posts
        WHERE id = #{id} AND status != 0
    </select>

    <select id="getListByCategory" resultType="com.unilife.model.entity.Post">
        SELECT * FROM posts
        <where>
            <if test="categoryId != null">
                category_id = #{categoryId}
            </if>
        </where>
        <choose>
            <when test="sort == 'hot'">
                ORDER BY view_count DESC
            </when>
            <when test="sort == 'likes'">
                ORDER BY like_count DESC
            </when>
            <when test="sort == 'comments'">
                ORDER BY comment_count DESC
            </when>
            <otherwise>
                ORDER BY created_at DESC
            </otherwise>
        </choose>
    </select>
    
    <select id="getCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM posts
        WHERE status != 0
        <if test="categoryId != null">
            AND category_id = #{categoryId}
        </if>
    </select>
    
    <update id="update" parameterType="com.unilife.model.entity.Post">
        UPDATE posts
        SET title = #{title},
            content = #{content},
            category_id = #{categoryId},
            status = #{status},
            updated_at = NOW()
        WHERE id = #{id}
    </update>
    
    <update id="delete">
        UPDATE posts
        SET status = 0,
            updated_at = NOW()
        WHERE id = #{id}
    </update>
    
    <update id="incrementViewCount">
        UPDATE posts
        SET view_count = view_count + 1
        WHERE id = #{id}
    </update>
    
    <update id="incrementLikeCount">
        UPDATE posts
        SET like_count = like_count + 1
        WHERE id = #{id}
    </update>
    
    <update id="decrementLikeCount">
        UPDATE posts
        SET like_count = GREATEST(like_count - 1, 0)
        WHERE id = #{id}
    </update>
    
    <update id="incrementCommentCount">
        UPDATE posts
        SET comment_count = comment_count + 1
        WHERE id = #{id}
    </update>
    
    <update id="decrementCommentCount">
        UPDATE posts
        SET comment_count = GREATEST(comment_count - 1, 0)
        WHERE id = #{id}
    </update>
</mapper>