feat:自动补货/手动采购逻辑修改:批量获取-》打包-》获取Token列表

This commit is contained in:
2026-10-29 16:05:39 +08:00
parent 8198bd6feb
commit 8494fabc82
23 changed files with 3679 additions and 85 deletions
+63
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"strings"
"sync"
"time"
@@ -42,6 +43,16 @@ type RockStatsResponse struct {
AutoPurchaseEnabled bool `json:"auto_purchase_enabled"`
Username string `json:"username"`
LastHeartbeatAt string `json:"last_heartbeat_at,omitempty"`
LastPurchaseError string `json:"last_purchase_error,omitempty"`
LastPurchaseErrorAt string `json:"last_purchase_error_at,omitempty"`
}
type RockDashboardResult struct {
AvailableBlack int `json:"available_black"`
AvailableBlue int `json:"available_blue"`
BlackTokenCost float64 `json:"black_token_cost"`
BlueTokenCost float64 `json:"blue_token_cost"`
CostUpdated bool `json:"cost_updated"`
}
type UpdateRockConfigReq struct {
@@ -66,6 +77,7 @@ func (s *RockService) GetConfig(ctx context.Context) (*model.RockConfig, error)
BaseURL: model.RockDefaultBaseURL,
BearerToken: "",
MinPointsWarning: 1050.00,
BlackTokenCost: 20.00,
TokenWarningStock: 10,
AutoPurchaseEnabled: false,
AutoPurchaseCount: 50,
@@ -170,6 +182,53 @@ func (s *RockService) TestConnection(ctx context.Context, bearerToken string) (*
return me, nil
}
// RefreshDashboard 拉取上游 dashboard,并在采购单价变化时同步到本系统。
func (s *RockService) RefreshDashboard(ctx context.Context) (*RockDashboardResult, error) {
cfg, err := s.GetConfig(ctx)
if err != nil {
return nil, err
}
return s.refreshDashboardWithConfig(ctx, cfg)
}
// refreshDashboardWithConfig 复用已读取的配置,避免同一次采购内重复查库。
func (s *RockService) refreshDashboardWithConfig(ctx context.Context, cfg *model.RockConfig) (*RockDashboardResult, error) {
if cfg == nil {
return nil, errors.New("Rock 配置不存在")
}
if strings.TrimSpace(cfg.BearerToken) == "" {
return nil, errors.New("请先配置 0666.pro Bearer Token")
}
dash, err := s.client.GetDashboard(ctx, cfg.BearerToken)
if err != nil {
return nil, err
}
result := &RockDashboardResult{
AvailableBlack: dash.Available.Black,
AvailableBlue: dash.Available.Blue,
BlueTokenCost: dash.ResolveBlueTokenCost(),
}
cost := dash.ResolveBlackTokenCost()
if cost <= 0 {
result.BlackTokenCost = cfg.BlackTokenCost
return result, nil
}
result.BlackTokenCost = cost
// 价格一样就完全不写库;只有变化超过 0.001 才更新。
if math.Abs(cfg.BlackTokenCost-cost) > 0.001 {
if err := s.db.WithContext(ctx).Model(&model.RockConfig{}).
Where("id = ?", cfg.ID).
Update("black_token_cost", cost).Error; err != nil {
return nil, err
}
result.CostUpdated = true
}
return result, nil
}
// GetStats 获取概览统计
func (s *RockService) GetStats(ctx context.Context) (*RockStatsResponse, error) {
cfg, err := s.GetConfig(ctx)
@@ -186,6 +245,10 @@ func (s *RockService) GetStats(ctx context.Context) (*RockStatsResponse, error)
if cfg.LastHeartbeatAt != nil {
stats.LastHeartbeatAt = cfg.LastHeartbeatAt.Format("2006-01-02 15:04:05")
}
stats.LastPurchaseError = cfg.LastError
if cfg.LastErrorAt != nil {
stats.LastPurchaseErrorAt = cfg.LastErrorAt.Format("2006-01-02 15:04:05")
}
type CountResult struct {
Status string