Files
junhong_cmp_fiber/internal/service/iot_card/stop_resume_unified.go
邹进 79b7580554
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m30s
feat: 下线设备保护期
- 后台设备停复机不再设置 1 小时保护期,也不再因保护期拒绝相反操作;单卡手动停复机去掉保护期检查
- 删除保护期一致性轮询(polling:protect)及其强制停复机方法;调度、初始化、监控、匹配预览不再包含保护期;已入队的旧任务由空处理函数直接确认,下一次发布移除
- 轮询配置接口不再接受与返回 protect_check_interval;并发配置不再返回 protect,查询、更新、重置该类型时拒绝
- 资产详情 device_protect_status 固定返回 none 并标记废弃,兼容老版本客户端
- 删除保护期 Redis key 与时长常量;停机原因 protect_period 保留供历史数据与开放接口映射
- 迁移 000249 只把 protect_check_interval、last_protect_check_at 列注释标记为已废弃,不删列不改数据
2026-10-25 17:19:10 +09:00

83 lines
3.1 KiB
Go

package iot_card
import (
"context"
stderrors "errors"
"gorm.io/gorm"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/pkg/config"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
)
// realnameControlEnabled 是通道实名管控开启时的取值。
const realnameControlEnabled = 1
// unifiedStopResumeEnabled 判定停复机新规则总开关是否开启。
//
// 开启后:复机不看停机原因;实名按卡所属通道的实名管控判断(不区分行业卡、不看卡/设备实名策略);
// 卡或所在设备暂停轮询时不自动停复机。关闭时全部停复机路径与原有行为一致。
func unifiedStopResumeEnabled() bool {
cfg := config.Get()
return cfg != nil && cfg.StopResume.UnifiedRuleEnabled
}
// autoResumableStopReason 判定该停因的停机卡能否进入自动复机判断:总开关开启时不看停因,
// 关闭时只处理轮询系统引起的停机。
func autoResumableStopReason(reason string) bool {
return unifiedStopResumeEnabled() || isPollingStopReason(reason)
}
// autoControlSuspended 判定总开关开启时该卡是否因暂停轮询而不自动停复机:卡自身或其有效绑定设备暂停轮询。
// 总开关关闭时恒为 false。
func (s *StopResumeService) autoControlSuspended(ctx context.Context, card *model.IotCard) (bool, error) {
if !unifiedStopResumeEnabled() || card == nil {
return false, nil
}
if !card.EnablePolling {
return true, nil
}
deviceID, bound, err := s.getCardDeviceID(ctx, card)
if err != nil {
return false, errors.Wrap(errors.CodeDatabaseError, err, "查询卡绑定设备失败")
}
if !bound {
return false, nil
}
var device model.Device
if err := s.db.WithContext(ctx).Select("id", "enable_polling").First(&device, deviceID).Error; err != nil {
if stderrors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return false, errors.Wrap(errors.CodeDatabaseError, err, "查询设备轮询状态失败")
}
return !device.EnablePolling, nil
}
// channelRealnameControlled 查询卡所属通道是否开启实名管控。
func (s *StopResumeService) channelRealnameControlled(ctx context.Context, card *model.IotCard) (bool, error) {
var values []int
if err := s.db.WithContext(ctx).Model(&model.Carrier{}).
Where("id = ?", card.CarrierID).Limit(1).Pluck("realname_control_enabled", &values).Error; err != nil {
return false, errors.Wrap(errors.CodeDatabaseError, err, "查询通道实名管控失败")
}
if len(values) == 0 {
return false, errors.New(errors.CodeNotFound, "卡所属运营商通道不存在,无法判断实名管控")
}
return values[0] == realnameControlEnabled, nil
}
// unifiedRealnameOK 是总开关开启后的实名门槛:通道未开启实名管控即满足;开启时只看卡是否已实名。
func (s *StopResumeService) unifiedRealnameOK(ctx context.Context, card *model.IotCard) (bool, error) {
controlled, err := s.channelRealnameControlled(ctx, card)
if err != nil {
return false, err
}
if !controlled {
return true, nil
}
return card.RealNameStatus == constants.RealNameStatusVerified, nil
}