fix(消息中心): 修复消息中心联调时发现的bug

This commit is contained in:
guoyuqi 2024-03-04 18:10:13 +08:00 committed by Craftsman
parent b717ecd71a
commit b1c1c2ad98
7 changed files with 57 additions and 40 deletions

View File

@ -3,10 +3,9 @@ package io.metersphere.system.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.project.domain.Notification;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.NotificationRequest;
import io.metersphere.system.log.dto.NotificationDTO;
import io.metersphere.system.service.NotificationService;
import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager;
@ -29,7 +28,7 @@ public class NotificationController {
@PostMapping(value = "/list/all/page")
@Operation(summary = "消息中心-获取消息中心所有消息列表")
public Pager<List<Notification>> listNotification(@Validated @RequestBody NotificationRequest notificationRequest) {
public Pager<List<NotificationDTO>> listNotification(@Validated @RequestBody NotificationRequest notificationRequest) {
Page<Object> page = PageHelper.startPage(notificationRequest.getCurrent(), notificationRequest.getPageSize(), true);
return PageUtils.setPageInfo(page, notificationService.listNotification(notificationRequest, SessionUtils.getUserId()));
}

View File

@ -22,7 +22,7 @@ public class NotificationRequest extends BasePageRequest {
@Schema(description = "状态")
private String status;
@Schema(description = "资源类型: TEST_PLAN/BUG/CASE/API/UI/LOAD/JENKINS")
@Schema(description = "资源类型: TEST_PLAN/BUG/CASE/API/UI/LOAD/JENKINS/SCHEDULE")
private String resourceType;
@Schema(description = "创建时间")

View File

@ -0,0 +1,15 @@
package io.metersphere.system.log.dto;
import io.metersphere.project.domain.Notification;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
public class NotificationDTO extends Notification {
@Schema(description = "头像")
private String avatar;
@Schema(description = "用户名")
private String userName;
}

View File

@ -1,14 +1,14 @@
package io.metersphere.system.mapper;
import io.metersphere.project.domain.Notification;
import io.metersphere.system.dto.sdk.request.NotificationRequest;
import io.metersphere.system.log.dto.NotificationDTO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BaseNotificationMapper {
List<Notification> listNotification(@Param("request") NotificationRequest notificationRequest);
List<NotificationDTO> listNotification(@Param("request") NotificationRequest notificationRequest);
}

View File

@ -2,22 +2,25 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.metersphere.system.mapper.BaseNotificationMapper">
<select id="listNotification" resultType="io.metersphere.project.domain.Notification">
SELECT * FROM notification
WHERE receiver = #{request.receiver} AND create_time &gt; (unix_timestamp() - 90 * 24 * 3600) * 1000
<select id="listNotification" resultType="io.metersphere.system.log.dto.NotificationDTO">
SELECT *, u.name as userName,
ux.avatar as userLogo FROM notification
left join user u on notification.operator = u.id
left join user_extend ux on notification.operator = ux.id
WHERE notification.receiver = #{request.receiver} AND notification.create_time &gt; (unix_timestamp() - 90 * 24 * 3600) * 1000
<if test='request.title != null and request.title != ""'>
AND ( title LIKE #{request.title} OR content LIKE #{request.title} )
AND ( notification.title LIKE #{request.title} OR notification.content LIKE #{request.title} )
</if>
<if test='request.type != null and request.type != ""'>
AND type = #{request.type}
AND notification.type = #{request.type}
</if>
<if test='request.status != null and request.status != ""'>
AND status = #{request.status}
AND notification.status = #{request.status}
</if>
<if test='request.resourceType != null and notificationRequest.resourceType != ""'>
AND resource_type LIKE #{request.resourceType}
<if test='request.resourceType != null and request.resourceType != ""'>
AND notification.resource_type LIKE #{request.resourceType}
</if>
ORDER BY create_time DESC
ORDER BY notification.create_time DESC
</select>
</mapper>

View File

@ -1,12 +1,12 @@
package io.metersphere.system.service;
import io.metersphere.project.domain.Notification;
import io.metersphere.project.domain.NotificationExample;
import io.metersphere.project.mapper.NotificationMapper;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.NotificationRequest;
import io.metersphere.system.log.dto.NotificationDTO;
import io.metersphere.system.mapper.BaseNotificationMapper;
import io.metersphere.system.notice.constants.NotificationConstants;
import jakarta.annotation.Resource;
@ -29,10 +29,8 @@ public class NotificationService {
@Resource
private BaseNotificationMapper baseNotificationMapper;
public List<Notification> listNotification(NotificationRequest notificationRequest, String userId) {
if (StringUtils.isBlank(notificationRequest.getReceiver())) {
notificationRequest.setReceiver(userId);
}
public List<NotificationDTO> listNotification(NotificationRequest notificationRequest, String userId) {
buildParam(notificationRequest, userId);
return baseNotificationMapper.listNotification(notificationRequest);
}
@ -54,10 +52,8 @@ public class NotificationService {
public List<OptionDTO> countNotification(NotificationRequest notificationRequest, String userId) {
List<OptionDTO>optionDTOS = new ArrayList<>();
if (StringUtils.isBlank(notificationRequest.getReceiver())) {
notificationRequest.setReceiver(userId);
}
List<Notification> notifications = baseNotificationMapper.listNotification(notificationRequest);
buildParam(notificationRequest, userId);
List<NotificationDTO> notifications = baseNotificationMapper.listNotification(notificationRequest);
OptionDTO totalOptionDTO = new OptionDTO();
totalOptionDTO.setId("total");
totalOptionDTO.setName(String.valueOf(notifications.size()));
@ -66,13 +62,22 @@ public class NotificationService {
return optionDTOS;
}
private static void buildSourceCount(List<Notification> notifications, List<OptionDTO> optionDTOS) {
private static void buildParam(NotificationRequest notificationRequest, String userId) {
if (StringUtils.isNotBlank(notificationRequest.getTitle())) {
notificationRequest.setTitle("%" + notificationRequest.getTitle() + "%");
}
if (StringUtils.isNotBlank(notificationRequest.getResourceType())) {
notificationRequest.setResourceType("%" + notificationRequest.getResourceType() + "%");
}
if (StringUtils.isBlank(notificationRequest.getReceiver())) {
notificationRequest.setReceiver(userId);
}
}
private static void buildSourceCount(List<NotificationDTO> notifications, List<OptionDTO> optionDTOS) {
Map<String,Integer>countMap = new HashMap<>();
Map<String, List<Notification>> resourceMap = notifications.stream().collect(Collectors.groupingBy(Notification::getResourceType));
resourceMap.forEach((k,v)->{
if (k.contains("TEST_PLAN")) {
countMap.merge("TEST_PLAN", v.size(), Integer::sum);
}
if (k.contains("BUG")) {
countMap.merge("BUG", v.size(), Integer::sum);
}
@ -82,20 +87,14 @@ public class NotificationService {
if (k.contains("API")) {
countMap.merge("API", v.size(), Integer::sum);
}
if (k.contains("UI")) {
countMap.merge("UI", v.size(), Integer::sum);
}
if (k.contains("LOAD")) {
countMap.merge("LOAD", v.size(), Integer::sum);
}
if (k.contains("JENKINS")) {
countMap.merge("JENKINS", v.size(), Integer::sum);
if (k.contains("SCHEDULE")) {
countMap.merge("SCHEDULE", v.size(), Integer::sum);
}
});
countMap.forEach((k,v)->{
OptionDTO optionDTO = new OptionDTO();
optionDTO.setId("total");
optionDTO.setName(String.valueOf(notifications.size()));
optionDTO.setId(k);
optionDTO.setName(String.valueOf(v));
optionDTOS.add(optionDTO);
});
}

View File

@ -8,6 +8,7 @@ import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.request.NotificationRequest;
import io.metersphere.system.log.dto.NotificationDTO;
import io.metersphere.system.notice.constants.NotificationConstants;
import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource;
@ -57,14 +58,14 @@ public class NotificationControllerTests extends BaseTest {
notificationRequest.setCurrent(1);
notificationRequest.setReceiver("admin");
notificationRequest.setType("SYSTEM_NOTICE");
notificationRequest.setResourceType("CASE");
MvcResult mvcResult = this.requestPostWithOkAndReturn(NOTIFICATION_LIST_PAGE, notificationRequest);
Pager<List<Notification>> tableData = JSON.parseObject(JSON.toJSONString(
Pager<List<NotificationDTO>> tableData = JSON.parseObject(JSON.toJSONString(
JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData()),
Pager.class);
//返回值的页码和当前页码相同
Assertions.assertEquals(tableData.getCurrent(), notificationRequest.getCurrent());
Assertions.assertFalse(tableData.getList().isEmpty());
}
@Test