feat(phase2c): 群聊与已读回执功能完整实现
Phase 2c 全部 14 个 Task 完成,包含: 后端(Go): - MinIO 文件存储服务集成(Docker + Go SDK + 通用上传 API) - Group 模块完整实现(DAO + Service + Controller + Router + Wire) - 18 个群管理 REST API + 11 个 WS 群事件推送 - 群创建/解散/邀请/踢人/退出/转让群主/设管理员/禁言/全体禁言/群公告/群昵称/免打扰/搜索 - IM Service 扩展(群消息发送/撤回 + @提醒 + 管理员无时限撤回) - 已读回执后端(单聊会话级 last_read_msg_id + 群聊消息级 im_message_reads) - 管理端群组管理(列表/详情/解散) - 数据库迁移(3 张新表 + 2 张表字段扩展) 前端(uni-app): - 群聊 Store + API 封装 + 11 个 WS 事件监听 - 已读回执 UI(单聊已读/未读标记 + 群聊 X人已读 + 已读详情页) - 7 个群聊页面(对话/创建/设置/成员/邀请/审批/搜索) - 会话列表改造(全部/单聊/群聊 Tab + @标记 + 免打扰标识) 管理端(Vue 3 + Element Plus): - 群组列表页(搜索/分页/详情弹窗/解散群聊) - 侧边栏群组管理入口 文档同步:进度/架构/设计/API/规范文档全部更新 Made-with: Cursor
This commit is contained in:
346
backend/go-service/app/group/dao/group_dao.go
Normal file
346
backend/go-service/app/group/dao/group_dao.go
Normal file
@@ -0,0 +1,346 @@
|
||||
// Package dao 提供 group 模块的数据库访问操作
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/echochat/backend/app/constants"
|
||||
"github.com/echochat/backend/app/dto"
|
||||
"github.com/echochat/backend/app/group/model"
|
||||
imModel "github.com/echochat/backend/app/im/model"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GroupDAO 群聊数据访问对象
|
||||
type GroupDAO struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGroupDAO 创建 GroupDAO 实例
|
||||
func NewGroupDAO(db *gorm.DB) *GroupDAO {
|
||||
return &GroupDAO{db: db}
|
||||
}
|
||||
|
||||
// CreateGroupWithMembers 在事务中创建群聊(会话 + 群信息 + 群成员)
|
||||
// 创建者自动成为群主(role=2)
|
||||
func (d *GroupDAO) CreateGroupWithMembers(ctx context.Context, ownerID int64, name, avatar string, memberIDs []int64) (*model.Group, error) {
|
||||
funcName := "dao.group_dao.CreateGroupWithMembers"
|
||||
logs.Info(ctx, funcName, "创建群聊",
|
||||
zap.Int64("owner_id", ownerID), zap.String("name", name), zap.Int("member_count", len(memberIDs)+1))
|
||||
|
||||
var group model.Group
|
||||
err := d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
conv := &imModel.Conversation{
|
||||
Type: constants.ConversationTypeGroup,
|
||||
CreatorID: ownerID,
|
||||
}
|
||||
if err := tx.Create(conv).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "创建会话失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
group = model.Group{
|
||||
ConversationID: conv.ID,
|
||||
Name: name,
|
||||
Avatar: avatar,
|
||||
OwnerID: ownerID,
|
||||
MaxMembers: constants.GroupDefaultMaxMembers,
|
||||
Status: constants.GroupStatusNormal,
|
||||
}
|
||||
if err := tx.Create(&group).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "创建群信息失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
ownerMember := &imModel.ConversationMember{
|
||||
ConversationID: conv.ID,
|
||||
UserID: ownerID,
|
||||
Role: constants.GroupRoleOwner,
|
||||
JoinedAt: &now,
|
||||
}
|
||||
if err := tx.Create(ownerMember).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "创建群主成员记录失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
for _, uid := range memberIDs {
|
||||
member := &imModel.ConversationMember{
|
||||
ConversationID: conv.ID,
|
||||
UserID: uid,
|
||||
Role: constants.GroupRoleNormal,
|
||||
JoinedAt: &now,
|
||||
}
|
||||
if err := tx.Create(member).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "创建群成员记录失败",
|
||||
zap.Int64("user_id", uid), zap.Error(err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "创建群聊失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
// GetByID 根据群 ID 获取群信息
|
||||
func (d *GroupDAO) GetByID(ctx context.Context, groupID int64) (*model.Group, error) {
|
||||
funcName := "dao.group_dao.GetByID"
|
||||
logs.Debug(ctx, funcName, "获取群信息", zap.Int64("group_id", groupID))
|
||||
|
||||
var group model.Group
|
||||
err := d.db.WithContext(ctx).First(&group, groupID).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
// GetByConversationID 根据会话 ID 获取群信息
|
||||
func (d *GroupDAO) GetByConversationID(ctx context.Context, conversationID int64) (*model.Group, error) {
|
||||
funcName := "dao.group_dao.GetByConversationID"
|
||||
logs.Debug(ctx, funcName, "按会话 ID 获取群信息", zap.Int64("conversation_id", conversationID))
|
||||
|
||||
var group model.Group
|
||||
err := d.db.WithContext(ctx).Where("conversation_id = ?", conversationID).First(&group).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
// UpdateGroupInfo 更新群基本信息(名称/头像/公告/可搜索性)
|
||||
func (d *GroupDAO) UpdateGroupInfo(ctx context.Context, groupID int64, updates map[string]interface{}) error {
|
||||
funcName := "dao.group_dao.UpdateGroupInfo"
|
||||
logs.Info(ctx, funcName, "更新群信息", zap.Int64("group_id", groupID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.Group{}).
|
||||
Where("id = ?", groupID).
|
||||
Updates(updates).Error
|
||||
}
|
||||
|
||||
// DissolveGroup 解散群聊(标记状态为已解散)
|
||||
func (d *GroupDAO) DissolveGroup(ctx context.Context, groupID int64) error {
|
||||
funcName := "dao.group_dao.DissolveGroup"
|
||||
logs.Info(ctx, funcName, "解散群聊", zap.Int64("group_id", groupID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.Group{}).
|
||||
Where("id = ?", groupID).
|
||||
Update("status", constants.GroupStatusDissolved).Error
|
||||
}
|
||||
|
||||
// TransferOwner 转让群主
|
||||
func (d *GroupDAO) TransferOwner(ctx context.Context, groupID, oldOwnerID, newOwnerID int64, conversationID int64) error {
|
||||
funcName := "dao.group_dao.TransferOwner"
|
||||
logs.Info(ctx, funcName, "转让群主",
|
||||
zap.Int64("group_id", groupID), zap.Int64("old_owner", oldOwnerID), zap.Int64("new_owner", newOwnerID))
|
||||
|
||||
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&model.Group{}).
|
||||
Where("id = ?", groupID).
|
||||
Update("owner_id", newOwnerID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, oldOwnerID).
|
||||
Update("role", constants.GroupRoleNormal).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, newOwnerID).
|
||||
Update("role", constants.GroupRoleOwner).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// SetAllMuted 设置/取消全体禁言
|
||||
func (d *GroupDAO) SetAllMuted(ctx context.Context, groupID int64, isMuted bool) error {
|
||||
funcName := "dao.group_dao.SetAllMuted"
|
||||
logs.Info(ctx, funcName, "设置全体禁言", zap.Int64("group_id", groupID), zap.Bool("is_muted", isMuted))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.Group{}).
|
||||
Where("id = ?", groupID).
|
||||
Update("is_all_muted", isMuted).Error
|
||||
}
|
||||
|
||||
// GetGroupBrief 获取群简要信息(满足 im/service.GroupInfoGetter 接口)
|
||||
func (d *GroupDAO) GetGroupBrief(ctx context.Context, conversationID int64) (*dto.GroupBrief, error) {
|
||||
funcName := "dao.group_dao.GetGroupBrief"
|
||||
logs.Debug(ctx, funcName, "获取群简要信息", zap.Int64("conversation_id", conversationID))
|
||||
|
||||
var group model.Group
|
||||
err := d.db.WithContext(ctx).Where("conversation_id = ?", conversationID).First(&group).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.GroupBrief{
|
||||
ID: group.ID,
|
||||
Name: group.Name,
|
||||
Avatar: group.Avatar,
|
||||
IsAllMuted: group.IsAllMuted,
|
||||
Status: group.Status,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetMemberCount 获取群成员数量
|
||||
func (d *GroupDAO) GetMemberCount(ctx context.Context, conversationID int64) (int64, error) {
|
||||
var count int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ?", conversationID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetMember 获取指定群成员记录
|
||||
func (d *GroupDAO) GetMember(ctx context.Context, conversationID, userID int64) (*imModel.ConversationMember, error) {
|
||||
funcName := "dao.group_dao.GetMember"
|
||||
logs.Debug(ctx, funcName, "查询群成员",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID))
|
||||
|
||||
var member imModel.ConversationMember
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
First(&member).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &member, nil
|
||||
}
|
||||
|
||||
// GetMembers 获取群所有成员列表
|
||||
func (d *GroupDAO) GetMembers(ctx context.Context, conversationID int64) ([]imModel.ConversationMember, error) {
|
||||
funcName := "dao.group_dao.GetMembers"
|
||||
logs.Debug(ctx, funcName, "获取群成员列表", zap.Int64("conversation_id", conversationID))
|
||||
|
||||
var members []imModel.ConversationMember
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("conversation_id = ?", conversationID).
|
||||
Order("role DESC, joined_at ASC").
|
||||
Find(&members).Error
|
||||
return members, err
|
||||
}
|
||||
|
||||
// GetMemberIDs 获取群所有成员 ID
|
||||
func (d *GroupDAO) GetMemberIDs(ctx context.Context, conversationID int64) ([]int64, error) {
|
||||
var ids []int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ?", conversationID).
|
||||
Pluck("user_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
// GetAdminIDs 获取群主和管理员 ID 列表(用于推送入群申请通知)
|
||||
func (d *GroupDAO) GetAdminIDs(ctx context.Context, conversationID int64) ([]int64, error) {
|
||||
var ids []int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND role >= ?", conversationID, constants.GroupRoleAdmin).
|
||||
Pluck("user_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
// AddMember 添加群成员
|
||||
func (d *GroupDAO) AddMember(ctx context.Context, conversationID, userID int64, role int) error {
|
||||
funcName := "dao.group_dao.AddMember"
|
||||
logs.Info(ctx, funcName, "添加群成员",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID))
|
||||
|
||||
now := time.Now()
|
||||
member := &imModel.ConversationMember{
|
||||
ConversationID: conversationID,
|
||||
UserID: userID,
|
||||
Role: role,
|
||||
JoinedAt: &now,
|
||||
}
|
||||
return d.db.WithContext(ctx).Create(member).Error
|
||||
}
|
||||
|
||||
// RemoveMember 移除群成员
|
||||
func (d *GroupDAO) RemoveMember(ctx context.Context, conversationID, userID int64) error {
|
||||
funcName := "dao.group_dao.RemoveMember"
|
||||
logs.Info(ctx, funcName, "移除群成员",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Delete(&imModel.ConversationMember{}).Error
|
||||
}
|
||||
|
||||
// UpdateMemberRole 更新成员角色
|
||||
func (d *GroupDAO) UpdateMemberRole(ctx context.Context, conversationID, userID int64, role int) error {
|
||||
funcName := "dao.group_dao.UpdateMemberRole"
|
||||
logs.Info(ctx, funcName, "更新成员角色",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID), zap.Int("role", role))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Update("role", role).Error
|
||||
}
|
||||
|
||||
// UpdateMemberMuted 更新成员禁言状态
|
||||
func (d *GroupDAO) UpdateMemberMuted(ctx context.Context, conversationID, userID int64, isMuted bool) error {
|
||||
funcName := "dao.group_dao.UpdateMemberMuted"
|
||||
logs.Info(ctx, funcName, "更新成员禁言状态",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID), zap.Bool("is_muted", isMuted))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Update("is_muted", isMuted).Error
|
||||
}
|
||||
|
||||
// UpdateMemberNickname 更新成员群内昵称
|
||||
func (d *GroupDAO) UpdateMemberNickname(ctx context.Context, conversationID, userID int64, nickname string) error {
|
||||
funcName := "dao.group_dao.UpdateMemberNickname"
|
||||
logs.Info(ctx, funcName, "更新群昵称",
|
||||
zap.Int64("conversation_id", conversationID), zap.Int64("user_id", userID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&imModel.ConversationMember{}).
|
||||
Where("conversation_id = ? AND user_id = ?", conversationID, userID).
|
||||
Update("nickname", nickname).Error
|
||||
}
|
||||
|
||||
// SearchGroups 搜索可发现的群聊(按群名称全文搜索)
|
||||
func (d *GroupDAO) SearchGroups(ctx context.Context, keyword string, offset, limit int) ([]model.Group, int64, error) {
|
||||
funcName := "dao.group_dao.SearchGroups"
|
||||
logs.Debug(ctx, funcName, "搜索群聊", zap.String("keyword", keyword))
|
||||
|
||||
var total int64
|
||||
query := d.db.WithContext(ctx).
|
||||
Model(&model.Group{}).
|
||||
Where("status = ? AND is_searchable = true AND to_tsvector('simple', name) @@ plainto_tsquery('simple', ?)",
|
||||
constants.GroupStatusNormal, keyword)
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
logs.Error(ctx, funcName, "搜索计数失败", zap.Error(err))
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var groups []model.Group
|
||||
err := query.Offset(offset).Limit(limit).Order("created_at DESC").Find(&groups).Error
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "搜索查询失败", zap.Error(err))
|
||||
return nil, 0, err
|
||||
}
|
||||
return groups, total, nil
|
||||
}
|
||||
112
backend/go-service/app/group/dao/join_request_dao.go
Normal file
112
backend/go-service/app/group/dao/join_request_dao.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/echochat/backend/app/constants"
|
||||
"github.com/echochat/backend/app/group/model"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// JoinRequestDAO 入群申请数据访问对象
|
||||
type JoinRequestDAO struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewJoinRequestDAO 创建 JoinRequestDAO 实例
|
||||
func NewJoinRequestDAO(db *gorm.DB) *JoinRequestDAO {
|
||||
return &JoinRequestDAO{db: db}
|
||||
}
|
||||
|
||||
// Create 创建入群申请
|
||||
func (d *JoinRequestDAO) Create(ctx context.Context, groupID, userID int64, message string) (*model.GroupJoinRequest, error) {
|
||||
funcName := "dao.join_request_dao.Create"
|
||||
logs.Info(ctx, funcName, "创建入群申请",
|
||||
zap.Int64("group_id", groupID), zap.Int64("user_id", userID))
|
||||
|
||||
req := &model.GroupJoinRequest{
|
||||
GroupID: groupID,
|
||||
UserID: userID,
|
||||
Message: message,
|
||||
Status: constants.JoinRequestStatusPending,
|
||||
}
|
||||
err := d.db.WithContext(ctx).Create(req).Error
|
||||
if err != nil {
|
||||
logs.Error(ctx, funcName, "创建入群申请失败", zap.Error(err))
|
||||
}
|
||||
return req, err
|
||||
}
|
||||
|
||||
// GetByID 根据 ID 获取入群申请
|
||||
func (d *JoinRequestDAO) GetByID(ctx context.Context, id int64) (*model.GroupJoinRequest, error) {
|
||||
var req model.GroupJoinRequest
|
||||
err := d.db.WithContext(ctx).First(&req, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
// GetPendingByGroupAndUser 查找用户对某群的待处理申请
|
||||
func (d *JoinRequestDAO) GetPendingByGroupAndUser(ctx context.Context, groupID, userID int64) (*model.GroupJoinRequest, error) {
|
||||
funcName := "dao.join_request_dao.GetPendingByGroupAndUser"
|
||||
logs.Debug(ctx, funcName, "查找待处理申请",
|
||||
zap.Int64("group_id", groupID), zap.Int64("user_id", userID))
|
||||
|
||||
var req model.GroupJoinRequest
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("group_id = ? AND user_id = ? AND status = ?", groupID, userID, constants.JoinRequestStatusPending).
|
||||
First(&req).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
// GetListByGroup 获取群的入群申请列表
|
||||
func (d *JoinRequestDAO) GetListByGroup(ctx context.Context, groupID int64) ([]model.GroupJoinRequest, error) {
|
||||
funcName := "dao.join_request_dao.GetListByGroup"
|
||||
logs.Debug(ctx, funcName, "获取入群申请列表", zap.Int64("group_id", groupID))
|
||||
|
||||
var requests []model.GroupJoinRequest
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("group_id = ?", groupID).
|
||||
Order("created_at DESC").
|
||||
Find(&requests).Error
|
||||
return requests, err
|
||||
}
|
||||
|
||||
// Approve 通过入群申请
|
||||
func (d *JoinRequestDAO) Approve(ctx context.Context, id, reviewerID int64) error {
|
||||
funcName := "dao.join_request_dao.Approve"
|
||||
logs.Info(ctx, funcName, "通过入群申请",
|
||||
zap.Int64("request_id", id), zap.Int64("reviewer_id", reviewerID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.GroupJoinRequest{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"status": constants.JoinRequestStatusApproved,
|
||||
"reviewer_id": reviewerID,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// Reject 拒绝入群申请
|
||||
func (d *JoinRequestDAO) Reject(ctx context.Context, id, reviewerID int64) error {
|
||||
funcName := "dao.join_request_dao.Reject"
|
||||
logs.Info(ctx, funcName, "拒绝入群申请",
|
||||
zap.Int64("request_id", id), zap.Int64("reviewer_id", reviewerID))
|
||||
|
||||
return d.db.WithContext(ctx).
|
||||
Model(&model.GroupJoinRequest{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"status": constants.JoinRequestStatusRejected,
|
||||
"reviewer_id": reviewerID,
|
||||
}).Error
|
||||
}
|
||||
129
backend/go-service/app/group/dao/message_read_dao.go
Normal file
129
backend/go-service/app/group/dao/message_read_dao.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/echochat/backend/app/group/model"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// MessageReadDAO 消息已读记录数据访问对象
|
||||
type MessageReadDAO struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewMessageReadDAO 创建 MessageReadDAO 实例
|
||||
func NewMessageReadDAO(db *gorm.DB) *MessageReadDAO {
|
||||
return &MessageReadDAO{db: db}
|
||||
}
|
||||
|
||||
// BatchCreate 批量创建已读记录(忽略重复冲突)
|
||||
func (d *MessageReadDAO) BatchCreate(ctx context.Context, reads []model.MessageRead) error {
|
||||
funcName := "dao.message_read_dao.BatchCreate"
|
||||
logs.Info(ctx, funcName, "批量创建已读记录", zap.Int("count", len(reads)))
|
||||
|
||||
if len(reads) == 0 {
|
||||
return nil
|
||||
}
|
||||
return d.db.WithContext(ctx).
|
||||
Clauses(clause.OnConflict{DoNothing: true}).
|
||||
Create(&reads).Error
|
||||
}
|
||||
|
||||
// GetReadCount 获取消息的已读人数
|
||||
func (d *MessageReadDAO) GetReadCount(ctx context.Context, messageID int64) (int64, error) {
|
||||
var count int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.MessageRead{}).
|
||||
Where("message_id = ?", messageID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetReadCountBatch 批量获取多条消息的已读人数
|
||||
func (d *MessageReadDAO) GetReadCountBatch(ctx context.Context, messageIDs []int64) (map[int64]int, error) {
|
||||
funcName := "dao.message_read_dao.GetReadCountBatch"
|
||||
logs.Debug(ctx, funcName, "批量获取已读计数", zap.Int("count", len(messageIDs)))
|
||||
|
||||
type result struct {
|
||||
MessageID int64 `gorm:"column:message_id"`
|
||||
ReadCount int `gorm:"column:read_count"`
|
||||
}
|
||||
|
||||
var results []result
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.MessageRead{}).
|
||||
Select("message_id, COUNT(*) as read_count").
|
||||
Where("message_id IN ?", messageIDs).
|
||||
Group("message_id").
|
||||
Scan(&results).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := make(map[int64]int, len(results))
|
||||
for _, r := range results {
|
||||
m[r.MessageID] = r.ReadCount
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetReadUsers 获取消息的已读用户列表
|
||||
func (d *MessageReadDAO) GetReadUsers(ctx context.Context, messageID int64) ([]model.MessageRead, error) {
|
||||
funcName := "dao.message_read_dao.GetReadUsers"
|
||||
logs.Debug(ctx, funcName, "获取已读用户列表", zap.Int64("message_id", messageID))
|
||||
|
||||
var reads []model.MessageRead
|
||||
err := d.db.WithContext(ctx).
|
||||
Where("message_id = ?", messageID).
|
||||
Order("read_at ASC").
|
||||
Find(&reads).Error
|
||||
return reads, err
|
||||
}
|
||||
|
||||
// BatchCreateReads 批量标记消息已读(满足 im/service.MessageReadRecorder 接口)
|
||||
func (d *MessageReadDAO) BatchCreateReads(ctx context.Context, messageIDs []int64, userID int64) error {
|
||||
funcName := "dao.message_read_dao.BatchCreateReads"
|
||||
logs.Info(ctx, funcName, "批量标记已读",
|
||||
zap.Int64("user_id", userID), zap.Int("count", len(messageIDs)))
|
||||
|
||||
if len(messageIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
reads := make([]model.MessageRead, 0, len(messageIDs))
|
||||
for _, msgID := range messageIDs {
|
||||
reads = append(reads, model.MessageRead{
|
||||
MessageID: msgID,
|
||||
UserID: userID,
|
||||
})
|
||||
}
|
||||
return d.BatchCreate(ctx, reads)
|
||||
}
|
||||
|
||||
// GetReadUserIDs 获取消息的已读用户 ID 列表(满足 im/service.MessageReadRecorder 接口)
|
||||
func (d *MessageReadDAO) GetReadUserIDs(ctx context.Context, messageID int64) ([]int64, error) {
|
||||
funcName := "dao.message_read_dao.GetReadUserIDs"
|
||||
logs.Debug(ctx, funcName, "获取已读用户 ID 列表", zap.Int64("message_id", messageID))
|
||||
|
||||
var ids []int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.MessageRead{}).
|
||||
Where("message_id = ?", messageID).
|
||||
Pluck("user_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
// HasRead 检查用户是否已读某消息
|
||||
func (d *MessageReadDAO) HasRead(ctx context.Context, messageID, userID int64) (bool, error) {
|
||||
var count int64
|
||||
err := d.db.WithContext(ctx).
|
||||
Model(&model.MessageRead{}).
|
||||
Where("message_id = ? AND user_id = ?", messageID, userID).
|
||||
Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
Reference in New Issue
Block a user