尝试优化一下
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m0s

This commit is contained in:
2026-05-06 10:21:38 +08:00
parent 001f8caf35
commit b0bd37ec12

View File

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