feat: Establish core backend services including SQLite database, structured logging, and initial owner management capabilities.
This commit is contained in:
@@ -459,6 +459,33 @@ func (d *DB) UpdateOwnerAccountID(id int64, accountID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// GetTeamOwnerIDs 获取所有符合条件的 owner ID(用于全选)
|
||||
func (d *DB) GetTeamOwnerIDs(status string) ([]int64, error) {
|
||||
query := "SELECT id FROM team_owners WHERE 1=1"
|
||||
args := []interface{}{}
|
||||
if status != "" {
|
||||
query += " AND status = ?"
|
||||
args = append(args, status)
|
||||
}
|
||||
query += " ORDER BY created_at DESC"
|
||||
|
||||
rows, err := d.db.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var ids []int64
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
continue
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// GetOwnerStats 获取统计
|
||||
func (d *DB) GetOwnerStats() map[string]int {
|
||||
stats := map[string]int{
|
||||
|
||||
@@ -238,7 +238,38 @@ func GetLogs(limit int) []LogEntry {
|
||||
}
|
||||
|
||||
// GetLogsByModule 按模块筛选日志并分页(最新的在前)
|
||||
// 优先从内存读取,内存无数据时回退到数据库
|
||||
func GetLogsByModule(module string, page, pageSize int) ([]LogEntry, int) {
|
||||
logsMu.RLock()
|
||||
// 检查内存中是否有该模块的日志
|
||||
hasMemoryLogs := false
|
||||
for i := len(logs) - 1; i >= 0; i-- {
|
||||
if logs[i].Module == module {
|
||||
hasMemoryLogs = true
|
||||
break
|
||||
}
|
||||
}
|
||||
logsMu.RUnlock()
|
||||
|
||||
// 内存中无数据,回退到数据库查询
|
||||
if !hasMemoryLogs && database.Instance != nil {
|
||||
dbLogs, total, err := database.Instance.GetLogsByModule(module, page, pageSize)
|
||||
if err == nil && total > 0 {
|
||||
entries := make([]LogEntry, len(dbLogs))
|
||||
for i, dl := range dbLogs {
|
||||
entries[i] = LogEntry{
|
||||
Timestamp: dl.Timestamp,
|
||||
Level: dl.Level,
|
||||
Message: dl.Message,
|
||||
Email: dl.Email,
|
||||
Module: dl.Module,
|
||||
}
|
||||
}
|
||||
return entries, total
|
||||
}
|
||||
}
|
||||
|
||||
// 从内存读取
|
||||
logsMu.RLock()
|
||||
defer logsMu.RUnlock()
|
||||
|
||||
@@ -283,7 +314,40 @@ func ClearLogs() {
|
||||
}
|
||||
|
||||
// GetLogsByModuleAndLevel 按模块和级别筛选日志并分页(最新的在前)
|
||||
// 优先从内存读取,内存无数据时回退到数据库
|
||||
func GetLogsByModuleAndLevel(module, level string, page, pageSize int) ([]LogEntry, int) {
|
||||
logsMu.RLock()
|
||||
// 检查内存中是否有该模块+级别的日志
|
||||
hasMemoryLogs := false
|
||||
for i := len(logs) - 1; i >= 0; i-- {
|
||||
if logs[i].Module == module {
|
||||
if level == "" || logs[i].Level == level {
|
||||
hasMemoryLogs = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
logsMu.RUnlock()
|
||||
|
||||
// 内存中无数据,回退到数据库查询
|
||||
if !hasMemoryLogs && database.Instance != nil {
|
||||
dbLogs, total, err := database.Instance.GetLogsByModuleAndLevel(module, level, page, pageSize)
|
||||
if err == nil && total > 0 {
|
||||
entries := make([]LogEntry, len(dbLogs))
|
||||
for i, dl := range dbLogs {
|
||||
entries[i] = LogEntry{
|
||||
Timestamp: dl.Timestamp,
|
||||
Level: dl.Level,
|
||||
Message: dl.Message,
|
||||
Email: dl.Email,
|
||||
Module: dl.Module,
|
||||
}
|
||||
}
|
||||
return entries, total
|
||||
}
|
||||
}
|
||||
|
||||
// 从内存读取
|
||||
logsMu.RLock()
|
||||
defer logsMu.RUnlock()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user