Files
subtrack/internal/store/export.go
T
chun_qiu f047662129
build / build-and-test (push) Failing after 1m24s
首次推送
2026-10-18 19:44:38 +08:00

82 lines
3.0 KiB
Go

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, &note, &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.StateUsedUp: "次数用尽", domain.StateClosed: "已结束",
}
for _, s := range subs {
e := s.Eval(today, warnDays)
remaining := ""
if s.Unit == domain.UnitDay {
remaining = fmt.Sprintf("%d 天", e.RemainingDays)
} else {
remaining = fmt.Sprintf("%d 次", e.RemainingCount)
}
out.Rows = append(out.Rows, []string{
fmt.Sprint(s.ID), s.CustomerTail, s.CustomerName, s.PackageName,
map[string]string{domain.UnitDay: "按日", domain.UnitCount: "按次"}[s.Unit],
stateLabel[e.State], s.StartDate, s.EndDate,
fmt.Sprint(s.TotalCount), fmt.Sprint(s.UsedCount), remaining, 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()
}