diff --git a/internal/task/device_import.go b/internal/task/device_import.go index 104a08c6..a0114cc2 100644 --- a/internal/task/device_import.go +++ b/internal/task/device_import.go @@ -258,22 +258,24 @@ func (h *DeviceImportHandler) processBatch(ctx context.Context, task *model.Devi return } - var existingCards map[string]*model.IotCard - var boundCards map[string]bool + existingCards := make(map[string]*model.IotCard) + boundCards := make(map[string]bool) if len(allICCIDs) > 0 { cards, err := h.iotCardStore.GetByICCIDs(ctx, allICCIDs) if err != nil { h.logger.Error("查询卡信息失败", zap.Error(err)) - } else { - existingCards = make(map[string]*model.IotCard) - for _, card := range cards { - existingCards[card.ICCID] = card - } + appendDeviceImportBatchQueryFailures(batch, result) + return + } + for _, card := range cards { + existingCards[card.ICCID] = card } boundCards, err = h.deviceSimBindingStore.GetBoundICCIDs(ctx, allICCIDs) if err != nil { h.logger.Error("查询卡绑定状态失败", zap.Error(err)) + appendDeviceImportBatchQueryFailures(batch, result) + return } } @@ -442,6 +444,17 @@ func (h *DeviceImportHandler) processBatch(ctx context.Context, task *model.Devi } } +func appendDeviceImportBatchQueryFailures(batch []utils.DeviceRow, result *deviceImportResult) { + for _, row := range batch { + result.failedItems = append(result.failedItems, model.ImportResultItem{ + Line: row.Line, + ICCID: row.VirtualNo, + Reason: "数据库查询失败", + }) + result.failCount++ + } +} + func (h *DeviceImportHandler) appendDeviceCreateAudit(ctx context.Context, tx *gorm.DB, task *model.DeviceImportTask, device *model.Device) error { if h.auditWriter == nil || task == nil || device == nil || device.ID == 0 { return pkgerrors.New(pkgerrors.CodeInvalidStatus, "设备导入统一审计接缝未配置或资源不完整")