提交 33947361 authored 作者: xc's avatar xc

xc

上级 bb5333b1
......@@ -79,6 +79,11 @@
<artifactId>efs-ftp</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
......
package com.zjty.efs.bus.Dao;
import com.zjty.efs.bus.entity.RedDot;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ReddotDao extends JpaRepository<RedDot, String> {
}
......@@ -77,7 +77,6 @@ public class NoticeController {
}else {
data = noticeService.getListByStatus(user, status, pageNum, pageSize,type);
}
return ResponseEntity.ok(data);
}
......
package com.zjty.efs.bus.entity;
import com.zjty.efs.misc.config.AutoDocument;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@AutoDocument
//@ApiModel(value = "通知", description = "通知实体类")
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name = "reddot")
public class RedDot {
/**
* id
*/
@ApiModelProperty(value = "id", example = "1")
@Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private String id;
/**
* 标题
*/
@ApiModelProperty(value = "notice", example = "验收通知", dataType = "int", name = "标题")
@Column(name = "notice")
private int notice;
/**
* 收件人列表
*/
@ApiModelProperty(value = "attention", example = "1", dataType = "int", name = "收件人")
@Column(name = "attention")
private int attention;
}
......@@ -35,4 +35,10 @@ public interface AttentionService {
* @return
*/
int getRedDot(String userId);
/**
* websokect发送信息
* @param userId 用户编号
*/
void sendMessage(String userId);
}
package com.zjty.efs.bus.service;
import com.zjty.efs.bus.entity.RedDot;
public interface ReddotService {
RedDot findByUserId(String userId);
boolean addReddot(RedDot redDot);
}
package com.zjty.efs.bus.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.zjty.efs.bus.Dao.AttentionDao;
import com.zjty.efs.bus.entity.Attention;
import com.zjty.efs.bus.entity.Paging;
import com.zjty.efs.bus.entity.RedDot;
import com.zjty.efs.bus.service.AttentionService;
import com.zjty.efs.bus.service.NoticeService;
import com.zjty.efs.bus.service.ReddotService;
import com.zjty.efs.bus.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
......@@ -18,6 +23,11 @@ import java.util.List;
public class AttentionServiceImpl implements AttentionService {
@Autowired
AttentionDao attentionDao;
@Autowired
WebSocketServer webSocketServer;
@Autowired
ReddotService reddotService;
@Override
public Paging<Attention> getAttentionList(String userId, int pageNum, int pageSize, int staus) {
try {
......@@ -48,6 +58,7 @@ public class AttentionServiceImpl implements AttentionService {
public boolean addAttention(Attention attention) {
try{
attentionDao.save(attention);
sendMessage(attention.getUserId());
return true;
}catch (Exception e){
e.printStackTrace();
......@@ -59,10 +70,13 @@ public class AttentionServiceImpl implements AttentionService {
@Transactional
public boolean updateStatus(List<Attention> attentions) {
try{
String userId = "";
for(Attention attention:attentions){
userId = attention.getUserId();
attention.setStatus(1);
attentionDao.save(attention);
}
sendMessage(userId);
return true;
}catch (Exception e){
e.printStackTrace();
......@@ -77,4 +91,16 @@ public class AttentionServiceImpl implements AttentionService {
return attentions.size();
}
@Override
public void sendMessage(String userId){
int attentionNum = attentionDao.findOutByUserIdAndStatus(userId,0).size();
RedDot redDot1 = reddotService.findByUserId(userId);
if (redDot1 == null){
redDot1 = new RedDot();
}
redDot1.setAttention(attentionNum);
reddotService.addReddot(redDot1);
webSocketServer.sendData(userId, JSONObject.toJSONString(redDot1));
}
}
......@@ -4,12 +4,11 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.zjty.efs.bus.Dao.NoticeDao;
import com.zjty.efs.bus.entity.Addressee;
import com.zjty.efs.bus.entity.Attention;
import com.zjty.efs.bus.entity.Notice;
import com.zjty.efs.bus.entity.Paging;
import com.zjty.efs.bus.entity.*;
import com.zjty.efs.bus.service.AttentionService;
import com.zjty.efs.bus.service.NoticeService;
import com.zjty.efs.bus.service.ReddotService;
import com.zjty.efs.bus.websocket.WebSocketServer;
import com.zjty.efs.log.subject.entity.EfsLog;
import com.zjty.efs.log.tool.EfsLogUtil;
import com.zjty.efs.user.subject.entity.UserDo;
......@@ -22,6 +21,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.websocket.Session;
import java.lang.reflect.Array;
import java.util.*;
......@@ -36,6 +36,10 @@ public class NoticeServiceImpl implements NoticeService {
UserService userService;
@Autowired
AttentionService attentionService;
@Autowired
ReddotService reddotService;
@Override
@Transactional
......@@ -52,8 +56,6 @@ public class NoticeServiceImpl implements NoticeService {
notice.setName(userDo.getName());
notice.setLabel(0);
if (notice.getType() == 2){
//保存为草稿、、、
List<Addressee> addresseeList = new ArrayList<>();
......@@ -122,8 +124,14 @@ public class NoticeServiceImpl implements NoticeService {
attention.setData(data2);
attention.setUserId(add.getId());
attention.setNoticeId(notice2.getId());
attentionService.addAttention(attention);
int noticeNum = noticeDao.findOutByReceiverAndStatus(add.getId(),0).size();
System.out.println("-------------------------"+noticeNum);
RedDot redDot = new RedDot();
redDot.setId(add.getId());
redDot.setNotice(noticeNum);
reddotService.addReddot(redDot);
attentionService.addAttention(attention);
}
notice.setAddressee(add2);
......@@ -198,6 +206,12 @@ public class NoticeServiceImpl implements NoticeService {
String data = "[" +userDo.getUnit() + "_" + userDo.getName()
+ "]查看了《" + notice.getTitle() + "》";
efsLogUtil.addLog(new EfsLog(null, userDo.getId(), data, new Date()));
int noticeNum = noticeDao.findOutByReceiverAndStatus(userId,0).size();
RedDot redDot = new RedDot();
redDot.setId(userId);
redDot.setNotice(noticeNum);
reddotService.addReddot(redDot);
attentionService.sendMessage(userId);
return notice;
}catch (Exception e){
......@@ -253,7 +267,13 @@ public class NoticeServiceImpl implements NoticeService {
}
data = data + "为已读";
int noticeNum = noticeDao.findOutByReceiverAndStatus(userId,0).size();
RedDot redDot = new RedDot();
redDot.setId(userId);
redDot.setNotice(noticeNum);
reddotService.addReddot(redDot);
attentionService.sendMessage(userId);
efsLogUtil.addLog(new EfsLog(null, userDo.getId(), data, new Date()));
return true;
}catch (Exception e){
......
package com.zjty.efs.bus.service.impl;
import com.zjty.efs.bus.Dao.ReddotDao;
import com.zjty.efs.bus.entity.RedDot;
import com.zjty.efs.bus.service.ReddotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ReddotServiceImpl implements ReddotService {
@Autowired
ReddotDao reddotDao;
@Override
public RedDot findByUserId(String userId) {
RedDot redDot = reddotDao.findById(userId).get();
return redDot;
}
@Override
public boolean addReddot(RedDot redDot) {
reddotDao.save(redDot);
return true;
}
}
......@@ -4,10 +4,10 @@ import com.zjty.efs.bus.entity.Attention;
import com.zjty.efs.bus.entity.Notice;
import com.zjty.efs.bus.service.AttentionService;
import com.zjty.efs.bus.service.NoticeService;
import com.zjty.efs.bus.websocket.WebSocketServer;
import com.zjty.efs.ftp.entity.DownLoadCount;
import com.zjty.efs.ftp.entity.DownLoadRequest;
import com.zjty.efs.ftp.service.DownLoadService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
......@@ -119,4 +119,6 @@ public class Job {
noticeService.saveNotice(notice);
}
}
package com.zjty.efs.bus.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Component
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
package com.zjty.efs.bus.websocket;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
@ServerEndpoint(value = "/webServer/{userId}")
@Component
@Slf4j
public class WebSocketServer {
@PostConstruct
public void init() {
log.info("websocket 加载");
}
private static final AtomicInteger OnlineCount = new AtomicInteger(0);
private static Map<Session,String> map = new HashMap<>();
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session,@PathParam("userId")String userId) {
map.put(session,userId);
int cnt = OnlineCount.incrementAndGet(); // 在线数加1
log.info("有连接加入,当前连接数为:{}", cnt);
SendMessage(session, "连接成功");
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose(Session session) {
map.remove(session);
int cnt = OnlineCount.decrementAndGet();
log.info("有连接关闭,当前连接数为:{}", cnt);
}
/**
* 收到客户端消息后调用的方法
*
* @param message
* 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("来自客户端的消息:{}",message);
SendMessage(session, "收到消息,消息内容:"+message);
}
/**
* 出现错误
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("发生错误:{},Session ID: {}",error.getMessage(),session.getId());
error.printStackTrace();
}
/**
* 发送消息,实践表明,每次浏览器刷新,session会发生变化。
* @param session
* @param message
*/
private static void SendMessage(Session session, String message) {
try {
session.getBasicRemote().sendText(String.format("%s (From Server,Session ID=%s)",message,session.getId()));
} catch (IOException e) {
log.error("发送消息出错:{}", e.getMessage());
e.printStackTrace();
}
}
/**
* 给所有的Session推送信息
* @param message 需要往客户端推送的信息
* @param userId 客户端登录用户的编号
*/
public void sendData(String userId,String message){
log.info("websocket推送信息: "+message);
List<Session> list = getKey(map, userId);
if (!list.isEmpty()){
for(Session session:list){
if(session.isOpen()){
SendMessage(session, message);
}
}
}
}
//根据map的value获取map的key
private static List<Session> getKey(Map<Session,String> map, String value){
List<Session> sessionList = new ArrayList<>();
for (Map.Entry<Session, String> entry : map.entrySet()) {
if(value.equals(entry.getValue())){
sessionList.add(entry.getKey());
}
}
return sessionList;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论