fail_msg.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "adm-ods/common.in/utils"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type FailMsg struct {
  11. ID int64 `gorm:"primary_key"`
  12. Msg string `json:"msg"`
  13. CreatedAt int64 `json:"created_at"`
  14. UpdatedAt int64 `json:"updated_at"`
  15. }
  16. func (FailMsg) TableName() string {
  17. return "t_adm_ods_fail_msg"
  18. }
  19. // Insert 插入一条记录
  20. func (p *FailMsg) Insert(db *gorm.DB) error {
  21. timeNow := time.Now().Unix()
  22. p.CreatedAt = timeNow
  23. p.UpdatedAt = timeNow
  24. err := db.Create(p).Error
  25. if err != nil {
  26. l.Error("mysql",
  27. zap.String("sql", "insert into "+p.TableName()),
  28. zap.String("fields", utils.MarshalJsonString(*p)),
  29. zap.String("error", err.Error()))
  30. }
  31. return err
  32. }
  33. func (p *FailMsg) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  34. data["updated_at"] = time.Now().Unix()
  35. err := db.Model(p).Create(data).Error
  36. if err != nil {
  37. l.Error("mysql",
  38. zap.String("sql", "insert into "+p.TableName()),
  39. zap.String("fields", utils.MarshalJsonString(data)),
  40. zap.String("error", err.Error()))
  41. }
  42. return err
  43. }
  44. func (p *FailMsg) Delete(db *gorm.DB, filter map[string]interface{}) error {
  45. err := db.Where(filter).Delete(p).Error
  46. if err != nil {
  47. l.Error("mysql",
  48. zap.String("sql", "delete from "+p.TableName()),
  49. zap.String("fields", utils.MarshalJsonString(filter)),
  50. zap.String("error", err.Error()))
  51. }
  52. return err
  53. }
  54. func (p *FailMsg) Save(db *gorm.DB) error {
  55. timeNow := time.Now().Unix()
  56. p.UpdatedAt = timeNow
  57. //db.Model().Update()
  58. err := db.Save(p).Error
  59. if err != nil {
  60. l.Error("mysql",
  61. zap.String("sql", "save "+p.TableName()),
  62. zap.String("fields", utils.MarshalJsonString(*p)),
  63. zap.String("error", err.Error()))
  64. }
  65. return err
  66. }
  67. func (p *FailMsg) Update(db *gorm.DB) error {
  68. timeNow := time.Now().Unix()
  69. p.CreatedAt = 0
  70. p.UpdatedAt = timeNow
  71. err := db.Model(p).Updates(p).Error
  72. if err != nil {
  73. l.Error("mysql",
  74. zap.String("sql", "update "+p.TableName()),
  75. zap.String("fields", utils.MarshalJsonString(*p)),
  76. zap.String("error", err.Error()))
  77. }
  78. return err
  79. }
  80. func (p *FailMsg) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  81. data["updated_at"] = time.Now().Unix()
  82. err := db.Model(p).Updates(data).Error
  83. if err != nil {
  84. l.Error("mysql",
  85. zap.String("sql", "update "+p.TableName()),
  86. zap.String("fields", utils.MarshalJsonString(data)),
  87. zap.String("error", err.Error()))
  88. }
  89. return err
  90. }
  91. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  92. func (p *FailMsg) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  93. /*if filed == nil {
  94. return errors.ParamsError
  95. }*/
  96. timeNow := time.Now().Unix()
  97. filed["updated_at"] = timeNow
  98. err := db.Model(p).Updates(filed).Error
  99. if err != nil {
  100. l.Error("mysql",
  101. zap.String("sql", "update "+p.TableName()),
  102. zap.String("fields", utils.MarshalJsonString(filed)),
  103. zap.String("error", err.Error()))
  104. }
  105. return err
  106. }
  107. func (p *FailMsg) Query(db *gorm.DB, filter map[string]interface{}) error {
  108. err := db.Where(filter).Find(p).Error
  109. if err != nil {
  110. l.Error("mysql",
  111. zap.String("sql", "select from "+p.TableName()),
  112. zap.String("fields", utils.MarshalJsonString(filter)),
  113. zap.String("error", err.Error()))
  114. }
  115. if p.ID == 0 {
  116. return gorm.ErrRecordNotFound
  117. }
  118. return err
  119. }
  120. func (p *FailMsg) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  121. err := db.Where(filter).Find(out).Error
  122. if err != nil {
  123. l.Error("mysql",
  124. zap.String("sql", "select from "+p.TableName()),
  125. zap.String("fields", utils.MarshalJsonString(filter)),
  126. zap.String("error", err.Error()))
  127. }
  128. return err
  129. }