package space.anyi.serve.controller; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import space.anyi.serve.entity.Response; import space.anyi.serve.entity.auth.JwtUserDetails; import space.anyi.serve.entity.notification.NotificationVo; import space.anyi.serve.entity.notification.UnreadCountVo; import space.anyi.serve.service.NotificationService; import java.util.List; @Tag(name = "NotificationController", description = "系统通知") @RestController @RequestMapping("notifications") public class NotificationController { private final NotificationService notificationService; public NotificationController(NotificationService notificationService) { this.notificationService = notificationService; } @Operation(summary = "获取通知列表") @PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')") @GetMapping public Response> listNotifications(Authentication authentication) { JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal(); Long userId = details.getUser().getId(); return Response.ok(notificationService.listNotifications(userId)); } @Operation(summary = "获取未读数") @PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')") @GetMapping("unread-count") public Response getUnreadCount(Authentication authentication) { JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal(); Long userId = details.getUser().getId(); long count = notificationService.getUnreadCount(userId); return Response.ok(new UnreadCountVo(count)); } @Operation(summary = "标记已读") @PreAuthorize("hasAnyRole('ROLE_user', 'ROLE_expert', 'ROLE_admin')") @PutMapping("{id}/read") public Response markRead(@PathVariable Long id, Authentication authentication) { JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal(); Long userId = details.getUser().getId(); notificationService.markRead(id, userId); return Response.ok(); } }