From 720e52f91163c9938cc290489e7f4ff6ea9123df Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Wed, 18 Aug 2021 12:39:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B6=88=E6=81=AF=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/mapper/ext/ExtProjectMapper.java | 3 + .../base/mapper/ext/ExtProjectMapper.xml | 7 +++ .../controller/NotificationController.java | 59 +++++++++++++++++++ .../notice/service/NoticeService.java | 21 ++++--- .../metersphere/service/ProjectService.java | 56 ++++++++++-------- 5 files changed, 110 insertions(+), 36 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/notice/controller/NotificationController.java diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java index 876b113e84..aa7ef79255 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java @@ -1,5 +1,6 @@ package io.metersphere.base.mapper.ext; +import io.metersphere.base.domain.Organization; import io.metersphere.base.domain.Project; import io.metersphere.controller.request.ProjectRequest; import io.metersphere.dto.ProjectDTO; @@ -27,4 +28,6 @@ public interface ExtProjectMapper { @MapKey("id") Map queryNameByIds(@Param("ids") List ids); + + Organization getOrganizationByProjectId(@Param("projectId")String projectId); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml index 8bba5ecabe..03eb5ca727 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml @@ -151,4 +151,11 @@ where organization_id = #{orgId})) as a) + diff --git a/backend/src/main/java/io/metersphere/notice/controller/NotificationController.java b/backend/src/main/java/io/metersphere/notice/controller/NotificationController.java new file mode 100644 index 0000000000..ab687564db --- /dev/null +++ b/backend/src/main/java/io/metersphere/notice/controller/NotificationController.java @@ -0,0 +1,59 @@ +package io.metersphere.notice.controller; + + +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; +import io.metersphere.base.domain.Notification; +import io.metersphere.commons.utils.PageUtils; +import io.metersphere.commons.utils.Pager; +import io.metersphere.notice.service.NotificationService; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +@RestController +@RequestMapping(value = "notification") +public class NotificationController { + + @Resource + private NotificationService notificationService; + + @PostMapping(value = "/list/all/{goPage}/{pageSize}") + public Pager> listNotification(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody Notification notification) { + Page page = PageHelper.startPage(goPage, pageSize, true); + return PageUtils.setPageInfo(page, notificationService.listNotification(notification)); + } + + @PostMapping(value = "/list/read/{goPage}/{pageSize}") + public Pager> listReadNotification(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody Notification notification) { + Page page = PageHelper.startPage(goPage, pageSize, true); + return PageUtils.setPageInfo(page, notificationService.listReadNotification(notification)); + } + + @PostMapping(value = "/list/unread/{goPage}/{pageSize}") + public Pager> listUnreadNotification(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody Notification notification) { + Page page = PageHelper.startPage(goPage, pageSize, true); + return PageUtils.setPageInfo(page, notificationService.listUnreadNotification(notification)); + } + + @GetMapping(value = "/get/{id}") + public Notification getNotification(@PathVariable int id) { + return notificationService.getNotification(id); + } + + @GetMapping(value = "/read/{id}") + public Integer read(@PathVariable int id) { + return notificationService.read(id); + } + + @GetMapping(value = "/read/all") + public Integer readAll() { + return notificationService.readAll(); + } + + @PostMapping(value = "/count") + public Integer countNotification(@RequestBody Notification notification) { + return notificationService.countNotification(notification); + } +} diff --git a/backend/src/main/java/io/metersphere/notice/service/NoticeService.java b/backend/src/main/java/io/metersphere/notice/service/NoticeService.java index 52170e6689..6bfe911955 100644 --- a/backend/src/main/java/io/metersphere/notice/service/NoticeService.java +++ b/backend/src/main/java/io/metersphere/notice/service/NoticeService.java @@ -3,11 +3,11 @@ package io.metersphere.notice.service; import com.alibaba.fastjson.JSON; import io.metersphere.base.domain.MessageTask; import io.metersphere.base.domain.MessageTaskExample; +import io.metersphere.base.domain.Organization; import io.metersphere.base.mapper.LoadTestReportMapper; import io.metersphere.base.mapper.MessageTaskMapper; -import io.metersphere.base.mapper.UserMapper; +import io.metersphere.base.mapper.ext.ExtProjectMapper; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.i18n.Translator; @@ -17,6 +17,7 @@ import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.log.vo.StatusReference; import io.metersphere.log.vo.system.SystemReference; import io.metersphere.notice.domain.MessageDetail; +import io.metersphere.service.ProjectService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -34,7 +35,7 @@ public class NoticeService { @Resource private LoadTestReportMapper loadTestReportMapper; @Resource - private UserMapper userMapper; + private ExtProjectMapper extProjectMapper; public void saveMessageTask(MessageDetail messageDetail) { MessageTaskExample example = new MessageTaskExample(); @@ -43,8 +44,7 @@ public class NoticeService { if (messageTaskLists.size() > 0) { delMessage(messageDetail.getIdentification()); } - SessionUser user = SessionUtils.getUser(); - String orgId = user.getLastOrganizationId(); + String orgId = SessionUtils.getCurrentOrganizationId(); long time = System.currentTimeMillis(); String identification = messageDetail.getIdentification(); if (StringUtils.isBlank(identification)) { @@ -117,8 +117,7 @@ public class NoticeService { public List searchMessageByType(String type) { try { - SessionUser user = SessionUtils.getUser(); - String orgId = user.getLastOrganizationId(); + String orgId = SessionUtils.getCurrentOrganizationId(); List messageDetails = new ArrayList<>(); MessageTaskExample example = new MessageTaskExample(); @@ -150,11 +149,11 @@ public class NoticeService { try { String orgId = ""; if (null == SessionUtils.getUser()) { - String userId = loadTestReportMapper.selectByPrimaryKey(id).getUserId(); - orgId = userMapper.selectByPrimaryKey(userId).getLastOrganizationId(); + String projectId = loadTestReportMapper.selectByPrimaryKey(id).getProjectId(); + Organization organization = extProjectMapper.getOrganizationByProjectId(projectId); + orgId = organization.getId(); } else { - SessionUser user = SessionUtils.getUser(); - orgId = user.getLastOrganizationId(); + orgId = SessionUtils.getCurrentOrganizationId(); } List messageDetails = new ArrayList<>(); MessageTaskExample example = new MessageTaskExample(); diff --git a/backend/src/main/java/io/metersphere/service/ProjectService.java b/backend/src/main/java/io/metersphere/service/ProjectService.java index d87671832d..0c5fc362f0 100644 --- a/backend/src/main/java/io/metersphere/service/ProjectService.java +++ b/backend/src/main/java/io/metersphere/service/ProjectService.java @@ -128,18 +128,18 @@ public class ProjectService { private String genSystemId() { String maxSystemIdInDb = extProjectMapper.getMaxSystemId(); String systemId = "10001"; - if(StringUtils.isNotEmpty(maxSystemIdInDb)){ + if (StringUtils.isNotEmpty(maxSystemIdInDb)) { systemId = String.valueOf(Long.parseLong(maxSystemIdInDb) + 1); } return systemId; } - public Project checkSystemId(Project project){ - if(project!=null){ + public Project checkSystemId(Project project) { + if (project != null) { ProjectExample example = new ProjectExample(); example.createCriteria().andSystemIdEqualTo(project.getSystemId()); long count = projectMapper.countByExample(example); - if(count > 1){ + if (count > 1) { String systemId = this.genSystemId(); Project updateModel = new Project(); updateModel.setId(project.getId()); @@ -269,7 +269,7 @@ public class ProjectService { //查询之前的TCP端口,用于检查是否需要开启/关闭 TCP接口 int lastTcpNum = 0; Project oldData = projectMapper.selectByPrimaryKey(project.getId()); - if(oldData!=null && oldData.getMockTcpPort() != null){ + if (oldData != null && oldData.getMockTcpPort() != null) { lastTcpNum = oldData.getMockTcpPort().intValue(); } @@ -287,20 +287,20 @@ public class ProjectService { ApiTestEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); apiTestEnvironmentService.getMockEnvironmentByProjectId(project.getId()); //开启tcp mock - if(project.getIsMockTcpOpen()){ - this.reloadMockTcp(project,lastTcpNum); - }else { + if (project.getIsMockTcpOpen()) { + this.reloadMockTcp(project, lastTcpNum); + } else { this.closeMockTcp(project); } } private void checkProjectTcpPort(Project project) { //判断端口是否重复 - if(project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0){ + if (project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0) { ProjectExample example = new ProjectExample(); example.createCriteria().andMockTcpPortEqualTo(project.getMockTcpPort()); long countResult = projectMapper.countByExample(example); - if(countResult > 0){ + if (countResult > 0) { MSException.throwException("TCP Port is not unique!"); } } @@ -521,40 +521,42 @@ public class ProjectService { return extProjectMapper.queryNameByIds(ids); } - public void openMockTcp(Project project){ - if(project == null){ + public void openMockTcp(Project project) { + if (project == null) { MSException.throwException("Project not found!"); - }else { - if(project.getMockTcpPort() == null){ + } else { + if (project.getMockTcpPort() == null) { MSException.throwException("Mock tcp port is not Found!"); - }else { + } else { TCPPool.createTcp(project.getMockTcpPort()); } } } - public void reloadMockTcp(Project project,int oldPort){ + + public void reloadMockTcp(Project project, int oldPort) { this.closeMockTcp(oldPort); this.openMockTcp(project); } - public void closeMockTcp(String projectId){ + public void closeMockTcp(String projectId) { Project project = projectMapper.selectByPrimaryKey(projectId); this.closeMockTcp(project); } - public void closeMockTcp(Project project){ - if(project == null){ + + public void closeMockTcp(Project project) { + if (project == null) { MSException.throwException("Project not found!"); - }else { - if(project.getMockTcpPort() == null){ + } else { + if (project.getMockTcpPort() == null) { MSException.throwException("Mock tcp port is not Found!"); - }else { + } else { this.closeMockTcp(project.getMockTcpPort().intValue()); } } } - public void closeMockTcp(int tcpPort){ - if(tcpPort != 0){ + public void closeMockTcp(int tcpPort) { + if (tcpPort != 0) { TCPPool.closeTcp(tcpPort); } } @@ -566,8 +568,12 @@ public class ProjectService { example.createCriteria().andIsMockTcpOpenEqualTo(statusBoolean).andMockTcpPortNotEqualTo(portInteger); List projectList = projectMapper.selectByExample(example); - for (Project p :projectList) { + for (Project p : projectList) { this.openMockTcp(p); } } + + public Organization getOrganizationByProjectId(String projectId) { + return extProjectMapper.getOrganizationByProjectId(projectId); + } }