post第三次上传

Change-Id: I8ee9f57af704012dc22a69771fae4053258bad01
diff --git a/src/main/java/com/pt5/pthouduan/controller/PostController.java b/src/main/java/com/pt5/pthouduan/controller/PostController.java
new file mode 100644
index 0000000..a3a9017
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/controller/PostController.java
@@ -0,0 +1,149 @@
+package com.pt5.pthouduan.controller;
+
+import com.pt5.pthouduan.entity.Post;
+import com.pt5.pthouduan.service.PostService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * <p>
+ *  帖子控制器
+ * </p>
+ *
+ * 功能:创建帖子(支持上传图片、置顶、范围控制)、点赞、取消点赞、关键词搜索、删除、更新、置顶与取消置顶
+ *
+ * @author
+ * @since 2025-05-10
+ */
+@CrossOrigin(origins = "http://localhost:5173")
+@Controller
+@RequestMapping("/post")
+public class PostController {
+
+    @Autowired
+    private PostService postService;
+
+    // 创建帖子(支持图片上传)
+    @PostMapping("/create")
+    @ResponseBody
+    public boolean createPost(
+            @RequestParam("userid") Long userid,
+            @RequestParam("post_title") String post_title,
+            @RequestParam("post_content") String post_content,
+            @RequestParam(value = "tags", required = false) String tags,
+            @RequestParam(value = "rannge", required = false) String rannge,
+            @RequestParam(value = "is_pinned", required = false) Boolean is_pinned,
+            @RequestParam(value = "photo", required = false) MultipartFile photoFile
+    ) {
+        Post post = new Post();
+        post.setUserid(userid);
+        post.setPostTitle(post_title);
+        post.setPostContent(post_content);
+        post.setTags(tags);
+        post.setRannge(rannge);
+        post.setIsSticky(is_pinned != null && is_pinned);
+        post.setPostCreatedTime(LocalDateTime.now());
+        post.setUpdatedTime(LocalDateTime.now());
+        post.setLikes(0);
+
+
+        // 保存图片
+        if (photoFile != null && !photoFile.isEmpty()) {
+            String uploadDir = "D:/postuploads/";
+            File dir = new File(uploadDir);
+            if (!dir.exists()) dir.mkdirs();
+
+            String fileName = UUID.randomUUID() + "_" + photoFile.getOriginalFilename();
+            File dest = new File(uploadDir + fileName);
+            try {
+                photoFile.transferTo(dest);
+                post.setPhoto("/images/" + fileName);
+            } catch (IOException e) {
+                e.printStackTrace();
+                return false;
+            }
+        }
+
+        return postService.createPost(post) != null;
+    }
+
+
+
+    // 删除帖子
+    @DeleteMapping("/delete/{postid}")
+    @ResponseBody
+    public boolean deletePost(@PathVariable Integer postid) {
+        return postService.deletePost(postid);
+    }
+
+    // 更新帖子(包括置顶、范围、照片等)
+    @PutMapping("/update")
+    @ResponseBody
+    public boolean updatePost(@RequestBody Post post) {
+        return postService.updatePost(post);
+    }
+
+    // 关键词搜索
+    @GetMapping("/search")
+    @ResponseBody
+    public List<Post> searchPosts(@RequestParam String keyword) {
+        return postService.searchPostsByKeyword(keyword);
+    }
+
+    // 点赞
+    @PutMapping("/like/{postid}")
+    @ResponseBody
+    public boolean likePost(@PathVariable Integer postid) {
+        return postService.incrementLikes(postid);
+    }
+
+    @GetMapping("/all")
+    @ResponseBody
+    public List<Post> getAllPostsSorted() {
+        return postService.getAllPostsSorted();
+    }
+
+
+    // 取消点赞
+    @PutMapping("/unlike/{postid}")
+    @ResponseBody
+    public boolean unlikePost(@PathVariable Integer postid) {
+        return postService.decrementLikes(postid);
+    }
+
+    // 置顶帖子
+    @PutMapping("/pin/{postid}")
+    @ResponseBody
+    public boolean pinPost(@PathVariable Integer postid) {
+        return postService.setPinnedStatus(postid, true);
+    }
+
+    // 取消置顶
+    @PutMapping("/unpin/{postid}")
+    @ResponseBody
+    public boolean unpinPost(@PathVariable Integer postid) {
+        return postService.setPinnedStatus(postid, false);
+    }
+
+    // 根据用户ID获取该用户所有帖子
+    @GetMapping("/findByUserid")
+    @ResponseBody
+    public List<Post> findByUserid(@RequestParam Long userid) {
+        return postService.findByUserid(userid);
+    }
+
+    // 根据是否置顶查找帖子
+    @GetMapping("/findPinned")
+    @ResponseBody
+    public List<Post> findPinnedPosts() {
+        return postService.findPinnedPosts();
+    }
+}
diff --git a/src/main/java/com/pt5/pthouduan/entity/Post.java b/src/main/java/com/pt5/pthouduan/entity/Post.java
index d04a601..843800d 100644
--- a/src/main/java/com/pt5/pthouduan/entity/Post.java
+++ b/src/main/java/com/pt5/pthouduan/entity/Post.java
@@ -11,7 +11,7 @@
  * 
  * </p>
  *
