diff --git a/internal/service/device/service.go b/internal/service/device/service.go index f7f10f9..cd97d25 100644 --- a/internal/service/device/service.go +++ b/internal/service/device/service.go @@ -1018,6 +1018,30 @@ func (s *Service) StartDevice(ctx context.Context, deviceID uint) error { return nil } +// UpdateRealnamePolicy 更新设备的实名认证策略 +func (s *Service) UpdateRealnamePolicy(ctx context.Context, deviceID uint, realnamePolicy string) error { + // 检查设备是否存在 + device, err := s.deviceStore.GetByID(ctx, deviceID) + if err != nil { + if err == gorm.ErrRecordNotFound { + return errors.New(errors.CodeNotFound, "设备不存在") + } + return errors.Wrap(errors.CodeDatabaseError, err, "查询设备失败") + } + + // 幂等检查 + if device.RealnamePolicy == realnamePolicy { + return nil + } + + // 更新数据库 + if err := s.deviceStore.UpdateRealnamePolicy(ctx, deviceID, realnamePolicy); err != nil { + return errors.Wrap(errors.CodeDatabaseError, err, "更新实名认证策略失败") + } + + return nil +} + func (s *Service) invalidatePollingCardCache(cardID uint) { if s.redis == nil { return diff --git a/internal/service/iot_card/service.go b/internal/service/iot_card/service.go index 5b5359a..267d6ac 100644 --- a/internal/service/iot_card/service.go +++ b/internal/service/iot_card/service.go @@ -1124,3 +1124,32 @@ func (s *Service) BatchDeleteCards(ctx context.Context, cardIDs []uint) error { return nil } + +// UpdateRealnamePolicy 更新卡的实名认证策略 +func (s *Service) UpdateRealnamePolicy(ctx context.Context, cardID uint, realnamePolicy string) error { + // 检查卡是否存在 + card, err := s.iotCardStore.GetByID(ctx, cardID) + if err != nil { + if err == gorm.ErrRecordNotFound { + return errors.New(errors.CodeNotFound, "IoT卡不存在") + } + return errors.Wrap(errors.CodeDatabaseError, err, "查询IoT卡失败") + } + + // 幂等检查 + if card.RealnamePolicy == realnamePolicy { + return nil + } + + // 更新数据库 + if err := s.iotCardStore.UpdateRealnamePolicy(ctx, cardID, realnamePolicy); err != nil { + return errors.Wrap(errors.CodeDatabaseError, err, "更新实名认证策略失败") + } + + s.logger.Info("更新卡实名认证策略", + zap.Uint("card_id", cardID), + zap.String("realname_policy", realnamePolicy), + ) + + return nil +}