ads16.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Ads16 struct {
  11. ID int64 `gorm:"primary_key" json:"-"`
  12. Vin string `json:"vin"`
  13. InitialRegistrationDate string `json:"initial_registration_date"`
  14. VehicleType string `json:"vehicle_type"`
  15. UseProperty string `json:"use_property"`
  16. FuelType string `json:"fuel_type"`
  17. ModelNo string `json:"model_no"`
  18. VehicleBodyColor string `json:"vehicle_body_color"`
  19. CreatedAt int64 `json:"-"`
  20. UpdatedAt int64 `json:"-"`
  21. }
  22. func (Ads16) TableName() string {
  23. return "t_adm_ads16"
  24. }
  25. // Insert 插入一条记录
  26. func (p *Ads16) Insert(db *gorm.DB) error {
  27. if p.CreatedAt == 0 {
  28. timeNow := time.Now().Unix()
  29. p.CreatedAt = timeNow
  30. p.UpdatedAt = timeNow
  31. }
  32. err := db.Create(p).Error
  33. if err != nil {
  34. l.Error("mysql",
  35. zap.String("sql", "insert into "+p.TableName()),
  36. zap.String("fields", utils.MarshalJsonString(*p)),
  37. zap.String("error", err.Error()))
  38. }
  39. return err
  40. }
  41. func (p *Ads16) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  42. err := db.Model(p).Create(data).Error
  43. if err != nil {
  44. l.Error("mysql",
  45. zap.String("sql", "insert into "+p.TableName()),
  46. zap.String("fields", utils.MarshalJsonString(data)),
  47. zap.String("error", err.Error()))
  48. }
  49. return err
  50. }
  51. func (p *Ads16) Delete(db *gorm.DB, filter map[string]interface{}) error {
  52. err := db.Where(filter).Delete(p).Error
  53. if err != nil {
  54. l.Error("mysql",
  55. zap.String("sql", "delete from "+p.TableName()),
  56. zap.String("fields", utils.MarshalJsonString(filter)),
  57. zap.String("error", err.Error()))
  58. }
  59. return err
  60. }
  61. func (p *Ads16) Save(db *gorm.DB) error {
  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 *Ads16) Update(db *gorm.DB) error {
  72. p.CreatedAt = 0
  73. if p.UpdatedAt == 0 {
  74. timeNow := time.Now().Unix()
  75. p.UpdatedAt = timeNow
  76. }
  77. err := db.Model(p).UpdateColumns(p).Error
  78. if err != nil {
  79. l.Error("mysql",
  80. zap.String("sql", "update "+p.TableName()),
  81. zap.String("fields", utils.MarshalJsonString(*p)),
  82. zap.String("error", err.Error()))
  83. }
  84. return err
  85. }
  86. func (p *Ads16) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  87. err := db.Model(p).Updates(data).Error
  88. if err != nil {
  89. l.Error("mysql",
  90. zap.String("sql", "update "+p.TableName()),
  91. zap.String("fields", utils.MarshalJsonString(data)),
  92. zap.String("error", err.Error()))
  93. }
  94. return err
  95. }
  96. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  97. func (p *Ads16) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  98. err := db.Model(p).Updates(filed).Error
  99. if err != nil {
  100. l.Error("mysql",
  101. zap.String("sql", "update "+p.TableName()),
  102. zap.String("fields", utils.MarshalJsonString(filed)),
  103. zap.String("error", err.Error()))
  104. }
  105. return err
  106. }
  107. func (p *Ads16) Query(db *gorm.DB, filter map[string]interface{}) error {
  108. err := db.Where(filter).Find(p).Error
  109. if err != nil {
  110. l.Error("mysql",
  111. zap.String("sql", "select from "+p.TableName()),
  112. zap.String("fields", utils.MarshalJsonString(filter)),
  113. zap.String("error", err.Error()))
  114. }
  115. if p.ID == 0 {
  116. return gorm.ErrRecordNotFound
  117. }
  118. return err
  119. }
  120. func (p *Ads16) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  121. err := db.Where(filter).Find(out).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. return err
  129. }