diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleTagDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleTagDTO.java index 7eb15f7..ed69468 100644 --- a/src/main/java/com/rymcu/vertical/dto/ArticleTagDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/ArticleTagDTO.java @@ -7,8 +7,13 @@ import lombok.Data; */ @Data public class ArticleTagDTO { + + private Integer idArticleTag; + private Integer idTag; + private Integer idArticle; + private String tagTitle; private String tagUri; diff --git a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java index 05bc666..d7408b6 100644 --- a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java @@ -111,4 +111,11 @@ public interface ArticleMapper extends Mapper
{ * @return */ List selectDrafts(@Param("idUser") Integer idUser); + + /** + * 删除未使用的文章标签 + * @param idArticleTag + * @return + */ + Integer deleteUnusedArticleTag(@Param("idArticleTag") Integer idArticleTag); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java index 256e56a..1b658cb 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java @@ -41,13 +41,13 @@ public class TagServiceImpl extends AbstractService implements TagService { String articleTags = article.getArticleTags(); if(StringUtils.isNotBlank(articleTags)){ String[] tags = articleTags.split(","); + List articleTagDTOList = articleMapper.selectTags(article.getIdArticle()); for (int i = 0; i < tags.length; i++) { boolean addTagArticle = false; boolean addUserTag = false; Tag tag = new Tag(); tag.setTagTitle(tags[i]); tag = tagMapper.selectOne(tag); - List articleTagDTOList = articleMapper.selectTags(article.getIdArticle()); if(tag == null){ tag = new Tag(); tag.setTagTitle(tags[i]); @@ -59,6 +59,11 @@ public class TagServiceImpl extends AbstractService implements TagService { addTagArticle = true; addUserTag = true; } else { + for(ArticleTagDTO articleTag : articleTagDTOList) { + if (articleTag.getIdTag().equals(tag.getIdTag())) { + articleTagDTOList.remove(articleTag); + } + } Integer count = tagMapper.selectCountTagArticleById(tag.getIdTag(),article.getIdArticle()); if(count == 0){ tag.setTagArticleCount(tag.getTagArticleCount() + 1); @@ -70,6 +75,9 @@ public class TagServiceImpl extends AbstractService implements TagService { addUserTag = true; } } + articleTagDTOList.forEach(articleTagDTO -> { + articleMapper.deleteUnusedArticleTag(articleTagDTO.getIdArticleTag()); + }); if(addTagArticle){ tagMapper.insertTagArticle(tag.getIdTag(),article.getIdArticle()); } diff --git a/src/main/java/mapper/ArticleMapper.xml b/src/main/java/mapper/ArticleMapper.xml index 49335d9..d2426a5 100644 --- a/src/main/java/mapper/ArticleMapper.xml +++ b/src/main/java/mapper/ArticleMapper.xml @@ -51,11 +51,13 @@ - - - - - + + + + + + + insert into vertical_article_content (id_article,article_content,article_content_html,created_time,updated_time) @@ -70,6 +72,9 @@ delete from vertical_tag_article where id_article = #{id} + + delete from vertical_tag_article where id = #{idArticleTag} + @@ -95,7 +100,7 @@ select art.*,su.nickname,su.avatar_url from vertical_article art left join vertical_user su on su.id = #{idUser} and art.article_author_id = su.id where article_author_id = #{idUser} order by updated_time desc