Skip to content

Commit 05b6f1d

Browse files
committed
Optimization: logs query
1 parent 28be125 commit 05b6f1d

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

repository/loginlog_repository.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"shadmin/domain"
66
"shadmin/ent"
77
"shadmin/ent/loginlog"
8+
"shadmin/ent/predicate"
89
)
910

1011
// Helper function to convert domain status string to ent status enum
@@ -62,29 +63,30 @@ func (lr *entLoginLogRepository) Create(c context.Context, log *domain.LoginLog)
6263

6364
func (lr *entLoginLogRepository) Query(c context.Context, filter domain.LoginLogQueryFilter) (*domain.LoginLogPagedResult, error) {
6465
// 构建基础查询
65-
baseQuery := lr.client.LoginLog.Query()
66-
66+
// 构建查询条件
67+
var predicates []predicate.LoginLog
6768
if filter.Username != "" {
68-
baseQuery = baseQuery.Where(loginlog.UsernameContains(filter.Username))
69+
predicates = append(predicates, loginlog.UsernameContains(filter.Username))
6970
}
7071
if filter.LoginIP != "" {
71-
baseQuery = baseQuery.Where(loginlog.LoginIPContains(filter.LoginIP))
72+
predicates = append(predicates, loginlog.LoginIPContains(filter.LoginIP))
7273
}
7374
if filter.Status != "" {
74-
baseQuery = baseQuery.Where(loginlog.StatusEQ(domainStatusToEntLoginLogStatus(filter.Status)))
75+
predicates = append(predicates, loginlog.StatusEQ(domainStatusToEntLoginLogStatus(filter.Status)))
7576
}
7677
if filter.Browser != "" {
77-
baseQuery = baseQuery.Where(loginlog.BrowserContains(filter.Browser))
78+
predicates = append(predicates, loginlog.BrowserContains(filter.Browser))
7879
}
7980
if filter.OS != "" {
80-
baseQuery = baseQuery.Where(loginlog.OsContains(filter.OS))
81+
predicates = append(predicates, loginlog.OsContains(filter.OS))
8182
}
8283
if filter.StartTime != nil {
83-
baseQuery = baseQuery.Where(loginlog.LoginTimeGTE(*filter.StartTime))
84+
predicates = append(predicates, loginlog.LoginTimeGTE(*filter.StartTime))
8485
}
8586
if filter.EndTime != nil {
86-
baseQuery = baseQuery.Where(loginlog.LoginTimeLTE(*filter.EndTime))
87+
predicates = append(predicates, loginlog.LoginTimeLTE(*filter.EndTime))
8788
}
89+
baseQuery := lr.client.LoginLog.Query().Where(predicates...)
8890

8991
// 获取总数
9092
total, err := baseQuery.Clone().Count(c)

0 commit comments

Comments
 (0)