tenantId(); $sections = ItemSection::where('tenant_id', $tenantId) ->orderBy('created_at', 'desc') ->get(); // entity_relationships 기반이므로 각 섹션별로 관련 엔티티 로드 $sections->each(fn ($section) => $section->loadRelatedEntities()); return $sections; } /** * 독립 섹션 생성 (page_id가 있으면 링크도 연결) * * @param array $data ['page_id'(optional), 'title', 'type', 'description', 'is_default'] */ public function store(array $data): ItemSection { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); $pageId = $data['page_id'] ?? null; // page_id가 있으면 페이지 존재 확인 if ($pageId) { $page = ItemPage::where('tenant_id', $tenantId)->find($pageId); if (! $page) { throw new NotFoundHttpException(__('error.page_not_found')); } } // 1. 독립 섹션 생성 $section = ItemSection::create([ 'tenant_id' => $tenantId, 'group_id' => 1, 'title' => $data['title'], 'type' => $data['type'], 'order_no' => 0, 'is_template' => false, 'is_default' => $data['is_default'] ?? false, 'description' => $data['description'] ?? null, 'created_by' => $userId, ]); // 2. page_id가 있으면 링크 연결 if ($pageId) { $maxOrderNo = EntityRelationship::where('tenant_id', $tenantId) ->where('parent_type', EntityRelationship::TYPE_PAGE) ->where('parent_id', $pageId) ->where('child_type', EntityRelationship::TYPE_SECTION) ->max('order_no') ?? -1; EntityRelationship::link( $tenantId, EntityRelationship::TYPE_PAGE, $pageId, EntityRelationship::TYPE_SECTION, $section->id, $maxOrderNo + 1 ); } return $section->loadRelatedEntities(); } /** * 섹션 수정 */ public function update(int $id, array $data): ItemSection { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); $section = ItemSection::where('tenant_id', $tenantId) ->where('id', $id) ->first(); if (! $section) { throw new NotFoundHttpException(__('error.section_not_found')); } $section->update([ 'title' => $data['title'] ?? $section->title, 'type' => $data['type'] ?? $section->type, 'description' => $data['description'] ?? $section->description, 'is_default' => $data['is_default'] ?? $section->is_default, 'updated_by' => $userId, ]); return $section->fresh()->loadRelatedEntities(); } /** * 섹션 삭제 (Soft Delete) + 링크 해제 */ public function destroy(int $id): void { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); $section = ItemSection::where('tenant_id', $tenantId) ->where('id', $id) ->first(); if (! $section) { throw new NotFoundHttpException(__('error.section_not_found')); } // 1. 모든 부모 링크 해제 (페이지-섹션 관계) EntityRelationship::where('tenant_id', $tenantId) ->where('child_type', EntityRelationship::TYPE_SECTION) ->where('child_id', $id) ->delete(); // 2. 모든 자식 링크 해제 (섹션-필드, 섹션-BOM 관계) EntityRelationship::where('tenant_id', $tenantId) ->where('parent_type', EntityRelationship::TYPE_SECTION) ->where('parent_id', $id) ->delete(); // 3. 섹션 Soft Delete $section->update(['deleted_by' => $userId]); $section->delete(); // 4. 하위 필드/BOM도 Soft Delete $section->loadRelatedEntities(); foreach ($section->getRelation('fields') as $field) { $field->update(['deleted_by' => $userId]); $field->delete(); } foreach ($section->getRelation('bomItems') as $bomItem) { $bomItem->update(['deleted_by' => $userId]); $bomItem->delete(); } } }