75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"next-terminal/server/model"
|
|
)
|
|
|
|
var DatabaseSQLLogRepository = new(databaseSQLLogRepository)
|
|
|
|
type databaseSQLLogRepository struct {
|
|
baseRepository
|
|
}
|
|
|
|
func (r databaseSQLLogRepository) FindByPage(c context.Context, pageIndex, pageSize int, assetId, userId, status, source string, order, field string) (o []model.DatabaseSQLLog, total int64, err error) {
|
|
m := model.DatabaseSQLLog{}
|
|
db := r.GetDB(c).Table(m.TableName())
|
|
dbCounter := r.GetDB(c).Table(m.TableName())
|
|
|
|
if assetId != "" {
|
|
db = db.Where("asset_id = ?", assetId)
|
|
dbCounter = dbCounter.Where("asset_id = ?", assetId)
|
|
}
|
|
|
|
if userId != "" {
|
|
db = db.Where("user_id = ?", userId)
|
|
dbCounter = dbCounter.Where("user_id = ?", userId)
|
|
}
|
|
|
|
if status != "" {
|
|
db = db.Where("status = ?", status)
|
|
dbCounter = dbCounter.Where("status = ?", status)
|
|
}
|
|
|
|
if source != "" {
|
|
db = db.Where("source = ?", source)
|
|
dbCounter = dbCounter.Where("source = ?", source)
|
|
}
|
|
|
|
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.DatabaseSQLLog, 0)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r databaseSQLLogRepository) Create(c context.Context, o *model.DatabaseSQLLog) error {
|
|
return r.GetDB(c).Create(o).Error
|
|
}
|
|
|
|
func (r databaseSQLLogRepository) DeleteById(c context.Context, id string) error {
|
|
return r.GetDB(c).Where("id = ?", id).Delete(&model.DatabaseSQLLog{}).Error
|
|
}
|
|
|
|
func (r databaseSQLLogRepository) DeleteAll(c context.Context) error {
|
|
return r.GetDB(c).Where("1 = 1").Delete(&model.DatabaseSQLLog{}).Error
|
|
}
|
|
|
|
func (r databaseSQLLogRepository) FindOutTimeLog(c context.Context, dayLimit int) (o []model.DatabaseSQLLog, err error) {
|
|
limitTime := time.Now().Add(time.Duration(-dayLimit*24) * time.Hour)
|
|
err = r.GetDB(c).Where("created < ?", limitTime).Find(&o).Error
|
|
return
|
|
}
|