From 15eca0d0b5f1101c665a2a2085a54615ec831f97 Mon Sep 17 00:00:00 2001 From: qingfeng1121 Date: Thu, 18 Dec 2025 15:19:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(controller/service/repository):=20?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=88=86=E7=B1=BB=E5=B1=9E=E6=80=A7=E3=80=81?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=92=8C=E6=B6=88=E6=81=AF=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构分类属性相关类名从Category_attribute改为Categoryattribute 优化文章和消息的分页查询功能,新增分页DTO类 移除旧的分页DTO类PageDto,新增ArriclePageDto和MessagePageDto 调整消息统计逻辑,区分文章评论和独立消息 更新安全配置,开放消息新增接口权限 --- .../myafterprojecy/config/SecurityConfig.java | 2 + .../controller/ArticleController.java | 17 +++-- .../CategoryAttributeController.java | 12 ++-- .../controller/MessageController.java | 29 +++++--- .../controller/NonsenseController.java | 1 - .../com/qf/myafterprojecy/pojo/Category.java | 12 ++-- ..._attribute.java => Categoryattribute.java} | 2 +- .../pojo/dto/ArriclePageDto.java | 55 ++++++++++++++ .../myafterprojecy/pojo/dto/CategoryDto.java | 10 +-- .../pojo/dto/MessagePageDto.java | 34 +++++++++ .../qf/myafterprojecy/pojo/dto/PageDto.java | 45 ------------ .../repository/ArticleRepository.java | 36 +++++++++- .../CategoryAttributeRepository.java | 14 ++-- .../repository/MessageRepository.java | 37 ++++++++-- .../service/IArticleService.java | 10 ++- .../service/ICategoryAttributeService.java | 12 ++-- .../service/IMessageService.java | 23 +++--- .../service/impl/ArticleService.java | 71 +++++++++++++++---- .../impl/CategoryAttributeService.java | 26 +++---- .../service/impl/MessageService.java | 56 ++++++++++++--- 20 files changed, 360 insertions(+), 144 deletions(-) rename src/main/java/com/qf/myafterprojecy/pojo/{Category_attribute.java => Categoryattribute.java} (96%) create mode 100644 src/main/java/com/qf/myafterprojecy/pojo/dto/ArriclePageDto.java create mode 100644 src/main/java/com/qf/myafterprojecy/pojo/dto/MessagePageDto.java delete mode 100644 src/main/java/com/qf/myafterprojecy/pojo/dto/PageDto.java diff --git a/src/main/java/com/qf/myafterprojecy/config/SecurityConfig.java b/src/main/java/com/qf/myafterprojecy/config/SecurityConfig.java index 771dcc9..6a7fecd 100644 --- a/src/main/java/com/qf/myafterprojecy/config/SecurityConfig.java +++ b/src/main/java/com/qf/myafterprojecy/config/SecurityConfig.java @@ -67,6 +67,8 @@ public class SecurityConfig { .antMatchers(HttpMethod.POST,"/api/articles/view/**").permitAll() // 所有GET请求公开 .antMatchers(HttpMethod.GET,"/api/**").permitAll() + // 公开评论新增接口 + .antMatchers(HttpMethod.POST,"/api/messages").permitAll() // 新增、删除、修改操作需要管理员权限 .antMatchers(HttpMethod.POST,"/api/**").hasRole("ADMIN") .antMatchers(HttpMethod.PUT,"/api/**").hasRole("ADMIN") diff --git a/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java b/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java index a73e245..49c9d07 100644 --- a/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java +++ b/src/main/java/com/qf/myafterprojecy/controller/ArticleController.java @@ -3,7 +3,7 @@ package com.qf.myafterprojecy.controller; import com.qf.myafterprojecy.exceptopn.ResponseMessage; import com.qf.myafterprojecy.pojo.Article; import com.qf.myafterprojecy.pojo.dto.ArticleDto; -import com.qf.myafterprojecy.pojo.dto.PageDto; +import com.qf.myafterprojecy.pojo.dto.ArriclePageDto; import com.qf.myafterprojecy.service.IArticleService; import org.springframework.data.domain.Page; @@ -66,9 +66,8 @@ public class ArticleController { // api/articles/status/page?status=1&page=1&size=2 // get 只能这样不能传递json @GetMapping("/status/page") - public ResponseMessage> getArticlesByStatusWithPagination(PageDto pageDto) { - System.out.println(pageDto); - return articleService.getArticlesByStatusWithPagination(pageDto.getStatus(), pageDto.getPage(), pageDto.getSize()); + public ResponseMessage> getArticlesByStatusWithPagination(ArriclePageDto pageDto) { + return articleService.getArticlesByStatusWithPagination(pageDto); } /** @@ -79,7 +78,15 @@ public class ArticleController { public ResponseMessage> getAllArticles() { return articleService.getAllArticles(); } - + /** + * 获取文章数量 + * @param status 文章状态(0:未发表 1:已发表 2:已删除) + * @return 返回文章数量 + */ + @GetMapping("/count/{status}") + public ResponseMessage getArticleCount(@PathVariable Integer status) { + return articleService.getArticleCount(status); + } /** * 根据标题查询文章列表 * @param title 文章标题 diff --git a/src/main/java/com/qf/myafterprojecy/controller/CategoryAttributeController.java b/src/main/java/com/qf/myafterprojecy/controller/CategoryAttributeController.java index 857a567..b158146 100644 --- a/src/main/java/com/qf/myafterprojecy/controller/CategoryAttributeController.java +++ b/src/main/java/com/qf/myafterprojecy/controller/CategoryAttributeController.java @@ -1,7 +1,7 @@ package com.qf.myafterprojecy.controller; import com.qf.myafterprojecy.exceptopn.ResponseMessage; -import com.qf.myafterprojecy.pojo.Category_attribute; +import com.qf.myafterprojecy.pojo.Categoryattribute; import com.qf.myafterprojecy.pojo.dto.CategoryAttributeDto; import com.qf.myafterprojecy.service.ICategoryAttributeService; @@ -31,12 +31,12 @@ public class CategoryAttributeController { * @return 属性信息 */ @GetMapping("/{id}") - public ResponseMessage getAttributeById(@PathVariable Integer id) { + public ResponseMessage getAttributeById(@PathVariable Integer id) { log.info("接收根据ID获取分类属性的请求: ID={}", id); return categoryAttributeService.getCategoryAttributeById(id); } @GetMapping - public ResponseMessage> getAttributeCount() { + public ResponseMessage> getAttributeCount() { log.info("接收获取分类属性数量的请求"); return categoryAttributeService.getAllCategoryAttributes(); } @@ -47,7 +47,7 @@ public class CategoryAttributeController { * @return 属性列表 */ @GetMapping("/category/{categoryId}") - public ResponseMessage> getAttributesByCategory(@PathVariable Integer categoryId) { + public ResponseMessage> getAttributesByCategory(@PathVariable Integer categoryId) { log.info("接收根据分类ID获取属性列表的请求: 分类ID={}", categoryId); return categoryAttributeService.getAttributesByCategoryId(categoryId); } @@ -59,7 +59,7 @@ public class CategoryAttributeController { */ @PostMapping @PreAuthorize("hasRole('ADMIN')") - public ResponseMessage createAttribute(@Valid @RequestBody CategoryAttributeDto dto) { + public ResponseMessage createAttribute(@Valid @RequestBody CategoryAttributeDto dto) { log.info("接收创建分类属性的请求: 分类ID={}, 属性名称={}", dto.getCategoryid(), dto.getAttributename()); return categoryAttributeService.saveCategoryAttribute(dto); @@ -73,7 +73,7 @@ public class CategoryAttributeController { */ @PutMapping("/{id}") @PreAuthorize("hasRole('ADMIN')") - public ResponseMessage updateAttribute( + public ResponseMessage updateAttribute( @PathVariable Integer id, @Valid @RequestBody CategoryAttributeDto dto) { log.info("接收更新分类属性的请求: ID={}, 分类ID={}, 属性名称={}", diff --git a/src/main/java/com/qf/myafterprojecy/controller/MessageController.java b/src/main/java/com/qf/myafterprojecy/controller/MessageController.java index af4abc3..1054a2b 100644 --- a/src/main/java/com/qf/myafterprojecy/controller/MessageController.java +++ b/src/main/java/com/qf/myafterprojecy/controller/MessageController.java @@ -3,6 +3,7 @@ package com.qf.myafterprojecy.controller; import com.qf.myafterprojecy.exceptopn.ResponseMessage; import com.qf.myafterprojecy.pojo.Message; import com.qf.myafterprojecy.pojo.dto.MessageDto; +import com.qf.myafterprojecy.pojo.dto.MessagePageDto; import com.qf.myafterprojecy.service.IMessageService; import org.slf4j.Logger; @@ -30,7 +31,25 @@ public class MessageController { logger.info("接收获取所有消息的请求"); return messageService.getAllMessages(); } - + /** + * 分页查询消息 + */ + @GetMapping("/page") + public ResponseMessage> getMessagesByPage(MessagePageDto messagePageDto) { + logger.info("接收分页查询消息的请求: {}", messagePageDto); + return messageService.getMessagesByPage(messagePageDto); + } + /** + * 获取消息数量 + * @param articleId 文章ID (可选) + * @return 消息数量 + * 文章ID为null时返回所有消息数量 + */ + @GetMapping("/count") + public ResponseMessage getMessageCount( Integer articleId) { + logger.info("接收获取消息数量的请求: {}", articleId); + return messageService.getMessageCountByArticleId(articleId); + } /** * 根据ID获取消息 */ @@ -76,14 +95,6 @@ public class MessageController { return messageService.searchMessagesByNickname(nickname); } - /** - * 获取指定文章的评论数量 - */ - @GetMapping("/count/article/{articleId}") - public ResponseMessage getMessageCountByArticleId(@PathVariable Integer articleId) { - logger.info("接收获取文章评论数量的请求: {}", articleId); - return messageService.getMessageCountByArticleId(articleId); - } /** * 创建新消息 */ diff --git a/src/main/java/com/qf/myafterprojecy/controller/NonsenseController.java b/src/main/java/com/qf/myafterprojecy/controller/NonsenseController.java index 2d03fb7..26d0ba8 100644 --- a/src/main/java/com/qf/myafterprojecy/controller/NonsenseController.java +++ b/src/main/java/com/qf/myafterprojecy/controller/NonsenseController.java @@ -66,7 +66,6 @@ public class NonsenseController { * @return 创建结果 */ @PostMapping - @PreAuthorize("hasRole('ADMIN')") public ResponseMessage saveNonsense(@Valid @RequestBody NonsenseDto nonsenseDto) { logger.info("请求保存疯言疯语内容"); return nonsenseService.saveNonsense(nonsenseDto); diff --git a/src/main/java/com/qf/myafterprojecy/pojo/Category.java b/src/main/java/com/qf/myafterprojecy/pojo/Category.java index 3f43daa..746d5f5 100644 --- a/src/main/java/com/qf/myafterprojecy/pojo/Category.java +++ b/src/main/java/com/qf/myafterprojecy/pojo/Category.java @@ -9,8 +9,8 @@ import java.time.LocalDateTime; public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "typeid") - private Integer typeid; + @Column(name = "categoryid") + private Integer categoryid; @NotBlank(message = "分类名称不能为空") @Column(name = "typename") @@ -26,12 +26,12 @@ public class Category { private LocalDateTime updatedAt; // Getters and Setters - public Integer getTypeid() { - return typeid; + public Integer getCategoryid() { + return categoryid; } - public void setTypeid(Integer typeid) { - this.typeid = typeid; + public void setCategoryid(Integer categoryid) { + this.categoryid = categoryid; } public String getTypename() { diff --git a/src/main/java/com/qf/myafterprojecy/pojo/Category_attribute.java b/src/main/java/com/qf/myafterprojecy/pojo/Categoryattribute.java similarity index 96% rename from src/main/java/com/qf/myafterprojecy/pojo/Category_attribute.java rename to src/main/java/com/qf/myafterprojecy/pojo/Categoryattribute.java index 6fb3237..d6b3b6a 100644 --- a/src/main/java/com/qf/myafterprojecy/pojo/Category_attribute.java +++ b/src/main/java/com/qf/myafterprojecy/pojo/Categoryattribute.java @@ -4,7 +4,7 @@ import javax.persistence.*; @Entity @Table(name = "category_attribute") -public class Category_attribute { +public class Categoryattribute { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "attributeid") diff --git a/src/main/java/com/qf/myafterprojecy/pojo/dto/ArriclePageDto.java b/src/main/java/com/qf/myafterprojecy/pojo/dto/ArriclePageDto.java new file mode 100644 index 0000000..0cd6bdd --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/pojo/dto/ArriclePageDto.java @@ -0,0 +1,55 @@ +package com.qf.myafterprojecy.pojo.dto; +public class ArriclePageDto { + + private Integer status; + private String title; + private Integer attributeid; + private Integer categoryid; + private Integer pagenum; + private Integer pagesize; + public Integer getStatus() { + return status; + } + public void setStatus(Integer status) { + this.status = status; + } + public Integer getPagenum() { + return pagenum; + } + public void setPagenum(Integer pagenum) { + this.pagenum = pagenum; + } + public Integer getPagesize() { + return pagesize; + } + public void setPagesize(Integer pagesize) { + this.pagesize = pagesize; + } + + public String getTitle() { + return title; + } + public void setTitle(String title) { + this.title = title; + } + public Integer getAttributeid() { + return attributeid; + } + public void setAttributeid(Integer attributeid) { + this.attributeid = attributeid; + } + public Integer getCategoryid() { + return categoryid; + } + public void setCategoryid(Integer categoryid) { + this.categoryid = categoryid; + } + @Override + public String toString() { + return "ArPageDto{" + + "status=" + status + + ", pagenum=" + pagenum + + ", pagesize=" + pagesize + + '}'; + } +} diff --git a/src/main/java/com/qf/myafterprojecy/pojo/dto/CategoryDto.java b/src/main/java/com/qf/myafterprojecy/pojo/dto/CategoryDto.java index 87ed062..0988af8 100644 --- a/src/main/java/com/qf/myafterprojecy/pojo/dto/CategoryDto.java +++ b/src/main/java/com/qf/myafterprojecy/pojo/dto/CategoryDto.java @@ -4,7 +4,7 @@ import javax.validation.constraints.NotBlank; import java.time.LocalDateTime; public class CategoryDto { - private Integer typeid; + private Integer Categoryid; @NotBlank(message = "分类名称不能为空") private String typename; @@ -16,12 +16,12 @@ public class CategoryDto { private LocalDateTime updatedAt; // Getters and Setters - public Integer getTypeid() { - return typeid; + public Integer getCategoryid() { + return Categoryid; } - public void setTypeid(Integer typeid) { - this.typeid = typeid; + public void setCategoryid(Integer Categoryid) { + this.Categoryid = Categoryid; } public String getTypename() { diff --git a/src/main/java/com/qf/myafterprojecy/pojo/dto/MessagePageDto.java b/src/main/java/com/qf/myafterprojecy/pojo/dto/MessagePageDto.java new file mode 100644 index 0000000..7d3c95f --- /dev/null +++ b/src/main/java/com/qf/myafterprojecy/pojo/dto/MessagePageDto.java @@ -0,0 +1,34 @@ +package com.qf.myafterprojecy.pojo.dto; + +public class MessagePageDto { + private Integer pageNum; + private Integer pageSize; + private Integer articleid; + + public Integer getPageNum() { + return pageNum; + } + public void setPageNum(Integer pageNum) { + this.pageNum = pageNum; + } + public Integer getPageSize() { + return pageSize; + } + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } + public Integer getArticleid() { + return articleid; + } + public void setArticleid(Integer articleid) { + this.articleid = articleid; + } + @Override + public String toString() { + return "MessagePageDto{" + + "pageNum=" + pageNum + + ", pageSize=" + pageSize + + ", articleid=" + articleid + + '}'; + } +} diff --git a/src/main/java/com/qf/myafterprojecy/pojo/dto/PageDto.java b/src/main/java/com/qf/myafterprojecy/pojo/dto/PageDto.java deleted file mode 100644 index 5567bf3..0000000 --- a/src/main/java/com/qf/myafterprojecy/pojo/dto/PageDto.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.qf.myafterprojecy.pojo.dto; -public class PageDto { - - private Integer status; - private Integer page; - private Integer size; - // 数据验证 - public void validate() { - if ( status > 0 && status <= 2) { - throw new IllegalArgumentException("Status must be 0 or 2"); - } - if (page < 0) { - throw new IllegalArgumentException("Page number must be non-negative"); - } - if (size <= 0 || size > 100) { - throw new IllegalArgumentException("Page size must be between 1 and 100"); - } - } - public Integer getStatus() { - return status; - } - public void setStatus(Integer status) { - this.status = status; - } - public Integer getPage() { - return page; - } - public void setPage(Integer page) { - this.page = page; - } - public Integer getSize() { - return size; - } - public void setSize(Integer size) { - this.size = size; - } - @Override - public String toString() { - return "PageDto{" + - "status=" + status + - ", page=" + page + - ", size=" + size + - '}'; - } -} diff --git a/src/main/java/com/qf/myafterprojecy/repository/ArticleRepository.java b/src/main/java/com/qf/myafterprojecy/repository/ArticleRepository.java index 882a31f..bb3e075 100644 --- a/src/main/java/com/qf/myafterprojecy/repository/ArticleRepository.java +++ b/src/main/java/com/qf/myafterprojecy/repository/ArticleRepository.java @@ -97,5 +97,39 @@ public interface ArticleRepository extends JpaRepository { * @return 返回符合状态条件的文章分页结果 */ @Query("SELECT a FROM Article a WHERE a.status = :status ORDER BY a.createdAt DESC") - Page
findByStatusWithPagination(@Param("status") Integer status, Pageable pageable); + Page
findByStatusWithPagination(@Param("status") Integer status, Pageable pageable); + /** + * 根据属性ID数组分页查询文章列表 + * @param attributeids 文章属性ID数组 + * @param status 文章状态,0-草稿,1-已发布,2-已删除 + * @param pageable 分页参数,包含页码、每页大小和排序信息 + * @return 返回符合状态条件的文章分页结果 + */ + @Query("SELECT a FROM Article a WHERE a.status = :status AND a.attributeid IN :attributeids ORDER BY a.createdAt DESC") + Page
findByStatusWithPagination(@Param("status") Integer status, @Param("attributeids") List attributeids, Pageable pageable); + /** + * 根据属性ID分页查询文章列表 + * @param attributeid 文章属性ID + * @param status 文章状态,0-草稿,1-已发布,2-已删除 + * @param pageable 分页参数,包含页码、每页大小和排序信息 + * @return 返回符合状态条件的文章分页结果 + */ + @Query("SELECT a FROM Article a WHERE a.status = :status AND a.attributeid = :attributeid ORDER BY a.createdAt DESC") + Page
findByStatusWithPagination(@Param("status") Integer status, @Param("attributeid") Integer attributeid, Pageable pageable); + /** + * 根据文章标题分页模糊查询文章列表 + * @param title 文章标题 + * @param status 文章状态,0-草稿,1-已发布,2-已删除 + * @param pageable 分页参数,包含页码、每页大小和排序信息 + * @return 返回符合状态条件的文章分页结果 + */ + @Query("SELECT a FROM Article a WHERE a.status = :status AND a.title LIKE %:title% ORDER BY a.createdAt DESC") + Page
findByStatusWithPagination(@Param("status") Integer status, @Param("title") String title, Pageable pageable); + /** + * 统计指定状态的文章数量 + * @param status 文章状态,0-草稿,1-已发布,2-已删除 + * @return 返回符合状态条件的文章数量 + */ + @Query("SELECT COUNT(a) FROM Article a WHERE a.status = :status") + Integer countByStatus(@Param("status") Integer status); } diff --git a/src/main/java/com/qf/myafterprojecy/repository/CategoryAttributeRepository.java b/src/main/java/com/qf/myafterprojecy/repository/CategoryAttributeRepository.java index f1a4fec..9d9a476 100644 --- a/src/main/java/com/qf/myafterprojecy/repository/CategoryAttributeRepository.java +++ b/src/main/java/com/qf/myafterprojecy/repository/CategoryAttributeRepository.java @@ -1,6 +1,6 @@ package com.qf.myafterprojecy.repository; -import com.qf.myafterprojecy.pojo.Category_attribute; +import com.qf.myafterprojecy.pojo.Categoryattribute; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -10,23 +10,23 @@ import java.util.List; import java.util.Optional; @Repository -public interface CategoryAttributeRepository extends JpaRepository { +public interface CategoryAttributeRepository extends JpaRepository { /** * 根据分类ID查询分类属性列表 * @param categoryid 分类ID * @return 返回该分类下的所有属性列表 */ - @Query("SELECT ca FROM Category_attribute ca WHERE ca.categoryid = :categoryid") - List findByCategoryId(@Param("categoryid") Integer categoryid); + @Query("SELECT ca FROM Categoryattribute ca WHERE ca.categoryid = :categoryid") + List findByCategoryId(@Param("categoryid") Integer categoryid); /** * 根据属性ID查询属性信息 * @param attributeid 属性ID * @return 返回属性对象 */ - @Query("SELECT ca FROM Category_attribute ca WHERE ca.attributeid = :attributeid") - Optional findByAttributeId(@Param("attributeid") Integer attributeid); + @Query("SELECT ca FROM Categoryattribute ca WHERE ca.attributeid = :attributeid") + Optional findByAttributeId(@Param("attributeid") Integer attributeid); /** * 检查分类下是否存在指定名称的属性 @@ -42,5 +42,5 @@ public interface CategoryAttributeRepository extends JpaRepository findByCategoryidAndAttributename(Integer categoryid, String attributename); + Optional findByCategoryidAndAttributename(Integer categoryid, String attributename); } \ No newline at end of file diff --git a/src/main/java/com/qf/myafterprojecy/repository/MessageRepository.java b/src/main/java/com/qf/myafterprojecy/repository/MessageRepository.java index e63ab16..b9147e1 100644 --- a/src/main/java/com/qf/myafterprojecy/repository/MessageRepository.java +++ b/src/main/java/com/qf/myafterprojecy/repository/MessageRepository.java @@ -1,6 +1,10 @@ package com.qf.myafterprojecy.repository; import com.qf.myafterprojecy.pojo.Message; + +import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -60,10 +64,33 @@ public interface MessageRepository extends JpaRepository { void incrementLikes(@Param("messageId") Integer messageId); // 统计指定文章的评论数量 /** - * 统计指定文章的评论数量 - * @param articleId 文章ID - * @return 评论数量 + * 根据文章ID分页查询消息 + * @param articleid 文章ID + * @param pageable 分页信息 + * @return 分页消息列表 */ - @Query("SELECT COUNT(m) FROM Message m WHERE m.articleid = :articleId") - Long countByArticleId(@Param("articleId") Integer articleId); + @Query("SELECT m FROM Message m WHERE m.articleid = :articleId ORDER BY m.createdAt DESC") + Page findByArticleId(@Param("articleId") Integer articleId, PageRequest pageable); + /** + * 根据页查询消息 + * @param pageable 分页信息 + * @return 分页消息列表 + */ + @Query("SELECT m FROM Message m WHERE m.articleid IS NULL ORDER BY m.createdAt DESC") + Page findAllMessages(PageRequest pageable); + /** + * 统计指定文章下的回复消息数量 + * @param articleId 文章ID + * @return 回复消息数量 + */ + @Query("SELECT COUNT(m) FROM Message m WHERE m.articleid = :articleId AND m.parentid IS NULL") + Integer countReplyByArticleId(@Param("articleId") Integer articleId); + + /** + * 统计指定文章id parentid为空的回复消息数量 + * @param articleId 文章ID + * @return 回复消息数量 + */ + @Query("SELECT COUNT(m) FROM Message m WHERE m.articleid IS NULL AND m.parentid IS NULL") + Integer countReplyByArticleIdIsNull(); } diff --git a/src/main/java/com/qf/myafterprojecy/service/IArticleService.java b/src/main/java/com/qf/myafterprojecy/service/IArticleService.java index 2ca3b66..6fc31e3 100644 --- a/src/main/java/com/qf/myafterprojecy/service/IArticleService.java +++ b/src/main/java/com/qf/myafterprojecy/service/IArticleService.java @@ -2,6 +2,7 @@ package com.qf.myafterprojecy.service; import com.qf.myafterprojecy.exceptopn.ResponseMessage; import com.qf.myafterprojecy.pojo.Article; +import com.qf.myafterprojecy.pojo.dto.ArriclePageDto; import com.qf.myafterprojecy.pojo.dto.ArticleDto; import org.springframework.data.domain.Page; @@ -25,7 +26,12 @@ public interface IArticleService { * @return 返回包含文章列表的ResponseMessage对象 */ ResponseMessage> getArticlesByStatus(Integer status); - + /** + * 获取文章数量 + * @param status 文章状态(0:未发表 1:已发表 2:已删除) + * @return 返回文章数量 + */ + ResponseMessage getArticleCount(Integer status); /** * 创建新文章 * 仅限AUTHOR角色用户访问 @@ -98,5 +104,5 @@ public interface IArticleService { * @param size 每页大小 * @return 返回包含分页文章列表的ResponseMessage对象 */ - ResponseMessage> getArticlesByStatusWithPagination(Integer status, Integer page, Integer size); + ResponseMessage> getArticlesByStatusWithPagination(ArriclePageDto arriclePageDto); } diff --git a/src/main/java/com/qf/myafterprojecy/service/ICategoryAttributeService.java b/src/main/java/com/qf/myafterprojecy/service/ICategoryAttributeService.java index e578895..c8e1660 100644 --- a/src/main/java/com/qf/myafterprojecy/service/ICategoryAttributeService.java +++ b/src/main/java/com/qf/myafterprojecy/service/ICategoryAttributeService.java @@ -1,7 +1,7 @@ package com.qf.myafterprojecy.service; import com.qf.myafterprojecy.exceptopn.ResponseMessage; -import com.qf.myafterprojecy.pojo.Category_attribute; +import com.qf.myafterprojecy.pojo.Categoryattribute; import com.qf.myafterprojecy.pojo.dto.CategoryAttributeDto; import java.util.List; @@ -11,28 +11,28 @@ public interface ICategoryAttributeService { * 获取全部分类属性 * @return 所有分类属性列表 */ - ResponseMessage> getAllCategoryAttributes(); + ResponseMessage> getAllCategoryAttributes(); /** * 根据ID获取分类属性 * @param id 属性ID * @return 分类属性信息 */ - ResponseMessage getCategoryAttributeById(Integer id); + ResponseMessage getCategoryAttributeById(Integer id); /** * 根据分类ID获取属性列表 * @param categoryId 分类ID * @return 属性列表 */ - ResponseMessage> getAttributesByCategoryId(Integer categoryId); + ResponseMessage> getAttributesByCategoryId(Integer categoryId); /** * 创建新的分类属性 * @param dto 分类属性数据 * @return 创建结果 */ - ResponseMessage saveCategoryAttribute(CategoryAttributeDto dto); + ResponseMessage saveCategoryAttribute(CategoryAttributeDto dto); /** * 更新分类属性 @@ -40,7 +40,7 @@ public interface ICategoryAttributeService { * @param dto 分类属性数据 * @return 更新结果 */ - ResponseMessage updateCategoryAttribute(Integer id, CategoryAttributeDto dto); + ResponseMessage updateCategoryAttribute(Integer id, CategoryAttributeDto dto); /** * 删除分类属性 diff --git a/src/main/java/com/qf/myafterprojecy/service/IMessageService.java b/src/main/java/com/qf/myafterprojecy/service/IMessageService.java index 51387bb..a98b9fd 100644 --- a/src/main/java/com/qf/myafterprojecy/service/IMessageService.java +++ b/src/main/java/com/qf/myafterprojecy/service/IMessageService.java @@ -3,6 +3,9 @@ package com.qf.myafterprojecy.service; import com.qf.myafterprojecy.exceptopn.ResponseMessage; import com.qf.myafterprojecy.pojo.Message; import com.qf.myafterprojecy.pojo.dto.MessageDto; +import com.qf.myafterprojecy.pojo.dto.MessagePageDto; + + import java.util.List; @@ -14,7 +17,18 @@ public interface IMessageService { * ResponseMessage是响应消息的包装类,Iterable表示可迭代的消息集合 */ ResponseMessage> getAllMessages(); - + /** + * 分页 + * @param id + * @return + */ + ResponseMessage> getMessagesByPage(MessagePageDto messagePageDto); + /** + * 获取回复消息条数 如果id为空获取文章id为空的消息条数 + * @param articleId 文章id + * @return 回复消息条数 + */ + ResponseMessage getMessageCountByArticleId(Integer articleId); /** * 根据消息ID获取消息的方法 * @@ -82,11 +96,4 @@ public interface IMessageService { // 删除所有评论 ResponseMessage deleteAllMessages(); - /** - * 获取指定文章的评论数量 - * - * @param articleId 文章ID - * @return 评论数量 - */ - ResponseMessage getMessageCountByArticleId(Integer articleId); } diff --git a/src/main/java/com/qf/myafterprojecy/service/impl/ArticleService.java b/src/main/java/com/qf/myafterprojecy/service/impl/ArticleService.java index b18d8cb..38fc500 100644 --- a/src/main/java/com/qf/myafterprojecy/service/impl/ArticleService.java +++ b/src/main/java/com/qf/myafterprojecy/service/impl/ArticleService.java @@ -2,6 +2,8 @@ package com.qf.myafterprojecy.service.impl; import com.qf.myafterprojecy.exceptopn.ResponseMessage; import com.qf.myafterprojecy.pojo.Article; +import com.qf.myafterprojecy.pojo.Categoryattribute; +import com.qf.myafterprojecy.pojo.dto.ArriclePageDto; import com.qf.myafterprojecy.pojo.dto.ArticleDto; import com.qf.myafterprojecy.repository.ArticleRepository; import com.qf.myafterprojecy.repository.CategoryAttributeRepository; @@ -19,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.List; +import java.util.stream.Collectors; @Service public class ArticleService implements IArticleService { @@ -63,6 +66,28 @@ public class ArticleService implements IArticleService { return ResponseMessage.error("获取文章失败"); } } + /** + * 获取文章数量 + * @param status 文章状态(0:未发表 1:已发表 2:已删除) + * @return 返回文章数量 + */ + @Override + @Transactional(readOnly = true) + public ResponseMessage getArticleCount(Integer status) { + try { + if (status == null) { + return ResponseMessage.badRequest("文章状态不能为空"); + } + if (status < 0 || status > 2) { + return ResponseMessage.badRequest("文章状态值必须在0到2之间"); + } + Integer count = articleRepository.countByStatus(status); + return ResponseMessage.success(count, "获取文章数量成功"); + } catch (Exception e) { + log.error("获取文章数量失败: {}", e.getMessage()); + return ResponseMessage.error("获取文章数量失败"); + } + } /** * 根据状态获取文章列表 * @param status 文章状态(0:未发表 1:已发表 2:已删除) @@ -103,23 +128,43 @@ public class ArticleService implements IArticleService { @Override @Transactional(readOnly = true) - public ResponseMessage> getArticlesByStatusWithPagination(Integer status, Integer page, Integer size) { - try { - if (status == null) { - return ResponseMessage.badRequest("文章状态不能为空"); + public ResponseMessage> getArticlesByStatusWithPagination(ArriclePageDto arriclePageDto) { + if (arriclePageDto.getPagenum() == null || arriclePageDto.getPagenum() < 0) { + arriclePageDto.setPagenum(0); // 默认第一页 } - if (status < 0 || status > 2) { + if (arriclePageDto.getPagesize() == null || arriclePageDto.getPagesize() <= 0 || arriclePageDto.getPagesize() > 100) { + arriclePageDto.setPagesize(10); // 默认每页10条,最大100条 + } + try { + // 如果文章状态值是否在0到2之间则根据文章状态查询文章列表 + if (arriclePageDto.getStatus() < 0 || arriclePageDto.getStatus() > 2) { return ResponseMessage.badRequest("文章状态值必须在0到2之间"); } - if (page == null || page < 0) { - page = 0; // 默认第一页 + PageRequest pageRequest = PageRequest.of(arriclePageDto.getPagenum(), arriclePageDto.getPagesize()); + // 如果文章分类ID不为空则根据文章分类ID查询文章列表 + if (arriclePageDto.getCategoryid() != null && arriclePageDto.getCategoryid() > 0) { + // 如果文章分类ID不为空则根据文章分类ID查询文章列表 + List categoryAttribute = categoryAttributeRepository.findByCategoryId(arriclePageDto.getCategoryid()); + if (categoryAttribute.isEmpty()) { + return ResponseMessage.badRequest("分类下没有属性"); + } + // 如果文章属性ID数组不为空则根据文章属性ID数组查询文章列表 + List attributeids = categoryAttribute.stream().map(Categoryattribute::getAttributeid).collect(Collectors.toList()); + // 根据分类ID对应的属性ID数组分页查询文章列表 + Page
articlePage = articleRepository.findByStatusWithPagination(arriclePageDto.getStatus(), attributeids, pageRequest); + return ResponseMessage.success(articlePage, "根据分类ID分页查询文章成功"); } - if (size == null || size <= 0 || size > 100) { - size = 10; // 默认每页10条,最大100条 + // 如果文章属性ID不为空则根据文章属性ID查询文章列表 + if (arriclePageDto.getAttributeid() != null && arriclePageDto.getAttributeid() > 0) { + Page
articlePage = articleRepository.findByStatusWithPagination(arriclePageDto.getStatus(), arriclePageDto.getAttributeid(), pageRequest); + return ResponseMessage.success(articlePage, "根据属性ID分页查询文章成功"); } - - PageRequest pageRequest = PageRequest.of(page, size); - Page
articlePage = articleRepository.findByStatusWithPagination(status, pageRequest); + // 如果文章标题不为空则根据文章标题查询文章列表 + if (arriclePageDto.getTitle() != null && !arriclePageDto.getTitle().isEmpty()) { + Page
articlePage = articleRepository.findByStatusWithPagination(arriclePageDto.getStatus(), arriclePageDto.getTitle(), pageRequest); + return ResponseMessage.success(articlePage, "根据标题分页查询文章成功"); + } + Page
articlePage = articleRepository.findByStatusWithPagination(arriclePageDto.getStatus(), pageRequest); return ResponseMessage.success(articlePage, "根据状态分页查询文章成功"); } catch (Exception e) { log.error("根据状态分页查询文章列表失败: {}", e.getMessage()); @@ -224,8 +269,6 @@ public class ArticleService implements IArticleService { @Transactional(readOnly = true) public ResponseMessage> getArticlesByCategory(Integer categoryId) { try { - // 为了兼容旧接口,这里需要调整逻辑 - // 旧接口使用typeid,但我们现在用attributeid // 可以考虑查询该分类下的所有属性,然后获取相关文章 log.warn("使用了旧接口getArticlesByCategory,请考虑迁移到getArticlesByAttribute"); return ResponseMessage.success(articleRepository.findPublishedByAttribute(categoryId), "获取分类文章成功"); diff --git a/src/main/java/com/qf/myafterprojecy/service/impl/CategoryAttributeService.java b/src/main/java/com/qf/myafterprojecy/service/impl/CategoryAttributeService.java index 3711857..0226209 100644 --- a/src/main/java/com/qf/myafterprojecy/service/impl/CategoryAttributeService.java +++ b/src/main/java/com/qf/myafterprojecy/service/impl/CategoryAttributeService.java @@ -1,7 +1,7 @@ package com.qf.myafterprojecy.service.impl; import com.qf.myafterprojecy.exceptopn.ResponseMessage; -import com.qf.myafterprojecy.pojo.Category_attribute; +import com.qf.myafterprojecy.pojo.Categoryattribute; import com.qf.myafterprojecy.pojo.dto.CategoryAttributeDto; import com.qf.myafterprojecy.repository.CategoryAttributeRepository; import com.qf.myafterprojecy.service.ICategoryAttributeService; @@ -30,9 +30,9 @@ public class CategoryAttributeService implements ICategoryAttributeService { */ @Override @Transactional(readOnly = true) - public ResponseMessage> getAllCategoryAttributes() { + public ResponseMessage> getAllCategoryAttributes() { try { - List attributes = categoryAttributeRepository.findAll(); + List attributes = categoryAttributeRepository.findAll(); return ResponseMessage.success(attributes, "获取所有分类属性成功"); } catch (DataAccessException e) { log.error("获取所有分类属性失败: {}", e.getMessage()); @@ -47,13 +47,13 @@ public class CategoryAttributeService implements ICategoryAttributeService { */ @Override @Transactional(readOnly = true) - public ResponseMessage getCategoryAttributeById(Integer id) { + public ResponseMessage getCategoryAttributeById(Integer id) { try { if (id == null || id <= 0) { return ResponseMessage.badRequest("属性ID无效"); } - Category_attribute attribute = categoryAttributeRepository.findById(id) + Categoryattribute attribute = categoryAttributeRepository.findById(id) .orElseThrow(() -> new RuntimeException("分类属性不存在")); return ResponseMessage.success(attribute, "获取分类属性成功"); @@ -76,13 +76,13 @@ public class CategoryAttributeService implements ICategoryAttributeService { */ @Override @Transactional(readOnly = true) - public ResponseMessage> getAttributesByCategoryId(Integer categoryId) { + public ResponseMessage> getAttributesByCategoryId(Integer categoryId) { try { if (categoryId == null || categoryId <= 0) { return ResponseMessage.badRequest("分类ID无效"); } - List attributes = categoryAttributeRepository.findByCategoryId(categoryId); + List attributes = categoryAttributeRepository.findByCategoryId(categoryId); return ResponseMessage.success(attributes, "获取分类属性列表成功"); } catch (DataAccessException e) { log.error("获取分类属性列表失败: {}", e.getMessage()); @@ -97,7 +97,7 @@ public class CategoryAttributeService implements ICategoryAttributeService { */ @Override @Transactional(rollbackFor = Exception.class) - public ResponseMessage saveCategoryAttribute(CategoryAttributeDto dto) { + public ResponseMessage saveCategoryAttribute(CategoryAttributeDto dto) { try { // 检查属性名称是否已存在于该分类下 if (categoryAttributeRepository.existsByCategoryidAndAttributename( @@ -105,10 +105,10 @@ public class CategoryAttributeService implements ICategoryAttributeService { return ResponseMessage.badRequest("该分类下已存在同名属性"); } - Category_attribute attribute = new Category_attribute(); + Categoryattribute attribute = new Categoryattribute(); BeanUtils.copyProperties(dto, attribute); - Category_attribute savedAttribute = categoryAttributeRepository.save(attribute); + Categoryattribute savedAttribute = categoryAttributeRepository.save(attribute); log.info("成功创建分类属性: {}, 分类ID: {}", savedAttribute.getAttributename(), savedAttribute.getCategoryid()); @@ -127,13 +127,13 @@ public class CategoryAttributeService implements ICategoryAttributeService { */ @Override @Transactional(rollbackFor = Exception.class) - public ResponseMessage updateCategoryAttribute(Integer id, CategoryAttributeDto dto) { + public ResponseMessage updateCategoryAttribute(Integer id, CategoryAttributeDto dto) { try { if (id == null || id <= 0) { return ResponseMessage.badRequest("属性ID无效"); } - Category_attribute attribute = categoryAttributeRepository.findById(id) + Categoryattribute attribute = categoryAttributeRepository.findById(id) .orElseThrow(() -> new RuntimeException("分类属性不存在")); // 如果修改了属性名称,检查新名称是否已存在 @@ -146,7 +146,7 @@ public class CategoryAttributeService implements ICategoryAttributeService { BeanUtils.copyProperties(dto, attribute); attribute.setAttributeid(id); // 确保ID不变 - Category_attribute updatedAttribute = categoryAttributeRepository.save(attribute); + Categoryattribute updatedAttribute = categoryAttributeRepository.save(attribute); log.info("成功更新分类属性: ID={}, 名称={}", updatedAttribute.getAttributeid(), updatedAttribute.getAttributename()); diff --git a/src/main/java/com/qf/myafterprojecy/service/impl/MessageService.java b/src/main/java/com/qf/myafterprojecy/service/impl/MessageService.java index 30cf1f2..e37bc4b 100644 --- a/src/main/java/com/qf/myafterprojecy/service/impl/MessageService.java +++ b/src/main/java/com/qf/myafterprojecy/service/impl/MessageService.java @@ -3,6 +3,7 @@ package com.qf.myafterprojecy.service.impl; import com.qf.myafterprojecy.exceptopn.ResponseMessage; import com.qf.myafterprojecy.pojo.Message; import com.qf.myafterprojecy.pojo.dto.MessageDto; +import com.qf.myafterprojecy.pojo.dto.MessagePageDto; import com.qf.myafterprojecy.repository.MessageRepository; import com.qf.myafterprojecy.service.IMessageService; @@ -10,7 +11,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties.Pageable; import org.springframework.dao.DataAccessException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -231,20 +235,52 @@ public class MessageService implements IMessageService { } } - @Override - public ResponseMessage getMessageCountByArticleId(Integer articleId) { - if (articleId == null || articleId <= 0) { - logger.warn("获取文章评论数量时ID无效: {}", articleId); - return ResponseMessage.badRequest("文章ID无效"); - } + @Override + public ResponseMessage> getMessagesByPage(MessagePageDto messagePageDto) { + if (messagePageDto == null) { + logger.warn("分页查询消息时参数为空"); + return ResponseMessage.badRequest("分页参数不能为空"); + } + if (messagePageDto.getPageNum() == null) { + logger.warn("分页查询消息时页码无效: {}", messagePageDto.getPageNum()); + return ResponseMessage.badRequest("页码无效"); + } + if (messagePageDto.getPageSize() == null || messagePageDto.getPageSize() <= 0) { + logger.warn("分页查询消息时每页数量无效: {}", messagePageDto.getPageSize()); + return ResponseMessage.badRequest("每页数量无效"); + } try { - logger.info("获取文章评论数量: {}", articleId); - Long count = messageRepository.countByArticleId(articleId); + // 如何文章id为空,默认根据分页基础信息查询消息 + PageRequest pageable = PageRequest.of(messagePageDto.getPageNum(), messagePageDto.getPageSize()); + if (messagePageDto.getArticleid() != null && messagePageDto.getArticleid() > 0) { + // 如果文章ID存在,根据文章ID查询消息 + Page messagePage = messageRepository.findByArticleId(messagePageDto.getArticleid(), pageable); + return ResponseMessage.success(messagePage.getContent(), "查询成功"); + } + // 如果文章ID不存在,根据分页基础信息查询所有消息 + Page messagePage = messageRepository.findAllMessages(pageable); + return ResponseMessage.success(messagePage.getContent(), "查询成功"); + } catch (DataAccessException e) { + logger.error("分页查询消息失败: {}", messagePageDto, e); + return ResponseMessage.error("查询消息失败:" + e.getMessage()); + } + } + // 获取回复消息条数 如果id为空获取文章id为空的消息条数 + @Override + public ResponseMessage getMessageCountByArticleId(Integer articleId) { + + try { + logger.info("获取文章回复数量: {}", articleId); + if (articleId == null || articleId <= 0) { + Integer count = messageRepository.countReplyByArticleIdIsNull(); + return ResponseMessage.success(count, "查询成功"); + } + Integer count = messageRepository.countReplyByArticleId(articleId); return ResponseMessage.success(count, "查询成功"); } catch (DataAccessException e) { - logger.error("获取文章评论数量失败: {}", articleId, e); - return ResponseMessage.error("查询评论数量失败:" + e.getMessage()); + logger.error("获取文章回复数量失败: {}", articleId, e); + return ResponseMessage.error("查询回复数量失败:" + e.getMessage()); } } }