From 5d505eec6a0d16ca1ee43f575f5b44782c3afc7c Mon Sep 17 00:00:00 2001 From: ronger Date: Wed, 26 Jul 2023 11:10:05 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=A2=9E=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E4=B8=8B=E6=9E=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forest/dto/ArticleUpdateStatusDTO.java | 21 ++++++++++++++++++ .../rymcu/forest/handler/ArticleHandler.java | 8 +++++++ .../handler/event/ArticleStatusEvent.java | 22 +++++++++++++++++++ .../rymcu/forest/mapper/ArticleMapper.java | 2 ++ .../rymcu/forest/service/ArticleService.java | 10 +++++++++ .../service/impl/ArticleServiceImpl.java | 22 +++++++++++++++++++ .../rymcu/forest/util/NotificationUtils.java | 2 +- .../web/api/admin/AdminArticleController.java | 9 ++++++++ src/main/java/mapper/ArticleMapper.xml | 3 +++ 9 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/rymcu/forest/dto/ArticleUpdateStatusDTO.java create mode 100644 src/main/java/com/rymcu/forest/handler/event/ArticleStatusEvent.java diff --git a/src/main/java/com/rymcu/forest/dto/ArticleUpdateStatusDTO.java b/src/main/java/com/rymcu/forest/dto/ArticleUpdateStatusDTO.java new file mode 100644 index 0000000..4c39b5e --- /dev/null +++ b/src/main/java/com/rymcu/forest/dto/ArticleUpdateStatusDTO.java @@ -0,0 +1,21 @@ +package com.rymcu.forest.dto; + +import lombok.Data; + +/** + * Created on 2023/7/26 10:20. + * + * @author ronger + * @email ronger-x@outlook.com + * @desc : com.rymcu.forest.dto + */ +@Data +public class ArticleUpdateStatusDTO { + + private Long idArticle; + + private String articleStatus; + + private String remarks; + +} diff --git a/src/main/java/com/rymcu/forest/handler/ArticleHandler.java b/src/main/java/com/rymcu/forest/handler/ArticleHandler.java index b54f61f..446ffc5 100644 --- a/src/main/java/com/rymcu/forest/handler/ArticleHandler.java +++ b/src/main/java/com/rymcu/forest/handler/ArticleHandler.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.rymcu.forest.core.constant.NotificationConstant; import com.rymcu.forest.handler.event.ArticleDeleteEvent; import com.rymcu.forest.handler.event.ArticleEvent; +import com.rymcu.forest.handler.event.ArticleStatusEvent; import com.rymcu.forest.lucene.service.LuceneService; import com.rymcu.forest.util.NotificationUtils; import lombok.extern.slf4j.Slf4j; @@ -11,6 +12,7 @@ import org.springframework.stereotype.Component; import org.springframework.transaction.event.TransactionalEventListener; import javax.annotation.Resource; +import javax.mail.MessagingException; /** * Created on 2022/8/16 20:42. @@ -58,4 +60,10 @@ public class ArticleHandler { luceneService.deleteArticle(articleDeleteEvent.getIdArticle()); log.info("执行完成文章删除相关事件...id={}", articleDeleteEvent.getIdArticle()); } + + @TransactionalEventListener + public void processArticleStatusEvent(ArticleStatusEvent articleStatusEvent) throws MessagingException { + log.info(String.format("执行文章删除相关事件:[%s]", JSON.toJSONString(articleStatusEvent))); + NotificationUtils.saveNotification(articleStatusEvent.getArticleAuthor(), articleStatusEvent.getIdArticle(), NotificationConstant.UpdateArticleStatus, articleStatusEvent.getMessage()); + } } diff --git a/src/main/java/com/rymcu/forest/handler/event/ArticleStatusEvent.java b/src/main/java/com/rymcu/forest/handler/event/ArticleStatusEvent.java new file mode 100644 index 0000000..203cbc5 --- /dev/null +++ b/src/main/java/com/rymcu/forest/handler/event/ArticleStatusEvent.java @@ -0,0 +1,22 @@ +package com.rymcu.forest.handler.event; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * Created on 2022/8/20 18:51. + * + * @author ronger + * @email ronger-x@outlook.com + */ +@Data +@AllArgsConstructor +public class ArticleStatusEvent { + + private Long idArticle; + + private Long articleAuthor; + + private String message; + +} diff --git a/src/main/java/com/rymcu/forest/mapper/ArticleMapper.java b/src/main/java/com/rymcu/forest/mapper/ArticleMapper.java index 6ec1905..f23aeab 100644 --- a/src/main/java/com/rymcu/forest/mapper/ArticleMapper.java +++ b/src/main/java/com/rymcu/forest/mapper/ArticleMapper.java @@ -220,4 +220,6 @@ public interface ArticleMapper extends Mapper
{ * @return */ List selectAnnouncements(); + + int updateStatus(@Param("idArticle") Long idArticle, @Param("articleStatus") String articleStatus); } diff --git a/src/main/java/com/rymcu/forest/service/ArticleService.java b/src/main/java/com/rymcu/forest/service/ArticleService.java index 1e1ad57..0d089f2 100644 --- a/src/main/java/com/rymcu/forest/service/ArticleService.java +++ b/src/main/java/com/rymcu/forest/service/ArticleService.java @@ -140,4 +140,14 @@ public interface ArticleService extends Service
{ * @return */ List findAnnouncements(); + + /** + * 变更文章状态 + * + * @param idArticle + * @param articleStatus + * @param remarks + * @return + */ + Boolean updateStatus(Long idArticle, String articleStatus, String remarks); } diff --git a/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java index 2e62f0f..afe2e32 100644 --- a/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java @@ -12,6 +12,7 @@ import com.rymcu.forest.entity.Tag; import com.rymcu.forest.entity.User; import com.rymcu.forest.handler.event.ArticleDeleteEvent; import com.rymcu.forest.handler.event.ArticleEvent; +import com.rymcu.forest.handler.event.ArticleStatusEvent; import com.rymcu.forest.mapper.ArticleMapper; import com.rymcu.forest.service.*; import com.rymcu.forest.util.Html2TextUtil; @@ -60,6 +61,9 @@ public class ArticleServiceImpl extends AbstractService
implements Arti private static final String DEFAULT_TOPIC_URI = "news"; private static final int ADMIN_ROLE_WEIGHTS = 2; + @Resource + private ApplicationEventPublisher applicationEventPublisher; + @Override public List findArticles(ArticleSearchDTO searchDTO) { List list; @@ -250,6 +254,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } @Override + @Transactional(rollbackFor = Exception.class) public Boolean updatePerfect(Long idArticle, String articlePerfect) { if (articleMapper.updatePerfect(idArticle, articlePerfect) == 0) { throw new ContentNotExistException("设置优选文章失败!"); @@ -264,6 +269,23 @@ public class ArticleServiceImpl extends AbstractService
implements Arti return list; } + @Override + @Transactional(rollbackFor = Exception.class) + public Boolean updateStatus(Long idArticle, String articleStatus, String remarks) { + if (articleMapper.updateStatus(idArticle, articleStatus) == 0) { + throw new ContentNotExistException("设置文章状态失败!"); + } + Article article = articleMapper.selectByPrimaryKey(idArticle); + String message = "你的文章《"+article.getArticleTitle()+"》"; + if ("1".equals(articleStatus)) { + message += "已下架, 下架原因: " + remarks; + } else { + message += "已上架!"; + } + applicationEventPublisher.publishEvent(new ArticleStatusEvent(idArticle, article.getArticleAuthorId(), message)); + return true; + } + private ArticleDTO genArticle(ArticleDTO article, Integer type) { Integer articleList = 0; Integer articleView = 1; diff --git a/src/main/java/com/rymcu/forest/util/NotificationUtils.java b/src/main/java/com/rymcu/forest/util/NotificationUtils.java index b137188..9a2529f 100644 --- a/src/main/java/com/rymcu/forest/util/NotificationUtils.java +++ b/src/main/java/com/rymcu/forest/util/NotificationUtils.java @@ -41,7 +41,7 @@ public class NotificationUtils { public static void saveNotification(Long idUser, Long dataId, String dataType, String dataSummary) throws MessagingException { Notification notification = notificationService.findNotification(idUser, dataId, dataType); - if (notification == null || NotificationConstant.UpdateArticle.equals(dataType)) { + if (notification == null || NotificationConstant.UpdateArticle.equals(dataType) || NotificationConstant.UpdateArticleStatus.equals(dataType)) { System.out.println("------------------- 开始执行消息通知 ------------------"); Integer result = notificationService.save(idUser, dataId, dataType, dataSummary); if (result == 0) { diff --git a/src/main/java/com/rymcu/forest/web/api/admin/AdminArticleController.java b/src/main/java/com/rymcu/forest/web/api/admin/AdminArticleController.java index c94b02b..e56de0b 100644 --- a/src/main/java/com/rymcu/forest/web/api/admin/AdminArticleController.java +++ b/src/main/java/com/rymcu/forest/web/api/admin/AdminArticleController.java @@ -2,6 +2,7 @@ package com.rymcu.forest.web.api.admin; import com.rymcu.forest.core.result.GlobalResult; import com.rymcu.forest.core.result.GlobalResultGenerator; +import com.rymcu.forest.dto.ArticleUpdateStatusDTO; import com.rymcu.forest.entity.Article; import com.rymcu.forest.service.ArticleService; import org.springframework.web.bind.annotation.PatchMapping; @@ -30,4 +31,12 @@ public class AdminArticleController { String articlePerfect = article.getArticlePerfect(); return GlobalResultGenerator.genSuccessResult(articleService.updatePerfect(idArticle, articlePerfect)); } + + @PatchMapping("/update-status") + public GlobalResult updateArticleStatus(@RequestBody ArticleUpdateStatusDTO article) { + Long idArticle = article.getIdArticle();; + String articleStatus = article.getArticleStatus(); + String remarks = article.getRemarks(); + return GlobalResultGenerator.genSuccessResult(articleService.updateStatus(idArticle, articleStatus, remarks)); + } } diff --git a/src/main/java/mapper/ArticleMapper.xml b/src/main/java/mapper/ArticleMapper.xml index 378fd90..673f944 100644 --- a/src/main/java/mapper/ArticleMapper.xml +++ b/src/main/java/mapper/ArticleMapper.xml @@ -88,6 +88,9 @@ update forest_article set article_perfect = #{articlePerfect} where id = #{idArticle} + + update forest_article set article_status = #{articleStatus} where id = #{idArticle} + delete from forest_tag_article where id_article = #{id}