dws16.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "gorm.io/gorm"
  7. )
  8. type Dws16 struct {
  9. ID int64 `gorm:"primary_key" json:"-"`
  10. ModelNo string `json:"model_no"`
  11. VehicleBatch string `json:"vehicle_batch"`
  12. BrandName string `json:"brand_name"`
  13. VehicleTypeDetail string `json:"vehicle_type_detail"`
  14. ApprovedLoad string `json:"approved_load"`
  15. GrossMass string `json:"gross_mass"`
  16. UnladenMass string `json:"unladen_mass"`
  17. FuelTypeDetail string `json:"fuel_type_detail"`
  18. AxleNumber string `json:"axle_number"`
  19. EmissionStandard string `json:"emission_standard"`
  20. Wheelbase string `json:"wheel_base"`
  21. AxleWeight string `json:"axle_weight"`
  22. NumberOfLeafSprings string `json:"number_of_leaf_springs"`
  23. TyreNumber string `json:"tyre_number"`
  24. TyreSize string `json:"tyre_size"`
  25. ApproachingDepartureAngle string `json:"approaching_departure_angle"`
  26. FrontBackSuspention string `json:"front_back_suspention"`
  27. FrontWheelDistance string `json:"front_wheel_distance"`
  28. BackWheelDistance string `json:"back_wheel_distance"`
  29. VinRules string `json:"vin_rules"`
  30. Long string `json:"long"`
  31. Wide string `json:"wide"`
  32. High string `json:"high"`
  33. CargoLong string `json:"cargo_long"`
  34. CargoWide string `json:"cargo_wide"`
  35. CargoHigh string `json:"cargo_high"`
  36. TopSpeed string `json:"top_speed"`
  37. ApprovedNumber string `json:"approved_number"`
  38. NumberOfPassengers string `json:"number_of_passengers"`
  39. SteeringMode string `json:"steering_mode"`
  40. TractionMass string `json:"traction_mass"`
  41. LoadMassCoefficient string `json:"load_mass_coefficient"`
  42. SemitrailerSaddleMaxAllowGrossMass string `json:"semitrailer_saddle_max_allow_gross_mass"`
  43. CompanyName string `json:"company_name"`
  44. CompanyAddress string `json:"company_address"`
  45. CreatedAt int64 `json:"-"`
  46. UpdatedAt int64 `json:"-"`
  47. }
  48. func (Dws16) TableName() string {
  49. return "t_adm_dws16"
  50. }
  51. // Insert 插入一条记录
  52. func (p *Dws16) Insert(db *gorm.DB) error {
  53. timeNow := time.Now().Unix()
  54. p.CreatedAt = timeNow
  55. p.UpdatedAt = timeNow
  56. err := db.Create(p).Error
  57. return err
  58. }
  59. func (p *Dws16) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  60. data["updated_at"] = time.Now().Unix()
  61. err := db.Model(p).Create(data).Error
  62. return err
  63. }
  64. func (p *Dws16) Delete(db *gorm.DB, filter map[string]interface{}) error {
  65. err := db.Where(filter).Delete(p).Error
  66. return err
  67. }
  68. func (p *Dws16) Save(db *gorm.DB) error {
  69. timeNow := time.Now().Unix()
  70. p.UpdatedAt = timeNow
  71. //db.Model().Update()
  72. err := db.Save(p).Error
  73. return err
  74. }
  75. func (p *Dws16) Update(db *gorm.DB) error {
  76. timeNow := time.Now().Unix()
  77. p.CreatedAt = 0
  78. p.UpdatedAt = timeNow
  79. err := db.Model(p).Updates(p).Error
  80. return err
  81. }
  82. func (p *Dws16) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  83. data["updated_at"] = time.Now().Unix()
  84. err := db.Model(p).Updates(data).Error
  85. return err
  86. }
  87. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  88. func (p *Dws16) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  89. /*if filed == nil {
  90. return errors.ParamsError
  91. }*/
  92. timeNow := time.Now().Unix()
  93. filed["updated_at"] = timeNow
  94. err := db.Model(p).Updates(filed).Error
  95. return err
  96. }
  97. func (p *Dws16) Query(db *gorm.DB, filter map[string]interface{}) error {
  98. err := db.Where(filter).Order("vehicle_batch DESC").Find(p).Error
  99. if p.ID == 0 {
  100. return gorm.ErrRecordNotFound
  101. }
  102. return err
  103. }
  104. func (p *Dws16) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  105. err := db.Where(filter).Find(out).Error
  106. return err
  107. }