潘志宝
2024-12-16 df99e46312fdd5ee830f1451e478f6658e09f9ed
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.controller.admin.notice;
H 2
3 import cn.hutool.core.lang.Assert;
4 import com.iailab.framework.common.enums.UserTypeEnum;
5 import com.iailab.framework.common.pojo.CommonResult;
6 import com.iailab.framework.common.pojo.PageResult;
7 import com.iailab.framework.common.util.object.BeanUtils;
8 import com.iailab.module.infra.api.websocket.WebSocketSenderApi;
9 import com.iailab.module.system.controller.admin.notice.vo.NoticePageReqVO;
10 import com.iailab.module.system.controller.admin.notice.vo.NoticeRespVO;
11 import com.iailab.module.system.controller.admin.notice.vo.NoticeSaveReqVO;
12 import com.iailab.module.system.dal.dataobject.notice.NoticeDO;
13 import com.iailab.module.system.service.notice.NoticeService;
14 import io.swagger.v3.oas.annotations.Operation;
15 import io.swagger.v3.oas.annotations.Parameter;
16 import io.swagger.v3.oas.annotations.tags.Tag;
17 import org.springframework.security.access.prepost.PreAuthorize;
18 import org.springframework.validation.annotation.Validated;
19 import org.springframework.web.bind.annotation.*;
20
21 import javax.annotation.Resource;
22 import javax.validation.Valid;
23
24 import static com.iailab.framework.common.pojo.CommonResult.success;
25
26 @Tag(name = "管理后台 - 通知公告")
27 @RestController
28 @RequestMapping("/system/notice")
29 @Validated
30 public class NoticeController {
31
32     @Resource
33     private NoticeService noticeService;
34
35     @Resource
36     private WebSocketSenderApi webSocketSenderApi;
37
38     @PostMapping("/create")
39     @Operation(summary = "创建通知公告")
40     @PreAuthorize("@ss.hasPermission('system:notice:create')")
41     public CommonResult<Long> createNotice(@Valid @RequestBody NoticeSaveReqVO createReqVO) {
42         Long noticeId = noticeService.createNotice(createReqVO);
43         return success(noticeId);
44     }
45
46     @PutMapping("/update")
47     @Operation(summary = "修改通知公告")
48     @PreAuthorize("@ss.hasPermission('system:notice:update')")
49     public CommonResult<Boolean> updateNotice(@Valid @RequestBody NoticeSaveReqVO updateReqVO) {
50         noticeService.updateNotice(updateReqVO);
51         return success(true);
52     }
53
54     @DeleteMapping("/delete")
55     @Operation(summary = "删除通知公告")
56     @Parameter(name = "id", description = "编号", required = true, example = "1024")
57     @PreAuthorize("@ss.hasPermission('system:notice:delete')")
58     public CommonResult<Boolean> deleteNotice(@RequestParam("id") Long id) {
59         noticeService.deleteNotice(id);
60         return success(true);
61     }
62
63     @GetMapping("/page")
64     @Operation(summary = "获取通知公告列表")
65     @PreAuthorize("@ss.hasPermission('system:notice:query')")
66     public CommonResult<PageResult<NoticeRespVO>> getNoticePage(@Validated NoticePageReqVO pageReqVO) {
67         PageResult<NoticeDO> pageResult = noticeService.getNoticePage(pageReqVO);
68         return success(BeanUtils.toBean(pageResult, NoticeRespVO.class));
69     }
70
71     @GetMapping("/get")
72     @Operation(summary = "获得通知公告")
73     @Parameter(name = "id", description = "编号", required = true, example = "1024")
74     @PreAuthorize("@ss.hasPermission('system:notice:query')")
75     public CommonResult<NoticeRespVO> getNotice(@RequestParam("id") Long id) {
76         NoticeDO notice = noticeService.getNotice(id);
77         return success(BeanUtils.toBean(notice, NoticeRespVO.class));
78     }
79
80     @PostMapping("/push")
81     @Operation(summary = "推送通知公告", description = "只发送给 websocket 连接在线的用户")
82     @Parameter(name = "id", description = "编号", required = true, example = "1024")
83     @PreAuthorize("@ss.hasPermission('system:notice:update')")
84     public CommonResult<Boolean> push(@RequestParam("id") Long id) {
85         NoticeDO notice = noticeService.getNotice(id);
86         Assert.notNull(notice, "公告不能为空");
87         // 通过 websocket 推送给在线的用户
88         webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), "notice-push", notice);
89         return success(true);
90     }
91
92 }