tenantId(); $query = ItemSection::where('tenant_id', $tenantId) ->with(['fields', 'bomItems']); if ($isTemplate === true) { $query->templates(); } elseif ($isTemplate === false) { $query->nonTemplates(); } return $query->orderBy('created_at', 'desc')->get(); } /** * 독립 섹션 생성 (페이지 연결 없음) * * POST /api/v1/item-master/sections */ public function storeIndependent(array $data): ItemSection { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); $section = ItemSection::create([ 'tenant_id' => $tenantId, 'group_id' => $data['group_id'] ?? 1, 'page_id' => null, 'title' => $data['title'], 'type' => $data['type'], 'order_no' => 0, 'is_template' => $data['is_template'] ?? false, 'is_default' => $data['is_default'] ?? false, 'description' => $data['description'] ?? null, 'created_by' => $userId, ]); return $section->load(['fields', 'bomItems']); } /** * 섹션 복제 * * POST /api/v1/item-master/sections/{id}/clone */ public function clone(int $id): ItemSection { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); $original = ItemSection::where('tenant_id', $tenantId) ->where('id', $id) ->with(['fields', 'bomItems']) ->first(); if (! $original) { throw new NotFoundHttpException(__('error.section_not_found')); } // 섹션 복제 $cloned = ItemSection::create([ 'tenant_id' => $tenantId, 'group_id' => $original->group_id, 'page_id' => null, 'title' => $original->title.' (복사본)', 'type' => $original->type, 'order_no' => 0, 'is_template' => $original->is_template, 'is_default' => false, 'description' => $original->description, 'created_by' => $userId, ]); // 필드 복제 foreach ($original->fields as $field) { ItemField::create([ 'tenant_id' => $tenantId, 'group_id' => $field->group_id, 'section_id' => $cloned->id, 'field_name' => $field->field_name, 'field_type' => $field->field_type, 'order_no' => $field->order_no, 'is_required' => $field->is_required, 'default_value' => $field->default_value, 'placeholder' => $field->placeholder, 'display_condition' => $field->display_condition, 'validation_rules' => $field->validation_rules, 'options' => $field->options, 'properties' => $field->properties, 'created_by' => $userId, ]); } // BOM 항목 복제 foreach ($original->bomItems as $bom) { ItemBomItem::create([ 'tenant_id' => $tenantId, 'group_id' => $bom->group_id, 'section_id' => $cloned->id, 'item_code' => $bom->item_code, 'item_name' => $bom->item_name, 'quantity' => $bom->quantity, 'unit' => $bom->unit, 'unit_price' => $bom->unit_price, 'total_price' => $bom->total_price, 'spec' => $bom->spec, 'note' => $bom->note, 'created_by' => $userId, ]); } return $cloned->load(['fields', 'bomItems']); } /** * 섹션 사용처 조회 (어떤 페이지에 연결되어 있는지) * * GET /api/v1/item-master/sections/{id}/usage */ public function getUsage(int $id): array { $tenantId = $this->tenantId(); $section = ItemSection::where('tenant_id', $tenantId) ->where('id', $id) ->first(); if (! $section) { throw new NotFoundHttpException(__('error.section_not_found')); } // 1. 기존 FK 기반 연결 (page_id) $directPage = $section->page; // 2. entity_relationships 기반 연결 $linkedPageIds = EntityRelationship::where('child_type', EntityRelationship::TYPE_SECTION) ->where('child_id', $id) ->where('parent_type', EntityRelationship::TYPE_PAGE) ->pluck('parent_id') ->toArray(); return [ 'section_id' => $id, 'direct_page' => $directPage, 'linked_pages' => $section->linkedPages()->get(), 'total_usage_count' => ($directPage ? 1 : 0) + count($linkedPageIds), ]; } /** * 섹션 생성 */ public function store(int $pageId, array $data): ItemSection { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); // order_no 자동 계산 (해당 페이지의 마지막 섹션 + 1) $maxOrder = ItemSection::where('tenant_id', $tenantId) ->where('page_id', $pageId) ->max('order_no'); $section = ItemSection::create([ 'tenant_id' => $tenantId, 'page_id' => $pageId, 'title' => $data['title'], 'type' => $data['type'], 'order_no' => ($maxOrder ?? -1) + 1, 'created_by' => $userId, ]); $section->load(['fields', 'bomItems']); return $section; } /** * 섹션 수정 */ 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.not_found')); } $section->update([ 'title' => $data['title'] ?? $section->title, 'updated_by' => $userId, ]); $section->load(['fields', 'bomItems']); return $section; } /** * 섹션 삭제 (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.not_found')); } $section->update(['deleted_by' => $userId]); $section->delete(); // Cascade: 하위 필드도 Soft Delete foreach ($section->fields as $field) { $field->update(['deleted_by' => $userId]); $field->delete(); } foreach ($section->bomItems as $bomItem) { $bomItem->update(['deleted_by' => $userId]); $bomItem->delete(); } } /** * 섹션 순서 변경 * * @param array $items [['id' => 1, 'order_no' => 0], ['id' => 2, 'order_no' => 1], ...] */ public function reorder(int $pageId, array $items): void { $tenantId = $this->tenantId(); $userId = $this->apiUserId(); foreach ($items as $item) { ItemSection::where('tenant_id', $tenantId) ->where('page_id', $pageId) ->where('id', $item['id']) ->update([ 'order_no' => $item['order_no'], 'updated_by' => $userId, ]); } } }