ods1.go 4.3 KB

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