feat:Rock刷新去UID拦截,新增绑定错误自动刷新与使用/绑定超时预警
This commit is contained in:
@@ -38,17 +38,23 @@ func NewRockService(db *gorm.DB, client *RockClient, codecs ...*SecretCodec) *Ro
|
||||
|
||||
// RockStatsResponse 看板与库存统计返回
|
||||
type RockStatsResponse struct {
|
||||
AvailableStock int64 `json:"available_stock"`
|
||||
UsingStock int64 `json:"using_stock"`
|
||||
BoundStock int64 `json:"bound_stock"`
|
||||
RedeemedStock int64 `json:"redeemed_stock"`
|
||||
MismatchStock int64 `json:"mismatch_stock"` // 绑定错误(待排查)
|
||||
AbnormalStock int64 `json:"abnormal_stock"`
|
||||
DeletedStock int64 `json:"deleted_stock"` // 回收站中已软删除的数量
|
||||
AvailableStock int64 `json:"available_stock"`
|
||||
UsingStock int64 `json:"using_stock"`
|
||||
BoundStock int64 `json:"bound_stock"`
|
||||
RedeemedStock int64 `json:"redeemed_stock"`
|
||||
MismatchStock int64 `json:"mismatch_stock"` // 绑定错误(待排查)
|
||||
AbnormalStock int64 `json:"abnormal_stock"`
|
||||
DeletedStock int64 `json:"deleted_stock"` // 回收站中已软删除的数量
|
||||
// 过期预警计数:仅统计对应主状态(使用中 / 已绑定),用于筛选栏徽标。
|
||||
UsingOverdueStock int64 `json:"using_overdue_stock"`
|
||||
UsingDangerStock int64 `json:"using_danger_stock"`
|
||||
BoundOverdueStock int64 `json:"bound_overdue_stock"`
|
||||
UpstreamPoints float64 `json:"upstream_points"`
|
||||
AutoDeliveryEnabled bool `json:"auto_delivery_enabled"`
|
||||
TokenWarningStock int `json:"token_warning_stock"`
|
||||
AutoPurchaseEnabled bool `json:"auto_purchase_enabled"`
|
||||
// 自动刷新绑定错误开关状态,供筛选栏 Switch 回显
|
||||
AutoRefreshMismatchEnabled bool `json:"auto_refresh_mismatch_enabled"`
|
||||
// 自动补货冷却状态:供页面提示“下次可补货时间”
|
||||
AutoPurchaseCooldownSeconds int64 `json:"auto_purchase_cooldown_seconds"`
|
||||
NextAutoPurchaseAt string `json:"next_auto_purchase_at,omitempty"`
|
||||
@@ -76,18 +82,20 @@ func (e *RockConfigValidationError) Error() string {
|
||||
}
|
||||
|
||||
type UpdateRockConfigReq struct {
|
||||
AutoDeliveryEnabled *bool `json:"auto_delivery_enabled"`
|
||||
BaseURL *string `json:"base_url"`
|
||||
BearerToken *string `json:"bearer_token"`
|
||||
MinPointsWarning *float64 `json:"min_points_warning"`
|
||||
TokenWarningStock *int `json:"token_warning_stock"`
|
||||
UsingWarnDays *int `json:"using_warn_days"`
|
||||
UsingDangerDays *int `json:"using_danger_days"`
|
||||
AutoPurchaseEnabled *bool `json:"auto_purchase_enabled"`
|
||||
AutoPurchaseCount *int `json:"auto_purchase_count"`
|
||||
PurchaseCooldownMinutes *int `json:"purchase_cooldown_minutes"`
|
||||
LoginUsername *string `json:"login_username"`
|
||||
LoginPassword *string `json:"login_password"`
|
||||
AutoDeliveryEnabled *bool `json:"auto_delivery_enabled"`
|
||||
BaseURL *string `json:"base_url"`
|
||||
BearerToken *string `json:"bearer_token"`
|
||||
MinPointsWarning *float64 `json:"min_points_warning"`
|
||||
TokenWarningStock *int `json:"token_warning_stock"`
|
||||
UsingWarnDays *int `json:"using_warn_days"`
|
||||
UsingDangerDays *int `json:"using_danger_days"`
|
||||
BoundWarnDays *int `json:"bound_warn_days"`
|
||||
AutoRefreshMismatchEnabled *bool `json:"auto_refresh_mismatch_enabled"`
|
||||
AutoPurchaseEnabled *bool `json:"auto_purchase_enabled"`
|
||||
AutoPurchaseCount *int `json:"auto_purchase_count"`
|
||||
PurchaseCooldownMinutes *int `json:"purchase_cooldown_minutes"`
|
||||
LoginUsername *string `json:"login_username"`
|
||||
LoginPassword *string `json:"login_password"`
|
||||
}
|
||||
|
||||
// GetConfig 获取当前配置,若无则初始化
|
||||
@@ -105,6 +113,7 @@ func (s *RockService) GetConfig(ctx context.Context) (*model.RockConfig, error)
|
||||
TokenWarningStock: 10,
|
||||
UsingWarnDays: 3,
|
||||
UsingDangerDays: 5,
|
||||
BoundWarnDays: 2,
|
||||
AutoPurchaseEnabled: false,
|
||||
AutoPurchaseCount: 50,
|
||||
PurchaseCooldownMinutes: 10,
|
||||
@@ -209,6 +218,19 @@ func (s *RockService) UpdateConfig(ctx context.Context, req UpdateRockConfigReq)
|
||||
return nil, &RockConfigValidationError{Message: "使用中红色预警天数必须大于橙色预警天数"}
|
||||
}
|
||||
|
||||
// 绑定过期预警:已绑定超过 N 天视为绑定超时,独立于使用中两级预警。
|
||||
boundWarnDays := cfg.BoundWarnDays
|
||||
if req.BoundWarnDays != nil {
|
||||
boundWarnDays = *req.BoundWarnDays
|
||||
updates["bound_warn_days"] = boundWarnDays
|
||||
}
|
||||
if boundWarnDays < 1 || boundWarnDays > 365 {
|
||||
return nil, &RockConfigValidationError{Message: "绑定过期预警天数必须在 1-365 之间"}
|
||||
}
|
||||
if req.AutoRefreshMismatchEnabled != nil {
|
||||
updates["auto_refresh_mismatch_enabled"] = *req.AutoRefreshMismatchEnabled
|
||||
}
|
||||
|
||||
if len(updates) > 0 {
|
||||
updates["updated_at"] = time.Now()
|
||||
if err := s.db.WithContext(ctx).Model(&model.RockConfig{}).Where("id = ?", cfg.ID).Updates(updates).Error; err != nil {
|
||||
@@ -354,6 +376,26 @@ func (s *RockService) GetStats(ctx context.Context) (*RockStatsResponse, error)
|
||||
return nil, err
|
||||
}
|
||||
stats.DeletedStock = deletedCount
|
||||
stats.AutoRefreshMismatchEnabled = cfg.AutoRefreshMismatchEnabled
|
||||
|
||||
// 过期预警计数:仅统计 using / bound 主状态,避免把其它状态混进筛选徽标。
|
||||
now := time.Now()
|
||||
warnDays, dangerDays, boundWarnDays := rockOverdueThresholds(cfg)
|
||||
if err := s.db.WithContext(ctx).Model(&model.RockToken{}).
|
||||
Where("status = ? AND using_at IS NOT NULL AND using_at <= ?", model.RockStatusUsing, rockOverdueCutoff(now, warnDays)).
|
||||
Count(&stats.UsingOverdueStock).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.db.WithContext(ctx).Model(&model.RockToken{}).
|
||||
Where("status = ? AND using_at IS NOT NULL AND using_at <= ?", model.RockStatusUsing, rockOverdueCutoff(now, dangerDays)).
|
||||
Count(&stats.UsingDangerStock).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.db.WithContext(ctx).Model(&model.RockToken{}).
|
||||
Where("status = ? AND bound_at IS NOT NULL AND bound_at <= ?", model.RockStatusBound, rockOverdueCutoff(now, boundWarnDays)).
|
||||
Count(&stats.BoundOverdueStock).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cfg.AutoPurchaseEnabled {
|
||||
if remain := rockAutoPurchaseCooldownRemaining(cfg, time.Now()); remain > 0 {
|
||||
|
||||
Reference in New Issue
Block a user