Files
junhong_cmp_fiber/internal/service/iot_card/stop_resume_decide.go
邹进 d4fde99cc3
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m29s
fix: 停复机成功后上游状态滞后导致暂停轮询的卡到期或退款漏停
- 停复机成功后保护窗口内(polling.stop_resume.status_grace_seconds,默认 300 秒)查卡状态返回操作前的状态视为上游滞后,不回写网络状态
- 套餐到期、退款场景不看库里的开停状态,资产无可用主套餐即停机,上游已停时按已是目标状态结束
- 迁移 000257 插入保护窗口配置

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 10:41:45 +09:00

112 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package iot_card
import (
"context"
"go.uber.org/zap"
stopresumedomain "github.com/break/junhong_cmp_fiber/internal/domain/stopresume"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// WithStopResumeScene 声明本次自动停复机评估的触发场景;未声明时按套餐步骤(周期评估)处理。
func WithStopResumeScene(ctx context.Context, scene string) context.Context {
return stopresumedomain.WithScene(ctx, scene)
}
// stopResumeScenes 返回 ctx 中声明的场景集合(单个场景)。
func stopResumeScenes(ctx context.Context) []string {
return []string{stopresumedomain.Scene(ctx)}
}
// stopResumeDecision 是统一判断函数的结果。
type stopResumeDecision struct {
Action string // constants.StopResumeActionStop / StopResumeActionStart;空表示不动作
Reason string // 停机原因
Reasons []string // 全部命中的停机原因(按优先级)
DeviceID uint // 设备维度动作的设备;0 表示单卡
}
// decideCard 统一判断函数:暂停轮询(按场景) → 停机条件 → 复机条件。
// 暂停轮询的卡只在套餐失效类场景(套餐到期、退款)下放行无套餐停机,其余自动停复机一律不做;
// 套餐失效类场景不看库里的开停状态,资产无可用主套餐即停机。
// 设备维度原因(无套餐、流量耗尽)与绑定设备的复机返回 DeviceID,由调用方按设备处理。通道阈值锁由复机执行方判断。
func (s *StopResumeService) decideCard(ctx context.Context, card *model.IotCard, scenes []string) (stopResumeDecision, error) {
suspended, err := s.autoControlSuspended(ctx, card)
if err != nil {
return stopResumeDecision{}, err
}
allowSuspendedStop := constants.StopResumeSceneAllowsSuspendedStop(scenes)
if suspended && !allowSuspendedStop {
return stopResumeDecision{}, nil
}
if allowSuspendedStop {
// 套餐失效类场景(套餐到期、退款):不看库里的开停状态,资产无可用主套餐即停机。
// 停复机成功后上游状态有延迟,库里的开停状态可能是旧值;上游已停时调用按已是目标状态结束。
reasons, err := s.checkStopReasons(ctx, card)
if err != nil {
return stopResumeDecision{}, err
}
if containsStopReason(reasons, constants.StopReasonNoPackage) {
decision := stopResumeDecision{Action: constants.StopResumeActionStop, Reason: constants.StopReasonNoPackage, Reasons: reasons}
if !card.IsStandalone {
decision.DeviceID = s.boundDeviceID(ctx, card)
}
return decision, nil
}
if suspended {
return stopResumeDecision{}, nil
}
}
switch card.NetworkStatus {
case constants.NetworkStatusOnline:
reasons, err := s.checkStopReasons(ctx, card)
if err != nil || len(reasons) == 0 {
return stopResumeDecision{}, err
}
primary := reasons[0]
if suspended {
// 暂停轮询:只有套餐失效(无可用主套餐)才停机。
if !containsStopReason(reasons, constants.StopReasonNoPackage) {
return stopResumeDecision{}, nil
}
primary = constants.StopReasonNoPackage
}
decision := stopResumeDecision{Action: constants.StopResumeActionStop, Reason: primary, Reasons: reasons}
if !card.IsStandalone && isDeviceScopeReason(primary) {
decision.DeviceID = s.boundDeviceID(ctx, card)
}
return decision, nil
case constants.NetworkStatusOffline:
if suspended {
return stopResumeDecision{}, nil
}
// 不看停机原因,是否复机只由本系统规则决定。
resume, err := s.shouldResume(ctx, card)
if err != nil || !resume {
return stopResumeDecision{}, err
}
decision := stopResumeDecision{Action: constants.StopResumeActionStart}
if !card.IsStandalone {
decision.DeviceID = s.boundDeviceID(ctx, card)
}
return decision, nil
default:
return stopResumeDecision{}, nil
}
}
// boundDeviceID 返回卡的有效绑定设备;查询失败时降级为单卡处理并记日志。
func (s *StopResumeService) boundDeviceID(ctx context.Context, card *model.IotCard) uint {
deviceID, bound, err := s.getCardDeviceID(ctx, card)
if err != nil {
s.logger.Error("查询设备绑定关系失败,降级为单卡处理", zap.Uint("card_id", card.ID), zap.Error(err))
return 0
}
if !bound {
return 0
}
return deviceID
}