Skip to content

Commit

Permalink
通知消息
Browse files Browse the repository at this point in the history
  • Loading branch information
lgt1126 committed Aug 25, 2021
1 parent 5d974ac commit c1070b3
Show file tree
Hide file tree
Showing 11 changed files with 550 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.zmops.iot.domain.messages;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author: yefei
**/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MessageBody {
private String msg;
private String type;
private List<Long> to;
private Map<String, Object> body;
private boolean persist = false;


public void addBody(String k, Object v) {
if (this.body == null) {
this.body = new HashMap<>();
}
this.body.put(k, v);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.zmops.iot.domain.messages;


import com.zmops.iot.constant.IdTypeConsts;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* @author yefei
*/

@EqualsAndHashCode(callSuper = false)
@Data
@Entity
@Table(name = "messages")
public class Messages {
@Id
private Integer id;
private Integer classify;
private String title;
private String content;
private Long clock;
private String module;
private Integer readed;
private Long userId;
}
6 changes: 6 additions & 0 deletions zeus-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<version>1.0-beta</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.zmops</groupId>
<artifactId>zeus-webapp</artifactId>
<version>1.0-beta</version>
<scope>compile</scope>
</dependency>
</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.zmops.iot.rest;

import com.alibaba.fastjson.JSON;
import com.zmops.iot.async.executor.Async;
import com.zmops.iot.async.wrapper.WorkerWrapper;
import com.zmops.iot.model.response.ResponseData;
import org.springframework.web.bind.annotation.*;
import com.zmops.iot.web.alarm.service.AlarmNoticeWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

Expand All @@ -16,6 +22,8 @@
@RequestMapping("/rest/device")
public class DeviceStatusWebhookController {

@Autowired
private AlarmNoticeWorker alarmNoticeWorker;

/**
* 在线状态 回调
Expand All @@ -29,6 +37,16 @@ public ResponseData deviceStatusWebhook(@RequestBody Map<String, String> params)

System.out.println(JSON.toJSONString(params));

WorkerWrapper<Map<String, String>, Boolean> alarmNoticeWork = WorkerWrapper.<Map<String, String>, Boolean>builder().id("alarmNoticeWork")
.worker(alarmNoticeWorker).param(params)
.build();

try {
Async.work(3000, alarmNoticeWork).awaitFinish();
} catch (Exception e) {
e.printStackTrace();
}

return ResponseData.success("OK");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void configure(WebSecurity web) throws Exception {
.and().ignoring().antMatchers(HttpMethod.GET,
"/assets/**",
"/favicon.ico",
"/activiti-editor/**"
"/activiti-editor/**","/websocket/alarm/**"
);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.zmops.iot.web.alarm.controller;


import com.zmops.iot.domain.messages.Messages;
import com.zmops.iot.model.page.Pager;
import com.zmops.iot.web.alarm.dto.param.MessageParam;
import com.zmops.iot.web.alarm.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
* @author yefei
**/
@RestController
@RequestMapping("/message")
public class MessageController {

@Autowired
MessageService messageService;

/**
* 消息分页列表
*
* @param messageParam
* @return
*/
@RequestMapping("/getMessageByPage")
public Pager getMessageByPage(@RequestBody MessageParam messageParam) {
return messageService.list(messageParam);
}

/**
* 消息详情
*
* @param id
* @return
*/
@RequestMapping("/info")
public Messages info(@RequestParam("id") Integer id) {
return messageService.info(id);
}


/**
* 消息置为已读
*
* @param messageVo
* @return
*/
@RequestMapping("/read")
public List<Integer> read(@RequestBody MessageParam messageVo) {
return messageService.read(messageVo);
}

/**
* 未读消息数
*
* @return
*/
@GetMapping("/num")
public Map<String, Object> unReadNum() {
return messageService.unReadNum();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.zmops.iot.web.alarm.dto.param;

import com.zmops.iot.web.sys.dto.param.BaseQueryParam;
import lombok.Data;

import java.util.List;

/**
* @author yefei
**/
@Data
public class MessageParam extends BaseQueryParam {
private Integer readed;

private List<Integer> ids;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.zmops.iot.web.alarm.service;

import com.zmops.iot.async.callback.IWorker;
import com.zmops.iot.async.wrapper.WorkerWrapper;
import com.zmops.iot.domain.messages.MessageBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author yefei
**/
@Service
public class AlarmNoticeWorker implements IWorker<Map<String, String>, Boolean> {

@Autowired
MessageService messageService;

@Override
public Boolean action(Map<String, String> alarmInfo, Map<String, WorkerWrapper<?, ?>> allWrappers) {
Map<String, Object> alarmInfo2 = new HashMap<>(alarmInfo);
messageService.push(buildMessage(alarmInfo2));
return null;
}

private MessageBody buildMessage(Map<String, Object> alarmInfo) {

return MessageBody.builder().msg("告警消息").body(alarmInfo).build();
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.zmops.iot.web.alarm.service;

import com.alibaba.fastjson.JSON;
import com.zmops.iot.core.auth.context.LoginContextHolder;
import com.zmops.iot.core.auth.model.LoginUser;
import com.zmops.iot.domain.messages.MessageBody;
import com.zmops.iot.domain.messages.Messages;
import com.zmops.iot.domain.messages.query.QMessages;
import com.zmops.iot.domain.sys.SysUser;
import com.zmops.iot.domain.sys.query.QSysUser;
import com.zmops.iot.model.page.Pager;
import com.zmops.iot.util.ToolUtil;
import com.zmops.iot.web.alarm.dto.param.MessageParam;
import com.zmops.iot.web.alarm.socket.WebSocketServer;
import io.ebean.DB;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author yefei
* <p>
* 消息服务
**/
@Service
public class MessageService {

protected final static int sys = 1;

/**
* 发送消息
*
* @param body
*/
public void push(MessageBody body) {
Objects.requireNonNull(body.getMsg());
if (body.isPersist()) {
List<Long> tos;
if (!CollectionUtils.isEmpty(body.getTo())) {
tos = body.getTo();
} else {
List<SysUser> userList = new QSysUser().findList();
tos = userList.parallelStream().map(SysUser::getUserId).collect(Collectors.toList());
}
tos.forEach(to -> {
Messages messages = new Messages();
messages.setClassify(sys);
messages.setTitle(body.getMsg());
messages.setUserId(to);
String content = "";
if (ToolUtil.isNotEmpty(body.getBody())) {
content = JSON.toJSONString(body.getBody());
messages.setContent(content);
}
messages.setClock(System.currentTimeMillis() / 1000);
saveMessage(messages);
});
}
body.addBody("classify", sys);

if (!CollectionUtils.isEmpty(body.getTo())) {
body.getTo().forEach(to -> {
WebSocketServer.sendMessageTo(JSON.toJSONString(body), to + "");
});
} else {
WebSocketServer.sendMessageToAll(JSON.toJSONString(body));
}
}

/**
* 消息列表
*
* @param messageParam
* @return
*/
public Pager<Messages> list(MessageParam messageParam) {
LoginUser user = LoginContextHolder.getContext().getUser();
QMessages qMessages = new QMessages();
qMessages.userId.eq(user.getId());

if (messageParam.getReaded() != null) {
qMessages.readed.eq(messageParam.getReaded());
}
qMessages.orderBy(" readed asc, clock desc");
List<Messages> list = qMessages.setFirstRow((messageParam.getPage() - 1) * messageParam.getMaxRow())
.setMaxRows(messageParam.getMaxRow()).findList();

return new Pager<>(list, qMessages.findCount());
}

/**
* 读消息
*
* @param messageParam
* @return
*/
public List<Integer> read(MessageParam messageParam) {
LoginUser user = LoginContextHolder.getContext().getUser();
Messages messages = new Messages();
messages.setReaded(1);
QMessages qMessages = new QMessages();
if (!CollectionUtils.isEmpty(messageParam.getIds())) {
qMessages.id.in(messageParam.getIds());
}
qMessages.userId.eq(user.getId());

new QMessages().asUpdate().set("readed", 1).update();
return messageParam.getIds();
}

/**
* 未读消息数
*
* @return
*/
public Map<String, Object> unReadNum() {
LoginUser user = LoginContextHolder.getContext().getUser();
Map<String, Object> map = new HashMap<>();
int count = new QMessages().readed.eq(0).userId.eq(user.getId()).findCount();
map.put("count", count);
return map;
}

/**
* 消息详情
*
* @param id
* @return
*/
public Messages info(Integer id) {
return new QMessages().id.eq(id).findOne();
}


private void saveMessage(Messages messages) {
DB.save(messages);
}

}
Loading

0 comments on commit c1070b3

Please sign in to comment.