关闭轮询的卡/设备已导入其他卡管,去掉套餐到期、退款场景对暂停轮询卡的停机放行, 任何场景都不停机也不复机(单卡判断与设备下逐卡停机同步收口)。 场景判断函数更名为 StopResumeSceneIgnoresNetworkStatus,只保留不看库里开停状态的语义。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
3.8 KiB
Go
98 lines
3.8 KiB
Go
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
|
||
}
|
||
if suspended {
|
||
return stopResumeDecision{}, nil
|
||
}
|
||
if constants.StopResumeSceneIgnoresNetworkStatus(scenes) {
|
||
// 套餐失效类场景(套餐到期、退款):不看库里的开停状态,资产无可用主套餐即停机。
|
||
// 停复机成功后上游状态有延迟,库里的开停状态可能是旧值;上游已停时调用按已是目标状态结束。
|
||
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
|
||
}
|
||
}
|
||
switch card.NetworkStatus {
|
||
case constants.NetworkStatusOnline:
|
||
reasons, err := s.checkStopReasons(ctx, card)
|
||
if err != nil || len(reasons) == 0 {
|
||
return stopResumeDecision{}, err
|
||
}
|
||
primary := reasons[0]
|
||
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:
|
||
// 不看停机原因,是否复机只由本系统规则决定。
|
||
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
|
||
}
|