|
@@ -28,9 +28,8 @@ import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.text.ParseException;
|
|
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) {
|
|
if (isSensitiveContent) {
|
|
return AjaxResult.error(MessageUtils.message("article.content.error"));
|
|
return AjaxResult.error(MessageUtils.message("article.content.error"));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
communityArticleService.insertCommunityArticle(communityArticle);
|
|
communityArticleService.insertCommunityArticle(communityArticle);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
System.out.println(e.getMessage());
|
|
System.out.println(e.getMessage());
|
|
@@ -1335,4 +1334,74 @@ public class CommunityArticleController extends BaseController {
|
|
return AjaxResult.success(communityArticleAt);
|
|
return AjaxResult.success(communityArticleAt);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 置顶文章
|
|
|
|
+ */
|
|
|
|
+ @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);
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|