ads10.go 6.4 KB

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