Browse Source

新增编辑文章接口,获取文章列表接口优化

fangqing 5 months ago
parent
commit
fa597923d1

+ 36 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityArticleController.java

@@ -163,6 +163,42 @@ public class CommunityArticleController extends BaseController {
         return AjaxResult.success("文章发布成功!");
     }
 
+    /**
+     * 编辑文章
+     */
+    @ApiOperation("编辑文章")
+    @PostMapping("/editArticle")
+    @Transactional
+    //@Anonymous
+    public AjaxResult editArticle(@RequestBody CommunityArticle communityArticle) {
+        try {
+            if (communityArticle.getClassIds().isEmpty()) {
+                return AjaxResult.error("板块不能为空");
+            }
+            if (communityArticle.getId() == null) {
+                return AjaxResult.error("文章ID不能为空");
+            }
+            String title = communityArticle.getTitle();
+            String content = communityArticle.getContent();
+            boolean isSensitiveTitle = SensitiveWordUtil.containsSensitiveWord(title);
+            if (isSensitiveTitle) {
+                return AjaxResult.error(MessageUtils.message("article.title.error"));
+            }
+            boolean isSensitiveContent = SensitiveWordUtil.containsSensitiveWord(content);
+            if (isSensitiveContent) {
+                return AjaxResult.error(MessageUtils.message("article.content.error"));
+            }
+
+            communityArticleService.editCommunityArticle(communityArticle);
+
+        } catch (Exception e) {
+           // e.printStackTrace();
+            System.out.println(e.getMessage());
+            throw new ProjectException();
+        }
+        return AjaxResult.success("文章发布成功!");
+    }
+
     /**
      * 发布文章
      */

+ 7 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityArticleClass.java

@@ -51,4 +51,11 @@ public class CommunityArticleClass implements Serializable {
     @ApiModelProperty("创建时间")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date createTime;
+
+    /**
+     * 是否已删除
+     */
+    @ApiModelProperty("是否已删除")
+    private Boolean isDelete;
+
 }

+ 7 - 1
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityArticleImages.java

@@ -61,4 +61,10 @@ public class CommunityArticleImages implements Serializable {
     /** 更新时间 */
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date updateTime;
-}
+
+    /**
+     * 是否已删除
+     */
+    @ApiModelProperty("是否已删除")
+    private Boolean isDelete;
+}

+ 6 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityArticleTag.java

@@ -62,4 +62,10 @@ public class CommunityArticleTag implements Serializable {
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date updateTime;
 
+    /**
+     * 是否已删除
+     */
+    @ApiModelProperty("是否已删除")
+    private Boolean isDelete;
+
 }

+ 170 - 3
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityArticleServiceImpl.java

@@ -14,6 +14,7 @@ import com.ruoyi.common.utils.ip.AddressUtils;
 import com.ruoyi.generator.domain.Community.*;
 import com.ruoyi.generator.mapper.community.*;
 import com.ruoyi.generator.vo.*;
+import com.ruoyi.system.domain.CommunityChatMsg;
 import com.ruoyi.system.mapper.SysUserMapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -225,12 +226,14 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
             List<CommunityArticleClass> articleId = communityArticleCircleMapper.selectList(
                     new QueryWrapper<CommunityArticleClass>()
                             .eq("article_id", articleVo.getId())
+                            .and((wrapper) -> {
+                                wrapper.ne("is_delete", true).or().isNull("is_delete");
+                            })
             );
             // 使用 Java Streams API 提取 classId,并转换成 List<Long>
             List<Long> classIdss = articleId.stream()
                     .map(CommunityArticleClass::getClassId)
                     .collect(Collectors.toList());
-
             articleVo.setClassIds(classIdss);
 
 
@@ -317,7 +320,11 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
             articleVo.setRecommend(!Objects.isNull(recommend));
 
             //获取文章标签
