management_log.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 TManagementLog 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. }
  21. func (p *TManagementLog) TableName() string {
  22. return "t_management_log"
  23. }
  24. func (p *TManagementLog) Find(db *gorm.DB, where map[string]interface{}) error {
  25. err := db.Table(p.TableName()).Where(where).Find(p).Error
  26. if err != nil {
  27. fields, _ := util.MarshalToString(where)
  28. logger.Error("mysql",
  29. zap.String("sql", "select from "+p.TableName()),
  30. zap.String("fields", fields),
  31. zap.String("error", err.Error()))
  32. }
  33. return err
  34. }
  35. func (p *TManagementLog) Last(db *gorm.DB) error {
  36. err := db.Table(p.TableName()).Last(p).Error
  37. if err != nil {
  38. logger.Error("mysql",
  39. zap.String("sql", "select last from "+p.TableName()),
  40. zap.String("fields", ""),
  41. zap.String("error", err.Error()))
  42. }
  43. return err
  44. }
  45. // Insert 插入一条记录
  46. func (p *TManagementLog) Insert(db *gorm.DB) error {
  47. err := db.Create(p).Error
  48. if err != nil {
  49. fields, _ := util.MarshalToString(*p)
  50. logger.Error("mysql",
  51. zap.String("sql", "insert into "+p.TableName()),
  52. zap.String("fields", fields),
  53. zap.String("error", err.Error()))
  54. }
  55. return err
  56. }
  57. func (p *TManagementLog) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  58. cond, val, err := whereBuildAndOr(where, or)
  59. if err != nil {
  60. return 0, err
  61. }
  62. ret := int64(0)
  63. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  64. if err != nil {
  65. fields, _ := util.MarshalToString(where)
  66. logger.Error("mysql",
  67. zap.String("sql", "select count "+p.TableName()),
  68. zap.String("fields", fields),
  69. zap.String("error", err.Error()))
  70. }
  71. return ret, err
  72. }
  73. func (p *TManagementLog) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TManagementLog, err error) {
  74. cond, val, err := whereBuildAndOr(where, or)
  75. if err != nil {
  76. return list, err
  77. }
  78. if pageSize < 0 {
  79. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  80. if result.Error != nil {
  81. wherefields, _ := util.MarshalToString(where)
  82. logger.Error("mysql",
  83. zap.String("sql", "select * from "+p.TableName()),
  84. zap.String("where", wherefields),
  85. zap.String("error", err.Error()))
  86. }
  87. return list, result.Error
  88. }
  89. offset := (page - 1) * pageSize
  90. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  91. if result.Error != nil {
  92. wherefields, _ := util.MarshalToString(where)
  93. logger.Error("mysql",
  94. zap.String("sql", "select * from "+p.TableName()),
  95. zap.String("where", wherefields),
  96. zap.String("error", err.Error()))
  97. }
  98. return list, result.Error
  99. }