- * @author ljx
+ * @author ym
  * @since 2025-04-14
  */
 @TableName("post")
@@ -22,16 +22,26 @@
     @TableId("postid")
     private Integer postid;
 
+    private Integer likes;
+
     private Long userid;
 
-    private LocalDateTime updatedTime;
+    private String photo;
 
-    private Boolean isSticky;
+    private String rannge;
 
-    private String postTitle;
+    private LocalDateTime updated_time;
+
+    private Boolean is_pinned;
+
+    private String post_title;
 
     private LocalDateTime postCreatedTime;
 
+    private String post_content;
+
+    private String tags;
+
     public Integer getPostid() {
         return postid;
     }
@@ -40,6 +50,14 @@
         this.postid = postid;
     }
 
+    public Integer getLikes() {
+        return likes;
+    }
+
+    public void setLikes(Integer likes) {
+        this.likes = likes;
+    }
+
     public Long getUserid() {
         return userid;
     }
@@ -49,27 +67,52 @@
     }
 
     public LocalDateTime getUpdatedTime() {
-        return updatedTime;
+        return updated_time;
     }
 
-    public void setUpdatedTime(LocalDateTime updatedTime) {
-        this.updatedTime = updatedTime;
+    public void setUpdatedTime(LocalDateTime updated_time) {
+        this.updated_time = updated_time;
+    }
+
+    public String getTags() {//获取标签
+        return tags;
+    }
+
+    public void setTags(String tags) {
+        this.tags = tags;
+    }
+
+
+    public String getPhoto() {//获取标签
+        return photo;
+    }
+
+    public void setPhoto(String photo) {
+        this.photo = photo;
+    }
+
+    public String getRannge() {
+        return rannge;
+    }
+
+    public void setRannge(String rannge) {
+        this.rannge = rannge;
     }
 
     public Boolean getIsSticky() {
-        return isSticky;
+        return is_pinned;
     }
 
-    public void setIsSticky(Boolean isSticky) {
-        this.isSticky = isSticky;
+    public void setIsSticky(Boolean is_pinned) {
+        this.is_pinned = is_pinned;
     }
 
     public String getPostTitle() {
-        return postTitle;
+        return post_title;
     }
 
-    public void setPostTitle(String postTitle) {
-        this.postTitle = postTitle;
+    public void setPostTitle(String post_title) {
+        this.post_title = post_title;
     }
 
     public LocalDateTime getPostCreatedTime() {
@@ -80,15 +123,28 @@
         this.postCreatedTime = postCreatedTime;
     }
 
+    public String getPostContent() {
+        return post_content;
+    }
+
+    public void setPostContent(String post_content) {
+        this.post_content = post_content;
+    }
+
     @Override
     public String toString() {
         return "Post{" +
-        "postid = " + postid +
-        ", userid = " + userid +
-        ", updatedTime = " + updatedTime +
-        ", isSticky = " + isSticky +
-        ", postTitle = " + postTitle +
-        ", postCreatedTime = " + postCreatedTime +
-        "}";
+                "postid=" + postid +
+                ", userid=" + userid +
+                ", photo='" + photo + '\'' +
+                ", updatedTime=" + updated_time +
+                ", likes=" + likes +
+                ", is_pinned=" + is_pinned +
+                ", post_title='" + post_title + '\'' +
+                ", post_content='" + post_content + '\'' +
+                ", postCreatedTime=" + postCreatedTime +
+                ", tags='" + tags + '\'' +
+                ", rannge='" + rannge + '\'' +
+                '}';
     }
 }
diff --git a/src/main/java/com/pt5/pthouduan/mapper/PostMapper.java b/src/main/java/com/pt5/pthouduan/mapper/PostMapper.java
index 817e555..6b95c73 100644
--- a/src/main/java/com/pt5/pthouduan/mapper/PostMapper.java
+++ b/src/main/java/com/pt5/pthouduan/mapper/PostMapper.java
@@ -3,16 +3,50 @@
 import com.pt5.pthouduan.entity.Post;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
  * <p>
- *  Mapper 接口
+ *  帖子 Mapper 接口
  * </p>
  *
- * @author ljx
+ * 功能:增、删、改、查(按关键词)、点赞、置顶、用户帖子查询、置顶帖子查询
+ *
+ * 作者:杨蔓
  * @since 2025-04-14
  */
 @Mapper
 public interface PostMapper extends BaseMapper<Post> {
 
+    // 创建帖子
+    void save(Post post);
+
+    // 根据帖子ID删除
+    int deleteByPostid(@Param("postid") Integer postid);
+
+    // 更新帖子
+    int updatePost(Post post);
+
+    // 模糊搜索(标题或标签)
+    List<Post> searchByKeyword(@Param("keyword") String keyword);
+
+    // 点赞 +1
+    int incrementLikes(@Param("postid") Integer postid);
+
+    // 取消点赞 -1(最小为0)
+    int decrementLikes(@Param("postid") Integer postid);
+
+    // 设置置顶状态
+    int updatePinnedStatus(@Param("postid") Integer postid, @Param("pinned") boolean pinned);
+
+    // 根据用户ID查询该用户所有帖子
+    List<Post> findByUserid(@Param("userid") Long userid);
+
+    // 查询所有置顶帖子
+    List<Post> findPinnedPosts();
+
+    // ✅ 新增:查询所有帖子(置顶优先,时间倒序)
+    List<Post> selectAllSorted();
 }
diff --git a/src/main/java/com/pt5/pthouduan/service/PostService.java b/src/main/java/com/pt5/pthouduan/service/PostService.java
new file mode 100644
index 0000000..fabf254
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/PostService.java
@@ -0,0 +1,46 @@
+package com.pt5.pthouduan.service;
+
+import com.pt5.pthouduan.entity.Post;
+
+import java.util.List;
+
+/**
+ * <p>
+ *  帖子服务接口
+ * </p>
+ *
+ * 功能:
+ *  - 增、删、改、查(按标签或标题)
+ *  - 点赞、取消点赞
+ *  - 设置置顶状态
+ *  - 可设置可见范围
+ *  - 上传图片(通常可选放在 Controller)
+ *  - 查找用户所有帖子
+ *  - 查找置顶帖子
+ *  - 获取所有帖子(置顶优先,时间倒序)
+ *
+ * @author 杨蔓
+ * @since 2025-04-14
+ */
+public interface PostService {
+
+    Post createPost(Post post);                      // 创建帖子
+
+    boolean deletePost(Integer postid);              // 删除帖子
+
+    boolean updatePost(Post post);                   // 更新帖子
+
+    List<Post> searchPostsByKeyword(String keyword); // 查找帖子(标题或标签)
+
+    boolean incrementLikes(Integer postid);          // 点赞(likes + 1)
+
+    boolean decrementLikes(Integer postid);          // 取消点赞(likes - 1,最小为0)
+
+    boolean setPinnedStatus(Integer postid, boolean pinned); // 设置是否置顶
+
+    List<Post> findByUserid(Long userid);            // 根据用户 ID 获取帖子
+
+    List<Post> findPinnedPosts();                    // 查找所有置顶帖子
+
+    List<Post> getAllPostsSorted();                  // ✅ 获取所有帖子(置顶优先,按时间排序)
+}
diff --git a/src/main/java/com/pt5/pthouduan/service/impl/PostServiceImpl.java b/src/main/java/com/pt5/pthouduan/service/impl/PostServiceImpl.java
new file mode 100644
index 0000000..d1bda7f
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/impl/PostServiceImpl.java
@@ -0,0 +1,76 @@
+package com.pt5.pthouduan.service.impl;
+
+import com.pt5.pthouduan.entity.Post;
+import com.pt5.pthouduan.mapper.PostMapper;
+import com.pt5.pthouduan.service.PostService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * <p>
+ *  帖子服务实现类
+ * </p>
+ *
+ * @author 杨蔓
+ * @since 2025-04-14
+ */
+@Service
+public class PostServiceImpl implements PostService {
+
+    @Autowired
+    private PostMapper postMapper;
+
+    @Override
+    public Post createPost(Post post) {
+        postMapper.save(post);
+        return post;
+    }
+
+    @Override
+    public boolean deletePost(Integer postid) {
+        return postMapper.deleteByPostid(postid) > 0;
+    }
+
+    @Override
+    public boolean updatePost(Post post) {
+        return postMapper.updatePost(post) > 0;
+    }
+
+    @Override
+    public List<Post> searchPostsByKeyword(String keyword) {
+        return postMapper.searchByKeyword(keyword);
+    }
+
+    @Override
+    public boolean incrementLikes(Integer postid) {
+        return postMapper.incrementLikes(postid) >= 0;
+    }
+
+    @Override
+    public boolean decrementLikes(Integer postid) {
+        return postMapper.decrementLikes(postid) >= 0;
+    }
+
+    @Override
+    public boolean setPinnedStatus(Integer postid, boolean pinned) {
+        return postMapper.updatePinnedStatus(postid, pinned) > 0;
+    }
+
+    @Override
+    public List<Post> findByUserid(Long userid) {
+        return postMapper.findByUserid(userid);
+    }
+
+    @Override
+    public List<Post> findPinnedPosts() {
+        return postMapper.findPinnedPosts();
+    }
+
+    /** ✅ 新增:获取所有帖子(置顶优先,时间倒序) */
+    @Override
+    public List<Post> getAllPostsSorted() {
+        return postMapper.selectAllSorted();
+    }
+}
diff --git a/src/main/resources/mapper/PostMapper.xml b/src/main/resources/mapper/PostMapper.xml
new file mode 100644
index 0000000..e676fcc
--- /dev/null
+++ b/src/main/resources/mapper/PostMapper.xml
@@ -0,0 +1,103 @@
+<?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.pt5.pthouduan.mapper.PostMapper">
+
+    <!-- 插入帖子 -->
+    <insert id="save" parameterType="com.pt5.pthouduan.entity.Post"
+            useGeneratedKeys="true" keyProperty="postid" keyColumn="postid">
+        INSERT INTO post (
+            userid,
+            photo,
+            rannge,
+            is_pinned,
+            post_title,
+            post_content,
+            postCreatedTime,
+            updated_time,
+            tags,
+            likes
+        ) VALUES (
+                     #{userid},
+                     #{photo},
+                     #{rannge},
+                     #{is_pinned},
+                     #{post_title},
+                     #{post_content},
+                     #{postCreatedTime},
+                     #{updated_time},
+                     #{tags},
+                     COALESCE(#{likes}, 0)
+                 )
+    </insert>
+
+    <!-- 删除帖子 -->
+    <delete id="deleteByPostid" parameterType="int">
+        DELETE FROM post WHERE postid = #{postid}
+    </delete>
+
+    <!-- 更新帖子 -->
+    <update id="updatePost" parameterType="com.pt5.pthouduan.entity.Post">
+        UPDATE post
+        SET
+            userid = #{userid},
+            photo = #{photo},
+            rannge = #{rannge},
+            updated_time = #{updated_time},
+            is_pinned = #{is_pinned},
+            post_title = #{post_title},
+            post_content = #{post_content},
+            postCreatedTime = #{postCreatedTime},
+            tags = #{tags},
+            likes = COALESCE(#{likes}, 0)
+        WHERE postid = #{postid}
+    </update>
+
+    <!-- 模糊搜索帖子 -->
+    <select id="searchByKeyword" resultType="com.pt5.pthouduan.entity.Post">
+        SELECT * FROM post
+        WHERE post_title LIKE CONCAT('%', #{keyword}, '%')
+           OR tags LIKE CONCAT('%', #{keyword}, '%')
+    </select>
+
+    <!-- 点赞 +1 -->
+    <update id="incrementLikes" parameterType="int">
+        UPDATE post
+        SET likes = likes + 1
+        WHERE postid = #{postid}
+    </update>
+
+    <!-- 取消点赞 -->
+    <update id="decrementLikes" parameterType="int">
+        UPDATE post
+        SET likes = CASE WHEN likes > 0 THEN likes - 1 ELSE 0 END
+        WHERE postid = #{postid}
+    </update>
+
+    <!-- 更新置顶状态 -->
+    <update id="updatePinnedStatus" parameterType="map">
+        UPDATE post
+        SET is_pinned = #{pinned}
+        WHERE postid = #{postid}
+    </update>
+
+    <!-- 根据用户 ID 查询其所有帖子 -->
+    <select id="findByUserid" resultType="com.pt5.pthouduan.entity.Post">
+        SELECT * FROM post
+        WHERE userid = #{userid}
+    </select>
+
+    <!-- 查询所有置顶帖子 -->
+    <select id="findPinnedPosts" resultType="com.pt5.pthouduan.entity.Post">
+        SELECT * FROM post
+        WHERE is_pinned = TRUE
+    </select>
+
+    <!-- 查询所有帖子,按置顶优先、创建时间倒序 -->
+    <select id="selectAllSorted" resultType="com.pt5.pthouduan.entity.Post">
+        SELECT * FROM post
+        ORDER BY is_pinned DESC, postCreatedTime DESC
+    </select>
+
+</mapper>