ads1.go 3.6 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 Ads1 struct {
  11. ID int64 `gorm:"primary_key"`
  12. PlateNo string `json:"plate_no"`
  13. PlateType string `json:"plate_type"`
  14. Vin string `json:"vin"`
  15. Source int `json:"source"`
  16. CreatedAt int64 `json:"created_at"`
  17. UpdatedAt int64 `json:"updated_at"`
  18. }
  19. func (Ads1) TableName() string {
  20. return "t_adm_ads1"
  21. }
  22. // Insert 插入一条记录
  23. func (p *Ads1) Insert(db *gorm.DB) error {
  24. if p.CreatedAt == 0 {
  25. timeNow := time.Now().Unix()
  26. p.CreatedAt = timeNow
  27. p.UpdatedAt = timeNow
  28. }
  29. err := db.Create(p).Error
  30. if err != nil {
  31. l.Error("mysql",
  32. zap.String("sql", "insert into "+p.TableName()),
  33. zap.String("fields", utils.MarshalJsonString(*p)),
  34. zap.String("error", err.Error()))
  35. }
  36. return err
  37. }
  38. func (p *Ads1)InsertByMap(db *gorm.DB,data map[string]interface{}) error {
  39. err := db.Model(p).Create(data).Error
  40. if err != nil{
  41. l.Error("mysql",
  42. zap.String("sql", "insert into "+p.TableName()),
  43. zap.String("fields", utils.MarshalJsonString(data)),
  44. zap.String("error", err.Error()))
  45. }
  46. return err
  47. }
  48. func (p *Ads1) Delete(db *gorm.DB, filter map[string]interface{}) error {
  49. err := db.Where(filter).Delete(p).Error
  50. if err != nil {
  51. l.Error("mysql",
  52. zap.String("sql", "delete from "+p.TableName()),
  53. zap.String("fields", utils.MarshalJsonString(filter)),
  54. zap.String("error", err.Error()))
  55. }
  56. return err
  57. }
  58. func (p *Ads1) Save(db *gorm.DB) error {
  59. err := db.Save(p).Error
  60. if err != nil {
  61. l.Error("mysql",
  62. zap.String("sql", "save "+p.TableName()),
  63. zap.String("fields", utils.MarshalJsonString(*p)),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. func (p *Ads1) Update(db *gorm.DB) error {
  69. p.CreatedAt = 0
  70. if p.UpdatedAt == 0 {
  71. timeNow := time.Now().Unix()
  72. p.UpdatedAt = timeNow
  73. }
  74. err := db.Model(p).UpdateColumns(p).Error
  75. if err != nil {
  76. l.Error("mysql",
  77. zap.String("sql", "update "+p.TableName()),
  78. zap.String("fields", utils.MarshalJsonString(*p)),
  79. zap.String("error", err.Error()))
  80. }
  81. return err
  82. }
  83. func (p *Ads1)UpdateByMap(db *gorm.DB,data map[string]interface{}) error {
  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 *Ads1) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  95. err := db.Model(p).Updates(filed).Error
  96. if err != nil {
  97. l.Error("mysql",
  98. zap.String("sql", "update "+p.TableName()),
  99. zap.String("fields", utils.MarshalJsonString(filed)),
  100. zap.String("error", err.Error()))
  101. }
  102. return err
  103. }
  104. func (p *Ads1) Query(db *gorm.DB, filter map[string]interface{}) error {
  105. err := db.Where(filter).Find(p).Error
  106. if err != nil {
  107. l.Error("mysql",
  108. zap.String("sql", "select from "+p.TableName()),
  109. zap.String("fields", utils.MarshalJsonString(filter)),
  110. zap.String("error", err.Error()))
  111. }
  112. if p.ID == 0 {
  113. return gorm.ErrRecordNotFound
  114. }
  115. return err
  116. }
  117. func (p *Ads1) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  118. err := db.Where(filter).Find(out).Error
  119. if err != nil {
  120. l.Error("mysql",
  121. zap.String("sql", "select from "+p.TableName()),
  122. zap.String("fields", utils.MarshalJsonString(filter)),
  123. zap.String("error", err.Error()))
  124. }
  125. return err
  126. }