ods17.go 4.3 KB

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