operation_log.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 TOperationLog struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. User string `gorm:"column:user" json:"user"`
  14. Uid int64 `gorm:"column:uid" json:"uid"`
  15. Module int32 `gorm:"column:module" json:"module"`
  16. Operation int32 `gorm:"column:operation" json:"operation"`
  17. OriginContent string `gorm:"column:origin_content" json:"origin_content"`
  18. TargetContent string `gorm:"column:target_content" json:"target_content"`
  19. OperationTime int64 `gorm:"column:operation_time" json:"operation_time"`
  20. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  21. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  22. }
  23. func (p *TOperationLog) TableName() string {
  24. return "t_operation_log"
  25. }
  26. func (p *TOperationLog) Find(db *gorm.DB, where map[string]interface{}) error {
  27. err := db.Table(p.TableName()).Where(where).First(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 *TOperationLog) 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 *TOperationLog) 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 *TOperationLog) Delete(db *gorm.DB, filter map[string]interface{}) error {
  60. err := db.Where(filter).Delete(p).Error
  61. if err != nil {
  62. fields, _ := util.MarshalToString(filter)
  63. logger.Error("mysql",
  64. zap.String("sql", "delete from "+p.TableName()),
  65. zap.String("where", fields),
  66. zap.String("error", err.Error()))
  67. }
  68. return err
  69. }
  70. func (p *TOperationLog) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  71. cond, val, err := whereBuild(where)
  72. if err != nil {
  73. if err != nil {
  74. fields, _ := util.MarshalToString(values)
  75. logger.Error("mysql",
  76. zap.String("sql", "update "+p.TableName()),
  77. zap.String("fields", fields),
  78. zap.String("error", err.Error()))
  79. }
  80. return err
  81. }
  82. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  83. }
  84. func (p *TOperationLog) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  85. cond, val, err := whereBuildAndOr(where, or)
  86. if err != nil {
  87. return 0, err
  88. }
  89. ret := int64(0)
  90. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  91. if err != nil {
  92. fields, _ := util.MarshalToString(where)
  93. logger.Error("mysql",
  94. zap.String("sql", "select count "+p.TableName()),
  95. zap.String("fields", fields),
  96. zap.String("error", err.Error()))
  97. }
  98. return ret, err
  99. }
  100. func (p *TOperationLog) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TOperationLog, err error) {
  101. cond, val, err := whereBuildAndOr(where, or)
  102. if err != nil {
  103. return list, err
  104. }
  105. if pageSize < 0 {
  106. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  107. if result.Error != nil {
  108. wherefields, _ := util.MarshalToString(where)
  109. logger.Error("mysql",
  110. zap.String("sql", "select * from "+p.TableName()),
  111. zap.String("where", wherefields),
  112. zap.String("error", result.Error.Error()))
  113. }
  114. return list, result.Error
  115. }
  116. offset := (page - 1) * pageSize
  117. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).Find(&list)
  118. if result.Error != nil {
  119. wherefields, _ := util.MarshalToString(where)
  120. logger.Error("mysql",
  121. zap.String("sql", "select * from "+p.TableName()),
  122. zap.String("where", wherefields),
  123. zap.String("error", result.Error.Error()))
  124. }
  125. return list, result.Error
  126. }