dws12.go 4.3 KB

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