90 lines
3.4 KiB
Go
90 lines
3.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CaptainAutoReplyRuleRepo provides data access for CaptainAutoReplyRule.
|
|
type CaptainAutoReplyRuleRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCaptainAutoReplyRuleRepo creates a new CaptainAutoReplyRuleRepo.
|
|
func NewCaptainAutoReplyRuleRepo(db *gorm.DB) *CaptainAutoReplyRuleRepo {
|
|
return &CaptainAutoReplyRuleRepo{db: db}
|
|
}
|
|
|
|
// Create inserts a new auto-reply rule.
|
|
func (r *CaptainAutoReplyRuleRepo) Create(ctx context.Context, rule *model.CaptainAutoReplyRule) error {
|
|
return r.db.WithContext(ctx).Create(rule).Error
|
|
}
|
|
|
|
// GetByID retrieves a rule by its primary key.
|
|
func (r *CaptainAutoReplyRuleRepo) GetByID(ctx context.Context, id uint) (*model.CaptainAutoReplyRule, error) {
|
|
var rule model.CaptainAutoReplyRule
|
|
if err := r.db.WithContext(ctx).First(&rule, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &rule, nil
|
|
}
|
|
|
|
// Update saves changes to an existing rule.
|
|
func (r *CaptainAutoReplyRuleRepo) Update(ctx context.Context, rule *model.CaptainAutoReplyRule) error {
|
|
return r.db.WithContext(ctx).Save(rule).Error
|
|
}
|
|
|
|
// Delete removes a rule by ID.
|
|
func (r *CaptainAutoReplyRuleRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.CaptainAutoReplyRule{}, id).Error
|
|
}
|
|
|
|
// ListByAccount retrieves all rules for an account, ordered by priority desc.
|
|
func (r *CaptainAutoReplyRuleRepo) ListByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.CaptainAutoReplyRule, int64, error) {
|
|
var rules []model.CaptainAutoReplyRule
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CaptainAutoReplyRule{}).Where("account_id = ?", accountID)
|
|
db.Count(&count)
|
|
if err := db.Order("priority DESC, id ASC").Offset(offset).Limit(limit).Find(&rules).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return rules, count, nil
|
|
}
|
|
|
|
// ListByAssistant retrieves all rules for a specific assistant.
|
|
func (r *CaptainAutoReplyRuleRepo) ListByAssistant(ctx context.Context, assistantID uint, offset, limit int) ([]model.CaptainAutoReplyRule, int64, error) {
|
|
var rules []model.CaptainAutoReplyRule
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CaptainAutoReplyRule{}).Where("assistant_id = ?", assistantID)
|
|
db.Count(&count)
|
|
if err := db.Order("priority DESC, id ASC").Offset(offset).Limit(limit).Find(&rules).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return rules, count, nil
|
|
}
|
|
|
|
// FindActiveByAssistant retrieves all active rules for a specific assistant.
|
|
func (r *CaptainAutoReplyRuleRepo) FindActiveByAssistant(ctx context.Context, assistantID uint) ([]model.CaptainAutoReplyRule, error) {
|
|
var rules []model.CaptainAutoReplyRule
|
|
if err := r.db.WithContext(ctx).
|
|
Where("assistant_id = ? AND status = ?", assistantID, model.AutoReplyRuleStatusActive).
|
|
Order("priority DESC, id ASC").
|
|
Find(&rules).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return rules, nil
|
|
}
|
|
|
|
// FindActiveByInbox retrieves active rules scoped to an inbox or account-wide (inbox_id IS NULL).
|
|
func (r *CaptainAutoReplyRuleRepo) FindActiveByInbox(ctx context.Context, accountID, inboxID uint) ([]model.CaptainAutoReplyRule, error) {
|
|
var rules []model.CaptainAutoReplyRule
|
|
if err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND status = ? AND (inbox_id = ? OR inbox_id IS NULL)", accountID, model.AutoReplyRuleStatusActive, inboxID).
|
|
Order("priority DESC, id ASC").
|
|
Find(&rules).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return rules, nil
|
|
} |