//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.luozhihui.project.mapper;

import com.luozhihui.project.entity.Post;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface PostMapper {
    @Select({"SELECT p.*, u.UserName, c.name AS categoryName FROM post p  JOIN user u ON p.userId = u.UserId LEFT JOIN category c ON p.categoryId = c.id WHERE p.status = 1 AND (#{categoryId} IS NULL OR p.categoryId = #{categoryId}) AND (#{searchText} IS NULL OR p.title LIKE CONCAT('%', #{searchText}, '%')) ORDER BY p.createTime DESC LIMIT #{offset}, #{pageSize}"})
    List<Post> selectPostList(@Param("categoryId") Long categoryId, @Param("searchText") String searchText, @Param("offset") int offset, @Param("pageSize") int pageSize);

    @Select({"SELECT COUNT(*) FROM post p  WHERE p.status = 1  AND (#{categoryId} IS NULL OR p.categoryId = #{categoryId}) AND (#{searchText} IS NULL OR p.title LIKE CONCAT('%', #{searchText}, '%'))"})
    int countPosts(@Param("categoryId") Long categoryId, @Param("searchText") String searchText);

    @Select({"SELECT p.*, c.name AS categoryName, u.userName FROM post p JOIN category c ON p.categoryId = c.id JOIN user u ON p.userId = u.UserId WHERE p.id = #{id} AND p.status = 1"})
    Post selectById(@Param("id") Long id);

    @Update({" UPDATE post SET viewCount = #{viewCount}, likeCount = #{likeCount}, replyCount = #{replyCount}, updateTime = NOW() WHERE id = #{id}"})
    int updateById(Post post);

    @Insert({" INSERT INTO post (title, content, userId, categoryId, status,viewCount, likeCount, replyCount, createTime, updateTime)VALUES (#{title}, #{content}, #{userId}, #{categoryId}, #{status},#{viewCount}, #{likeCount}, #{replyCount}, NOW(), NOW())"})
    @Options(
            useGeneratedKeys = true,
            keyProperty = "id"
    )
    int insert(Post post);

    @Delete({"DELETE FROM post WHERE id = #{id}"})
    int deletePost(@Param("id") Long id);
}
