// Copyright 2019 autocareai.com. All rights reserved. // Use of this source code is governed by autocareai.com. package model import ( "gadm-ods/common.in/utils" "go.uber.org/zap" "gorm.io/gorm" "time" ) type Ods7 struct { ID int64 `gorm:"primary_key"` IdCard string `json:"id_card"` Name string `json:"name"` Content string `json:"content"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } func (Ods7) TableName() string { return "t_adm_ods7" } // Insert 插入一条记录 func (p *Ods7) 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 *Ods7) 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 *Ods7) 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 *Ods7) 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 *Ods7) 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 *Ods7) UpdateWhere(db *gorm.DB, where map[string]interface{}) error { timeNow := time.Now().Unix() p.CreatedAt = 0 p.UpdatedAt = timeNow err := db.Model(p).Where(where).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 *Ods7) 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 *Ods7) 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 *Ods7) 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 *Ods7) 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 }