diff --git a/app/Http/Resources/Api/V1/GuiderailModelResource.php b/app/Http/Resources/Api/V1/GuiderailModelResource.php index ba94ab04..415fe099 100644 --- a/app/Http/Resources/Api/V1/GuiderailModelResource.php +++ b/app/Http/Resources/Api/V1/GuiderailModelResource.php @@ -50,7 +50,7 @@ public function toArray(Request $request): array 'image_file_id' => $this->getImageFileId(), 'image_url' => $this->getImageUrl(), // 부품 조합 - 'components' => $this->enrichComponentsWithImageUrls($components), + 'components' => $this->enrichComponents($components), 'material_summary' => $materialSummary, 'component_count' => count($components), // 메타 @@ -88,23 +88,32 @@ private function getImageUrl(): ?string return $this->getImageFile()?->presignedUrl(); } - private function enrichComponentsWithImageUrls(array $components): array + private function enrichComponents(array $components): array { - $fileIds = array_filter(array_column($components, 'image_file_id')); - if (empty($fileIds)) { - return $components; - } + // sam_item_id → 기초자료 품목코드 매핑 + $itemIds = array_filter(array_column($components, 'sam_item_id')); + $itemCodes = ! empty($itemIds) + ? \App\Models\BendingItem::withoutGlobalScopes()->whereIn('id', $itemIds)->pluck('code', 'id')->toArray() + : []; - $files = \App\Models\Commons\File::whereIn('id', $fileIds) - ->whereNull('deleted_at') - ->get() - ->keyBy('id'); + // image_file_id → presigned URL 매핑 + $fileIds = array_filter(array_column($components, 'image_file_id')); + $files = ! empty($fileIds) + ? \App\Models\Commons\File::whereIn('id', $fileIds)->whereNull('deleted_at')->get()->keyBy('id') + : collect(); foreach ($components as &$comp) { + $samId = $comp['sam_item_id'] ?? null; + $comp['item_code'] = $samId ? ($itemCodes[$samId] ?? null) : null; + $fileId = $comp['image_file_id'] ?? null; - $comp['image_url'] = $fileId && isset($files[$fileId]) - ? $files[$fileId]->presignedUrl() - : null; + try { + $comp['image_url'] = $fileId && $files->has($fileId) + ? $files[$fileId]->presignedUrl() + : null; + } catch (\Throwable) { + $comp['image_url'] = null; + } } unset($comp); @@ -122,6 +131,7 @@ private function calcMaterialSummary(array $components): array $summary[$material] = ($summary[$material] ?? 0) + ($widthSum * $qty); } } + return $summary; } }