From c0dd5c4868176d87acdc2ca4dc4c6dc0138d55bb Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Mon, 20 Feb 2023 17:39:03 +0800 Subject: [PATCH] notice receiver rule support get by pages --- .../controller/NoticeConfigController.java | 31 ++++++++++++++----- .../manager/service/NoticeConfigService.java | 6 ++-- .../service/impl/NoticeConfigServiceImpl.java | 10 +++--- .../service/NoticeConfigServiceTest.java | 14 --------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/manager/src/main/java/com/zmops/open/manager/controller/NoticeConfigController.java b/manager/src/main/java/com/zmops/open/manager/controller/NoticeConfigController.java index c9f7065..629e8c9 100644 --- a/manager/src/main/java/com/zmops/open/manager/controller/NoticeConfigController.java +++ b/manager/src/main/java/com/zmops/open/manager/controller/NoticeConfigController.java @@ -25,6 +25,9 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -85,8 +88,12 @@ public ResponseEntity> deleteNoticeReceiver( @GetMapping(path = "/receivers") @Operation(summary = "Get a list of message notification recipients based on query filter items", description = "根据查询过滤项获取消息通知接收人列表") - public ResponseEntity>> getReceivers( - @Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "tom") @RequestParam(required = false) final String name) { + public ResponseEntity>> getReceivers( + @Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "tom") @RequestParam(required = false) final String name, + @Parameter(description = "en: Sort Field,default id,zh: 排序字段,默认更新时间", example = "name") @RequestParam(defaultValue = "gmtUpdate") final String sort, + @Parameter(description = "en: Sort by,zh: 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") final String order, + @Parameter(description = "en: List current page,zh: 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex, + @Parameter(description = "en: Number of list pagination,zh: 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) { Specification specification = (root, query, criteriaBuilder) -> { Predicate predicate = criteriaBuilder.conjunction(); if (name != null && !"".equals(name)) { @@ -95,8 +102,10 @@ public ResponseEntity>> getReceivers( } return predicate; }; - List receivers = noticeConfigService.getNoticeReceivers(specification); - Message> message = new Message<>(receivers); + Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort)); + PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp); + Page receivers = noticeConfigService.getNoticeReceivers(specification, pageRequest); + Message> message = new Message<>(receivers); return ResponseEntity.ok(message); } @@ -131,8 +140,12 @@ public ResponseEntity> deleteNoticeRule( @GetMapping(path = "/rules") @Operation(summary = "Get a list of message notification policies based on query filter items", description = "根据查询过滤项获取消息通知策略列表") - public ResponseEntity>> getRules( - @Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "rule1") @RequestParam(required = false) final String name) { + public ResponseEntity>> getRules( + @Parameter(description = "en: Recipient name,zh: 接收人名称,模糊查询", example = "rule1") @RequestParam(required = false) final String name, + @Parameter(description = "en: Sort Field,default id,zh: 排序字段,默认更新时间", example = "name") @RequestParam(defaultValue = "gmtUpdate") final String sort, + @Parameter(description = "en: Sort by,zh: 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") final String order, + @Parameter(description = "en: List current page,zh: 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex, + @Parameter(description = "en: Number of list pagination,zh: 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) { Specification specification = (root, query, criteriaBuilder) -> { Predicate predicate = criteriaBuilder.conjunction(); if (name != null && !"".equals(name)) { @@ -141,8 +154,10 @@ public ResponseEntity>> getRules( } return predicate; }; - List receiverPage = noticeConfigService.getNoticeRules(specification); - Message> message = new Message<>(receiverPage); + Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort)); + PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp); + Page receiverPage = noticeConfigService.getNoticeRules(specification, pageRequest); + Message> message = new Message<>(receiverPage); return ResponseEntity.ok(message); } diff --git a/manager/src/main/java/com/zmops/open/manager/service/NoticeConfigService.java b/manager/src/main/java/com/zmops/open/manager/service/NoticeConfigService.java index f0d157d..27d39c7 100644 --- a/manager/src/main/java/com/zmops/open/manager/service/NoticeConfigService.java +++ b/manager/src/main/java/com/zmops/open/manager/service/NoticeConfigService.java @@ -20,6 +20,8 @@ import com.zmops.open.common.entity.alerter.Alert; import com.zmops.open.common.entity.manager.NoticeReceiver; import com.zmops.open.common.entity.manager.NoticeRule; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; import java.util.List; @@ -40,7 +42,7 @@ public interface NoticeConfigService { * @param specification Query conditions 查询条件 * @return Search result 查询结果 */ - List getNoticeReceivers(Specification specification); + Page getNoticeReceivers(Specification specification, PageRequest pageRequest); /** * Dynamic conditional query @@ -49,7 +51,7 @@ public interface NoticeConfigService { * @param specification Query conditions 查询条件 * @return Search result 查询结果 */ - List getNoticeRules(Specification specification); + Page getNoticeRules(Specification specification, PageRequest pageRequest); /** * Add a notification recipient diff --git a/manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java b/manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java index 1431045..2928e48 100644 --- a/manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java +++ b/manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java @@ -28,6 +28,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -64,13 +66,13 @@ public class NoticeConfigServiceImpl implements NoticeConfigService { private DispatcherAlarm dispatcherAlarm; @Override - public List getNoticeReceivers(Specification specification) { - return noticeReceiverDao.findAll(specification); + public Page getNoticeReceivers(Specification specification, PageRequest pageRequest) { + return noticeReceiverDao.findAll(specification, pageRequest); } @Override - public List getNoticeRules(Specification specification) { - return noticeRuleDao.findAll(specification); + public Page getNoticeRules(Specification specification, PageRequest pageRequest) { + return noticeRuleDao.findAll(specification, pageRequest); } @Override diff --git a/manager/src/test/java/com/zmops/open/manager/service/NoticeConfigServiceTest.java b/manager/src/test/java/com/zmops/open/manager/service/NoticeConfigServiceTest.java index 8801adb..bddd118 100644 --- a/manager/src/test/java/com/zmops/open/manager/service/NoticeConfigServiceTest.java +++ b/manager/src/test/java/com/zmops/open/manager/service/NoticeConfigServiceTest.java @@ -52,20 +52,6 @@ class NoticeConfigServiceTest { void setUp() { } - @Test - void getNoticeReceivers() { - final Specification specification = mock(Specification.class); - noticeConfigService.getNoticeReceivers(specification); - verify(noticeReceiverDao, times(1)).findAll(specification); - } - - @Test - void getNoticeRules() { - final Specification specification = mock(Specification.class); - noticeConfigService.getNoticeRules(specification); - verify(noticeRuleDao, times(1)).findAll(specification); - } - @Test void addReceiver() { final NoticeReceiver noticeReceiver = mock(NoticeReceiver.class);