Files
sam-api/app/Http/Resources/Api/V1/GuiderailModelResource.php
강영보 25db0df38b fix: [절곡] bending_models lot_no 제거 — legacy_code로 이관
- bending_models에는 LOT 개념 없음, lot_no 컬럼 불필요
- lot_no 값 → legacy_code로 이관 후 컬럼 DROP
- BendingModel fillable에서 lot_no 제거
- GuiderailModelResource에서 lot_no 제거
- BendingModelImport: lot_no → legacy_code
- 마이그레이션 rollback 로직 수정
2026-03-21 15:39:09 +09:00

138 lines
4.9 KiB
PHP

<?php
namespace App\Http\Resources\Api\V1;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class GuiderailModelResource extends JsonResource
{
public function toArray(Request $request): array
{
$components = $this->components ?? [];
$materialSummary = $this->material_summary;
if (empty($materialSummary) && ! empty($components)) {
$materialSummary = $this->calcMaterialSummary($components);
}
return [
'id' => $this->id,
'code' => $this->code,
'name' => $this->name,
'is_active' => $this->is_active,
// MNG2 호환
'item_type' => 'FG',
'item_category' => $this->model_type,
// 모델 속성 (정규 컬럼)
'model_name' => $this->model_name,
'check_type' => $this->check_type,
'rail_width' => $this->rail_width ? (int) $this->rail_width : null,
'rail_length' => $this->rail_length ? (int) $this->rail_length : null,
'finishing_type' => $this->finishing_type,
'item_sep' => $this->item_sep,
'model_UA' => $this->model_UA,
'search_keyword' => $this->search_keyword,
'author' => $this->author,
'memo' => $this->getOption('memo'),
'registration_date' => $this->registration_date?->format('Y-m-d'),
// 케이스 전용
'exit_direction' => $this->exit_direction,
'front_bottom_width' => $this->front_bottom_width ? (int) $this->front_bottom_width : null,
'box_width' => $this->box_width ? (int) $this->box_width : null,
'box_height' => $this->box_height ? (int) $this->box_height : null,
// 하단마감재 전용
'bar_width' => $this->bar_width ? (int) $this->bar_width : null,
'bar_height' => $this->bar_height ? (int) $this->bar_height : null,
// 수정자
'modified_by' => $this->getOption('modified_by'),
// 이미지
'image_file_id' => $this->getImageFileId(),
'image_url' => $this->getImageUrl(),
// 부품 조합
'components' => $this->enrichComponents($components),
'material_summary' => $materialSummary,
'component_count' => count($components),
// 메타
'created_at' => $this->created_at?->format('Y-m-d H:i:s'),
'updated_at' => $this->updated_at?->format('Y-m-d H:i:s'),
];
}
private function getImageFile(): ?\App\Models\Commons\File
{
$file = \App\Models\Commons\File::where('document_id', $this->id)
->where('document_type', 'bending_model')
->where('field_key', 'assembly_image')
->whereNull('deleted_at')
->orderByDesc('id')
->first();
if (! $file) {
$file = $this->files()
->where('field_key', 'bending_diagram')
->orderByDesc('id')
->first();
}
return $file;
}
private function getImageFileId(): ?int
{
return $this->getImageFile()?->id;
}
private function getImageUrl(): ?string
{
return $this->getImageFile()?->presignedUrl();
}
private function enrichComponents(array $components): array
{
// 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()
: [];
// 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;
try {
$comp['image_url'] = $fileId && $files->has($fileId)
? $files[$fileId]->presignedUrl()
: null;
} catch (\Throwable) {
$comp['image_url'] = null;
}
}
unset($comp);
return $components;
}
private function calcMaterialSummary(array $components): array
{
$summary = [];
foreach ($components as $comp) {
$material = $comp['material'] ?? null;
$widthSum = $comp['widthsum'] ?? $comp['width_sum'] ?? 0;
$qty = $comp['quantity'] ?? 1;
if ($material && $widthSum) {
$summary[$material] = ($summary[$material] ?? 0) + ($widthSum * $qty);
}
}
return $summary;
}
}