12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "time"
- "gorm.io/gorm"
- )
- type Dws11 struct {
- ID int64 `gorm:"primary_key" json:"-"`
- VinRule string `json:"vin_rule"`
- ModelNo string `json:"model_no"`
- Uniq string `json:"uniq"`
- CreatedAt int64 `json:"-"`
- UpdatedAt int64 `json:"-"`
- }
- func (Dws11) TableName() string {
- return "t_adm_dws11"
- }
- // Insert 插入一条记录
- func (p *Dws11) Insert(db *gorm.DB) error {
- timeNow := time.Now().Unix()
- p.CreatedAt = timeNow
- p.UpdatedAt = timeNow
- err := db.Create(p).Error
- return err
- }
- func (p *Dws11) 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 *Dws11) Delete(db *gorm.DB, filter map[string]interface{}) error {
- err := db.Where(filter).Delete(p).Error
- return err
- }
- func (p *Dws11) Save(db *gorm.DB) error {
- timeNow := time.Now().Unix()
- p.UpdatedAt = timeNow
- //db.Model().Update()
- err := db.Save(p).Error
- return err
- }
- func (p *Dws11) Update(db *gorm.DB) error {
- timeNow := time.Now().Unix()
- p.CreatedAt = 0
- p.UpdatedAt = timeNow
- err := db.Model(p).Updates(p).Error
- return err
- }
- func (p *Dws11) 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 *Dws11) 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 *Dws11) Query(db *gorm.DB, filter map[string]interface{}) error {
- err := db.Where(filter).Order("id DESC").Find(p).Error
- if p.ID == 0 {
- return gorm.ErrRecordNotFound
- }
- return err
- }
- func (p *Dws11) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
- err := db.Where(filter).Find(out).Error
- return err
- }
|