dws10.go 4.3 KB

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