Explorar el Código

增加与ai大模型对话

wwh hace 1 día
padre
commit
8c4062e5e9

+ 16 - 2
huimv-employment/fe-api/src/main/java/com/huimv/employment/controller/mp/NotificationController.java

@@ -4,22 +4,25 @@ import com.huimv.employment.common.web.R;
4 4
 import com.huimv.employment.security.LoginUserHolder;
5 5
 import com.huimv.employment.service.notification.NotificationService;
6 6
 import com.huimv.employment.service.notification.dto.NotificationPageResponse;
7
+import com.huimv.employment.service.notification.dto.NotificationReadResponse;
7 8
 import io.swagger.v3.oas.annotations.Operation;
8 9
 import io.swagger.v3.oas.annotations.Parameter;
9 10
 import io.swagger.v3.oas.annotations.enums.ParameterIn;
10 11
 import io.swagger.v3.oas.annotations.security.SecurityRequirement;
11 12
 import io.swagger.v3.oas.annotations.tags.Tag;
12 13
 import org.springframework.web.bind.annotation.GetMapping;
14
+import org.springframework.web.bind.annotation.PathVariable;
15
+import org.springframework.web.bind.annotation.PostMapping;
13 16
 import org.springframework.web.bind.annotation.RequestMapping;
14 17
 import org.springframework.web.bind.annotation.RequestParam;
15 18
 import org.springframework.web.bind.annotation.RestController;
16 19
 
17 20
 /**
18
- * 消息与待办中心:当前登录用户查询本人通知
21
+ * 消息与待办中心:当前登录用户查询、标记已读
19 22
  */
20 23
 @RestController
21 24
 @RequestMapping("/api/v1/mp/notifications")
22
-@Tag(name = "消息与待办", description = "查询当前用户消息与待办")
25
+@Tag(name = "消息与待办", description = "查询当前用户消息与待办,标记已读")
23 26
 @SecurityRequirement(name = "Authorization")
24 27
 public class NotificationController {
25 28
 
@@ -51,4 +54,15 @@ public class NotificationController {
51 54
         Long userId = LoginUserHolder.require().getUserId();
52 55
         return R.ok(notificationService.listByUser(userId, notifyType, category, readFlag, handledFlag, page, size));
53 56
     }
57
+
58
+    @PostMapping("/{id}/read")
59
+    @Operation(summary = "标记消息已读",
60
+            description = "将当前用户名下指定消息的 read_flag 置为 true,并写入 read_time;已读则幂等返回。"
61
+                    + "同时返回最新 unread_count。")
62
+    public R<NotificationReadResponse> markRead(
63
+            @Parameter(name = "id", in = ParameterIn.PATH, description = "消息 ID", required = true)
64
+            @PathVariable("id") Long id) {
65
+        Long userId = LoginUserHolder.require().getUserId();
66
+        return R.ok(notificationService.markRead(userId, id));
67
+    }
54 68
 }

+ 39 - 0
huimv-employment/fe-service/src/main/java/com/huimv/employment/service/notification/NotificationService.java

@@ -2,12 +2,15 @@ package com.huimv.employment.service.notification;
2 2
 
3 3
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4 4
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
5
+import com.huimv.employment.common.exception.BizException;
6
+import com.huimv.employment.common.exception.ErrorCode;
5 7
 import com.huimv.employment.dao.entity.FeEmploymentOrder;
6 8
 import com.huimv.employment.dao.entity.FeNotification;
7 9
 import com.huimv.employment.dao.entity.FeSettlementOrder;
8 10
 import com.huimv.employment.dao.mapper.FeNotificationMapper;
9 11
 import com.huimv.employment.service.notification.dto.NotificationItemResponse;
10 12
 import com.huimv.employment.service.notification.dto.NotificationPageResponse;
13
+import com.huimv.employment.service.notification.dto.NotificationReadResponse;
11 14
 import org.slf4j.Logger;
12 15
 import org.slf4j.LoggerFactory;
13 16
 import org.springframework.stereotype.Service;
@@ -98,6 +101,42 @@ public class NotificationService {
98 101
         return response;
99 102
     }
100 103
 
104
+    /**
105
+     * 标记消息已读:更新 {@code read_flag=true}、{@code read_time}(幂等)。
106
+     */
107
+    @Transactional(rollbackFor = Exception.class)
108
+    public NotificationReadResponse markRead(Long userId, Long notificationId) {
109
+        if (userId == null) {
110
+            throw new BizException(ErrorCode.UNAUTHORIZED, "请先登录");
111
+        }
112
+        if (notificationId == null) {
113
+            throw new BizException(ErrorCode.BAD_REQUEST, "消息 ID 不能为空");
114
+        }
115
+        FeNotification existing = feNotificationMapper.selectById(notificationId);
116
+        if (existing == null) {
117
+            throw new BizException(ErrorCode.NOT_FOUND, "消息不存在");
118
+        }
119
+        if (existing.getUserId() == null || !userId.equals(existing.getUserId())) {
120
+            throw new BizException(ErrorCode.FORBIDDEN, "无权操作该消息");
121
+        }
122
+
123
+        if (!Boolean.TRUE.equals(existing.getReadFlag())) {
124
+            LocalDateTime now = LocalDateTime.now();
125
+            FeNotification update = new FeNotification();
126
+            update.setId(existing.getId());
127
+            update.setReadFlag(Boolean.TRUE);
128
+            update.setReadTime(now);
129
+            feNotificationMapper.updateById(update);
130
+            existing.setReadFlag(Boolean.TRUE);
131
+            existing.setReadTime(now);
132
+        }
133
+
134
+        NotificationReadResponse response = new NotificationReadResponse();
135
+        response.setItem(toItem(existing));
136
+        response.setUnreadCount(countUnread(userId));
137
+        return response;
138
+    }
139
+
101 140
     private long countUnread(Long userId) {
102 141
         Long count = feNotificationMapper.selectCount(new LambdaQueryWrapper<FeNotification>()
103 142
                 .eq(FeNotification::getUserId, userId)

+ 31 - 0
huimv-employment/fe-service/src/main/java/com/huimv/employment/service/notification/dto/NotificationReadResponse.java

@@ -0,0 +1,31 @@
1
+package com.huimv.employment.service.notification.dto;
2
+
3
+import com.fasterxml.jackson.annotation.JsonProperty;
4
+import io.swagger.v3.oas.annotations.media.Schema;
5
+
6
+@Schema(description = "消息已读结果")
7
+public class NotificationReadResponse {
8
+
9
+    @Schema(description = "已读后的消息详情")
10
+    private NotificationItemResponse item;
11
+
12
+    @JsonProperty("unread_count")
13
+    @Schema(description = "当前用户未读消息数")
14
+    private Long unreadCount;
15
+
16
+    public NotificationItemResponse getItem() {
17
+        return item;
18
+    }
19
+
20
+    public void setItem(NotificationItemResponse item) {
21
+        this.item = item;
22
+    }
23
+
24
+    public Long getUnreadCount() {
25
+        return unreadCount;
26
+    }
27
+
28
+    public void setUnreadCount(Long unreadCount) {
29
+        this.unreadCount = unreadCount;
30
+    }
31
+}