Commit e513d613 by 邹磊浩

修改代码

parent 546515ac
......@@ -35,6 +35,8 @@
<xxl-job.version>2.4.0</xxl-job.version>
<lombok.version>1.18.26</lombok.version>
<bouncycastle.version>1.72</bouncycastle.version>
<joda.time.version>2.9.9</joda.time.version>
<!-- 离线IP地址定位库 -->
<ip2region.version>2.7.0</ip2region.version>
......@@ -273,6 +275,12 @@
<version>${snakeyaml.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda.time.version}</version>
</dependency>
<!-- 加密包引入 -->
<dependency>
<groupId>org.bouncycastle</groupId>
......@@ -342,6 +350,7 @@
<version>${PeiZhen-Java.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
......@@ -356,7 +365,6 @@
<module>pz-accompany</module>
<module>pz-merchant</module>
<module>pz-applet</module>
<module>pz-chat</module>
</modules>
<packaging>pom</packaging>
......
......@@ -52,6 +52,11 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.pz</groupId>
<artifactId>pz-system</artifactId>
</dependency>
......
package com.pz.chat.controller;
import java.util.List;
import java.util.Arrays;
import com.pz.common.core.domain.entity.SysUser;
import com.pz.system.domain.SessionList;
import com.pz.system.mapper.SessionListMapper;
import com.pz.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
package com.pz.web.controller.system;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.pz.common.annotation.RepeatSubmit;
import com.pz.common.annotation.Log;
import com.pz.common.annotation.RepeatSubmit;
import com.pz.common.core.controller.BaseController;
import com.pz.common.core.domain.PageQuery;
import com.pz.common.core.domain.R;
import com.pz.common.core.domain.entity.SysUser;
import com.pz.common.core.validate.AddGroup;
import com.pz.common.core.validate.EditGroup;
import com.pz.common.enums.BusinessType;
import com.pz.common.utils.poi.ExcelUtil;
import com.pz.system.domain.vo.SessionListVo;
import com.pz.system.domain.SessionList;
import com.pz.system.domain.bo.SessionListBo;
import com.pz.system.service.ISessionListService;
import com.pz.common.core.page.TableDataInfo;
import com.pz.system.mapper.SessionListMapper;
import com.pz.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotEmpty;
/**
* 会话列
......
package com.pz.web.controller.system;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pz.common.utils.DateUtils;
import com.pz.system.domain.Message;
import com.pz.system.mapper.MessageMapper;
import com.pz.system.mapper.SysUserMapper;
import com.pz.system.service.ISysUserService;
import com.pz.web.controller.websocket.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author lisw
* @program ly-project
* @description 聊天
* @createDate 2021-05-30 11:32:39
* <p>
* 描述:
* 一对一聊天
* @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端,
* 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端
*/
@Component
@Slf4j
@ServerEndpoint("/webSocketOneToOne/{sendId}/{roomId}")
public class WebSocketOneToOneController {
// 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount;
//实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key为用户标识
private static final Map<Long, WebSocketOneToOneController> connections = new ConcurrentHashMap<>();
// 与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
private Long sendId;
private String roomId;
@Resource
private MessageMapper messageMapper;
/**
* 连接建立成功调用的方法
*
* @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
@OnOpen
public void onOpen(@PathParam("sendId") Long sendId, @PathParam("roomId") String roomId, Session session) {
this.session = session;
this.sendId = sendId; //用户标识
this.roomId = roomId; //会话标识
connections.put(sendId, this); //添加到map中
addOnlineCount(); // 在线数加
log.info("sendId:" + sendId + "roomId:" + roomId);
System.out.println(this.session);
System.out.println("有新连接加入!新用户:" + sendId + ",当前在线人数为" + getOnlineCount());
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
connections.remove(sendId); // 从map中移除
subOnlineCount(); // 在线数减
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
* @param session 可选的参数
*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("来自客户端的消息:" + message);
JSONObject json = JSON.parseObject(message);
String msg = json.getString("content"); //需要发送的信息
String requestId = json.getString("requestId");
int msgType = json.getIntValue("messageType");
String lastMessageTime = null;
if (json.containsKey("lastMessageTime")) {
lastMessageTime = json.getString("lastMessageTime");
}
Long giftId = null;
Long receiveId = json.getLong("receiveId"); //发送对象的用户标识(接收者)
send(msg, sendId, receiveId, roomId, msgType, requestId, lastMessageTime);
}
/**
* 发生错误时调用
*
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
System.out.println("发生错误");
error.printStackTrace();
}
/**
* @param msg 消息内容
* @param sendId 发送人
* @param receiveId 接收人
* @param roomId 房间ID
* @param msgType 消息类型
* @param requestId 消息请求ID
* @param lastMessageTime 最后一次的消息时间
*/
public void send(String msg, Long sendId, Long receiveId, String roomId, int msgType, String requestId, String lastMessageTime) {
Message message = new Message();
message.setContent(msg);
Date now = new Date();
message.setReceiver(receiveId);
message.setSender(sendId);
message.setContentType(msgType);
message.setIsRead("0");
message.setRequestId(requestId);
message.setType(0);
if (StringUtils.isNotBlank(lastMessageTime)) {
Date lastTime = DateUtils.stringToDate(lastMessageTime, "yyyy-MM-dd HH:mm");
long minute = (now.getTime() - lastTime.getTime()) / 1000 / 60;
log.info("此二人聊天,距离上一次聊天时间相差分钟数:" + minute);
if (minute > 5) {
message.setType(1);
}
}
if (messageMapper == null) {
this.messageMapper = (MessageMapper) SpringContextUtil.getBean("messageMapper");
}
try {
int res = messageMapper.insert(message);
if (res == 1) {
message.setStatus("-1");
}
//发送
WebSocketOneToOneController con = connections.get(receiveId);
if (con != null) {
if (roomId.equals(con.roomId)) {
con.session.getBasicRemote().sendText(JSON.toJSONString(message));
message.setIsRead("1");
messageMapper.updateById(message);
}
}
//通知发送消息的用户,消息已经发送成功
WebSocketOneToOneController confrom = connections.get(sendId);
if (confrom != null) {
if (roomId.equals(confrom.roomId)) {
confrom.session.getBasicRemote().sendText(JSON.toJSONString(message));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketOneToOneController.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketOneToOneController.onlineCount--;
}
}
package com.pz.chat.util;
package com.pz.web.controller.websocket;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
......
package com.pz.web.controller.websocket;
import lombok.Data;
import javax.websocket.Session;
@Data
public class WebSocketData {
/**
* 当前连接
*/
private Session session;
/**
* 当前通讯ID
*/
private String communicationId;
}
......@@ -141,6 +141,7 @@ security:
# system 仅用于调试阶段
- /system/**
- /applet/**
- /websocket/**
# MyBatisPlus配置
# https://baomidou.com/config/
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.pz</groupId>
<artifactId>PeiZhen-Java</artifactId>
<version>4.8.0</version>
</parent>
<artifactId>pz-chat</artifactId>
<description>
聊天服务模块
</description>
<dependencies>
<!-- 通用工具-->
<dependency>
<groupId>com.pz</groupId>
<artifactId>pz-common</artifactId>
</dependency>
<dependency>
<groupId>com.pz</groupId>
<artifactId>pz-sms</artifactId>
</dependency>
<dependency>
<groupId>com.pz</groupId>
<artifactId>pz-system</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
</project>
package com.pz.chat.controller;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pz.chat.util.CurPool;
import com.pz.chat.util.JsonUtils;
import com.pz.chat.util.SpringContextUtil;
import com.pz.common.core.domain.entity.SysUser;
import com.pz.system.domain.MsgInfo;
import com.pz.system.domain.SessionList;
import com.pz.system.mapper.MsgInfoMapper;
import com.pz.system.mapper.SessionListMapper;
import com.pz.system.mapper.SysUserMapper;
import com.pz.system.service.IMsgInfoService;
import com.pz.system.service.ISessionListService;
import com.pz.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Component
@ServerEndpoint("/websocket/{userId}/{sessionId}")
public class ChatController {
@Resource
private SessionListMapper sessionListMapper;
@Resource
private SysUserMapper sysUserMapper;
@Resource
private MsgInfoMapper msgInfoMapper;
private Session session;
@OnOpen
public void onOpen(Session session, @PathParam(value = "userId") Integer userId, @PathParam(value = "sessionId") String sessionId) {
this.session = session;
// 打开连接时保存WebSocket的会话对象
CurPool.webSockets.put(userId, this);
// 将WebSocket对象放入WebSocket连接池中
List<Object> list = new ArrayList<>();
list.add(sessionId);
list.add(session);
// 将会话ID和会话对象放入会话池中
CurPool.sessionPool.put(userId, list);
}
@OnClose
public void onClose() {
// 断开连接删除用户删除session
Long userId = Long.parseLong(this.session.getRequestParameterMap().get("userId").get(0));
// 获取用户ID
CurPool.sessionPool.remove(userId);
CurPool.webSockets.remove(userId);
// 从会话池和连接池中移除WebSocket对象
if (sysUserMapper == null) {
this.sysUserMapper = (SysUserMapper) SpringContextUtil.getBean("sysUserMapper");
}
SysUser user = sysUserMapper.selectUserById(userId);
// 从用户池中移除用户
CurPool.curUserPool.remove(user.getNickName());
}
@OnMessage
public void onMessage(String message) {
String sessionId = this.session.getRequestParameterMap().get("sessionId").get(0);
// 获取会话ID
if (sessionId == null) {
System.out.println("sessionId 错误");
}
if (sessionListMapper == null) {
this.sessionListMapper = (SessionListMapper) SpringContextUtil.getBean("sessionListMapper");
}
// 根据会话ID注入SeesionListMapper
if (sysUserMapper == null) {
this.sysUserMapper = (SysUserMapper) SpringContextUtil.getBean("sysUserMapper");
}
// 根据会话ID注入UserMapper
if (msgInfoMapper == null) {
this.msgInfoMapper = (MsgInfoMapper) SpringContextUtil.getBean("msgInfoMapper");
}
SessionList sessionList = sessionListMapper.selectById(Long.parseLong(sessionId));
// 根据会话ID查询会话列表
SysUser user = sysUserMapper.selectUserById(sessionList.getUserId());
// 根据会话列表中的用户ID查询用户信息
MsgInfo msgInfo = new MsgInfo();
msgInfo.setContent(message);
msgInfo.setFromUserId(sessionList.getUserId());
msgInfo.setFromUserName(user.getNickName());
msgInfo.setToUserId(sessionList.getToUserId());
msgInfo.setToUserName(sessionList.getListName());
msgInfo.setUnReadFlag(0L);
// 创建消息对象,设置消息的内容、创建时间、发送方和接收方的信息
msgInfoMapper.insert(msgInfo);
// 将消息持久化到数据库中
List<Object> list = CurPool.sessionPool.get(sessionList.getToUserId());
// 根据接收方的用户ID从会话池中获取会话列表
if (list == null || list.isEmpty()) {
sessionListMapper.addUnReadCount(sessionList.getToUserId(), sessionList.getUserId());
// 用户不存在,更新未读消息数量
} else {
String id = sessionListMapper.selectIdByUser(sessionList.getToUserId(), sessionList.getUserId()) + "";
String o = list.get(0) + "";
if (id.equals(o)) {
sendTextMessage(sessionList.getToUserId(), JsonUtils.objectToJson(msgInfo));
// 会话存在,直接发送消息
} else {
if (StringUtils.isNotBlank(id)) {
SessionList tmpSessionList = new SessionList();
tmpSessionList.setUserId(sessionList.getToUserId());
tmpSessionList.setToUserId(sessionList.getUserId());
tmpSessionList.setListName(user.getNickName());
tmpSessionList.setUnReadCount(1L);
sessionListMapper.insert(tmpSessionList);
// 新增会话列表
} else {
sessionListMapper.addUnReadCount(sessionList.getToUserId(), sessionList.getUserId());
// 更新未读消息数量
}
List<SessionList> sessionLists = sessionListMapper.selectList(Wrappers.<SessionList>lambdaQuery().eq(SessionList::getUserId, sessionList.getToUserId()));
// 会话不存在,发送会话列表消息
sendTextMessage(sessionList.getToUserId(), JsonUtils.objectToJson(sessionLists));
}
}
// 输出收到的消息
System.out.println("【websocket消息】收到客户端消息:" + message);
}
public void sendTextMessage(Long userId, String message) {
Session session = (Session) CurPool.sessionPool.get(userId).get(1);
// 根据用户ID从会话池中获取会话对象
if (session != null) {
try {
session.getBasicRemote().sendText(message);
// 发送文本消息
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.pz.chat.util;
import com.pz.chat.controller.ChatController;
import com.pz.common.core.domain.entity.SysUser;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 统一管理session、websocket、curUser
*/
public class CurPool {
// public static CopyOnWriteArraySet<WebSocket> webSockets =new CopyOnWriteArraySet<>();
public static Map<Integer, ChatController> webSockets = new ConcurrentHashMap<>();
// list 里面第一个存sessionId,第二个存session
public static Map<Integer, List<Object>> sessionPool = new ConcurrentHashMap<>();
// 当前登录用户x
public static Map<String, SysUser> curUserPool = new ConcurrentHashMap<>();
}
......@@ -113,6 +113,17 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-extra</artifactId>
</dependency>
......
package com.pz.chat.util;
package com.pz.common;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
......
package com.pz.chat.config;
package com.pz.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......
......@@ -2,7 +2,10 @@ package com.pz.common.utils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
......@@ -12,6 +15,7 @@ import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
/**
......@@ -92,6 +96,20 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
}
/**
* 字符串转换成日期
* @param strDate 日期字符串
* @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN
*/
public static Date stringToDate(String strDate, String pattern) {
if (StringUtils.isBlank(strDate)){
return null;
}
DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
return fmt.parseLocalDateTime(strDate).toDate();
}
/**
* 日期路径 即年/月/日 如20180808
*/
public static String dateTime() {
......
package com.pz.system.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.pz.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author lisw
* @program message
* @description
* @createDate 2021-08-18 17:03:58
**/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("message")
public class Message extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@TableId
private Long id;
/**
* 是否显示时间
* 0:否 1:是
*/
private Integer type;
/**
* 消息内容
*/
private String content;
/**
* 内容类型 0文字1图片2视频 3礼物
*/
private Integer contentType;
/**
* 是否已读
*/
private String isRead;
/**
* 发送人
*/
private Long sender;
/**
* 接收人
*/
private Long receiver;
/**
* 发送消息请求ID
*/
private String requestId;
/**
* 是否已读
*/
private Boolean isLast;
/**
* 状态
*/
private String status;
/**
* 备注
*/
private String remark;
}
package com.pz.system.domain;
import com.baomidou.mybatisplus.annotation.*;
import com.pz.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 消息对象 msg_info
*
* @author ruoyi
* @date 2023-09-09
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("msg_info")
public class MsgInfo extends BaseEntity {
private static final long serialVersionUID=1L;
/**
* 消息id
*/
@TableId(value = "id")
private Long id;
/**
* 消息发送者id
*/
private Long fromUserId;
/**
* 消息发送者名称
*/
private String fromUserName;
/**
* 消息接收者id
*/
private Long toUserId;
/**
* 消息接收者名称
*/
private String toUserName;
/**
* 消息内容
*/
private String content;
/**
* 是否已读(1 已读)
*/
private Long unReadFlag;
}
......@@ -13,9 +13,8 @@ import lombok.EqualsAndHashCode;
* @date 2023-09-09
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("session_list")
public class SessionList extends BaseEntity {
public class SessionList {
private static final long serialVersionUID=1L;
......
package com.pz.system.domain.bo;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.pz.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author lisw
* @program message
* @description
* @createDate 2021-08-18 17:03:58
**/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("message")
public class MessageBo extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@TableId
private Long id;
/**
* 是否显示时间
* 0:否 1:是
*/
private Integer type;
/**
* 消息内容
*/
private String content;
/**
* 内容类型 0文字1图片2视频 3礼物
*/
private Integer contentType;
/**
* 是否已读
*/
private String isRead;
/**
* 发送人
*/
private Long sender;
/**
* 接收人
*/
private Long receiver;
/**
* 发送消息请求ID
*/
private String requestId;
/**
* 是否已读
*/
private Boolean isLast;
/**
* 状态
*/
private String status;
/**
* 备注
*/
private String remark;
}
package com.pz.system.domain.bo;
import com.pz.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.*;
/**
* 消息业务对象 msg_info
*
* @author ruoyi
* @date 2023-09-09
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class MsgInfoBo extends BaseEntity {
/**
* 消息id
*/
private Long id;
/**
* 消息发送者id
*/
private Long fromUserId;
/**
* 消息发送者名称
*/
private String fromUserName;
/**
* 消息接收者id
*/
private Long toUserId;
/**
* 消息接收者名称
*/
private String toUserName;
/**
* 消息内容
*/
private String content;
/**
* 是否已读(1 已读)
*/
private Long unReadFlag;
}
package com.pz.system.domain.vo;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
@Data
public class MessageVo implements Serializable {
/**
* 主键ID
*/
@TableId
private Long id;
/**
* 是否显示时间
* 0:否 1:是
*/
private Integer type;
/**
* 消息内容
*/
private String content;
/**
* 内容类型 0文字1图片2视频 3礼物
*/
private Integer contentType;
/**
* 是否已读
*/
private String isRead;
/**
* 发送人
*/
private Long sender;
/**
* 接收人
*/
private Long receiver;
/**
* 发送消息请求ID
*/
private String requestId;
/**
* 是否已读
*/
private Boolean isLast;
/**
* 状态
*/
private String status;
/**
* 备注
*/
private String remark;
}
package com.pz.system.domain.vo;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.pz.common.annotation.ExcelDictFormat;
import com.pz.common.convert.ExcelDictConvert;
import lombok.Data;
/**
* 消息视图对象 msg_info
*
* @author ruoyi
* @date 2023-09-09
*/
@Data
@ExcelIgnoreUnannotated
public class MsgInfoVo {
private static final long serialVersionUID = 1L;
/**
* 消息id
*/
@ExcelProperty(value = "消息id")
private Long id;
/**
* 消息发送者id
*/
@ExcelProperty(value = "消息发送者id")
private Long fromUserId;
/**
* 消息发送者名称
*/
@ExcelProperty(value = "消息发送者名称")
private String fromUserName;
/**
* 消息接收者id
*/
@ExcelProperty(value = "消息接收者id")
private Long toUserId;
/**
* 消息接收者名称
*/
@ExcelProperty(value = "消息接收者名称")
private String toUserName;
/**
* 消息内容
*/
@ExcelProperty(value = "消息内容")
private String content;
/**
* 是否已读(1 已读)
*/
@ExcelProperty(value = "是否已读", converter = ExcelDictConvert.class)
@ExcelDictFormat(readConverterExp = "1=,已=读")
private Long unReadFlag;
}
......@@ -3,6 +3,7 @@ package com.pz.system.mapper;
import com.pz.system.domain.Information;
import com.pz.system.domain.vo.InformationVo;
import com.pz.common.core.mapper.BaseMapperPlus;
import org.apache.ibatis.annotations.Mapper;
/**
* 新闻资讯Mapper接口
......@@ -10,6 +11,7 @@ import com.pz.common.core.mapper.BaseMapperPlus;
* @author ruoyi
* @date 2023-09-07
*/
@Mapper
public interface InformationMapper extends BaseMapperPlus<InformationMapper, Information, InformationVo> {
}
package com.pz.system.mapper;
import com.pz.common.core.mapper.BaseMapperPlus;
import com.pz.system.domain.Information;
import com.pz.system.domain.Message;
import com.pz.system.domain.vo.InformationVo;
import com.pz.system.domain.vo.MessageVo;
import org.apache.ibatis.annotations.Mapper;
/**
* 聊天消息Mapper接口
*/
@Mapper
public interface MessageMapper extends BaseMapperPlus<MessageMapper, Message, MessageVo> {
}
package com.pz.system.mapper;
import com.pz.system.domain.MsgInfo;
import com.pz.system.domain.vo.MsgInfoVo;
import com.pz.common.core.mapper.BaseMapperPlus;
/**
* 消息Mapper接口
*
* @author ruoyi
* @date 2023-09-09
*/
public interface MsgInfoMapper extends BaseMapperPlus<MsgInfoMapper, MsgInfo, MsgInfoVo> {
}
package com.pz.system.service;
public interface IMessageService {
}
package com.pz.system.service;
import com.pz.system.domain.MsgInfo;
import com.pz.system.domain.vo.MsgInfoVo;
import com.pz.system.domain.bo.MsgInfoBo;
import com.pz.common.core.page.TableDataInfo;
import com.pz.common.core.domain.PageQuery;
import java.util.Collection;
import java.util.List;
/**
* 消息Service接口
*
* @author ruoyi
* @date 2023-09-09
*/
public interface IMsgInfoService {
/**
* 查询消息
*/
MsgInfoVo queryById(Long id);
/**
* 查询消息列表
*/
TableDataInfo<MsgInfoVo> queryPageList(MsgInfoBo bo, PageQuery pageQuery);
/**
* 查询消息列表
*/
List<MsgInfoVo> queryList(MsgInfoBo bo);
/**
* 新增消息
*/
Boolean insertByBo(MsgInfoBo bo);
/**
* 修改消息
*/
Boolean updateByBo(MsgInfoBo bo);
/**
* 校验并批量删除消息信息
*/
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
}
package com.pz.system.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pz.system.domain.Department;
import com.pz.system.domain.Hospital;
import com.pz.system.domain.bo.DepartmentBo;
import com.pz.system.domain.vo.DepartmentVo;
import com.pz.system.mapper.DepartmentMapper;
import com.pz.system.mapper.HospitalMapper;
import com.pz.system.service.IDepartmentService;
import com.pz.system.service.IMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* 科室Service业务层处理
*
* @author ruoyi
* @date 2023-09-07
*/
@RequiredArgsConstructor
@Service
public class MessageServiceImpl implements IMessageService {
}
package com.pz.system.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pz.common.core.page.TableDataInfo;
import com.pz.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.pz.system.domain.bo.MsgInfoBo;
import com.pz.system.domain.vo.MsgInfoVo;
import com.pz.system.domain.MsgInfo;
import com.pz.system.mapper.MsgInfoMapper;
import com.pz.system.service.IMsgInfoService;
import java.util.List;
import java.util.Map;
import java.util.Collection;
/**
* 消息Service业务层处理
*
* @author ruoyi
* @date 2023-09-09
*/
@RequiredArgsConstructor
@Service
public class MsgInfoServiceImpl implements IMsgInfoService {
private final MsgInfoMapper baseMapper;
/**
* 查询消息
*/
@Override
public MsgInfoVo queryById(Long id){
return baseMapper.selectVoById(id);
}
/**
* 查询消息列表
*/
@Override
public TableDataInfo<MsgInfoVo> queryPageList(MsgInfoBo bo, PageQuery pageQuery) {
LambdaQueryWrapper<MsgInfo> lqw = buildQueryWrapper(bo);
Page<MsgInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
return TableDataInfo.build(result);
}
/**
* 查询消息列表
*/
@Override
public List<MsgInfoVo> queryList(MsgInfoBo bo) {
LambdaQueryWrapper<MsgInfo> lqw = buildQueryWrapper(bo);
return baseMapper.selectVoList(lqw);
}
private LambdaQueryWrapper<MsgInfo> buildQueryWrapper(MsgInfoBo bo) {
Map<String, Object> params = bo.getParams();
LambdaQueryWrapper<MsgInfo> lqw = Wrappers.lambdaQuery();
lqw.eq(bo.getFromUserId() != null, MsgInfo::getFromUserId, bo.getFromUserId());
lqw.like(StringUtils.isNotBlank(bo.getFromUserName()), MsgInfo::getFromUserName, bo.getFromUserName());
lqw.eq(bo.getToUserId() != null, MsgInfo::getToUserId, bo.getToUserId());
lqw.like(StringUtils.isNotBlank(bo.getToUserName()), MsgInfo::getToUserName, bo.getToUserName());
lqw.eq(StringUtils.isNotBlank(bo.getContent()), MsgInfo::getContent, bo.getContent());
lqw.eq(bo.getUnReadFlag() != null, MsgInfo::getUnReadFlag, bo.getUnReadFlag());
return lqw;
}
/**
* 新增消息
*/
@Override
public Boolean insertByBo(MsgInfoBo bo) {
MsgInfo add = BeanUtil.toBean(bo, MsgInfo.class);
validEntityBeforeSave(add);
boolean flag = baseMapper.insert(add) > 0;
if (flag) {
bo.setId(add.getId());
}
return flag;
}
/**
* 修改消息
*/
@Override
public Boolean updateByBo(MsgInfoBo bo) {
MsgInfo update = BeanUtil.toBean(bo, MsgInfo.class);
validEntityBeforeSave(update);
return baseMapper.updateById(update) > 0;
}
/**
* 保存前的数据校验
*/
private void validEntityBeforeSave(MsgInfo entity){
//TODO 做一些数据校验,如唯一约束
}
/**
* 批量删除消息
*/
@Override
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
if(isValid){
//TODO 做一些业务上的校验,判断是否需要校验
}
return baseMapper.deleteBatchIds(ids) > 0;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment