fix: use unprefixed kafka audit tables
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
@@ -23,64 +24,117 @@ func (s *sSysImKafkaAudit) List(ctx context.Context, in *sysin.ImKafkaAuditListI
|
||||
if err = in.Filter(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
mod := g.DB().Model("im_audit_events").Safe().Ctx(ctx).
|
||||
Fields("id", "event_id", "tenant_code", "event_type", "biz_type", "biz_id", "actor_type", "actor_id", "action", "resource", "resource_id", "result", "reason", "occurred_at", "received_at")
|
||||
if in.TenantCode != "" {
|
||||
mod = mod.Where("tenant_code", in.TenantCode)
|
||||
where, args := buildImKafkaAuditWhere(in)
|
||||
totalValue, err := g.DB().GetValue(ctx, "SELECT COUNT(*) FROM im_audit_events "+where, args...)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, "获取IM Kafka审计事件总数失败,请稍后重试!")
|
||||
return
|
||||
}
|
||||
if in.EventId != "" {
|
||||
mod = mod.WhereLike("event_id", "%"+in.EventId+"%")
|
||||
totalCount = totalValue.Int()
|
||||
|
||||
page := in.Page
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if in.EventType != "" {
|
||||
mod = mod.WhereLike("event_type", "%"+in.EventType+"%")
|
||||
perPage := in.PerPage
|
||||
if perPage <= 0 {
|
||||
perPage = 20
|
||||
}
|
||||
if in.BizType != "" {
|
||||
mod = mod.Where("biz_type", in.BizType)
|
||||
}
|
||||
if in.BizId != "" {
|
||||
mod = mod.WhereLike("biz_id", "%"+in.BizId+"%")
|
||||
}
|
||||
if in.ActorType != "" {
|
||||
mod = mod.Where("actor_type", in.ActorType)
|
||||
}
|
||||
if in.ActorId != "" {
|
||||
mod = mod.WhereLike("actor_id", "%"+in.ActorId+"%")
|
||||
}
|
||||
if in.Action != "" {
|
||||
mod = mod.WhereLike("action", "%"+in.Action+"%")
|
||||
}
|
||||
if in.Resource != "" {
|
||||
mod = mod.Where("resource", in.Resource)
|
||||
}
|
||||
if in.ResourceId != "" {
|
||||
mod = mod.WhereLike("resource_id", "%"+in.ResourceId+"%")
|
||||
}
|
||||
if in.Result != "" {
|
||||
mod = mod.Where("result", in.Result)
|
||||
}
|
||||
if len(in.CreatedAt) == 2 {
|
||||
mod = mod.WhereBetween("received_at", in.CreatedAt[0], in.CreatedAt[1])
|
||||
}
|
||||
mod = mod.Page(in.Page, in.PerPage).OrderDesc("id")
|
||||
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
||||
offset := (page - 1) * perPage
|
||||
query := `SELECT id,event_id,tenant_code,event_type,biz_type,biz_id,actor_type,actor_id,
|
||||
action,resource,resource_id,result,reason,occurred_at,received_at
|
||||
FROM im_audit_events ` + where + ` ORDER BY id DESC LIMIT ? OFFSET ?`
|
||||
queryArgs := append(append([]any{}, args...), perPage, offset)
|
||||
rows, err := g.DB().GetAll(ctx, query, queryArgs...)
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, "获取IM Kafka审计事件列表失败,请稍后重试!")
|
||||
return
|
||||
}
|
||||
if err = rows.Structs(&list); err != nil {
|
||||
err = gerror.Wrap(err, "解析IM Kafka审计事件列表失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func buildImKafkaAuditWhere(in *sysin.ImKafkaAuditListInp) (string, []any) {
|
||||
parts := []string{"1=1"}
|
||||
args := []any{}
|
||||
if in.TenantCode != "" {
|
||||
parts = append(parts, "tenant_code=?")
|
||||
args = append(args, in.TenantCode)
|
||||
}
|
||||
if in.EventId != "" {
|
||||
parts = append(parts, "event_id LIKE ?")
|
||||
args = append(args, "%"+in.EventId+"%")
|
||||
}
|
||||
if in.EventType != "" {
|
||||
parts = append(parts, "event_type LIKE ?")
|
||||
args = append(args, "%"+in.EventType+"%")
|
||||
}
|
||||
if in.BizType != "" {
|
||||
parts = append(parts, "biz_type=?")
|
||||
args = append(args, in.BizType)
|
||||
}
|
||||
if in.BizId != "" {
|
||||
parts = append(parts, "biz_id LIKE ?")
|
||||
args = append(args, "%"+in.BizId+"%")
|
||||
}
|
||||
if in.ActorType != "" {
|
||||
parts = append(parts, "actor_type=?")
|
||||
args = append(args, in.ActorType)
|
||||
}
|
||||
if in.ActorId != "" {
|
||||
parts = append(parts, "actor_id LIKE ?")
|
||||
args = append(args, "%"+in.ActorId+"%")
|
||||
}
|
||||
if in.Action != "" {
|
||||
parts = append(parts, "action LIKE ?")
|
||||
args = append(args, "%"+in.Action+"%")
|
||||
}
|
||||
if in.Resource != "" {
|
||||
parts = append(parts, "resource=?")
|
||||
args = append(args, in.Resource)
|
||||
}
|
||||
if in.ResourceId != "" {
|
||||
parts = append(parts, "resource_id LIKE ?")
|
||||
args = append(args, "%"+in.ResourceId+"%")
|
||||
}
|
||||
if in.Result != "" {
|
||||
parts = append(parts, "result=?")
|
||||
args = append(args, in.Result)
|
||||
}
|
||||
if len(in.CreatedAt) == 2 {
|
||||
parts = append(parts, "received_at BETWEEN ? AND ?")
|
||||
args = append(args, in.CreatedAt[0], in.CreatedAt[1])
|
||||
}
|
||||
return "WHERE " + strings.Join(parts, " AND "), args
|
||||
}
|
||||
|
||||
func (s *sSysImKafkaAudit) View(ctx context.Context, in *sysin.ImKafkaAuditViewInp) (res *sysin.ImKafkaAuditViewModel, err error) {
|
||||
if err = in.Filter(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mod := g.DB().Model("im_audit_events").Safe().Ctx(ctx).
|
||||
Fields("id", "event_id", "tenant_code", "event_type", "biz_type", "biz_id", "actor_type", "actor_id", "action", "resource", "resource_id", "result", "reason", "CAST(payload AS CHAR) payload", "CAST(envelope AS CHAR) envelope", "occurred_at", "received_at")
|
||||
query := `SELECT id,event_id,tenant_code,event_type,biz_type,biz_id,actor_type,actor_id,
|
||||
action,resource,resource_id,result,reason,CAST(payload AS CHAR) payload,
|
||||
CAST(envelope AS CHAR) envelope,occurred_at,received_at FROM im_audit_events WHERE `
|
||||
args := []any{}
|
||||
if in.Id > 0 {
|
||||
mod = mod.Where("id", in.Id)
|
||||
query += "id=?"
|
||||
args = append(args, in.Id)
|
||||
} else {
|
||||
mod = mod.Where("event_id", in.EventId)
|
||||
query += "event_id=?"
|
||||
args = append(args, in.EventId)
|
||||
}
|
||||
if err = mod.Scan(&res); err != nil {
|
||||
query += " LIMIT 1"
|
||||
row, err := g.DB().GetOne(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "获取IM Kafka审计事件详情失败,请稍后重试!")
|
||||
}
|
||||
if row.IsEmpty() {
|
||||
return nil, nil
|
||||
}
|
||||
if err = row.Struct(&res); err != nil {
|
||||
return nil, gerror.Wrap(err, "解析IM Kafka审计事件详情失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user