dws5.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // 合格证信息
  11. type Dws5 struct {
  12. ID int64 `gorm:"primary_key" json:"-"`
  13. //ThridStyleId string `json:"thrid_style_id" gorm:"-"`
  14. //StyleId string `json:"style_id" gorm:"-"`
  15. //FrontWheelSpecification string `json:"front_wheel_specification" gorm:"-"`
  16. //BackWheelSpecification string `json:"back_wheel_specification" gorm:"-"`
  17. Vin string `json:"vin"`
  18. CertificateNumber string `json:"certificate_number"`
  19. CertificateDate string `json:"certificate_date"`
  20. VehicleManufacturer string `json:"vehicle_manufacturer"`
  21. BrandName string `json:"brand_name"`
  22. ModelNo string `json:"model_no"`
  23. VehicleBodyColorDetail string `json:"vehicle_body_color_detail"`
  24. ChassisModel string `json:"chassis_model"`
  25. ChassisId string `json:"chassis_id"`
  26. ChassisCertificateNumber string `json:"chassis_certificate_number"`
  27. EngineType string `json:"engine_type"`
  28. EngineNo string `json:"engine_no"`
  29. FuelTypeDetail string `json:"fuel_type_detail"`
  30. Displacement string `json:"displacement"`
  31. DisplacementL string `json:"displacement_l"`
  32. RatedPower string `json:"rated_power"`
  33. EmissionStandard string `json:"emission_standard"`
  34. OilWear string `json:"oil_wear"`
  35. Long string `json:"long"`
  36. Wide string `json:"wide"`
  37. High string `json:"high"`
  38. CargoLong string `json:"cargo_long"`
  39. CargoWide string `json:"cargo_wide"`
  40. CargoHigh string `json:"cargo_high"`
  41. NumberOfLeafSprings string `json:"number_of_leaf_springs"`
  42. TyreNumber string `json:"tyre_number"`
  43. TyreSize string `json:"tyre_size"`
  44. FrontWheelDistance string `json:"front_wheel_distance"`
  45. BackWheelDistance string `json:"back_wheel_distance"`
  46. WheelBase string `json:"wheel_base"`
  47. AxleWeight string `json:"axle_weight"`
  48. AxleNumber string `json:"axle_number"`
  49. SteeringMode string `json:"steering_mode"`
  50. GrossMass string `json:"gross_mass"`
  51. UnladenMass string `json:"unladen_mass"`
  52. ApprovedLoad string `json:"approved_load"`
  53. LoadMassCoefficient string `json:"load_mass_coefficient"`
  54. TractionMass string `json:"traction_mass"`
  55. SemitrailerSaddleMaxAllowGrossMass string `json:"semitrailer_saddle_max_allow_gross_mass"`
  56. NumberOfPassengers string `json:"number_of_passengers"`
  57. ApprovedNumber string `json:"approved_number"`
  58. TopSpeed string `json:"top_speed"`
  59. ReleaseDate string `json:"release_date"`
  60. CreatedAt int64 `json:"-"`
  61. UpdatedAt int64 `json:"-"`
  62. }
  63. func (Dws5) TableName() string {
  64. return "t_adm_dws5"
  65. }
  66. // Insert 插入一条记录
  67. func (p *Dws5) Insert(db *gorm.DB) error {
  68. timeNow := time.Now().Unix()
  69. p.UpdatedAt = timeNow
  70. p.CreatedAt = timeNow
  71. err := db.Create(p).Error
  72. if err != nil {
  73. l.Error("mysql",
  74. zap.String("sql", "insert into "+p.TableName()),
  75. zap.String("fields", utils.MarshalJsonString(*p)),
  76. zap.String("error", err.Error()))
  77. }
  78. return err
  79. }
  80. func (p *Dws5) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  81. data["updated_at"] = time.Now().Unix()
  82. err := db.Model(p).Create(data).Error
  83. if err != nil {
  84. l.Error("mysql",
  85. zap.String("sql", "insert into "+p.TableName()),
  86. zap.String("fields", utils.MarshalJsonString(data)),
  87. zap.String("error", err.Error()))
  88. }
  89. return err
  90. }
  91. func (p *Dws5) Delete(db *gorm.DB, filter map[string]interface{}) error {
  92. err := db.Where(filter).Delete(p).Error
  93. if err != nil {
  94. l.Error("mysql",
  95. zap.String("sql", "delete from "+p.TableName()),
  96. zap.String("fields", utils.MarshalJsonString(filter)),
  97. zap.String("error", err.Error()))
  98. }
  99. return err
  100. }
  101. func (p *Dws5) Save(db *gorm.DB) error {
  102. timeNow := time.Now().Unix()
  103. p.UpdatedAt = timeNow
  104. p.CreatedAt = timeNow
  105. //db.Model().Update()
  106. err := db.Save(p).Error
  107. if err != nil {
  108. l.Error("mysql",
  109. zap.String("sql", "save "+p.TableName()),
  110. zap.String("fields", utils.MarshalJsonString(*p)),
  111. zap.String("error", err.Error()))
  112. }
  113. return err
  114. }
  115. func (p *Dws5) Update(db *gorm.DB) error {
  116. timeNow := time.Now().Unix()
  117. p.CreatedAt = 0
  118. p.UpdatedAt = timeNow
  119. err := db.Model(p).Updates(p).Error
  120. if err != nil {
  121. l.Error("mysql",
  122. zap.String("sql", "update "+p.TableName()),
  123. zap.String("fields", utils.MarshalJsonString(*p)),
  124. zap.String("error", err.Error()))
  125. }
  126. return err
  127. }
  128. func (p *Dws5) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  129. data["updated_at"] = time.Now().Unix()
  130. err := db.Model(p).Updates(data).Error
  131. if err != nil {
  132. l.Error("mysql",
  133. zap.String("sql", "update "+p.TableName()),
  134. zap.String("fields", utils.MarshalJsonString(data)),
  135. zap.String("error", err.Error()))
  136. }
  137. return err
  138. }
  139. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  140. func (p *Dws5) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  141. /*if filed == nil {
  142. return errors.ParamsError
  143. }*/
  144. timeNow := time.Now().Unix()
  145. filed["updated_at"] = timeNow
  146. err := db.Model(p).Updates(filed).Error
  147. if err != nil {
  148. l.Error("mysql",
  149. zap.String("sql", "update "+p.TableName()),
  150. zap.String("fields", utils.MarshalJsonString(filed)),
  151. zap.String("error", err.Error()))
  152. }
  153. return err
  154. }
  155. func (p *Dws5) Query(db *gorm.DB, filter map[string]interface{}) error {
  156. err := db.Where(filter).Find(p).Error
  157. if err != nil {
  158. l.Error("mysql",
  159. zap.String("sql", "select from "+p.TableName()),
  160. zap.String("fields", utils.MarshalJsonString(filter)),
  161. zap.String("error", err.Error()))
  162. }
  163. if p.ID == 0 {
  164. return gorm.ErrRecordNotFound
  165. }
  166. return err
  167. }
  168. func (p *Dws5) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  169. err := db.Where(filter).Find(out).Error
  170. if err != nil {
  171. l.Error("mysql",
  172. zap.String("sql", "select from "+p.TableName()),
  173. zap.String("fields", utils.MarshalJsonString(filter)),
  174. zap.String("error", err.Error()))
  175. }
  176. return err
  177. }