writer.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package operate_log
  2. import (
  3. "encoding/json"
  4. "git.sxidc.com/go-framework/baize/framework/core/domain"
  5. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  6. "time"
  7. )
  8. type ObjectInfo struct {
  9. Resource string
  10. ResourceID string
  11. }
  12. type OperatorInfo struct {
  13. OperatorID string
  14. OperatorName string
  15. }
  16. type CanLogObject interface {
  17. ObjectInfo() ObjectInfo
  18. OperatorInfo() OperatorInfo
  19. LogContent() map[string]any
  20. }
  21. func WriteLog(dbExecutor database.Executor, dbSchema string, operate string, objects ...CanLogObject) error {
  22. vs := make([]ValueObject, len(objects))
  23. now := time.Now().Local()
  24. for i, object := range objects {
  25. objectInfo := object.ObjectInfo()
  26. operatorInfo := object.OperatorInfo()
  27. content := object.LogContent()
  28. jsonContent, err := json.Marshal(content)
  29. if err != nil {
  30. return err
  31. }
  32. vs[i] = ValueObject{
  33. Resource: objectInfo.Resource,
  34. ResourceID: objectInfo.ResourceID,
  35. Operate: operate,
  36. OperatorID: operatorInfo.OperatorID,
  37. OperatorName: operatorInfo.OperatorName,
  38. OperateTime: now,
  39. Content: string(jsonContent),
  40. }
  41. }
  42. err := database.InsertEntity(dbExecutor, domain.TableName(dbSchema, &ValueObject{}), vs)
  43. if err != nil {
  44. return err
  45. }
  46. return nil
  47. }