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

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
}

View File

@@ -12,6 +12,7 @@ type Log struct {
ProviderId uint32 `field:"providerId"` // 供应商ID
Ip string `field:"ip"` // IP地址
Type string `field:"type"` // 类型admin, user
Day string `field:"day"` // 日期
}
type LogOperator struct {
@@ -25,6 +26,7 @@ type LogOperator struct {
ProviderId interface{} // 供应商ID
Ip interface{} // IP地址
Type interface{} // 类型admin, user
Day interface{} // 日期
}
func NewLogOperator() *LogOperator {

View File

@@ -9,6 +9,9 @@ type User struct {
CreatedAt uint64 `field:"createdAt"` // 创建时间
UpdatedAt uint64 `field:"updatedAt"` // 修改时间
State uint8 `field:"state"` // 状态
Mobile string `field:"mobile"` // 手机号
Remark string `field:"remark"` // 备注
Email string `field:"email"` // 邮箱地址
}
type UserOperator struct {
@@ -19,6 +22,9 @@ type UserOperator struct {
CreatedAt interface{} // 创建时间
UpdatedAt interface{} // 修改时间
State interface{} // 状态
Mobile interface{} // 手机号
Remark interface{} // 备注
Email interface{} // 邮箱地址
}
func NewUserOperator() *UserOperator {

View File

@@ -34,7 +34,7 @@ func (this *LogService) CountLogs(ctx context.Context, req *pb.CountLogRequest)
return nil, err
}
count, err := models.SharedLogDAO.CountAllLogs()
count, err := models.SharedLogDAO.CountLogs(req.DayFrom, req.DayTo, req.Keyword)
if err != nil {
return nil, err
}
@@ -49,7 +49,7 @@ func (this *LogService) ListLogs(ctx context.Context, req *pb.ListLogsRequest) (
return nil, err
}
logs, err := models.SharedLogDAO.ListLogs(req.Offset, req.Size)
logs, err := models.SharedLogDAO.ListLogs(req.Offset, req.Size, req.DayFrom, req.DayTo, req.Keyword)
if err != nil {
return nil, err
}