添加消息相关表

This commit is contained in:
zk
2026-04-28 12:13:05 +08:00
parent 0c61b89736
commit 6dec0db9fa
11 changed files with 421 additions and 5 deletions
@@ -0,0 +1,13 @@
package org.jiayunet.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.jiayunet.pojo.po.Message;
/**
* 站内信消息Mapper
*
* @author zk
*/
@Mapper
public interface MessageMapper extends CommonMapper<Message> {
}
@@ -0,0 +1,13 @@
package org.jiayunet.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.jiayunet.pojo.po.MessageRead;
/**
* 消息已读状态Mapper
*
* @author zk
*/
@Mapper
public interface MessageReadMapper extends CommonMapper<MessageRead> {
}
@@ -0,0 +1,46 @@
package org.jiayunet.pojo.po;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.Instant;
/**
* 站内信消息表(bg_message
* <p>统一存储系统消息、运营消息、订单消息</p>
*
* @author zk
*/
@Data
@TableName("bg_message")
public class Message {
@TableId(type = IdType.ASSIGN_ID)
private Long id;
/** 消息类型 1=系统消息 2=运营消息 3=订单消息 */
private Integer type;
/** 目标类型 1=指定用户 2=全部用户 */
private Integer targetType;
/** 目标用户IDtargetType=1时必填,2时为NULL */
private Long userId;
/** 消息标题 */
private String title;
/** 消息内容 */
private String content;
/** 关联业务类型,如 order/resume_diagnose */
private String bizType;
/** 关联业务ID,配合 bizType 跳转 */
private Long bizId;
/** 创建时间 */
private Instant createTime;
}
@@ -0,0 +1,31 @@
package org.jiayunet.pojo.po;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.Instant;
/**
* 消息已读状态表(bg_message_read
* <p>记录用户对消息的已读状态,用户读了才插入一条记录</p>
*
* @author zk
*/
@Data
@TableName("bg_message_read")
public class MessageRead {
@TableId(type = IdType.ASSIGN_ID)
private Long id;
/** 消息ID,关联 bg_message.id */
private Long messageId;
/** 用户ID */
private Long userId;
/** 阅读时间 */
private Instant readTime;
}
@@ -0,0 +1,76 @@
package org.jiayunet.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jiayunet.mapper.MessageMapper;
import org.jiayunet.mapper.MessageReadMapper;
import org.jiayunet.pojo.po.Message;
import org.jiayunet.pojo.po.MessageRead;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.time.Instant;
/**
* 站内信消息服务
* <p>提供消息发送和已读标记能力,供 C 端和 B 端共用</p>
* <p>使用表:bg_message(消息主表)、bg_message_read(已读状态表)</p>
*
* @author zk
*/
@Service
public class MessageService {
@Autowired
private MessageMapper messageMapper;
@Autowired
private MessageReadMapper messageReadMapper;
/**
* 发送消息(指定用户)
*/
@Transactional(rollbackFor = Exception.class)
public void sendToUser(Integer type, Long userId, String title, String content, String bizType, Long bizId) {
Assert.notNull(userId, "目标用户ID不能为空");
Message msg = new Message();
msg.setType(type);
msg.setTargetType(1);
msg.setUserId(userId);
msg.setTitle(title);
msg.setContent(content);
msg.setBizType(bizType);
msg.setBizId(bizId);
msg.setCreateTime(Instant.now());
messageMapper.insert(msg);
}
/**
* 发送消息(全部用户)
*/
@Transactional(rollbackFor = Exception.class)
public void sendToAll(Integer type, String title, String content) {
Message msg = new Message();
msg.setType(type);
msg.setTargetType(2);
msg.setTitle(title);
msg.setContent(content);
msg.setCreateTime(Instant.now());
messageMapper.insert(msg);
}
/**
* 标记消息已读(幂等,重复调用不报错)
*/
@Transactional(rollbackFor = Exception.class)
public void markRead(Long messageId, Long userId) {
Long count = messageReadMapper.selectCount(new LambdaQueryWrapper<MessageRead>().eq(MessageRead::getMessageId, messageId).eq(MessageRead::getUserId, userId));
if (count > 0) return;
MessageRead read = new MessageRead();
read.setMessageId(messageId);
read.setUserId(userId);
read.setReadTime(Instant.now());
messageReadMapper.insert(read);
}
}