ods6.go 4.2 KB

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