ads14.go 3.9 KB

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