ads9.go 3.9 KB

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