refactor: 消息通知

This commit is contained in:
Captain.B 2021-08-18 12:39:53 +08:00 committed by 刘瑞斌
parent e8c3f23fdf
commit 720e52f911
5 changed files with 110 additions and 36 deletions

View File

@ -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<String, Project> queryNameByIds(@Param("ids") List<String> ids);
Organization getOrganizationByProjectId(@Param("projectId")String projectId);
}

View File

@ -151,4 +151,11 @@
where organization_id = #{orgId})) as a)
</update>
<select id="getOrganizationByProjectId">
SELECT organization.*
FROM organization
JOIN workspace ON organization.id = organization_id
JOIN project ON workspace.id = workspace_id
WHERE project.id = #{projectId, jdbcType=VARCHAR}
</select>
</mapper>

View File

@ -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<List<Notification>> 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<List<Notification>> 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<List<Notification>> 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);
}
}

View File

@ -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<MessageDetail> searchMessageByType(String type) {
try {
SessionUser user = SessionUtils.getUser();
String orgId = user.getLastOrganizationId();
String orgId = SessionUtils.getCurrentOrganizationId();
List<MessageDetail> 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<MessageDetail> messageDetails = new ArrayList<>();
MessageTaskExample example = new MessageTaskExample();

View File

@ -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<Project> projectList = projectMapper.selectByExample(example);
for (Project p :projectList) {
for (Project p : projectList) {
this.openMockTcp(p);
}
}
public Organization getOrganizationByProjectId(String projectId) {
return extProjectMapper.getOrganizationByProjectId(projectId);
}
}