优化违禁词

This commit is contained in:
2026-07-06 12:18:09 +08:00
parent 713cca8f67
commit 89e0a9df9f
20 changed files with 412 additions and 94 deletions
+33 -3
View File
@@ -84,13 +84,43 @@ func (r *BannedWordRepository) Delete(ctx context.Context, id string) (int64, er
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) {
// RecordHit bumps the word's block counter, the user's 违禁词触发次数 (when userID
// is set), and appends a BannedWordHit row for the admin 违禁词触发列表.
// Best-effort bookkeeping.
func (r *BannedWordRepository) RecordHit(ctx context.Context, wordID, word, userID, userName, prompt 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
}
_ = r.db.WithContext(ctx).Create(&model.BannedWordHit{
ID: strings.ReplaceAll(uuid.NewString(), "-", "")[:32],
WordID: wordID,
Word: word,
UserID: userID,
UserName: userName,
Prompt: prompt,
CreatedAt: time.Now(),
}).Error
}
// ListHits returns trigger records newest first, with pagination + total.
// query — server-side search over 违禁词 / 用户名 / 提示词 (跨页).
func (r *BannedWordRepository) ListHits(ctx context.Context, query string, limit, offset int) ([]model.BannedWordHit, int64, error) {
var out []model.BannedWordHit
var total int64
q := r.db.WithContext(ctx).Model(&model.BannedWordHit{})
if term := strings.TrimSpace(query); term != "" {
like := "%" + term + "%"
q = q.Where("(word ILIKE ? OR user_name ILIKE ? OR user_id ILIKE ? OR prompt ILIKE ?)", like, like, like, like)
}
if err := q.Count(&total).Error; err != nil {
return nil, 0, err
}
if limit <= 0 {
limit = 50
}
err := q.Order("created_at desc").Limit(limit).Offset(offset).Find(&out).Error
return out, total, err
}
+9
View File
@@ -22,6 +22,8 @@ type EventListFilter struct {
Statuses []string // multiple statuses (status IN (?)) — used by the 画图台 grid
Since *time.Time
UserID string
UserIDs []string // when set, keep ONLY rows whose user_id is in this list (admin 用户搜索)
Query string // free-text search over prompt / model / error (server-side, 跨页)
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)
@@ -59,6 +61,13 @@ func (r *EventRepository) List(ctx context.Context, filter EventListFilter) ([]m
if filter.UserID != "" {
q = q.Where("user_id = ?", filter.UserID)
}
if len(filter.UserIDs) > 0 {
q = q.Where("user_id IN ?", filter.UserIDs)
}
if term := strings.TrimSpace(filter.Query); term != "" {
like := "%" + term + "%"
q = q.Where("(prompt ILIKE ? OR model ILIKE ? OR error ILIKE ?)", like, like, like)
}
if filter.ExcludeSource != "" {
q = q.Where("(source IS NULL OR source <> ?)", filter.ExcludeSource)
}
+18 -2
View File
@@ -2,6 +2,7 @@ package repo
import (
"context"
"strings"
"time"
"backend/internal/model"
@@ -29,13 +30,18 @@ func (r *OrderRepository) Update(ctx context.Context, id string, patch map[strin
}
// ListByUser returns a user's own orders, newest first, with pagination + total.
func (r *OrderRepository) ListByUser(ctx context.Context, userID, status string, limit, offset int) ([]model.Order, int64, error) {
// query — server-side search over 订单号 / 支付方式 / 金额 (跨页).
func (r *OrderRepository) ListByUser(ctx context.Context, userID, status, query string, limit, offset int) ([]model.Order, int64, error) {
var out []model.Order
var total int64
q := r.db.WithContext(ctx).Model(&model.Order{}).Where("user_id = ?", userID)
if status != "" {
q = q.Where("status = ?", status)
}
if term := strings.TrimSpace(query); term != "" {
like := "%" + term + "%"
q = q.Where("(id ILIKE ? OR method ILIKE ? OR CAST(amount AS TEXT) LIKE ?)", like, like, like)
}
if err := q.Count(&total).Error; err != nil {
return nil, 0, err
}
@@ -47,13 +53,23 @@ func (r *OrderRepository) ListByUser(ctx context.Context, userID, status string,
}
// List returns all orders (admin) with optional status filter + pagination.
func (r *OrderRepository) List(ctx context.Context, status string, limit, offset int) ([]model.Order, int64, error) {
// query — server-side search over 订单号 / 支付方式 / 金额; userIDs — additionally
// match orders belonging to these users (resolved from a 用户名 search upstream).
func (r *OrderRepository) List(ctx context.Context, status, query string, userIDs []string, limit, offset int) ([]model.Order, int64, error) {
var out []model.Order
var total int64
q := r.db.WithContext(ctx).Model(&model.Order{})
if status != "" {
q = q.Where("status = ?", status)
}
if term := strings.TrimSpace(query); term != "" {
like := "%" + term + "%"
if len(userIDs) > 0 {
q = q.Where("(id ILIKE ? OR method ILIKE ? OR CAST(amount AS TEXT) LIKE ? OR user_id IN ?)", like, like, like, userIDs)
} else {
q = q.Where("(id ILIKE ? OR method ILIKE ? OR CAST(amount AS TEXT) LIKE ?)", like, like, like)
}
}
if err := q.Count(&total).Error; err != nil {
return nil, 0, err
}