dws16.go 6.7 KB

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