// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package model import ( "adm-ads/common.in/utils" "go.uber.org/zap" "gorm.io/gorm" "time" ) // 合格证信息 type Ads10 struct { ID int64 `gorm:"primary_key" json:"-"` Vin string `json:"vin"` CertificateNumber string `json:"certificate_number"` CertificateDate string `json:"certificate_date"` VehicleManufacturer string `json:"vehicle_manufacturer"` BrandName string `json:"brand_name"` ModelNo string `json:"model_no"` VehicleBodyColorDetail string `json:"vehicle_body_color_detail"` ChassisModel string `json:"chassis_model"` ChassisId string `json:"chassis_id"` ChassisCertificateNumber string `json:"chassis_certificate_number"` EngineType string `json:"engine_type"` EngineNo string `json:"engine_no"` FuelTypeDetail string `json:"fuel_type_detail"` Displacement string `json:"displacement"` DisplacementL string `json:"displacement_l"` RatedPower string `json:"rated_power"` EmissionStandard string `json:"emission_standard"` OilWear string `json:"oil_wear"` 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"` NumberOfLeafSprings string `json:"number_of_leaf_springs"` TyreNumber string `json:"tyre_number"` TyreSize string `json:"tyre_size"` FrontWheelDistance string `json:"front_wheel_distance"` BackWheelDistance string `json:"back_wheel_distance"` WheelBase string `json:"wheel_base"` AxleWeight string `json:"axle_weight"` AxleNumber string `json:"axle_number"` SteeringMode string `json:"steering_mode"` GrossMass string `json:"gross_mass"` UnladenMass string `json:"unladen_mass"` ApprovedLoad string `json:"approved_load"` LoadMassCoefficient string `json:"load_mass_coefficient"` TractionMass string `json:"traction_mass"` SemitrailerSaddleMaxAllowGrossMass string `json:"semitrailer_saddle_max_allow_gross_mass"` NumberOfPassengers string `json:"number_of_passengers"` ApprovedNumber string `json:"approved_number"` TopSpeed string `json:"top_speed"` ReleaseDate string `json:"release_date"` CreatedAt int64 `json:"-"` UpdatedAt int64 `json:"-"` } func (Ads10) TableName() string { return "t_adm_ads10" } // Insert 插入一条记录 func (p *Ads10) Insert(db *gorm.DB) error { if p.CreatedAt == 0 { 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 *Ads10) InsertByMap(db *gorm.DB, data map[string]interface{}) error { 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 *Ads10) 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 *Ads10) Save(db *gorm.DB) error { 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 *Ads10) Update(db *gorm.DB) error { p.CreatedAt = 0 if p.UpdatedAt == 0 { timeNow := time.Now().Unix() p.UpdatedAt = timeNow } err := db.Model(p).UpdateColumns(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 *Ads10) UpdateByMap(db *gorm.DB, data map[string]interface{}) error { 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 *Ads10) UpdateSome(db *gorm.DB, filed map[string]interface{}) error { 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 *Ads10) 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 *Ads10) 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 }