dws7.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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-dws/common.in/utils"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. )
  10. type Dws7 struct {
  11. ID int64 `gorm:"primary_key" json:"-"`
  12. Vin string `json:"vin"`
  13. InitialRegistrationDate string `json:"initial_registration_date"`
  14. VehicleType string `json:"vehicle_type"`
  15. UseProperty string `json:"use_property"`
  16. FuelType string `json:"fuel_type"`
  17. ModelNo string `json:"model_no"`
  18. VehicleBodyColor string `json:"vehicle_body_color"`
  19. CreatedAt int64 `json:"-"`
  20. UpdatedAt int64 `json:"-"`
  21. }
  22. func (Dws7) TableName() string {
  23. return "t_adm_dws7"
  24. }
  25. // Insert 插入一条记录
  26. func (p *Dws7) Insert(db *gorm.DB) error {
  27. timeNow := time.Now().Unix()
  28. p.UpdatedAt = timeNow
  29. p.CreatedAt = timeNow
  30. err := db.Create(p).Error
  31. if err != nil {
  32. l.Error("mysql",
  33. zap.String("sql", "insert into "+p.TableName()),
  34. zap.String("fields", utils.MarshalJsonString(*p)),
  35. zap.String("error", err.Error()))
  36. }
  37. return err
  38. }
  39. func (p *Dws7) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  40. data["updated_at"] = time.Now().Unix()
  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 *Dws7) 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 *Dws7) Save(db *gorm.DB) error {
  61. timeNow := time.Now().Unix()
  62. p.UpdatedAt = timeNow
  63. p.CreatedAt = timeNow
  64. //db.Model().Update()
  65. err := db.Save(p).Error
  66. if err != nil {
  67. l.Error("mysql",
  68. zap.String("sql", "save "+p.TableName()),
  69. zap.String("fields", utils.MarshalJsonString(*p)),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. func (p *Dws7) Update(db *gorm.DB) error {
  75. timeNow := time.Now().Unix()
  76. p.CreatedAt = 0
  77. p.UpdatedAt = timeNow
  78. err := db.Model(p).Updates(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 *Dws7) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  88. data["updated_at"] = time.Now().Unix()
  89. err := db.Model(p).Updates(data).Error
  90. if err != nil {
  91. l.Error("mysql",
  92. zap.String("sql", "update "+p.TableName()),
  93. zap.String("fields", utils.MarshalJsonString(data)),
  94. zap.String("error", err.Error()))
  95. }
  96. return err
  97. }
  98. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  99. func (p *Dws7) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  100. /*if filed == nil {
  101. return errors.ParamsError
  102. }*/
  103. timeNow := time.Now().Unix()
  104. filed["updated_at"] = timeNow
  105. err := db.Model(p).Updates(filed).Error
  106. if err != nil {
  107. l.Error("mysql",
  108. zap.String("sql", "update "+p.TableName()),
  109. zap.String("fields", utils.MarshalJsonString(filed)),
  110. zap.String("error", err.Error()))
  111. }
  112. return err
  113. }
  114. func (p *Dws7) Query(db *gorm.DB, filter map[string]interface{}) error {
  115. err := db.Where(filter).Find(p).Error
  116. if err != nil {
  117. l.Error("mysql",
  118. zap.String("sql", "select from "+p.TableName()),
  119. zap.String("fields", utils.MarshalJsonString(filter)),
  120. zap.String("error", err.Error()))
  121. }
  122. if p.ID == 0 {
  123. return gorm.ErrRecordNotFound
  124. }
  125. return err
  126. }
  127. func (p *Dws7) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  128. err := db.Where(filter).Find(out).Error
  129. if err != nil {
  130. l.Error("mysql",
  131. zap.String("sql", "select from "+p.TableName()),
  132. zap.String("fields", utils.MarshalJsonString(filter)),
  133. zap.String("error", err.Error()))
  134. }
  135. return err
  136. }