system_log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TSystemLog struct {
  12. ID int64 `gorm:"column:id" json:"id"`
  13. Module string `gorm:"column:module" json:"module"`
  14. Action string `gorm:"column:action" json:"action"`
  15. Origin string `gorm:"column:origin" json:"origin"`
  16. Target string `gorm:"column:target" json:"target"`
  17. Username string `gorm:"column:username" json:"username"`
  18. Uid int64 `gorm:"column:uid" json:"uid"`
  19. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  20. Cid int64 `gorm:"column:cid" json:"cid"`
  21. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  22. }
  23. func (p *TSystemLog) TableName() string {
  24. return "t_system_log"
  25. }
  26. func (p *TSystemLog) Find(db *gorm.DB, where map[string]interface{}) error {
  27. err := db.Table(p.TableName()).Where(where).Find(p).Error
  28. if err != nil {
  29. fields, _ := util.MarshalToString(where)
  30. logger.Error("mysql",
  31. zap.String("sql", "select from "+p.TableName()),
  32. zap.String("fields", fields),
  33. zap.String("error", err.Error()))
  34. }
  35. return err
  36. }
  37. func (p *TSystemLog) Last(db *gorm.DB) error {
  38. err := db.Table(p.TableName()).Last(p).Error
  39. if err != nil {
  40. logger.Error("mysql",
  41. zap.String("sql", "select last from "+p.TableName()),
  42. zap.String("fields", ""),
  43. zap.String("error", err.Error()))
  44. }
  45. return err
  46. }
  47. // Insert 插入一条记录
  48. func (p *TSystemLog) Insert(db *gorm.DB) error {
  49. err := db.Create(p).Error
  50. if err != nil {
  51. fields, _ := util.MarshalToString(*p)
  52. logger.Error("mysql",
  53. zap.String("sql", "insert into "+p.TableName()),
  54. zap.String("fields", fields),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. func (p *TSystemLog) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  60. cond, val, err := whereBuildAndOr(where, or)
  61. if err != nil {
  62. return 0, err
  63. }
  64. ret := int64(0)
  65. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  66. if err != nil {
  67. fields, _ := util.MarshalToString(where)
  68. logger.Error("mysql",
  69. zap.String("sql", "select count "+p.TableName()),
  70. zap.String("fields", fields),
  71. zap.String("error", err.Error()))
  72. }
  73. return ret, err
  74. }
  75. func (p *TSystemLog) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TCompanyLog, err error) {
  76. cond, val, err := whereBuildAndOr(where, or)
  77. if err != nil {
  78. return list, err
  79. }
  80. if pageSize < 0 {
  81. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  82. if result.Error != nil {
  83. wherefields, _ := util.MarshalToString(where)
  84. logger.Error("mysql",
  85. zap.String("sql", "select * from "+p.TableName()),
  86. zap.String("where", wherefields),
  87. zap.String("error", err.Error()))
  88. }
  89. return list, result.Error
  90. }
  91. offset := (page - 1) * pageSize
  92. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  93. if result.Error != nil {
  94. wherefields, _ := util.MarshalToString(where)
  95. logger.Error("mysql",
  96. zap.String("sql", "select * from "+p.TableName()),
  97. zap.String("where", wherefields),
  98. zap.String("error", err.Error()))
  99. }
  100. return list, result.Error
  101. }