zhangzeli
2021-11-09 b62fdf04bc9bd36d4992847128bfd40292c3cac1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cn.exrick.xboot.base.controller.manage;
 
import cn.exrick.xboot.core.common.constant.ActivitiConstant;
import cn.exrick.xboot.core.common.constant.CommonConstant;
import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.common.vo.SearchVo;
import cn.exrick.xboot.core.entity.Message;
import cn.exrick.xboot.core.entity.MessageSend;
import cn.exrick.xboot.core.entity.User;
import cn.exrick.xboot.core.service.MessageSendService;
import cn.exrick.xboot.core.service.MessageService;
import cn.exrick.xboot.core.service.UserService;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.List;
 
 
/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(tags = "消息内容管理接口")
@RequestMapping("/xboot/message")
@Transactional
public class MessageController {
 
    @Autowired
    private MessageService messageService;
 
    @Autowired
    private MessageSendService sendService;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
 
    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<Message>> getByCondition(Message message,
                                                SearchVo searchVo,
                                                PageVo pageVo) {
        if(pageVo.getPageSize()==0){
            pageVo.setPageSize(5);
        }
        if(StrUtil.isEmpty(pageVo.getOrder())){
            pageVo.setOrder("desc");
            pageVo.setSort("createTime");
        }
        Page<Message> page = messageService.findByCondition(message, searchVo, PageUtil.initPage(pageVo));
        page.forEach(e->{
            e.setContentText(HtmlUtil.cleanHtmlTag(e.getContent()));
        });
        return new ResultUtil<Page<Message>>().setData(page);
    }
 
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Message> get(@PathVariable String id) {
 
        Message message = messageService.get(id);
        message.setContentText(HtmlUtil.filter(message.getContent()));
        return new ResultUtil<Message>().setData(message);
    }
 
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加消息")
    public Result<Object> addMessage(Message message) {
 
        Message m = messageService.save(message);
        // 保存消息发送表
//        List<MessageSend> messageSends = new ArrayList<>();
//        if (CommonConstant.MESSAGE_RANGE_ALL.equals(message.getRange())) {
//            // 全体
//            List<User> allUser = userService.getAll();
//            allUser.forEach(u -> {
//                MessageSend ms = new MessageSend().setMessageId(m.getId()).setUserId(u.getId());
//                messageSends.add(ms);
//            });
//            sendService.saveOrUpdateAll(messageSends);
//            // 推送
//            messagingTemplate.convertAndSend("/topic/subscribe", "您收到了新的系统消息");
//        } else if (CommonConstant.MESSAGE_RANGE_USER.equals(message.getRange())) {
//            // 指定用户
//            for (String id : message.getUserIds()) {
//                MessageSend ms = new MessageSend().setMessageId(m.getId()).setUserId(id);
//                messageSends.add(ms);
//            }
//            sendService.saveOrUpdateAll(messageSends);
//            // 推送
//            for (String id : message.getUserIds()) {
//                messagingTemplate.convertAndSendToUser(id, "/queue/subscribe", "您收到了新的消息");
//            }
//        }
        return ResultUtil.success("添加成功");
    }
 
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑消息")
    public Result<Object> editMessage(Message message) {
 
        Message m = messageService.update(message);
        return ResultUtil.success("编辑成功");
    }
 
    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除消息")
    public Result<Object> delMessage(@RequestParam String[] ids) {
 
        for (String id : ids) {
//            if (ActivitiConstant.MESSAGE_PASS_ID.equals(id) || ActivitiConstant.MESSAGE_BACK_ID.equals(id) || ActivitiConstant.MESSAGE_DELEGATE_ID.equals(id)
//                    || ActivitiConstant.MESSAGE_TODO_ID.equals(id)) {
//                return ResultUtil.error("抱歉,无法删除工作流相关系统消息");
//            }
            messageService.delete(id);
            // 删除发送表
            //sendService.deleteByMessageId(id);
        }
        return ResultUtil.success("编辑成功");
    }
}