Ver código fonte

置顶文章功能

fangqing 2 meses atrás
pai
commit
02b250cd50

+ 73 - 4
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityArticleController.java

@@ -28,9 +28,8 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
 import java.text.ParseException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * 社区文章管理
@@ -189,7 +188,7 @@ public class CommunityArticleController extends BaseController {
             if (isSensitiveContent) {
                 return AjaxResult.error(MessageUtils.message("article.content.error"));
             }
-            
+
             communityArticleService.insertCommunityArticle(communityArticle);
         } catch (Exception e) {
             System.out.println(e.getMessage());
@@ -1335,4 +1334,74 @@ public class CommunityArticleController extends BaseController {
         return AjaxResult.success(communityArticleAt);
     }
 
+    /**
+     * 插入at信息
+     */
+    @ApiOperation("置顶文章")
+    @PostMapping("/articleTop")
+    @Anonymous
+    public AjaxResult articleTop(@RequestBody CommunityArticle communityArticle) {
+        if (Objects.isNull(communityArticle.getId())) {
+            return AjaxResult.error("文章ID不能为空");
+        }
+        //先拿前端传的ID
+        Long userId = communityArticle.getUserId();
+        if (Objects.isNull(communityArticle.getUserId())) {
+            userId = SecurityUtils.getUserId();
+        }
+        String msg = "";
+        try {
+            CommunityArticle toparticle = communityArticleMapper.selectOne(new QueryWrapper<CommunityArticle>()
+                    .eq("user_id", userId)
+                    .eq("id", communityArticle.getId())
+                    .and((wrapper) -> {
+                        wrapper.ne("is_delete", 1).or().isNull("is_delete");
+                    }));
+            //如果没有找到数据置顶该文章 说明不是当前用户
+            if (Objects.isNull(toparticle) || Objects.isNull(toparticle.getId())) {
+                return AjaxResult.success("文章不属于当前用户,无法置顶");
+            }
+            Boolean isTop = toparticle.getIsTop();
+            if (!isTop){
+                communityArticleService.update(new UpdateWrapper<CommunityArticle>()
+                        .set("is_top", true)
+                        .set("top_time", DateUtils.parseDate(DateUtils.getTime()))
+                        .eq("id", communityArticle.getId()));
+                msg = "已置顶";
+            }else {
+                communityArticleService.update(new UpdateWrapper<CommunityArticle>()
+                        .set("is_top", false)
+                        .set("top_time", DateUtils.parseDate(DateUtils.getTime()))
+                        .eq("id", communityArticle.getId()));
+                msg = "已取消置顶";
+            }
+            //每个人只能有三条置顶数据,如果有第四条或者第五条数据,则按时间排序找到离当前时间最近的第三条数据更新掉小于当前时间的所有数据为false
+            List<CommunityArticle> communityArticles = communityArticleMapper.selectList(new QueryWrapper<CommunityArticle>()
+                    .eq("user_id", userId)
+                    .eq("is_top", 1)
+                    .and((wrapper) -> {
+                        wrapper.ne("is_delete", 1).or().isNull("is_delete");
+                    }));
+            if (communityArticles.size() > 3) {
+               //大于三条则 排序
+                communityArticles = communityArticles.stream()
+                        .sorted(Comparator.comparing(CommunityArticle::getTopTime).reversed()) // 降序排列(最新文章排在前面)
+                        .limit(3)
+                        .collect(Collectors.toList());
+
+                //最早文章的 is_top 更新为 false
+                UpdateWrapper<CommunityArticle> updateWrapper = new UpdateWrapper<>();
+                updateWrapper.eq("user_id", userId)
+                        .eq("is_top", 1)
+                        .notIn("id", communityArticles.stream().map(CommunityArticle::getId).collect(Collectors.toList()))
+                        .set("is_top", false); // 将 is_top 设置为 false
+                communityArticleMapper.update(null, updateWrapper);
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return AjaxResult.success(msg);
+    }
+
 }

+ 13 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityArticle.java

@@ -210,4 +210,17 @@ public class CommunityArticle implements Serializable {
      */
     @ApiModelProperty("文章类型")
     private Integer articleType;
+
+    /**
+     * 置顶标识
+     */
+    @ApiModelProperty("文章类型")
+    private Boolean isTop;
+
+
+    /**
+     * 置顶时间
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date topTime;
 }