12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package operate_log
- import (
- "encoding/json"
- "git.sxidc.com/go-framework/baize/framework/core/domain"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "time"
- )
- type ObjectInfo struct {
- Resource string
- ResourceID string
- }
- type OperatorInfo struct {
- OperatorID string
- OperatorName string
- }
- type CanLogObject interface {
- ObjectInfo() ObjectInfo
- OperatorInfo() OperatorInfo
- LogContent() map[string]any
- }
- func WriteLog(dbExecutor database.Executor, dbSchema string, operate string, objects ...CanLogObject) error {
- vs := make([]ValueObject, len(objects))
- now := time.Now().Local()
- for i, object := range objects {
- objectInfo := object.ObjectInfo()
- operatorInfo := object.OperatorInfo()
- content := object.LogContent()
- jsonContent, err := json.Marshal(content)
- if err != nil {
- return err
- }
- vs[i] = ValueObject{
- Resource: objectInfo.Resource,
- ResourceID: objectInfo.ResourceID,
- Operate: operate,
- OperatorID: operatorInfo.OperatorID,
- OperatorName: operatorInfo.OperatorName,
- OperateTime: now,
- Content: string(jsonContent),
- }
- }
- err := database.InsertEntity(dbExecutor, domain.TableName(dbSchema, &ValueObject{}), vs)
- if err != nil {
- return err
- }
- return nil
- }
|