|
|
@@ -0,0 +1,111 @@
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.Notice;
|
|
|
+import com.etotem.cfc.mapper.NoticeMapper;
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Tag(name = "公告管理", description = "平台公告的增删改查")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/admin/notices")
|
|
|
+public class AdminNoticeController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private NoticeMapper noticeMapper;
|
|
|
+
|
|
|
+ @Operation(summary = "公告列表")
|
|
|
+ @PostMapping("/list")
|
|
|
+ public Result<List<Notice>> list(@RequestBody java.util.Map<String, Object> body) {
|
|
|
+ Integer page = body.get("page") != null ? Integer.valueOf(body.get("page").toString()) : 1;
|
|
|
+ Integer size = body.get("size") != null ? Integer.valueOf(body.get("size").toString()) : 20;
|
|
|
+ String status = body.get("status") != null ? body.get("status").toString() : null;
|
|
|
+ String type = body.get("type") != null ? body.get("type").toString() : null;
|
|
|
+
|
|
|
+ LambdaQueryWrapper<Notice> q = new LambdaQueryWrapper<>();
|
|
|
+ q.eq("status", "deleted").ne(1, 0);
|
|
|
+ if (status != null && !status.isEmpty()) q.eq(Notice::getStatus, status);
|
|
|
+ if (type != null && !type.isEmpty()) q.eq(Notice::getType, type);
|
|
|
+ q.orderByDesc(Notice::getCreatedAt);
|
|
|
+ q.last("LIMIT " + ((page - 1) * size) + "," + size);
|
|
|
+
|
|
|
+ List<Notice> list = noticeMapper.selectList(q);
|
|
|
+ return Result.success(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "全量公告列表(不分页)")
|
|
|
+ @PostMapping("/all")
|
|
|
+ public Result<List<Notice>> all(@RequestBody java.util.Map<String, Object> body) {
|
|
|
+ String status = body.get("status") != null ? body.get("status").toString() : "active";
|
|
|
+ LambdaQueryWrapper<Notice> q = new LambdaQueryWrapper<>();
|
|
|
+ q.eq(Notice::getStatus, status);
|
|
|
+ q.orderByDesc(Notice::getCreatedAt);
|
|
|
+ q.last("LIMIT 20");
|
|
|
+ return Result.success(noticeMapper.selectList(q));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "创建公告")
|
|
|
+ @PostMapping("/create")
|
|
|
+ public Result<Notice> create(@RequestBody Notice notice,
|
|
|
+ @RequestAttribute("userId") Long userId) {
|
|
|
+ if (notice.getTitle() == null || notice.getTitle().trim().isEmpty()) {
|
|
|
+ return Result.error("标题不能为空");
|
|
|
+ }
|
|
|
+ if (notice.getContent() == null || notice.getContent().trim().isEmpty()) {
|
|
|
+ return Result.error("内容不能为空");
|
|
|
+ }
|
|
|
+ if (notice.getType() == null || notice.getType().isEmpty()) {
|
|
|
+ notice.setType("platform");
|
|
|
+ }
|
|
|
+ notice.setStatus("active");
|
|
|
+ notice.setAdminId(userId);
|
|
|
+ notice.setCreatedAt(new Date());
|
|
|
+ notice.setUpdatedAt(new Date());
|
|
|
+ noticeMapper.insert(notice);
|
|
|
+ return Result.success(notice);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "更新公告")
|
|
|
+ @PostMapping("/update")
|
|
|
+ public Result<Notice> update(@RequestBody Notice notice) {
|
|
|
+ if (notice.getId() == null) return Result.error("id不能为空");
|
|
|
+ Notice existing = noticeMapper.selectById(notice.getId());
|
|
|
+ if (existing == null) return Result.error("公告不存在");
|
|
|
+ existing.setTitle(notice.getTitle());
|
|
|
+ existing.setContent(notice.getContent());
|
|
|
+ existing.setType(notice.getType());
|
|
|
+ existing.setUpdatedAt(new Date());
|
|
|
+ noticeMapper.updateById(existing);
|
|
|
+ return Result.success(existing);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "删除公告(软删)")
|
|
|
+ @PostMapping("/delete")
|
|
|
+ public Result<String> delete(@RequestBody java.util.Map<String, Object> body) {
|
|
|
+ Long id = Long.valueOf(body.get("id").toString());
|
|
|
+ Notice notice = noticeMapper.selectById(id);
|
|
|
+ if (notice == null) return Result.error("公告不存在");
|
|
|
+ notice.setStatus("deleted");
|
|
|
+ notice.setUpdatedAt(new Date());
|
|
|
+ noticeMapper.updateById(notice);
|
|
|
+ return Result.success("已删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Operation(summary = "置顶(将指定公告移到最前)")
|
|
|
+ @PostMapping("/pin")
|
|
|
+ public Result<String> pin(@RequestBody java.util.Map<String, Object> body) {
|
|
|
+ Long id = Long.valueOf(body.get("id").toString());
|
|
|
+ Notice notice = noticeMapper.selectById(id);
|
|
|
+ if (notice == null) return Result.error("公告不存在");
|
|
|
+ notice.setCreatedAt(new Date());
|
|
|
+ notice.setUpdatedAt(new Date());
|
|
|
+ noticeMapper.updateById(notice);
|
|
|
+ return Result.success("已置顶");
|
|
|
+ }
|
|
|
+}
|