dws13.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package model
  4. import (
  5. "gadm-dws/common.in/utils"
  6. "gorm.io/gorm"
  7. "go.uber.org/zap"
  8. "time"
  9. )
  10. type Dws13 struct {
  11. ID int64 `gorm:"primary_key" json:"-"`
  12. ThirdBrandId string `json:"third_brand_id"`
  13. ThirdSeriesId string `json:"third_series_id"`
  14. ThirdStyleId string `json:"third_style_id"`
  15. ThirdBrandName string `json:"third_brand_name"`
  16. ThirdSeriesName string `json:"third_series_name"`
  17. ThirdStyleName string `json:"third_style_name"`
  18. ThirdMaker string `json:"third_maker"`
  19. ModelYear string `json:"model_year"`
  20. StyleId string `json:"style_id"`
  21. Source string `json:"source"`
  22. CreatedAt int64 `json:"-"`
  23. UpdatedAt int64 `json:"-"`
  24. }
  25. func (Dws13) TableName() string {
  26. return "t_adm_dws13"
  27. }
  28. // Insert 插入一条记录
  29. func (p *Dws13) Insert(db *gorm.DB) error {
  30. timeNow := time.Now().Unix()
  31. p.CreatedAt = timeNow
  32. p.UpdatedAt = timeNow
  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 *Dws13)InsertByMap(db *gorm.DB,data map[string]interface{}) error {
  43. data["updated_at"] = time.Now().Unix()
  44. err := db.Model(p).Create(data).Error
  45. if err != nil{
  46. l.Error("mysql",
  47. zap.String("sql", "insert into "+p.TableName()),
  48. zap.String("fields", utils.MarshalJsonString(data)),
  49. zap.String("error", err.Error()))
  50. }
  51. return err
  52. }
  53. func (p *Dws13) Delete(db *gorm.DB, filter map[string]interface{}) error {
  54. err := db.Where(filter).Delete(p).Error
  55. if err != nil {
  56. l.Error("mysql",
  57. zap.String("sql", "delete from "+p.TableName()),
  58. zap.String("fields", utils.MarshalJsonString(filter)),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *Dws13) Save(db *gorm.DB) error {
  64. timeNow := time.Now().Unix()
  65. p.UpdatedAt = timeNow
  66. //db.Model().Update()
  67. err := db.Save(p).Error
  68. if err != nil {
  69. l.Error("mysql",
  70. zap.String("sql", "save "+p.TableName()),
  71. zap.String("fields", utils.MarshalJsonString(*p)),
  72. zap.String("error", err.Error()))
  73. }
  74. return err
  75. }
  76. func (p *Dws13) Update(db *gorm.DB) error {
  77. timeNow := time.Now().Unix()
  78. p.CreatedAt = 0
  79. p.UpdatedAt = timeNow
  80. err := db.Model(p).Updates(p).Error
  81. if err != nil {
  82. l.Error("mysql",
  83. zap.String("sql", "update "+p.TableName()),
  84. zap.String("fields", utils.MarshalJsonString(*p)),
  85. zap.String("error", err.Error()))
  86. }
  87. return err
  88. }
  89. func (p *Dws13)UpdateByMap(db *gorm.DB,data map[string]interface{}) error {
  90. data["updated_at"] = time.Now().Unix()
  91. err := db.Model(p).Updates(data).Error
  92. if err != nil{
  93. l.Error("mysql",
  94. zap.String("sql", "update "+p.TableName()),
  95. zap.String("fields", utils.MarshalJsonString(data)),
  96. zap.String("error", err.Error()))
  97. }
  98. return err
  99. }
  100. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  101. func (p *Dws13) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  102. /*if filed == nil {
  103. return errors.ParamsError
  104. }*/
  105. timeNow := time.Now().Unix()
  106. filed["updated_at"] = timeNow
  107. err := db.Model(p).Updates(filed).Error
  108. if err != nil {
  109. l.Error("mysql",
  110. zap.String("sql", "update "+p.TableName()),
  111. zap.String("fields", utils.MarshalJsonString(filed)),
  112. zap.String("error", err.Error()))
  113. }
  114. return err
  115. }
  116. func (p *Dws13) Query(db *gorm.DB, filter map[string]interface{}) error {
  117. err := db.Where(filter).Find(p).Error
  118. if err != nil {
  119. l.Error("mysql",
  120. zap.String("sql", "select from "+p.TableName()),
  121. zap.String("fields", utils.MarshalJsonString(filter)),
  122. zap.String("error", err.Error()))
  123. }
  124. if p.ID == 0 {
  125. return gorm.ErrRecordNotFound
  126. }
  127. return err
  128. }
  129. func (p *Dws13) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  130. err := db.Where(filter).Find(out).Error
  131. if err != nil {
  132. l.Error("mysql",
  133. zap.String("sql", "select from "+p.TableName()),
  134. zap.String("fields", utils.MarshalJsonString(filter)),
  135. zap.String("error", err.Error()))
  136. }
  137. return err
  138. }