公告修改

This commit is contained in:
zyh
2025-08-29 23:04:47 +08:00
parent 19d9fa3e9e
commit 2c72161efa
7 changed files with 141 additions and 8 deletions

View File

@@ -6,11 +6,13 @@ import com.gameplatform.server.model.dto.admin.AnnouncementResponse;
import com.gameplatform.server.model.dto.common.PageResult; import com.gameplatform.server.model.dto.common.PageResult;
import com.gameplatform.server.model.entity.admin.Announcement; import com.gameplatform.server.model.entity.admin.Announcement;
import com.gameplatform.server.service.admin.AnnouncementService; import com.gameplatform.server.service.admin.AnnouncementService;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@@ -24,8 +26,8 @@ public class AnnouncementController {
private AnnouncementService announcementService; private AnnouncementService announcementService;
@PostMapping @PostMapping
@Operation(summary = "创建公告", description = "创建新的系统公告") @Operation(summary = "创建公告", description = "创建新的系统公告belongId会自动从JWT token中获取")
public ResponseEntity<Object> createAnnouncement(@RequestBody AnnouncementRequest request) { public ResponseEntity<Object> createAnnouncement(@RequestBody AnnouncementRequest request, Authentication authentication) {
if (request.getTitle() == null || request.getTitle().trim().isEmpty()) { if (request.getTitle() == null || request.getTitle().trim().isEmpty()) {
return ResponseEntity.badRequest().body(new Object() { return ResponseEntity.badRequest().body(new Object() {
public final boolean success = false; public final boolean success = false;
@@ -44,6 +46,33 @@ public class AnnouncementController {
request.setEnabled(false); // 默认禁用 request.setEnabled(false); // 默认禁用
} }
// 从JWT token中获取当前用户ID
if (authentication == null) {
return ResponseEntity.badRequest().body(new Object() {
public final boolean success = false;
public final String message = "用户未认证";
});
}
Claims claims = (Claims) authentication.getDetails();
if (claims == null) {
return ResponseEntity.badRequest().body(new Object() {
public final boolean success = false;
public final String message = "无法获取用户信息";
});
}
Long userId = claims.get("userId", Long.class);
if (userId == null) {
return ResponseEntity.badRequest().body(new Object() {
public final boolean success = false;
public final String message = "无法获取用户ID";
});
}
// 设置belongId为当前用户ID
request.setBelongId(userId.intValue());
Announcement announcement = AnnouncementConverter.toEntity(request); Announcement announcement = AnnouncementConverter.toEntity(request);
boolean success = announcementService.createAnnouncement(announcement); boolean success = announcementService.createAnnouncement(announcement);
final boolean finalSuccess = success; final boolean finalSuccess = success;
@@ -60,13 +89,20 @@ public class AnnouncementController {
public ResponseEntity<PageResult<AnnouncementResponse>> getAnnouncementList( public ResponseEntity<PageResult<AnnouncementResponse>> getAnnouncementList(
@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page, @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page,
@Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") int size, @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") int size,
@Parameter(description = "按启用状态筛选,不传则获取全部") @RequestParam(required = false) Boolean enabled) { @Parameter(description = "按启用状态筛选,不传则获取全部") @RequestParam(required = false) Boolean enabled,
@Parameter(description = "按归属ID筛选") @RequestParam(required = false) Integer belongId) {
int offset = (page - 1) * size; int offset = (page - 1) * size;
List<Announcement> announcements; List<Announcement> announcements;
long total; long total;
if (enabled != null) { if (belongId != null && enabled != null) {
announcements = announcementService.getAnnouncementsByBelongIdAndEnabled(belongId, enabled, size, offset);
total = announcementService.getAnnouncementCountByBelongIdAndEnabled(belongId, enabled);
} else if (belongId != null) {
announcements = announcementService.getAnnouncementsByBelongId(belongId, size, offset);
total = announcementService.getAnnouncementCountByBelongId(belongId);
} else if (enabled != null) {
announcements = announcementService.getAnnouncementsByEnabled(enabled, size, offset); announcements = announcementService.getAnnouncementsByEnabled(enabled, size, offset);
total = announcementService.getAnnouncementCountByEnabled(enabled); total = announcementService.getAnnouncementCountByEnabled(enabled);
} else { } else {
@@ -99,10 +135,11 @@ public class AnnouncementController {
} }
@PutMapping("/{id}") @PutMapping("/{id}")
@Operation(summary = "更新公告", description = "更新指定ID的公告信息") @Operation(summary = "更新公告", description = "更新指定ID的公告信息belongId会自动从JWT token中获取")
public ResponseEntity<Object> updateAnnouncement( public ResponseEntity<Object> updateAnnouncement(
@Parameter(description = "公告ID", example = "1") @PathVariable Long id, @Parameter(description = "公告ID", example = "1") @PathVariable Long id,
@RequestBody AnnouncementRequest request) { @RequestBody AnnouncementRequest request,
Authentication authentication) {
// 检查公告是否存在 // 检查公告是否存在
if (!announcementService.announcementExists(id)) { if (!announcementService.announcementExists(id)) {
@@ -123,6 +160,17 @@ public class AnnouncementController {
}); });
} }
// 从JWT token中获取当前用户ID
if (authentication != null) {
Claims claims = (Claims) authentication.getDetails();
if (claims != null) {
Long userId = claims.get("userId", Long.class);
if (userId != null) {
request.setBelongId(userId.intValue());
}
}
}
Announcement announcement = AnnouncementConverter.toEntity(request); Announcement announcement = AnnouncementConverter.toEntity(request);
announcement.setId(id); announcement.setId(id);
boolean success = announcementService.updateAnnouncement(announcement); boolean success = announcementService.updateAnnouncement(announcement);
@@ -178,8 +226,14 @@ public class AnnouncementController {
@GetMapping("/enabled") @GetMapping("/enabled")
@Operation(summary = "获取启用的公告", description = "获取所有启用状态的公告,用于前端显示") @Operation(summary = "获取启用的公告", description = "获取所有启用状态的公告,用于前端显示")
public ResponseEntity<List<AnnouncementResponse>> getEnabledAnnouncements() { public ResponseEntity<List<AnnouncementResponse>> getEnabledAnnouncements(
List<Announcement> announcements = announcementService.getEnabledAnnouncements(); @Parameter(description = "按归属ID筛选") @RequestParam(required = false) Integer belongId) {
List<Announcement> announcements;
if (belongId != null) {
announcements = announcementService.getEnabledAnnouncementsByBelongId(belongId);
} else {
announcements = announcementService.getEnabledAnnouncements();
}
List<AnnouncementResponse> responses = announcements.stream() List<AnnouncementResponse> responses = announcements.stream()
.map(AnnouncementConverter::toResponse) .map(AnnouncementConverter::toResponse)
.toList(); .toList();

View File

@@ -29,4 +29,18 @@ public interface AnnouncementMapper extends BaseMapper<Announcement> {
long countByEnabled(@Param("enabled") Boolean enabled); long countByEnabled(@Param("enabled") Boolean enabled);
int updateEnabled(@Param("id") Long id, @Param("enabled") Boolean enabled); int updateEnabled(@Param("id") Long id, @Param("enabled") Boolean enabled);
List<Announcement> findByBelongId(@Param("belongId") Integer belongId,
@Param("size") int size,
@Param("offset") int offset);
long countByBelongId(@Param("belongId") Integer belongId);
List<Announcement> findByBelongIdAndEnabled(@Param("belongId") Integer belongId,
@Param("enabled") Boolean enabled,
@Param("size") int size,
@Param("offset") int offset);
long countByBelongIdAndEnabled(@Param("belongId") Integer belongId,
@Param("enabled") Boolean enabled);
} }

View File

@@ -17,6 +17,7 @@ public class AnnouncementConverter {
announcement.setContent(request.getContent()); announcement.setContent(request.getContent());
announcement.setEnabled(request.getEnabled()); announcement.setEnabled(request.getEnabled());
announcement.setJumpUrl(request.getJumpUrl()); announcement.setJumpUrl(request.getJumpUrl());
announcement.setBelongId(request.getBelongId());
return announcement; return announcement;
} }
@@ -37,6 +38,7 @@ public class AnnouncementConverter {
response.setJumpUrl(announcement.getJumpUrl()); response.setJumpUrl(announcement.getJumpUrl());
response.setCreatedAt(announcement.getCreatedAt()); response.setCreatedAt(announcement.getCreatedAt());
response.setUpdatedAt(announcement.getUpdatedAt()); response.setUpdatedAt(announcement.getUpdatedAt());
response.setBelongId(announcement.getBelongId());
return response; return response;
} }

View File

@@ -17,6 +17,9 @@ public class AnnouncementRequest {
@Schema(description = "跳转链接", example = "https://example.com") @Schema(description = "跳转链接", example = "https://example.com")
private String jumpUrl; private String jumpUrl;
// belongId字段保留用于内部设置但不在API文档中暴露
private Integer belongId;
public String getTitle() { public String getTitle() {
return title; return title;
} }
@@ -48,4 +51,12 @@ public class AnnouncementRequest {
public void setJumpUrl(String jumpUrl) { public void setJumpUrl(String jumpUrl) {
this.jumpUrl = jumpUrl; this.jumpUrl = jumpUrl;
} }
public Integer getBelongId() {
return belongId;
}
public void setBelongId(Integer belongId) {
this.belongId = belongId;
}
} }

View File

@@ -27,6 +27,9 @@ public class AnnouncementResponse {
@Schema(description = "更新时间") @Schema(description = "更新时间")
private LocalDateTime updatedAt; private LocalDateTime updatedAt;
@Schema(description = "归属ID", example = "1")
private Integer belongId;
public Long getId() { public Long getId() {
return id; return id;
} }
@@ -82,4 +85,12 @@ public class AnnouncementResponse {
public void setUpdatedAt(LocalDateTime updatedAt) { public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt; this.updatedAt = updatedAt;
} }
public Integer getBelongId() {
return belongId;
}
public void setBelongId(Integer belongId) {
this.belongId = belongId;
}
} }

View File

@@ -26,6 +26,9 @@ public class Announcement {
@TableField("updated_at") @TableField("updated_at")
private LocalDateTime updatedAt; private LocalDateTime updatedAt;
@TableField("belong_id")
private Integer belongId;
public Long getId() { return id; } public Long getId() { return id; }
public void setId(Long id) { this.id = id; } public void setId(Long id) { this.id = id; }
@@ -46,4 +49,7 @@ public class Announcement {
public LocalDateTime getUpdatedAt() { return updatedAt; } public LocalDateTime getUpdatedAt() { return updatedAt; }
public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; } public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }
public Integer getBelongId() { return belongId; }
public void setBelongId(Integer belongId) { this.belongId = belongId; }
} }

View File

@@ -91,4 +91,39 @@ public class AnnouncementService {
public boolean announcementExists(Long id) { public boolean announcementExists(Long id) {
return getAnnouncementById(id) != null; return getAnnouncementById(id) != null;
} }
/**
* 根据归属ID获取公告分页
*/
public List<Announcement> getAnnouncementsByBelongId(Integer belongId, int size, int offset) {
return announcementMapper.findByBelongId(belongId, size, offset);
}
/**
* 根据归属ID获取公告数量
*/
public long getAnnouncementCountByBelongId(Integer belongId) {
return announcementMapper.countByBelongId(belongId);
}
/**
* 根据归属ID和启用状态获取公告分页
*/
public List<Announcement> getAnnouncementsByBelongIdAndEnabled(Integer belongId, Boolean enabled, int size, int offset) {
return announcementMapper.findByBelongIdAndEnabled(belongId, enabled, size, offset);
}
/**
* 根据归属ID和启用状态获取公告数量
*/
public long getAnnouncementCountByBelongIdAndEnabled(Integer belongId, Boolean enabled) {
return announcementMapper.countByBelongIdAndEnabled(belongId, enabled);
}
/**
* 获取指定归属ID的启用公告用于前端显示
*/
public List<Announcement> getEnabledAnnouncementsByBelongId(Integer belongId) {
return announcementMapper.findByBelongIdAndEnabled(belongId, true, 10, 0); // 最多返回10条启用的公告
}
} }