forked from zmops/zeus-iot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
550 additions
and
2 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
zeus-common/src/main/java/com/zmops/iot/domain/messages/MessageBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
zeus-common/src/main/java/com/zmops/iot/domain/messages/Messages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
zeus-webapp/src/main/java/com/zmops/iot/web/alarm/controller/MessageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
zeus-webapp/src/main/java/com/zmops/iot/web/alarm/dto/param/MessageParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
36 changes: 36 additions & 0 deletions
36
zeus-webapp/src/main/java/com/zmops/iot/web/alarm/service/AlarmNoticeWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
|
||
|
||
} |
144 changes: 144 additions & 0 deletions
144
zeus-webapp/src/main/java/com/zmops/iot/web/alarm/service/MessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.