-            List<CommunityArticleTag> articleTags = communityArticleTagMapper.selectList(new QueryWrapper<CommunityArticleTag>().eq("article_id", articleVo.getId()));
+            List<CommunityArticleTag> articleTags = communityArticleTagMapper.selectList(new QueryWrapper<CommunityArticleTag>()
+                    .eq("article_id", articleVo.getId())
+                    .and((wrapper) -> {
+                        wrapper.ne("is_delete", true).or().isNull("is_delete");
+                    }));
             List<Long> tagList = articleTags.stream().map(CommunityArticleTag::getTagId).collect(Collectors.toList());
             List<Long> tagIds = new ArrayList<>();
 
@@ -374,7 +381,7 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
             communityArticle.setId(null);
         }
 
-        System.out.println(communityArticle);
+
         Long userId = SecurityUtils.getLoginUser().getUserId();
         //插入文章信息
         communityArticle.setUserId(userId);
@@ -470,6 +477,166 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
     }
 
 
+    /**
+     * 编辑文章
+     *
+     * @param communityArticle 文章信息
+     */
+    @Override
+    public void editCommunityArticle(CommunityArticle communityArticle) {
+
+        Long userId = SecurityUtils.getLoginUser().getUserId();
+        //编辑文章信息
+        communityArticle.setUpdateBy(userId);
+        communityArticle.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
+
+
+        String address = null;
+        try {
+            address = AddressUtils.getAddress(null);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+        communityArticle.setAddress(address);
+
+        communityArticleMapper.updateById(communityArticle);
+
+
+        //插入标签日志
+        List<String> tags = communityArticle.getTags();
+
+        //不在本次更新的标签都传失效
+        communityArticleTagMapper.update(
+                null,
+                new UpdateWrapper<CommunityArticleTag>()
+                        .eq("article_id", communityArticle.getId())
+                        .notIn("tag_id", tags)
+                        .set("is_delete", true)
+                        .and((wrapper) -> {
+                            wrapper.ne("is_delete", 1).or().isNull("is_delete");
+                        })
+        );
+
+        if (tags != null && !tags.isEmpty()) {
+            CommunityArticleTag tagLog = null;
+            for (String tag : tags) {
+
+                //查询该标签是否在数据库中存在
+                CommunityArticleTag communityArticleTag = communityArticleTagMapper.selectOne((new QueryWrapper<CommunityArticleTag>()
+                        .eq("article_id", communityArticle.getId())
+                        .eq("tag_id", tag)
+                        .and((wrapper) -> {
+                            wrapper.ne("is_delete", true).or().isNull("is_delete");
+                        })));
+                //不为空就不操作
+                if (communityArticleTag  == null ){
+                    tagLog = new CommunityArticleTag();
+                    tagLog.setArticleId(communityArticle.getId());
+                    tagLog.setTagId(Long.parseLong(tag));
+                    tagLog.setUpdateBy(userId);
+                    tagLog.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
+                    tagLog.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
+                    tagLog.setCreateBy(userId);
+                    articleTagMapper.insert(tagLog);
+
+                    //标签热度自增
+                    CommunityTag communityTag = communityTagMapper.selectById(Long.parseLong(tag));
+                    communityTag.setTagHot(Objects.isNull(communityTag.getTagHot()) ? 1 : communityTag.getTagHot() + 1);
+                    communityTag.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
+                    communityTag.setUpdateBy(userId);
+                    communityTagMapper.updateById(communityTag);
+                }
+            }
+        }
+
+        //插入图片日志
+        List<String> images = communityArticle.getImages();
+
+        //插入图片日志 都失效
+        communityArticleImagesMapper.update(
+                null,
+                new UpdateWrapper<CommunityArticleImages>()
+                        .eq("article_id", communityArticle.getId())
+                        .set("is_delete", true)
+                        .and((wrapper) -> {
+                            wrapper.ne("is_delete", 1).or().isNull("is_delete");
+                        })
+        );
+
+        if (images != null && !images.isEmpty()) {
+            CommunityArticleImages articleImages = null;
+            for (String image : images) {
+                articleImages = new CommunityArticleImages();
+                articleImages.setArticleId(communityArticle.getId());
+                articleImages.setImageUrl(image);
+                articleImages.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
+                articleImages.setCreateBy(userId);
+                communityArticleImagesMapper.insert(articleImages);
+            }
+        }
+
+        //插入板块
+        List<Long> classIds = communityArticle.getClassIds();
+
+        //插入板块 都失效
+        communityArticleClassService.update(
+                null,
+                new UpdateWrapper<CommunityArticleClass>()
+                        .eq("article_id", communityArticle.getId())
+                        .set("is_delete", true)
+                        .and((wrapper) -> {
+                            wrapper.ne("is_delete", 1).or().isNull("is_delete");
+                        })
+        );
+
+        if (classIds != null && !classIds.isEmpty()) {
+            List<CommunityArticleClass> articleClasses = new ArrayList<>();
+            CommunityArticleClass articleClass = null;
+            for (Long classId : classIds) {
+                articleClass = new CommunityArticleClass();
+                articleClass.setClassId(classId);
+                articleClass.setArticleId(communityArticle.getId());
+                articleClass.setCreateBy(userId);
+                articleClass.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
+                articleClasses.add(articleClass);
+            }
+            communityArticleClassService.saveBatch(articleClasses);
+        }
+
+        //插入合集
+        List<String> collectionIds = communityArticle.getCollectionIds();
+
+
+        if (collectionIds != null && !collectionIds.isEmpty()) {
+            //插入合集 都失效
+            collectionArticleService.update(
+                    null,
+                    new UpdateWrapper<CommunityCollectionArticle>()
+                            .eq("article_id", communityArticle.getId())
+                            .in("collection_id",collectionIds)
+                            .set("is_delete", true)
+                            .and((wrapper) -> {
+                                wrapper.ne("is_delete", 1).or().isNull("is_delete");
+                            })
+            );
+
+            List<CommunityCollectionArticle> collectionArticles = new ArrayList<>();
+            CommunityCollectionArticle collectionArticle = null;
+            for (String collectionId : collectionIds) {
+                collectionArticle = new CommunityCollectionArticle();
+                collectionArticle.setCollectionId(Long.parseLong(collectionId));
+                collectionArticle.setArticleId(communityArticle.getId());
+                collectionArticle.setCreateBy(userId);
+                collectionArticle.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
+                collectionArticles.add(collectionArticle);
+            }
+            collectionArticleService.saveBatch(collectionArticles);
+        }
+
+    }
+
     /**
      * 发送评论
      *

+ 7 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/ICommunityArticleService.java

@@ -30,6 +30,13 @@ public interface ICommunityArticleService extends IService<CommunityArticle> {
      */
     void insertCommunityArticle(CommunityArticle communityArticle);
 
+    /**
+     * 编辑文章
+     *
+     * @param communityArticle 文章信息
+     */
+    void editCommunityArticle(CommunityArticle communityArticle);
+
     /**
      * 发送评论
      *

+ 4 - 1
ruoyi-generator/src/main/resources/mapper/community/ArticleMapper.xml

@@ -137,6 +137,7 @@
         community_article_images b
         WHERE
         b.article_id = a.id
+        and (b.is_delete != 1 or b.is_delete is null)
         ), '[]') AS images,
         c.user_name as user_name,
         c.nick_name as nick_name,
@@ -208,7 +209,8 @@
             <if test="communityArticle.isDraft != null and communityArticle.isDraft != '' ">
                 AND IFNULL(a.is_Draft,0) = #{communityArticle.isDraft}
             </if>
-            and (is_delete != 1 or is_delete is null)
+            and (a.is_delete != 1 or a.is_delete is null)
+            and (f.is_delete != 1 or f.is_delete is null)
             and a.user_id not in (select distinct peer_id from community_user_block where user_id = #{loginId} and is_block = 1 )
         </where>
         group by a.id,a.create_time
@@ -254,6 +256,7 @@
                 </foreach>
             </if>
             AND (a.is_delete != 1 OR a.is_delete IS NULL)
+            AND (f.is_delete != 1 OR f.is_delete is null)
         </where>
     </select>