70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"next-terminal/server/model"
|
|
)
|
|
|
|
var OperationLogRepository = new(operationLogRepository)
|
|
|
|
type operationLogRepository struct {
|
|
baseRepository
|
|
}
|
|
|
|
func (r operationLogRepository) FindByPage(c context.Context, pageIndex, pageSize int, accountName, action, status string, order, field string) (o []model.OperationLog, total int64, err error) {
|
|
m := model.OperationLog{}
|
|
db := r.GetDB(c).Table(m.TableName())
|
|
dbCounter := r.GetDB(c).Table(m.TableName())
|
|
|
|
if accountName != "" {
|
|
db = db.Where("account_name like ?", "%"+accountName+"%")
|
|
dbCounter = dbCounter.Where("account_name like ?", "%"+accountName+"%")
|
|
}
|
|
|
|
if action != "" {
|
|
db = db.Where("action = ?", action)
|
|
dbCounter = dbCounter.Where("action = ?", action)
|
|
}
|
|
|
|
if status != "" {
|
|
db = db.Where("status = ?", status)
|
|
dbCounter = dbCounter.Where("status = ?", status)
|
|
}
|
|
|
|
err = dbCounter.Count(&total).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
orderBy := "created desc"
|
|
if order != "" && field != "" {
|
|
orderBy = field + " " + order
|
|
}
|
|
|
|
err = db.Order(orderBy).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
|
if o == nil {
|
|
o = make([]model.OperationLog, 0)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r operationLogRepository) Create(c context.Context, o *model.OperationLog) error {
|
|
return r.GetDB(c).Create(o).Error
|
|
}
|
|
|
|
func (r operationLogRepository) DeleteById(c context.Context, id string) error {
|
|
return r.GetDB(c).Where("id = ?", id).Delete(&model.OperationLog{}).Error
|
|
}
|
|
|
|
func (r operationLogRepository) DeleteAll(c context.Context) error {
|
|
return r.GetDB(c).Where("1 = 1").Delete(&model.OperationLog{}).Error
|
|
}
|
|
|
|
func (r operationLogRepository) FindOutTimeLog(c context.Context, dayLimit int) (o []model.OperationLog, err error) {
|
|
limitTime := time.Now().Add(time.Duration(-dayLimit*24) * time.Hour)
|
|
err = r.GetDB(c).Where("created < ?", limitTime).Find(&o).Error
|
|
return
|
|
}
|