feat: iot_card/device Service 新增 UpdateRealnamePolicy 方法(含幂等检查)

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-04-17 17:30:13 +08:00
parent 1d75a4c31a
commit 71498883b5
2 changed files with 53 additions and 0 deletions

View File

@@ -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

View File

@@ -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
}