ads8.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Ads8 struct {
  11. ID int64 `gorm:"primary_key" json:"-"`
  12. Vin string `json:"vin"`
  13. ThirdStyleId string `json:"third_style_id"`
  14. Source string `json:"source"`
  15. CreatedAt int64 `json:"-"`
  16. UpdatedAt int64 `json:"-"`
  17. }
  18. func (Ads8) TableName() string {
  19. return "t_adm_ads8"
  20. }
  21. // Insert 插入一条记录
  22. func (p *Ads8) Insert(db *gorm.DB) error {
  23. if p.CreatedAt == 0 {
  24. timeNow := time.Now().Unix()
  25. p.CreatedAt = timeNow
  26. p.UpdatedAt = timeNow
  27. }
  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 *Ads8) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  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 *Ads8) 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 *Ads8) Save(db *gorm.DB) error {
  58. err := db.Save(p).Error
  59. if err != nil {
  60. l.Error("mysql",
  61. zap.String("sql", "save "+p.TableName()),
  62. zap.String("fields", utils.MarshalJsonString(*p)),
  63. zap.String("error", err.Error()))
  64. }
  65. return err
  66. }
  67. func (p *Ads8) Update(db *gorm.DB) error {
  68. p.CreatedAt = 0
  69. if p.UpdatedAt == 0 {
  70. timeNow := time.Now().Unix()
  71. p.UpdatedAt = timeNow
  72. }
  73. err := db.Model(p).UpdateColumns(p).Error
  74. if err != nil {
  75. l.Error("mysql",
  76. zap.String("sql", "update "+p.TableName()),
  77. zap.String("fields", utils.MarshalJsonString(*p)),
  78. zap.String("error", err.Error()))
  79. }
  80. return err
  81. }
  82. func (p *Ads8) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  83. err := db.Model(p).Updates(data).Error
  84. if err != nil {
  85. l.Error("mysql",
  86. zap.String("sql", "update "+p.TableName()),
  87. zap.String("fields", utils.MarshalJsonString(data)),
  88. zap.String("error", err.Error()))
  89. }
  90. return err
  91. }
  92. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  93. func (p *Ads8) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  94. err := db.Model(p).Updates(filed).Error
  95. if err != nil {
  96. l.Error("mysql",
  97. zap.String("sql", "update "+p.TableName()),
  98. zap.String("fields", utils.MarshalJsonString(filed)),
  99. zap.String("error", err.Error()))
  100. }
  101. return err
  102. }
  103. func (p *Ads8) Query(db *gorm.DB, filter map[string]interface{}) error {
  104. err := db.Where(filter).Find(p).Error
  105. if err != nil {
  106. l.Error("mysql",
  107. zap.String("sql", "select from "+p.TableName()),
  108. zap.String("fields", utils.MarshalJsonString(filter)),
  109. zap.String("error", err.Error()))
  110. }
  111. if p.ID == 0 {
  112. return gorm.ErrRecordNotFound
  113. }
  114. return err
  115. }
  116. func (p *Ads8) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  117. err := db.Where(filter).Find(out).Error
  118. if err != nil {
  119. l.Error("mysql",
  120. zap.String("sql", "select from "+p.TableName()),
  121. zap.String("fields", utils.MarshalJsonString(filter)),
  122. zap.String("error", err.Error()))
  123. }
  124. return err
  125. }