package store import ( "fmt" "time" "daili/internal/domain" ) // ExportRowSet 是导出的表头与数据行。 type ExportRowSet struct { Header []string Rows [][]string } // ExportCustomers 导出客户。 func (db *DB) ExportCustomers() (*ExportRowSet, error) { rows, err := db.sql.Query(`SELECT id,tail,name,game_name,game_server,contact,tags,source,note,created_at FROM customers ORDER BY id`) if err != nil { return nil, err } defer rows.Close() out := &ExportRowSet{Header: []string{"ID", "手机尾号", "客户名", "游戏名", "区服/UID", "联系方式", "标签", "来源", "备注", "创建时间"}} for rows.Next() { var id int64 var tail, name, game, server, contact, tags, source, note, created string if err := rows.Scan(&id, &tail, &name, &game, &server, &contact, &tags, &source, ¬e, &created); err != nil { return nil, err } out.Rows = append(out.Rows, []string{fmt.Sprint(id), tail, name, game, server, contact, tags, source, note, created}) } return out, rows.Err() } // ExportSubscriptions 导出订阅(含派生剩余量)。 func (db *DB) ExportSubscriptions(today time.Time, warnDays int) (*ExportRowSet, error) { subs, _, err := db.ListSubs(SubFilter{Today: today, Limit: 100000}) if err != nil { return nil, err } out := &ExportRowSet{Header: []string{"订阅ID", "手机尾号", "客户名", "套餐", "类型", "状态", "开始日", "结束日", "剩余", "原文状态", "备注", "更新时间"}} stateLabel := map[string]string{ domain.StateActive: "进行中", domain.StatePending: "未开始", domain.StatePaused: "暂停中", domain.StateExpired: "已到期", domain.StateClosed: "已结束", } for _, s := range subs { e := s.Eval(today, warnDays) out.Rows = append(out.Rows, []string{ fmt.Sprint(s.ID), s.CustomerTail, s.CustomerName, s.PackageName, "按日", stateLabel[e.State], s.StartDate, s.EndDate, fmt.Sprintf("%d 天", e.RemainingDays), s.Status, s.Note, s.UpdatedAt, }) } return out, nil } // ExportLogs 导出操作日志。 func (db *DB) ExportLogs() (*ExportRowSet, error) { rows, err := db.sql.Query(`SELECT created_at,operator_name,action,entity,summary,ip FROM op_logs ORDER BY id DESC`) if err != nil { return nil, err } defer rows.Close() out := &ExportRowSet{Header: []string{"时间", "操作人", "动作", "对象", "摘要", "IP"}} for rows.Next() { var created, operator, action, entity, summary, ip string if err := rows.Scan(&created, &operator, &action, &entity, &summary, &ip); err != nil { return nil, err } out.Rows = append(out.Rows, []string{created, operator, action, entity, summary, ip}) } return out, rows.Err() }