org 模块: - tenants 表 (UUID PK, name, slug, status, settings) - 完整 CRUD API: /api/v1/org/tenants - 多租户隔离 audit 模块: - audit_logs 表 (actor, action, resource, IP, 时间) - 查询 API: /api/v1/audit/logs (支持分页+筛选) - 按 tenant_id/user_id/action/resource 过滤
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package infrastructure
|
|
|
|
import (
|
|
"context"
|
|
|
|
"mengstack/internal/modules/audit/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type auditRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewAuditRepository(db *gorm.DB) domain.AuditRepository {
|
|
return &auditRepo{db: db}
|
|
}
|
|
|
|
func (r *auditRepo) Create(ctx context.Context, log *domain.AuditLog) error {
|
|
return r.db.WithContext(ctx).Create(log).Error
|
|
}
|
|
|
|
func (r *auditRepo) Find(ctx context.Context, query domain.AuditQuery) ([]domain.AuditLog, int64, error) {
|
|
var logs []domain.AuditLog
|
|
var total int64
|
|
|
|
tx := r.db.WithContext(ctx).Model(&domain.AuditLog{})
|
|
|
|
if query.TenantID != "" {
|
|
tx = tx.Where("tenant_id = ?", query.TenantID)
|
|
}
|
|
if query.UserID > 0 {
|
|
tx = tx.Where("user_id = ?", query.UserID)
|
|
}
|
|
if query.Action != "" {
|
|
tx = tx.Where("action = ?", query.Action)
|
|
}
|
|
if query.Resource != "" {
|
|
tx = tx.Where("resource = ?", query.Resource)
|
|
}
|
|
|
|
if err := tx.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
page := query.Page
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
pageSize := query.PageSize
|
|
if pageSize < 1 || pageSize > 100 {
|
|
pageSize = 20
|
|
}
|
|
offset := (page - 1) * pageSize
|
|
|
|
if err := tx.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&logs).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return logs, total, nil
|
|
}
|