// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package model import ( "cp-system-management/errors" "time" "github.com/jaryhe/gopkgs/logger" "github.com/jinzhu/gorm" jsoniter "github.com/json-iterator/go" "go.uber.org/zap" ) // 替换encoding/json包 var json = jsoniter.ConfigCompatibleWithStandardLibrary type SystemUser struct { Id int64 `json:"id" gorm:"primary_key"` Username string `json:"username"` Password string `json:"password"` CreatedAt time.Time `json:"created_at" gorm:"created_at"` UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"` } func (p *SystemUser) TableName() string { return "t_system_user" } func (p *SystemUser) Find(db *gorm.DB, where map[string]interface{}) error { err := db.Where(where).Find(p).Error if err != nil { fields, _ := json.MarshalToString(where) logger.Error("mysql", zap.String("sql", "select from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } // Insert 插入一条记录 func (p *SystemUser) Insert(db *gorm.DB) error { timeNow := time.Now() p.CreatedAt = timeNow p.UpdatedAt = timeNow err := db.Create(p).Error if err != nil { fields, _ := json.MarshalToString(*p) logger.Error("mysql", zap.String("sql", "insert into "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *SystemUser) Delete(db *gorm.DB, filter map[string]interface{}) error { err := db.Where(filter).Delete(p).Error if err != nil { fields, _ := json.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "delete from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *SystemUser) Save(db *gorm.DB) error { p.UpdatedAt = time.Now() err := db.Save(p).Error if err != nil { fields, _ := json.MarshalToString(*p) logger.Error("mysql", zap.String("sql", "save "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *SystemUser) Update(db *gorm.DB) error { p.UpdatedAt = time.Now() err := db.Model(p).Updates(p).Error if err != nil { fields, _ := json.MarshalToString(*p) logger.Error("mysql", zap.String("sql", "update "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。 func (p *SystemUser) UpdateSome(db *gorm.DB, filed map[string]interface{}) error { if filed == nil { return errors.ParamsError } filed["updated_at"] = time.Now() err := db.Model(p).Updates(filed).Error if err != nil { fields, _ := json.MarshalToString(filed) logger.Error("mysql", zap.String("sql", "update "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *SystemUser) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error { err := db.Where(filter).Find(out).Error if err != nil { fields, _ := json.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "select from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *SystemUser) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { return 0, err } ret := int64(0) err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error if err != nil { fields, _ := json.MarshalToString(where) logger.Error("mysql", zap.String("sql", "select count(1) from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return ret, err }