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