Files
dygou_go/internal/seckill/xiaodian_report.go
T

214 lines
6.0 KiB
Go

package seckill
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"douyu_go/internal/logger"
"douyu_go/internal/types"
)
const xiaodianReportFileName = "last_xiaodian_report.json"
func (s *Scheduler) startXiaodianReport(accounts []types.Account, products map[string]types.Product, sessionID string) {
s.reportMu.Lock()
defer s.reportMu.Unlock()
// 库存监控会把不同商品/账号拆成多次任务;同一会话追加账号,普通抢购则创建新报告。
if sessionID != "" && s.latestXiaodianReport != nil && s.latestXiaodianReport.SessionID == sessionID {
report := s.latestXiaodianReport
report.Stopped = false
report.FinishedAt = time.Time{}
existing := make(map[string]int, len(report.Accounts))
for index := range report.Accounts {
existing[report.Accounts[index].AccountName] = index
}
for _, account := range accounts {
product := products[account.Name]
if index, ok := existing[account.Name]; ok {
// 商品映射在会话内保持账号独立;仅补齐可能缺失的商品元数据。
item := &report.Accounts[index]
if item.ProductID == "" {
item.ProductID = product.CommodityID
}
if item.ProductName == "" {
item.ProductName = product.CommodityName
}
continue
}
report.Accounts = append(report.Accounts, types.XiaodianRunReportAccount{
AccountName: account.Name,
ProductID: product.CommodityID,
ProductName: product.CommodityName,
ProductPrice: product.Score,
PayType: product.PayType,
PayLabel: product.PayLabel,
Status: "待执行",
})
}
sort.Slice(report.Accounts, func(i, j int) bool {
return report.Accounts[i].AccountName < report.Accounts[j].AccountName
})
return
}
report := types.XiaodianRunReport{
ReportID: fmt.Sprintf("xiaodian_%s", time.Now().Format("20060102_150405.000")),
SessionID: sessionID,
StartedAt: time.Now(),
Accounts: make([]types.XiaodianRunReportAccount, 0, len(accounts)),
}
for _, account := range accounts {
product := products[account.Name]
report.Accounts = append(report.Accounts, types.XiaodianRunReportAccount{
AccountName: account.Name,
ProductID: product.CommodityID,
ProductName: product.CommodityName,
ProductPrice: product.Score,
PayType: product.PayType,
PayLabel: product.PayLabel,
Status: "待执行",
})
}
sort.Slice(report.Accounts, func(i, j int) bool {
return report.Accounts[i].AccountName < report.Accounts[j].AccountName
})
s.latestXiaodianReport = &report
}
func (s *Scheduler) updateXiaodianReport(results map[string]types.ExchangeResult) {
if len(results) == 0 {
return
}
s.reportMu.Lock()
defer s.reportMu.Unlock()
if s.latestXiaodianReport == nil {
return
}
for accountName, result := range results {
for i := range s.latestXiaodianReport.Accounts {
account := &s.latestXiaodianReport.Accounts[i]
if account.AccountName != accountName {
continue
}
account.Attempts++
account.LastMessage = result.Message
account.RoleName = result.RoleName
if result.OK {
account.Status = "成功"
account.FailureReason = ""
} else {
account.Status = "失败"
account.FailureReason = reportFailureReason(result.Message)
}
break
}
}
}
func (s *Scheduler) stopXiaodianReportAccounts(stopped map[string]string) {
if len(stopped) == 0 {
return
}
s.reportMu.Lock()
defer s.reportMu.Unlock()
if s.latestXiaodianReport == nil {
return
}
for i := range s.latestXiaodianReport.Accounts {
account := &s.latestXiaodianReport.Accounts[i]
if reason := stopped[account.AccountName]; reason != "" && account.Status != "成功" {
account.Status = "失败"
account.FailureReason = reason
}
}
}
func (s *Scheduler) finishXiaodianReport(stopped bool) {
s.reportMu.Lock()
if s.latestXiaodianReport == nil {
s.reportMu.Unlock()
return
}
report := s.latestXiaodianReport
report.FinishedAt = time.Now()
report.Stopped = stopped
if stopped {
for i := range report.Accounts {
account := &report.Accounts[i]
if account.Status == "待执行" {
account.Status = "已停止"
account.FailureReason = "任务已停止"
}
}
}
snapshot := cloneXiaodianReport(*report)
s.reportMu.Unlock()
if err := s.saveXiaodianReport(snapshot); err != nil {
logger.LogEvent("导出", "warn", "保存和平小店任务报告失败: %v", err)
}
}
// LatestXiaodianReport 返回最近一次和平小店任务报告的副本。
func (s *Scheduler) LatestXiaodianReport() (types.XiaodianRunReport, bool) {
s.reportMu.RLock()
defer s.reportMu.RUnlock()
if s.latestXiaodianReport == nil {
return types.XiaodianRunReport{}, false
}
return cloneXiaodianReport(*s.latestXiaodianReport), true
}
func (s *Scheduler) loadLatestXiaodianReport() {
data, err := os.ReadFile(s.xiaodianReportPath())
if err != nil {
return
}
var report types.XiaodianRunReport
if err := json.Unmarshal(data, &report); err != nil || report.ReportID == "" || len(report.Accounts) == 0 {
return
}
s.reportMu.Lock()
s.latestXiaodianReport = &report
s.reportMu.Unlock()
}
func (s *Scheduler) saveXiaodianReport(report types.XiaodianRunReport) error {
path := s.xiaodianReportPath()
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return err
}
data, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
}
tempPath := path + ".tmp"
if err := os.WriteFile(tempPath, data, 0600); err != nil {
return err
}
return os.Rename(tempPath, path)
}
func (s *Scheduler) xiaodianReportPath() string {
return filepath.Join(s.paths.DataDir, "reports", xiaodianReportFileName)
}
func cloneXiaodianReport(report types.XiaodianRunReport) types.XiaodianRunReport {
report.Accounts = append([]types.XiaodianRunReportAccount(nil), report.Accounts...)
return report
}
func reportFailureReason(message string) string {
if accountStopped, _, reason := classifyXiaodianRetryStop(message); accountStopped {
return reason
}
reason := strings.TrimSpace(strings.TrimPrefix(message, "失败|"))
return strings.TrimSpace(strings.TrimPrefix(reason, "和平小店兑换失败:"))
}