fix(platform): complete picker parity and message detail
Some checks failed
ci / build-test-parity (push) Has been cancelled
Some checks failed
ci / build-test-parity (push) Has been cancelled
This commit is contained in:
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
@@ -47,6 +48,8 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
* <ul>
|
||||
* <li>{@code GET /api/notice} — 当前用户消息分页列表,支持 unreadOnly 过滤</li>
|
||||
* <li>{@code GET /api/notice/unread-count} — 轻量未读数计数(顶栏轮询用)</li>
|
||||
* <li>{@code GET /api/notice/{id}/detail} — 已发送/可访问消息详情</li>
|
||||
* <li>{@code POST /api/notice/received/{id}/open} — 收到消息打开详情并置已读</li>
|
||||
* <li>{@code POST /api/notice/{id}/read} — 单条置已读</li>
|
||||
* <li>{@code POST /api/notice/read-all} — 当前用户全部已读</li>
|
||||
* </ul>
|
||||
@@ -119,6 +122,80 @@ public class NoticeController {
|
||||
return okList(items, total);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 消息详情
|
||||
|
||||
/**
|
||||
* 按 sys_message_id 加载消息详情。
|
||||
*
|
||||
* <p>用于「已发送消息」查看按钮,也允许当前用户作为收件人查看同一消息。访问守卫:
|
||||
* 当前用户是 createby,或当前用户在 sys_messageuser 中存在未删除收件记录。
|
||||
*/
|
||||
@Operation(summary = "消息详情(按 sys_message_id,只读)",
|
||||
description = "当前用户必须是发送人或收件人;返回 title/content/sender/receivers/document 等详情字段")
|
||||
@GetMapping("/api/notice/{id}/detail")
|
||||
public Map<String, Object> detail(
|
||||
@Parameter(description = "sys_message_id(36位)") @PathVariable String id) {
|
||||
|
||||
String userId = requireCurrentUserId();
|
||||
String sql = messageDetailSelect()
|
||||
+ " WHERE m.sys_message_id = CAST(? AS character(36))"
|
||||
+ " AND m.isdel = 0"
|
||||
+ " AND (m.createby = CAST(? AS character(36))"
|
||||
+ " OR EXISTS (SELECT 1 FROM eip3.sys_messageuser mu"
|
||||
+ " WHERE mu.sys_message_id = m.sys_message_id"
|
||||
+ " AND mu.sys_user_id = CAST(? AS character(36))"
|
||||
+ " AND mu.isdel = 0))";
|
||||
try {
|
||||
Map<String, Object> row = jdbcTemplate.queryForMap(sql, id.trim(), userId, userId);
|
||||
return okDetail(row, false, false);
|
||||
} catch (EmptyResultDataAccessException ex) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "消息不存在或无权查看");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 sys_messageuser_id 打开收到消息详情,并将当前用户这条接收记录置为已读。
|
||||
*
|
||||
* <p>复刻老 {@code MessageReceiveView.detail()}:先按收到消息主键加载
|
||||
* {@code SYS_MESSAGEUSER},再取 {@code SYS_MESSAGE_ID} 加载 {@code SYS_MESSAGE},未读时更新
|
||||
* {@code ISREAD=true}。这里用一个 POST 完成,避免 GET initApi 承担写副作用。
|
||||
*/
|
||||
@Operation(summary = "打开收到消息详情并置已读",
|
||||
description = "按 sys_messageuser_id + 当前用户校验;未读时 UPDATE sys_messageuser.isread=1")
|
||||
@PostMapping("/api/notice/received/{id}/open")
|
||||
public Map<String, Object> openReceived(
|
||||
@Parameter(description = "sys_messageuser_id(36位)") @PathVariable String id) {
|
||||
|
||||
String userId = requireCurrentUserId();
|
||||
String sql = "SELECT TRIM(mu.sys_messageuser_id) AS receive_id,"
|
||||
+ " mu.isread AS receive_isread,"
|
||||
+ " " + messageDetailColumns()
|
||||
+ " FROM eip3.sys_messageuser mu"
|
||||
+ " JOIN eip3.sys_message m ON m.sys_message_id = mu.sys_message_id"
|
||||
+ " LEFT JOIN view_sys_user u ON u.view_sys_user_id = m.createby"
|
||||
+ " WHERE mu.sys_messageuser_id = CAST(? AS character(36))"
|
||||
+ " AND mu.sys_user_id = CAST(? AS character(36))"
|
||||
+ " AND mu.isdel = 0"
|
||||
+ " AND m.isdel = 0";
|
||||
try {
|
||||
Map<String, Object> row = jdbcTemplate.queryForMap(sql, id.trim(), userId);
|
||||
boolean wasRead = isTruthy(row.get("receive_isread"));
|
||||
boolean changed = false;
|
||||
if (!wasRead) {
|
||||
int affected = jdbcTemplate.update(
|
||||
"UPDATE eip3.sys_messageuser SET isread = 1"
|
||||
+ " WHERE sys_messageuser_id = CAST(? AS character(36))"
|
||||
+ " AND sys_user_id = CAST(? AS character(36))"
|
||||
+ " AND isdel = 0",
|
||||
id.trim(), userId);
|
||||
changed = affected > 0;
|
||||
}
|
||||
return okDetail(row, true, changed);
|
||||
} catch (EmptyResultDataAccessException ex) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "消息不存在或无权查看");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 未读数计数
|
||||
|
||||
/**
|
||||
@@ -239,4 +316,71 @@ public class NoticeController {
|
||||
resp.put("data", data);
|
||||
return resp;
|
||||
}
|
||||
|
||||
private Map<String, Object> okDetail(Map<String, Object> row, boolean received, boolean readChanged) {
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("id", string(row.get("id")));
|
||||
data.put("receiveId", string(row.get("receive_id")));
|
||||
data.put("title", row.get("title"));
|
||||
data.put("content", row.get("content"));
|
||||
data.put("type", row.get("type"));
|
||||
data.put("createTime", row.get("createtime"));
|
||||
data.put("senderId", string(row.get("sender_id")));
|
||||
data.put("sender", fallback(row.get("sender"), row.get("sender_id")));
|
||||
data.put("receivers", row.get("receivers"));
|
||||
data.put("receiverIds", row.get("receiver_ids"));
|
||||
data.put("document", row.get("document"));
|
||||
data.put("params", row.get("params"));
|
||||
data.put("received", received);
|
||||
data.put("readChanged", readChanged);
|
||||
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", "");
|
||||
resp.put("data", data);
|
||||
return resp;
|
||||
}
|
||||
|
||||
private static String messageDetailSelect() {
|
||||
return "SELECT " + messageDetailColumns()
|
||||
+ " FROM eip3.sys_message m"
|
||||
+ " LEFT JOIN view_sys_user u ON u.view_sys_user_id = m.createby";
|
||||
}
|
||||
|
||||
private static String messageDetailColumns() {
|
||||
return "TRIM(m.sys_message_id) AS id,"
|
||||
+ " m.title,"
|
||||
+ " m.content,"
|
||||
+ " m.type,"
|
||||
+ " m.createtime,"
|
||||
+ " TRIM(m.createby) AS sender_id,"
|
||||
+ " COALESCE(NULLIF(TRIM(u.displayname), ''), TRIM(m.createby)) AS sender,"
|
||||
+ " m.tousersname AS receivers,"
|
||||
+ " m.tousers AS receiver_ids,"
|
||||
+ " m.document AS document,"
|
||||
+ " m.params AS params";
|
||||
}
|
||||
|
||||
private static String string(Object value) {
|
||||
return value == null ? "" : String.valueOf(value).trim();
|
||||
}
|
||||
|
||||
private static String fallback(Object value, Object fallback) {
|
||||
String s = string(value);
|
||||
return s.isEmpty() ? string(fallback) : s;
|
||||
}
|
||||
|
||||
private static boolean isTruthy(Object value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
if (value instanceof Boolean b) {
|
||||
return b;
|
||||
}
|
||||
if (value instanceof Number n) {
|
||||
return n.intValue() != 0;
|
||||
}
|
||||
String s = String.valueOf(value).trim();
|
||||
return "1".equals(s) || "true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -19,6 +20,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
@@ -30,7 +32,8 @@ import com.dsasp.server.common.util.UserSession;
|
||||
/**
|
||||
* F2-11 NoticeController 单测(mock JdbcTemplate + UserSession,不连库)。
|
||||
*
|
||||
* <p>覆盖:消息分页(全量/仅未读/perPage 截断)、未读数计数、单条置已读、全部已读、未认证拒绝。
|
||||
* <p>覆盖:消息分页(全量/仅未读/perPage 截断)、详情加载、收到消息打开即已读、
|
||||
* 未读数计数、单条置已读、全部已读、未认证拒绝。
|
||||
* 响应格式统一校验 amis crud 约定 {status:0, data:{items, total}} 或 {status:0, data:{count:N}}。
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
@@ -157,10 +160,62 @@ class NoticeControllerTest {
|
||||
assertThrows(ResponseStatusException.class, () -> controller.unreadCount());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 消息详情
|
||||
|
||||
@Test
|
||||
@DisplayName("8. 已发送消息详情 → 按 sys_message_id 返回只读详情")
|
||||
void detailByMessageId() {
|
||||
mockUser("user-008");
|
||||
when(jdbc.queryForMap(anyString(), eq("msg-011"), eq("user-008"), eq("user-008")))
|
||||
.thenReturn(fakeDetail("msg-011"));
|
||||
|
||||
Map<String, Object> resp = controller.detail("msg-011");
|
||||
|
||||
assertEquals(0, resp.get("status"));
|
||||
Map<String, Object> data = (Map<String, Object>) resp.get("data");
|
||||
assertEquals("msg-011", data.get("id"));
|
||||
assertEquals("测试消息", data.get("title"));
|
||||
assertEquals("张三[001]", data.get("sender"));
|
||||
assertEquals(false, data.get("received"));
|
||||
assertEquals(false, data.get("readChanged"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("9. 收到消息打开详情 → 未读时置已读并返回 readChanged=true")
|
||||
void openReceivedMarksRead() {
|
||||
mockUser("user-009");
|
||||
Map<String, Object> row = fakeDetail("msg-012");
|
||||
row.put("receive_id", "recv-001");
|
||||
row.put("receive_isread", 0);
|
||||
when(jdbc.queryForMap(anyString(), eq("recv-001"), eq("user-009"))).thenReturn(row);
|
||||
when(jdbc.update(anyString(), eq("recv-001"), eq("user-009"))).thenReturn(1);
|
||||
|
||||
Map<String, Object> resp = controller.openReceived("recv-001");
|
||||
|
||||
assertEquals(0, resp.get("status"));
|
||||
Map<String, Object> data = (Map<String, Object>) resp.get("data");
|
||||
assertEquals("recv-001", data.get("receiveId"));
|
||||
assertEquals(true, data.get("received"));
|
||||
assertEquals(true, data.get("readChanged"));
|
||||
verify(jdbc).update(anyString(), eq("recv-001"), eq("user-009"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("10. 消息详情不存在或无权 → 404 ResponseStatusException")
|
||||
void detailNotFound() {
|
||||
mockUser("user-010");
|
||||
when(jdbc.queryForMap(anyString(), eq("missing"), eq("user-010"), eq("user-010")))
|
||||
.thenThrow(new EmptyResultDataAccessException(1));
|
||||
|
||||
ResponseStatusException ex = assertThrows(ResponseStatusException.class,
|
||||
() -> controller.detail("missing"));
|
||||
assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 单条置已读
|
||||
|
||||
@Test
|
||||
@DisplayName("8. 单条置已读 → HTTP 200, status=0, affected=1")
|
||||
@DisplayName("11. 单条置已读 → HTTP 200, status=0, affected=1")
|
||||
void markRead() {
|
||||
mockUser("user-006");
|
||||
when(jdbc.update(anyString(), eq("msg-010"), eq("user-006"))).thenReturn(1);
|
||||
@@ -175,7 +230,7 @@ class NoticeControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("9. 未登录时 markRead → 抛 401 ResponseStatusException")
|
||||
@DisplayName("12. 未登录时 markRead → 抛 401 ResponseStatusException")
|
||||
void markReadUnauthenticated() {
|
||||
mockNotLoggedIn();
|
||||
assertThrows(ResponseStatusException.class, () -> controller.markRead("msg-010"));
|
||||
@@ -184,7 +239,7 @@ class NoticeControllerTest {
|
||||
// ---------------------------------------------------------------- 全部已读
|
||||
|
||||
@Test
|
||||
@DisplayName("10. 全部已读 → HTTP 200, status=0, affected=3")
|
||||
@DisplayName("13. 全部已读 → HTTP 200, status=0, affected=3")
|
||||
void markAllRead() {
|
||||
mockUser("user-007");
|
||||
when(jdbc.update(anyString(), eq("user-007"))).thenReturn(3);
|
||||
@@ -199,7 +254,7 @@ class NoticeControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("11. 未登录时 markAllRead → 抛 401 ResponseStatusException")
|
||||
@DisplayName("14. 未登录时 markAllRead → 抛 401 ResponseStatusException")
|
||||
void markAllReadUnauthenticated() {
|
||||
mockNotLoggedIn();
|
||||
assertThrows(ResponseStatusException.class, () -> controller.markAllRead());
|
||||
@@ -218,4 +273,20 @@ class NoticeControllerTest {
|
||||
m.put("isread", isread);
|
||||
return m;
|
||||
}
|
||||
|
||||
private Map<String, Object> fakeDetail(String id) {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", id);
|
||||
m.put("title", "测试消息");
|
||||
m.put("content", "消息内容");
|
||||
m.put("type", 2);
|
||||
m.put("createtime", 123456789L);
|
||||
m.put("sender_id", "user-001");
|
||||
m.put("sender", "张三[001]");
|
||||
m.put("receivers", "李四[002]");
|
||||
m.put("receiver_ids", "user-002");
|
||||
m.put("document", null);
|
||||
m.put("params", null);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user