ads21.go 3.8 KB

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