尝试优化一下
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)
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 优先,无则用 SNper 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 调用失败",
realtimeStart := time.Now()
result, realtimeErr := s.GetRealtimeStatus(ctx, "device", id)
logger.GetAppLogger().Info("资产刷新完成",
zap.Uint("device_id", id),
zap.Error(syncErr))
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")