126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
// Package sys
|
|
// @Link https://github.com/bufanyun/hotgo
|
|
// @Copyright Copyright (c) 2026 HotGo
|
|
// @Author Ms <133814250@qq.com>
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
package sys
|
|
|
|
import (
|
|
"context"
|
|
"hotgo/internal/library/imtenant"
|
|
"hotgo/internal/model/input/sysin"
|
|
"hotgo/internal/service"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
type sSysImAuditLog struct{}
|
|
|
|
func NewSysImAuditLog() *sSysImAuditLog {
|
|
return &sSysImAuditLog{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterSysImAuditLog(NewSysImAuditLog())
|
|
}
|
|
|
|
// List 获取IM审计日志列表
|
|
func (s *sSysImAuditLog) List(ctx context.Context, in *sysin.ImAuditLogListInp) (list []*sysin.ImAuditLogListModel, totalCount int, err error) {
|
|
if err = in.Filter(ctx); err != nil {
|
|
return
|
|
}
|
|
db, tenant, err := imtenant.DB(ctx, in.TenantId)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
mod := s.model(ctx, db)
|
|
mod = s.applyWhere(mod, in)
|
|
mod = mod.Page(in.Page, in.PerPage).OrderDesc("id")
|
|
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取IM审计日志列表失败,请稍后重试!")
|
|
return
|
|
}
|
|
for _, item := range list {
|
|
item.TenantId = tenant.Id
|
|
item.TenantName = tenant.Name
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *sSysImAuditLog) model(ctx context.Context, db gdb.DB) *gdb.Model {
|
|
return db.Model("im_audit_logs").Safe().Ctx(ctx).
|
|
Fields(
|
|
"id",
|
|
"actor_uid",
|
|
"actor_role",
|
|
"action",
|
|
"target_type",
|
|
"target_id",
|
|
"reason",
|
|
"request_id",
|
|
"ip",
|
|
"user_agent",
|
|
"CAST(before_snapshot AS CHAR) before_snapshot",
|
|
"CAST(after_snapshot AS CHAR) after_snapshot",
|
|
"CAST(metadata AS CHAR) metadata",
|
|
"created_at",
|
|
)
|
|
}
|
|
|
|
func (s *sSysImAuditLog) applyWhere(mod *gdb.Model, in *sysin.ImAuditLogListInp) *gdb.Model {
|
|
if in.ActorUid > 0 {
|
|
mod = mod.Where("actor_uid", in.ActorUid)
|
|
}
|
|
if in.ActorRole != "" {
|
|
mod = mod.WhereLike("actor_role", "%"+in.ActorRole+"%")
|
|
}
|
|
if in.Action != "" {
|
|
mod = mod.WhereLike("action", "%"+in.Action+"%")
|
|
}
|
|
if in.TargetType != "" {
|
|
mod = mod.Where("target_type", in.TargetType)
|
|
}
|
|
if in.TargetId != "" {
|
|
mod = mod.WhereLike("target_id", "%"+in.TargetId+"%")
|
|
}
|
|
if in.RequestId != "" {
|
|
mod = mod.WhereLike("request_id", "%"+in.RequestId+"%")
|
|
}
|
|
if in.Ip != "" {
|
|
mod = mod.WhereLike("ip", "%"+in.Ip+"%")
|
|
}
|
|
if in.Keyword != "" {
|
|
like := "%" + in.Keyword + "%"
|
|
mod = mod.Where("(action LIKE ? OR target_type LIKE ? OR target_id LIKE ? OR reason LIKE ? OR request_id LIKE ?)", like, like, like, like, like)
|
|
}
|
|
if len(in.CreatedAt) == 2 {
|
|
mod = mod.WhereBetween("created_at", in.CreatedAt[0], in.CreatedAt[1])
|
|
}
|
|
return mod
|
|
}
|
|
|
|
// View 获取IM审计日志指定信息
|
|
func (s *sSysImAuditLog) View(ctx context.Context, in *sysin.ImAuditLogViewInp) (res *sysin.ImAuditLogViewModel, err error) {
|
|
if err = in.Filter(ctx); err != nil {
|
|
return
|
|
}
|
|
db, tenant, err := imtenant.DB(ctx, in.TenantId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = s.model(ctx, db).Where("id", in.Id).Scan(&res)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "获取IM审计日志信息失败,请稍后重试!")
|
|
}
|
|
if res == nil {
|
|
return nil, nil
|
|
}
|
|
res.TenantId = tenant.Id
|
|
res.TenantName = tenant.Name
|
|
return
|
|
}
|