Просмотр исходного кода

feat(notification.center.view): S-cfc-381 实现分页与消息不存在 404

- /api/notification/list:支持 pageNum/pageSize 入参与 IPage 分页载体返回(total/pageNum/pageSize),兼顾原有单参数 LIMIT 100 调用语义
- /api/notification/read:消息 id/ids 不存在 → 返回 404(Result.notFound),不再静默 success

契约:NotificationCenterViewContractTest 9/9 通过(RED 缺口 ①分页/②不存在404 转 GREEN)
Administrator 3 дней назад
Родитель
Сommit
1edacf510d

+ 51 - 4
cfc-backend/src/main/java/com/etotem/cfc/controller/UserNotificationController.java

@@ -1,6 +1,8 @@
 package com.etotem.cfc.controller;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.etotem.cfc.common.Result;
 import com.etotem.cfc.entity.UserNotification;
 import com.etotem.cfc.mapper.UserNotificationMapper;
@@ -19,6 +21,9 @@ import java.util.Map;
 
 /**
  * 用户通知 — 改版v1(报告完成、奖励到账、物流等站内通知)
+ *
+ * <p>能力键 {@code notification.center.view}(SC-P-78):消息中心列表 + 已读。
+ * 契约见 {@code NotificationCenterViewContractTest}。</p>
  */
 @Tag(name = "用户通知", description = "站内通知列表与已读")
 @RestController
@@ -28,8 +33,42 @@ public class UserNotificationController {
     @Resource
     private UserNotificationMapper userNotificationMapper;
 
-    @Operation(summary = "通知列表(未读优先,倒序)")
+    /**
+     * 通知列表(未读优先,倒序,分页)。
+     *
+     * <p>入参:{@code pageNum}/{@code pageSize}(缺省 1/100,向后兼容旧调用);
+     * 返回:{@code IPage} 分页载体(含 {@code total/pageNum/pageSize})。</p>
+     */
+    @Operation(summary = "通知列表(未读优先,倒序,分页)")
     @PostMapping("/list")
+    public Result<IPage<UserNotification>> list(@RequestAttribute("userId") Long userId,
+                                                @RequestBody(required = false) Map<String, Object> params) {
+        long pageNum = 1;
+        long pageSize = 100;
+        if (params != null) {
+            Object pn = params.get("pageNum");
+            Object ps = params.get("pageSize");
+            if (pn != null) {
+                pageNum = Long.parseLong(pn.toString());
+            }
+            if (ps != null) {
+                pageSize = Long.parseLong(ps.toString());
+            }
+        }
+        Page<UserNotification> page = new Page<>(pageNum, pageSize);
+        LambdaQueryWrapper<UserNotification> wrapper = new LambdaQueryWrapper<UserNotification>()
+                .eq(UserNotification::getUserId, userId)
+                .orderByAsc(UserNotification::getIsRead)
+                .orderByDesc(UserNotification::getCreatedAt);
+        return Result.success(userNotificationMapper.selectPage(page, wrapper));
+    }
+
+    /**
+     * 通知列表(未读优先,倒序,LIMIT 100,扁平列表)。
+     *
+     * <p>兼容单参数调用:返回未分页扁平数组(保留原契约 §4.61.7 的 LIMIT 100 语义)。</p>
+     */
+    @Operation(summary = "通知列表(未读优先,倒序,LIMIT 100,扁平列表)")
     public Result<List<UserNotification>> list(@RequestAttribute("userId") Long userId) {
         List<UserNotification> list = userNotificationMapper.selectList(
                 new LambdaQueryWrapper<UserNotification>()
@@ -48,14 +87,22 @@ public class UserNotificationController {
         Object idsObj = params != null ? params.get("ids") : null;
         if (idObj != null) {
             UserNotification n = userNotificationMapper.selectById(Long.valueOf(idObj.toString()));
-            if (n != null && n.getUserId().equals(userId)) {
+            if (n == null) {
+                // 消息不存在 → 404(TC-P-78-E「不存在 404」)
+                return Result.notFound("消息不存在");
+            }
+            if (n.getUserId().equals(userId)) {
                 n.setIsRead(1);
                 userNotificationMapper.updateById(n);
             }
         } else if (idsObj != null) {
             for (Object o : (List<?>) idsObj) {
                 UserNotification n = userNotificationMapper.selectById(Long.valueOf(o.toString()));
-                if (n != null && n.getUserId().equals(userId)) {
+                if (n == null) {
+                    // 消息不存在 → 404(TC-P-78-E「不存在 404」)
+                    return Result.notFound("消息不存在");
+                }
+                if (n.getUserId().equals(userId)) {
                     n.setIsRead(1);
                     userNotificationMapper.updateById(n);
                 }
@@ -73,4 +120,4 @@ public class UserNotificationController {
         }
         return Result.success(true);
     }
-}
+}