[操作日志]增加若干查询条件

This commit is contained in:
刘祥超
2020-11-20 16:36:07 +08:00
parent f5e0c1749d
commit 34c941e4e9
4 changed files with 58 additions and 7 deletions

View File

@@ -4,6 +4,9 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"strings"
)
type LogDAO dbs.DAO
@@ -32,20 +35,51 @@ func (this *LogDAO) CreateLog(adminType string, adminId int64, level string, des
op := NewLogOperator()
op.Type = adminType
op.AdminId, op.Level, op.Description, op.Action, op.Ip = adminId, level, description, action, ip
op.Day = timeutil.Format("Ymd")
op.Type = LogTypeAdmin
_, err := this.Save(op)
return err
}
// 计算所有日志数量
func (this *LogDAO) CountAllLogs() (int64, error) {
return this.Query().
Count()
func (this *LogDAO) CountLogs(dayFrom string, dayTo string, keyword string) (int64, error) {
dayFrom = this.formatDay(dayFrom)
dayTo = this.formatDay(dayTo)
query := this.Query()
if len(dayFrom) > 0 {
query.Gte("day", dayFrom)
}
if len(dayTo) > 0 {
query.Lte("day", dayTo)
}
if len(keyword) > 0 {
query.Where("(description LIKE :keyword OR ip LIKE :keyword OR action LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
return query.Count()
}
// 列出单页日志
func (this *LogDAO) ListLogs(offset int64, size int64) (result []*Log, err error) {
_, err = this.Query().
func (this *LogDAO) ListLogs(offset int64, size int64, dayFrom string, dayTo string, keyword string) (result []*Log, err error) {
dayFrom = this.formatDay(dayFrom)
dayTo = this.formatDay(dayTo)
query := this.Query()
if len(dayFrom) > 0 {
query.Gte("day", dayFrom)
}
if len(dayTo) > 0 {
query.Lte("day", dayTo)
}
if len(keyword) > 0 {
query.Where("(description LIKE :keyword OR ip LIKE :keyword OR action LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
_, err = query.
Offset(offset).
Limit(size).
Slice(&result).
@@ -53,3 +87,12 @@ func (this *LogDAO) ListLogs(offset int64, size int64) (result []*Log, err error
FindAll()
return
}
// 格式化日期
func (this *LogDAO) formatDay(day string) string {
if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(day) {
return ""
}
day = strings.ReplaceAll(day, "-", "")
return day
}