diff --git a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java index b9e6863..81264f5 100644 --- a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java @@ -144,4 +144,18 @@ public interface ArticleMapper extends Mapper
{ * @return */ Integer updateArticleTags(@Param("idArticle") Integer idArticle, @Param("tags") String tags); + + /** + * 判断是否有评论 + * @param id + * @return + */ + boolean existsCommentWithPrimaryKey(@Param("id") Integer id); + + /** + * 删除关联作品集数据 + * @param id + * @return + */ + Integer deleteLinkedPortfolioData(@Param("id") Integer id); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index d66f258..dd620cc 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -229,21 +229,29 @@ public class ArticleServiceImpl extends AbstractService
implements Arti public Map delete(Integer id) { Map map = new HashMap(1); Integer result; - Article article = articleMapper.selectByPrimaryKey(id); - // 删除引用标签记录 - result = articleMapper.deleteTagArticle(id); - // 无标签情况下无法删除文章问题修复 - if (result > 0 || StringUtils.isBlank(article.getArticleTags())) { + // 判断是否有评论 + boolean isHavComment = articleMapper.existsCommentWithPrimaryKey(id); + if (isHavComment) { + map.put("message", "已有评论的文章不允许删除!"); + } else { + // 删除关联数据(作品集关联关系,标签关联关系) + deleteLinkedData(id); + // 删除文章 result = articleMapper.deleteByPrimaryKey(id); if (result < 1) { map.put("message", "删除失败!"); } - } else { - map.put("message", "删除失败!"); } return map; } + private void deleteLinkedData(Integer id) { + // 删除关联作品集 + articleMapper.deleteLinkedPortfolioData(id); + // 删除引用标签记录 + articleMapper.deleteTagArticle(id); + } + @Override @Transactional(rollbackFor = Exception.class) public void incrementArticleViewCount(Integer id) { diff --git a/src/main/java/mapper/ArticleMapper.xml b/src/main/java/mapper/ArticleMapper.xml index a66e180..971037e 100644 --- a/src/main/java/mapper/ArticleMapper.xml +++ b/src/main/java/mapper/ArticleMapper.xml @@ -80,12 +80,15 @@ delete from vertical_tag_article where id = #{idArticleTag} + + delete from vertical_portfolio_article where id_vertical_article = #{id} + @@ -128,4 +131,7 @@ + \ No newline at end of file