package model import ( "github.com/jaryhe/gopkgs/logger" "github.com/jinzhu/gorm" "go.uber.org/zap" "time" ) type TLog struct { ID int64 `gorm:"column:id" json:"id"` Module string `gorm:"column:module" json:"module"` Action string `gorm:"column:action" json:"action"` Origin string `gorm:"column:origin" json:"origin"` Target string `gorm:"column:target" json:"target"` Username string `gorm:"column:username" json:"username"` Uid int64 `gorm:"column:uid" json:"uid"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` } func (p *TLog) TableName() string { return "t_log" } func (p *TLog) Insert(db *gorm.DB) error { err := db.Create(p).Error if err != nil { logger.Error("mysql", zap.String("sql", "insert into "+p.TableName()), zap.String("error", err.Error())) } return err } func (p *TLog) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { if err != nil { logger.Error("mysql", zap.String("sql", "select count from "+p.TableName()), zap.String("error", err.Error())) } return 0, err } ret := int64(0) err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error return ret, err } func (p *TLog) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, limit, offset int64) (list []TLog, err error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { return list, err } result := db.Table(p.TableName()).Where(cond, val...).Limit(limit).Offset(offset).Find(&list) if result.Error != nil { logger.Error("mysql", zap.String("sql", "select * from "+p.TableName()), zap.String("error", result.Error.Error())) } return list, result.Error }