operation_log.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Table(p.TableName()).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. cond, val, err := whereBuild(filter)
  61. if err != nil {
  62. return err
  63. }
  64. err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  65. if err != nil {
  66. fields, _ := util.MarshalToString(filter)
  67. logger.Error("mysql",
  68. zap.String("sql", "delete from "+p.TableName()),
  69. zap.String("where", fields),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *TOperationLog) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  75. cond, val, err := whereBuild(where)
  76. if err != nil {
  77. if err != nil {
  78. fields, _ := util.MarshalToString(values)
  79. logger.Error("mysql",
  80. zap.String("sql", "update "+p.TableName()),
  81. zap.String("fields", fields),
  82. zap.String("error", err.Error()))
  83. }
  84. return err
  85. }
  86. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  87. }
  88. func (p *TOperationLog) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  89. cond, val, err := whereBuildAndOr(where, or)
  90. if err != nil {
  91. return 0, err
  92. }
  93. ret := int64(0)
  94. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  95. if err != nil {
  96. fields, _ := util.MarshalToString(where)
  97. logger.Error("mysql",
  98. zap.String("sql", "select count "+p.TableName()),
  99. zap.String("fields", fields),
  100. zap.String("error", err.Error()))
  101. }
  102. return ret, err
  103. }
  104. func (p *TOperationLog) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TOperationLog, err error) {
  105. cond, val, err := whereBuildAndOr(where, or)
  106. if err != nil {
  107. return list, err
  108. }
  109. if pageSize < 0 {
  110. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  111. if result.Error != nil {
  112. wherefields, _ := util.MarshalToString(where)
  113. logger.Error("mysql",
  114. zap.String("sql", "select * from "+p.TableName()),
  115. zap.String("where", wherefields),
  116. zap.String("error", result.Error.Error()))
  117. }
  118. return list, result.Error
  119. }
  120. offset := (page - 1) * pageSize
  121. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).Find(&list)
  122. if result.Error != nil {
  123. wherefields, _ := util.MarshalToString(where)
  124. logger.Error("mysql",
  125. zap.String("sql", "select * from "+p.TableName()),
  126. zap.String("where", wherefields),
  127. zap.String("error", result.Error.Error()))
  128. }
  129. return list, result.Error
  130. }