ads17.go 3.6 KB

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