1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package model
- import (
- "fmt"
- "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"`
- table string `json:"table"`
- }
- func (p *TLog) TableName() string {
- return p.table
- }
- func NewTLog(database string) *TLog {
- return &TLog{table: fmt.Sprintf("%s.%s", database, "t_log")}
- }
- func (p *TLog) CreateTable(db *gorm.DB) error {
- err := db.CreateTable(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
- zap.String("error", err.Error()))
- }
- return nil
- }
- func (p *TLog) Insert(db *gorm.DB) error {
- err := db.Table(p.TableName()).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
- }
|