增加违禁词管理 增加多选操作

This commit is contained in:
2026-07-05 03:00:49 +08:00
parent 4d5b49c2a9
commit 10d0732f8c
24 changed files with 836 additions and 35 deletions
+59
View File
@@ -0,0 +1,59 @@
package repo
import (
"context"
"errors"
"strings"
"time"
"backend/internal/model"
"github.com/google/uuid"
"gorm.io/gorm"
)
type BannedWordRepository struct {
db *gorm.DB
}
func NewBannedWordRepository(db *gorm.DB) *BannedWordRepository {
return &BannedWordRepository{db: db}
}
func (r *BannedWordRepository) List(ctx context.Context) ([]model.BannedWord, error) {
var items []model.BannedWord
err := r.db.WithContext(ctx).Order("hits DESC, created_at DESC").Find(&items).Error
return items, err
}
func (r *BannedWordRepository) Create(ctx context.Context, word string) (*model.BannedWord, error) {
word = strings.TrimSpace(word)
if word == "" {
return nil, errors.New("违禁词不能为空")
}
item := &model.BannedWord{
ID: strings.ReplaceAll(uuid.NewString(), "-", "")[:32],
Word: word,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if err := r.db.WithContext(ctx).Create(item).Error; err != nil {
return nil, errors.New("添加失败(可能已存在)")
}
return item, nil
}
func (r *BannedWordRepository) Delete(ctx context.Context, id string) (int64, error) {
res := r.db.WithContext(ctx).Delete(&model.BannedWord{}, "id = ?", id)
return res.RowsAffected, res.Error
}
// RecordHit bumps the word's block counter and, when userID is set, the user's
// 违禁词触发次数 shown on the admin users table. Best-effort bookkeeping.
func (r *BannedWordRepository) RecordHit(ctx context.Context, wordID, userID string) {
_ = r.db.WithContext(ctx).Model(&model.BannedWord{}).Where("id = ?", wordID).
UpdateColumn("hits", gorm.Expr("hits + 1")).Error
if userID != "" {
_ = r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userID).
UpdateColumn("banned_word_hits", gorm.Expr("banned_word_hits + 1")).Error
}
}
+9 -1
View File
@@ -24,7 +24,9 @@ type EventListFilter struct {
UserID string
ExcludeSource string // when set, omit rows with this source (e.g. hide API-key "v1" usage from the customer logs page)
Source string // when set, keep ONLY rows with this source (admin 来源 filter): "v1" (API key) / "user" (前台) / "admin" (测试模型)
HasFile bool // when true, keep ONLY rows with a non-empty file (the 创作记录 gallery — paginates over real media)
HasFile bool // when true, keep ONLY rows with a non-empty file (the 创作记录 gallery — paginates over real media)
ExcludeFiles []string // when set, omit rows whose file is in this list (e.g. hide homepage showcase media from user galleries)
MediaOnly bool // when true, keep only rows that are pending or have a stored file — the 画图台 grid, so deleted works don't eat a slot
}
type EventStats struct {
@@ -66,6 +68,12 @@ func (r *EventRepository) List(ctx context.Context, filter EventListFilter) ([]m
if filter.HasFile {
q = q.Where("file <> ''")
}
if len(filter.ExcludeFiles) > 0 {
q = q.Where("file NOT IN ?", filter.ExcludeFiles)
}
if filter.MediaOnly {
q = q.Where("(status = 'pending' OR file <> '')")
}
var total int64
if err := q.Count(&total).Error; err != nil {