123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "time"
- "adm-ads/common.in/utils"
- "go.uber.org/zap"
- "gorm.io/gorm"
- )
- type Ads19 struct {
- ID int64 `gorm:"primary_key" json:"-"`
- ModelNo string `json:"model_no"`
- VehicleBatch string `json:"vehicle_batch"`
- BrandName string `json:"brand_name"`
- VehicleTypeDetail string `json:"vehicle_type_detail"`
- ApprovedLoad string `json:"approved_load"`
- GrossMass string `json:"gross_mass"`
- UnladenMass string `json:"unladen_mass"`
- FuelTypeDetail string `json:"fuel_type_detail"`
- AxleNumber string `json:"axle_number"`
- EmissionStandard string `json:"emission_standard"`
- Wheelbase string `json:"wheel_base"`
- AxleWeight string `json:"axle_weight"`
- NumberOfLeafSprings string `json:"number_of_leaf_springs"`
- TyreNumber string `json:"tyre_number"`
- TyreSize string `json:"tyre_size"`
- ApproachingDepartureAngle string `json:"approaching_departure_angle"`
- FrontBackSuspention string `json:"front_back_suspention"`
- FrontWheelDistance string `json:"front_wheel_distance"`
- BackWheelDistance string `json:"back_wheel_distance"`
- VinRules string `json:"vin_rules"`
- Long string `json:"long"`
- Wide string `json:"wide"`
- High string `json:"high"`
- CargoLong string `json:"cargo_long"`
- CargoWide string `json:"cargo_wide"`
- CargoHigh string `json:"cargo_high"`
- TopSpeed string `json:"top_speed"`
- ApprovedNumber string `json:"approved_number"`
- NumberOfPassengers string `json:"number_of_passengers"`
- SteeringMode string `json:"steering_mode"`
- TractionMass string `json:"traction_mass"`
- LoadMassCoefficient string `json:"load_mass_coefficient"`
- SemitrailerSaddleMaxAllowGrossMass string `json:"semitrailer_saddle_max_allow_gross_mass"`
- CompanyName string `json:"company_name"`
- CompanyAddress string `json:"company_address"`
- CreatedAt int64 `json:"-"`
- UpdatedAt int64 `json:"-"`
- }
- func (Ads19) TableName() string {
- return "t_adm_ads19"
- }
- // Insert 插入一条记录
- func (p *Ads19) Insert(db *gorm.DB) error {
- timeNow := time.Now().Unix()
- p.CreatedAt = timeNow
- p.UpdatedAt = timeNow
- err := db.Create(p).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(*p)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Ads19) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
- data["updated_at"] = time.Now().Unix()
- err := db.Model(p).Create(data).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(data)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Ads19) Delete(db *gorm.DB, filter map[string]interface{}) error {
- err := db.Where(filter).Delete(p).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "delete from "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(filter)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Ads19) Save(db *gorm.DB) error {
- timeNow := time.Now().Unix()
- p.UpdatedAt = timeNow
- //db.Model().Update()
- err := db.Save(p).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "save "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(*p)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Ads19) Update(db *gorm.DB) error {
- timeNow := time.Now().Unix()
- p.CreatedAt = 0
- p.UpdatedAt = timeNow
- err := db.Model(p).Updates(p).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(*p)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Ads19) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
- data["updated_at"] = time.Now().Unix()
- err := db.Model(p).Updates(data).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(data)),
- zap.String("error", err.Error()))
- }
- return err
- }
- // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
- func (p *Ads19) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
- /*if filed == nil {
- return errors.ParamsError
- }*/
- timeNow := time.Now().Unix()
- filed["updated_at"] = timeNow
- err := db.Model(p).Updates(filed).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(filed)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *Ads19) Query(db *gorm.DB, filter map[string]interface{}) error {
- err := db.Where(filter).Find(p).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "select from "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(filter)),
- zap.String("error", err.Error()))
- }
- if p.ID == 0 {
- return gorm.ErrRecordNotFound
- }
- return err
- }
- func (p *Ads19) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
- err := db.Where(filter).Find(out).Error
- if err != nil {
- l.Error("mysql",
- zap.String("sql", "select from "+p.TableName()),
- zap.String("fields", utils.MarshalJsonString(filter)),
- zap.String("error", err.Error()))
- }
- return err
- }
|