44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package queues
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"hotgo/internal/consts"
|
|
"hotgo/internal/library/imkafka"
|
|
"hotgo/internal/library/queue"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
)
|
|
|
|
func init() {
|
|
queue.RegisterConsumer(ImAuditEvent)
|
|
}
|
|
|
|
var ImAuditEvent = &qImAuditEvent{}
|
|
|
|
type qImAuditEvent struct{}
|
|
|
|
func (q *qImAuditEvent) GetTopic() string {
|
|
return consts.QueueImAuditEventTopic
|
|
}
|
|
|
|
func (q *qImAuditEvent) Handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
|
var envelope imkafka.Envelope
|
|
if err = json.Unmarshal(mqMsg.Body, &envelope); err != nil {
|
|
return err
|
|
}
|
|
payload := map[string]any{}
|
|
if len(envelope.Payload) > 0 {
|
|
_ = json.Unmarshal(envelope.Payload, &payload)
|
|
}
|
|
_, err = g.DB().Exec(ctx, `INSERT IGNORE INTO im_audit_events
|
|
(event_id,tenant_code,event_type,biz_type,biz_id,actor_type,actor_id,action,resource,resource_id,result,reason,payload,envelope,occurred_at,received_at)
|
|
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,CAST(NULLIF(?, '') AS JSON),CAST(? AS JSON),?,?)`,
|
|
envelope.EventID, envelope.TenantCode, envelope.EventType, envelope.BizType, envelope.BizID,
|
|
firstString(payload["actor_type"]), firstString(payload["actor_id"]), firstString(payload["action"]),
|
|
firstString(payload["resource"]), firstString(payload["resource_id"]), firstString(payload["result"]),
|
|
firstString(payload["reason"]), string(envelope.Payload), string(mqMsg.Body), envelope.OccurredAt, gtime.Now())
|
|
return err
|
|
}
|