// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package model import ( "gorm.io/gorm" "time" ) type EngineInfo struct { ID int64 `gorm:"primary_key" json:"-"` ModelNo string `json:"model_no"` EngineType string `json:"engine_type"` Displacement string `json:"displacement"` RatedPower string `json:"rated_power"` } func (EngineInfo) TableName() string { return "v_gadm_engine_info" } // Insert 插入一条记录 func (p *EngineInfo) Insert(db *gorm.DB) error { err := db.Create(p).Error return err } func (p *EngineInfo) InsertByMap(db *gorm.DB, data map[string]interface{}) error { data["updated_at"] = time.Now().Unix() err := db.Model(p).Create(data).Error return err } func (p *EngineInfo) Delete(db *gorm.DB, filter map[string]interface{}) error { err := db.Where(filter).Delete(p).Error return err } func (p *EngineInfo) Save(db *gorm.DB) error { //db.Model().Update() err := db.Save(p).Error return err } func (p *EngineInfo) Update(db *gorm.DB) error { err := db.Model(p).Updates(p).Error return err } func (p *EngineInfo) UpdateByMap(db *gorm.DB, data map[string]interface{}) error { data["updated_at"] = time.Now().Unix() err := db.Model(p).Updates(data).Error return err } // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。 func (p *EngineInfo) 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 return err } func (p *EngineInfo) Query(db *gorm.DB, filter map[string]interface{}) error { err := db.Where(filter).Order("id DESC").Find(p).Error return err } func (p *EngineInfo) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error { err := db.Where(filter).Find(out).Error return err }