refactor: replace standard log with zap logger and optimize code patterns

- Migrate all log.Printf/log.Println calls to structured zap logging
- Use cmp.Or for cleaner default value handling
- Replace manual loops with slices.ContainsFunc/IndexFunc
- Add batch query methods (IsLikedBatch, IsFavoritedBatch) to solve N+1 problem
- Update JSON tags from omitempty to omitzero for numeric fields
- Use errgroup-style wg.Go for worker goroutines
- Remove duplicate casbin/v2 dependency (keeping v3)
- Add plans/ to gitignore
This commit is contained in:
lafay
2026-03-17 00:47:17 +08:00
parent b028f7e1d3
commit 5d6c982c9c
38 changed files with 2256 additions and 290 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"sync"
"time"
@@ -12,6 +11,7 @@ import (
"carrot_bbs/internal/model"
"carrot_bbs/internal/pkg/openai"
"go.uber.org/zap"
"gorm.io/gorm"
)
@@ -97,7 +97,9 @@ func (s *auditServiceImpl) AuditText(ctx context.Context, text string, auditType
// 使用 AI 审核文本
approved, reason, err := s.openaiClient.ModerateComment(ctx, text, nil)
if err != nil {
log.Printf("AI audit text error: %v", err)
zap.L().Error("AI audit text error",
zap.Error(err),
)
if s.config.StrictModeration {
return &AuditResult{
Pass: false,
@@ -163,7 +165,9 @@ func (s *auditServiceImpl) AuditImage(ctx context.Context, imageURL string) (*Au
// 使用 AI 审核图片
approved, reason, err := s.openaiClient.ModerateComment(ctx, "", []string{imageURL})
if err != nil {
log.Printf("AI audit image error: %v", err)
zap.L().Error("AI audit image error",
zap.Error(err),
)
if s.config.StrictModeration {
return &AuditResult{
Pass: false,
@@ -219,7 +223,9 @@ func (s *auditServiceImpl) AuditPost(ctx context.Context, title, content string,
// 使用 AI 审核帖子
approved, reason, err := s.openaiClient.ModeratePost(ctx, title, content, images)
if err != nil {
log.Printf("AI audit post error: %v", err)
zap.L().Error("AI audit post error",
zap.Error(err),
)
if s.config.StrictModeration {
return &AuditResult{
Pass: false,
@@ -279,7 +285,9 @@ func (s *auditServiceImpl) AuditComment(ctx context.Context, content string, ima
// 使用 AI 审核评论
approved, reason, err := s.openaiClient.ModerateComment(ctx, content, images)
if err != nil {
log.Printf("AI audit comment error: %v", err)
zap.L().Error("AI audit comment error",
zap.Error(err),
)
if s.config.StrictModeration {
return &AuditResult{
Pass: false,
@@ -395,7 +403,9 @@ func (s *auditServiceImpl) saveAuditLog(ctx context.Context, contentType, conten
}
if err := s.db.Create(&auditLog).Error; err != nil {
log.Printf("Failed to save audit log: %v", err)
zap.L().Error("Failed to save audit log",
zap.Error(err),
)
}
}
@@ -419,7 +429,11 @@ func (c *AuditCallback) HandleTextCallback(ctx context.Context, auditID string,
return fmt.Errorf("invalid parameters")
}
log.Printf("Processing text audit callback: auditID=%s, result=%+v", auditID, result)
zap.L().Debug("Processing text audit callback",
zap.String("auditID", auditID),
zap.Bool("pass", result.Pass),
zap.String("risk", result.Risk),
)
// 根据审核结果执行相应操作
// 例如: 更新帖子状态、发送通知等
@@ -433,7 +447,11 @@ func (c *AuditCallback) HandleImageCallback(ctx context.Context, auditID string,
return fmt.Errorf("invalid parameters")
}
log.Printf("Processing image audit callback: auditID=%s, result=%+v", auditID, result)
zap.L().Debug("Processing image audit callback",
zap.String("auditID", auditID),
zap.Bool("pass", result.Pass),
zap.String("risk", result.Risk),
)
// 根据审核结果执行相应操作
// 例如: 更新图片状态、删除违规图片等
@@ -568,7 +586,7 @@ func (s *AuditScheduler) processPendingAudits() {
// 3. 更新审核状态
// 示例逻辑,实际需要根据业务需求实现
log.Println("Processing pending audits...")
zap.L().Debug("Processing pending audits")
}
// CleanupOldLogs 清理旧的审核日志