feat: Add batch processing and upload functionality, including new backend APIs, logging system, SQLite database, and dedicated frontend pages.
This commit is contained in:
@@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -104,8 +105,26 @@ func (d *DB) createTables() error {
|
||||
status TEXT DEFAULT 'running',
|
||||
errors TEXT
|
||||
);
|
||||
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_batch_runs_started_at ON batch_runs(started_at);
|
||||
|
||||
-- 批次Team处理结果表
|
||||
CREATE TABLE IF NOT EXISTS batch_team_results (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
batch_id INTEGER NOT NULL,
|
||||
team_index INTEGER NOT NULL,
|
||||
owner_email TEXT NOT NULL,
|
||||
team_id TEXT,
|
||||
registered INTEGER DEFAULT 0,
|
||||
added_to_s2a INTEGER DEFAULT 0,
|
||||
member_emails TEXT,
|
||||
errors TEXT,
|
||||
duration_ms INTEGER DEFAULT 0,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (batch_id) REFERENCES batch_runs(id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_batch_team_results_batch_id ON batch_team_results(batch_id);
|
||||
`)
|
||||
return err
|
||||
}
|
||||
@@ -346,6 +365,15 @@ func (d *DB) DeleteTeamOwner(id int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteTeamOwnerByEmail 按邮箱删除母号
|
||||
func (d *DB) DeleteTeamOwnerByEmail(email string) (int64, error) {
|
||||
result, err := d.db.Exec("DELETE FROM team_owners WHERE email = ?", email)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
// ClearTeamOwners 清空
|
||||
func (d *DB) ClearTeamOwners() error {
|
||||
_, err := d.db.Exec("DELETE FROM team_owners")
|
||||
@@ -569,6 +597,146 @@ func (d *DB) GetBatchRunStats() map[string]interface{} {
|
||||
return stats
|
||||
}
|
||||
|
||||
// BatchTeamResult 批次Team处理结果
|
||||
type BatchTeamResult struct {
|
||||
ID int64 `json:"id"`
|
||||
BatchID int64 `json:"batch_id"`
|
||||
TeamIndex int `json:"team_index"`
|
||||
OwnerEmail string `json:"owner_email"`
|
||||
TeamID string `json:"team_id"`
|
||||
Registered int `json:"registered"`
|
||||
AddedToS2A int `json:"added_to_s2a"`
|
||||
MemberEmails []string `json:"member_emails"`
|
||||
Errors []string `json:"errors"`
|
||||
DurationMs int64 `json:"duration_ms"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// SaveBatchTeamResult 保存单个Team处理结果
|
||||
func (d *DB) SaveBatchTeamResult(batchID int64, result BatchTeamResult) error {
|
||||
memberEmailsJSON, _ := json.Marshal(result.MemberEmails)
|
||||
errorsJSON, _ := json.Marshal(result.Errors)
|
||||
|
||||
_, err := d.db.Exec(`
|
||||
INSERT INTO batch_team_results (batch_id, team_index, owner_email, team_id, registered, added_to_s2a, member_emails, errors, duration_ms)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, batchID, result.TeamIndex, result.OwnerEmail, result.TeamID, result.Registered, result.AddedToS2A, string(memberEmailsJSON), string(errorsJSON), result.DurationMs)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetBatchTeamResults 获取某批次的所有Team结果
|
||||
func (d *DB) GetBatchTeamResults(batchID int64) ([]BatchTeamResult, error) {
|
||||
rows, err := d.db.Query(`
|
||||
SELECT id, batch_id, team_index, owner_email, COALESCE(team_id, ''), registered, added_to_s2a,
|
||||
COALESCE(member_emails, '[]'), COALESCE(errors, '[]'), duration_ms, created_at
|
||||
FROM batch_team_results
|
||||
WHERE batch_id = ?
|
||||
ORDER BY team_index ASC
|
||||
`, batchID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []BatchTeamResult
|
||||
for rows.Next() {
|
||||
var r BatchTeamResult
|
||||
var memberEmailsJSON, errorsJSON string
|
||||
err := rows.Scan(&r.ID, &r.BatchID, &r.TeamIndex, &r.OwnerEmail, &r.TeamID, &r.Registered,
|
||||
&r.AddedToS2A, &memberEmailsJSON, &errorsJSON, &r.DurationMs, &r.CreatedAt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
json.Unmarshal([]byte(memberEmailsJSON), &r.MemberEmails)
|
||||
json.Unmarshal([]byte(errorsJSON), &r.Errors)
|
||||
if r.MemberEmails == nil {
|
||||
r.MemberEmails = []string{}
|
||||
}
|
||||
if r.Errors == nil {
|
||||
r.Errors = []string{}
|
||||
}
|
||||
results = append(results, r)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// GetBatchRunsWithPagination 分页获取批次记录
|
||||
func (d *DB) GetBatchRunsWithPagination(page, pageSize int) ([]BatchRun, int, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 5
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
var total int
|
||||
err := d.db.QueryRow("SELECT COUNT(*) FROM batch_runs").Scan(&total)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
rows, err := d.db.Query(`
|
||||
SELECT id, started_at, finished_at, total_owners, total_registered, total_added_to_s2a,
|
||||
success_rate, duration_seconds, status, COALESCE(errors, '')
|
||||
FROM batch_runs
|
||||
ORDER BY started_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`, pageSize, offset)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var runs []BatchRun
|
||||
for rows.Next() {
|
||||
var run BatchRun
|
||||
var finishedAt sql.NullTime
|
||||
err := rows.Scan(
|
||||
&run.ID, &run.StartedAt, &finishedAt, &run.TotalOwners, &run.TotalRegistered,
|
||||
&run.TotalAddedToS2A, &run.SuccessRate, &run.DurationSeconds, &run.Status, &run.Errors,
|
||||
)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if finishedAt.Valid {
|
||||
run.FinishedAt = finishedAt.Time
|
||||
}
|
||||
runs = append(runs, run)
|
||||
}
|
||||
return runs, total, nil
|
||||
}
|
||||
|
||||
// GetBatchRunWithResults 获取批次详情(包含Team结果)
|
||||
func (d *DB) GetBatchRunWithResults(batchID int64) (*BatchRun, []BatchTeamResult, error) {
|
||||
// 获取批次信息
|
||||
var run BatchRun
|
||||
var finishedAt sql.NullTime
|
||||
err := d.db.QueryRow(`
|
||||
SELECT id, started_at, finished_at, total_owners, total_registered, total_added_to_s2a,
|
||||
success_rate, duration_seconds, status, COALESCE(errors, '')
|
||||
FROM batch_runs WHERE id = ?
|
||||
`, batchID).Scan(
|
||||
&run.ID, &run.StartedAt, &finishedAt, &run.TotalOwners, &run.TotalRegistered,
|
||||
&run.TotalAddedToS2A, &run.SuccessRate, &run.DurationSeconds, &run.Status, &run.Errors,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if finishedAt.Valid {
|
||||
run.FinishedAt = finishedAt.Time
|
||||
}
|
||||
|
||||
// 获取Team结果
|
||||
results, err := d.GetBatchTeamResults(batchID)
|
||||
if err != nil {
|
||||
return &run, nil, err
|
||||
}
|
||||
|
||||
return &run, results, nil
|
||||
}
|
||||
|
||||
// Close 关闭数据库
|
||||
func (d *DB) Close() error {
|
||||
if d.db != nil {
|
||||
|
||||
Reference in New Issue
Block a user