diff --git a/internal/service/asset/service.go b/internal/service/asset/service.go index 678ad9c..0894e1f 100644 --- a/internal/service/asset/service.go +++ b/internal/service/asset/service.go @@ -531,6 +531,8 @@ func (s *Service) Refresh(ctx context.Context, assetType string, id uint) (*dto. return s.GetRealtimeStatus(ctx, "card", id) case "device": + totalStart := time.Now() + // 检查冷却期 cooldownKey := constants.RedisDeviceRefreshCooldownKey(id) if s.redis.Exists(ctx, cooldownKey).Val() > 0 { @@ -542,13 +544,30 @@ func (s *Service) Refresh(ctx context.Context, assetType string, id uint) (*dto. if err != nil { return nil, errors.Wrap(errors.CodeInternalError, err, "查询绑定卡失败") } + + cardIDs := make([]uint, 0, len(bindings)) for _, b := range bindings { - card, cardErr := s.iotCardStore.GetByID(ctx, b.IotCardID) - if cardErr != nil { - logger.GetAppLogger().Warn("刷新设备绑定卡失败:查卡失败", + cardIDs = append(cardIDs, b.IotCardID) + } + cards, err := s.iotCardStore.GetByIDs(ctx, cardIDs) + if err != nil { + return nil, errors.Wrap(errors.CodeInternalError, err, "查询绑定卡详情失败") + } + + cardMap := make(map[uint]*model.IotCard, len(cards)) + for _, card := range cards { + cardMap[card.ID] = card + } + + cardRefreshStart := time.Now() + refreshedCardCount := 0 + for _, b := range bindings { + card, ok := cardMap[b.IotCardID] + if !ok { + logger.GetAppLogger().Warn("刷新设备绑定卡失败:未找到卡信息", zap.Uint("device_id", id), zap.Uint("card_id", b.IotCardID), - zap.Error(cardErr)) + ) continue } if refreshErr := s.iotCardService.RefreshCardDataFromGateway(ctx, card.ICCID); refreshErr != nil { @@ -556,36 +575,29 @@ func (s *Service) Refresh(ctx context.Context, assetType string, id uint) (*dto. zap.Uint("device_id", id), zap.String("iccid", card.ICCID), zap.Error(refreshErr)) + continue } + refreshedCardCount++ } + cardRefreshDuration := time.Since(cardRefreshStart) // 设置冷却 Key s.redis.Set(ctx, cooldownKey, 1, constants.DeviceRefreshCooldownDuration) - // 刷新设备自身信息(在线状态、当前卡、固件版本等),失败不阻断刷新流程(per D-15) - if s.gatewayClient != nil { - device, devErr := s.deviceStore.GetByID(ctx, id) - if devErr == nil { - // IMEI 优先,无则用 SN(per D-3 注释说明) - cardNo := device.IMEI - if cardNo == "" { - cardNo = device.SN - } - if cardNo != "" { - if syncResp, syncErr := s.gatewayClient.SyncDeviceInfo(ctx, &gateway.SyncDeviceInfoReq{ - CardNo: cardNo, - }); syncErr == nil { - s.updateDeviceFromSyncInfo(ctx, id, syncResp) - } else { - logger.GetAppLogger().Warn("sync-info 调用失败", - zap.Uint("device_id", id), - zap.Error(syncErr)) - } - } - } + realtimeStart := time.Now() + result, realtimeErr := s.GetRealtimeStatus(ctx, "device", id) + logger.GetAppLogger().Info("资产刷新完成", + zap.Uint("device_id", id), + zap.Int("binding_count", len(bindings)), + zap.Int("refreshed_card_count", refreshedCardCount), + zap.Duration("card_refresh_duration", cardRefreshDuration), + zap.Duration("realtime_status_duration", time.Since(realtimeStart)), + zap.Duration("total_duration", time.Since(totalStart)), + ) + if realtimeErr != nil { + return nil, realtimeErr } - - return s.GetRealtimeStatus(ctx, "device", id) + return result, nil default: return nil, errors.New(errors.CodeInvalidParam, "不支持的资产类型,仅支持 card 或 device")