- 新增chat_session相关模型、仓库和服务 - 新增chat_group相关模型、仓库和服务 - 新增session_handler和chat_group_handler - 实现会话管理和群聊功能 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"x-agents/server/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ChatGroupRepository 群聊仓储
|
|
type ChatGroupRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewChatGroupRepository 创建群聊仓储
|
|
func NewChatGroupRepository(db *gorm.DB) *ChatGroupRepository {
|
|
return &ChatGroupRepository{db: db}
|
|
}
|
|
|
|
// Create 创建群聊
|
|
func (r *ChatGroupRepository) Create(group *model.ChatGroup) error {
|
|
return r.db.Create(group).Error
|
|
}
|
|
|
|
// FindByID 根据ID查询
|
|
func (r *ChatGroupRepository) FindByID(id string) (*model.ChatGroup, error) {
|
|
var group model.ChatGroup
|
|
err := r.db.Where("id = ?", id).First(&group).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &group, nil
|
|
}
|
|
|
|
// FindByUserID 根据用户ID查询群聊列表
|
|
func (r *ChatGroupRepository) FindByUserID(userID string, limit, offset int) ([]model.ChatGroup, int64, error) {
|
|
var groups []model.ChatGroup
|
|
query := r.db.Model(&model.ChatGroup{}).Where("user_id = ?", userID)
|
|
|
|
var total int64
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := query.Order("created_at DESC").Limit(limit).Offset(offset).Find(&groups).Error
|
|
return groups, total, err
|
|
}
|
|
|
|
// Update 更新群聊
|
|
func (r *ChatGroupRepository) Update(group *model.ChatGroup) error {
|
|
return r.db.Save(group).Error
|
|
}
|
|
|
|
// Delete 删除群聊
|
|
func (r *ChatGroupRepository) Delete(id string) error {
|
|
return r.db.Delete(&model.ChatGroup{}, "id = ?", id).Error
|
|
}
|