- ItemMasterService: 모든 독립 필드 목록 반환 추가 - Swagger: ItemMasterInitResponse 스키마에 fields 속성 추가
184 lines
6.3 KiB
PHP
184 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ItemMaster;
|
|
|
|
use App\Models\ItemMaster\CustomTab;
|
|
use App\Models\ItemMaster\EntityRelationship;
|
|
use App\Models\ItemMaster\ItemBomItem;
|
|
use App\Models\ItemMaster\ItemField;
|
|
use App\Models\ItemMaster\ItemPage;
|
|
use App\Models\ItemMaster\ItemSection;
|
|
use App\Models\ItemMaster\UnitOption;
|
|
use App\Services\Service;
|
|
|
|
class ItemMasterService extends Service
|
|
{
|
|
/**
|
|
* 초기화 데이터 로드
|
|
*
|
|
* - pages (linkedSections 기반 중첩)
|
|
* - sections (모든 독립 섹션)
|
|
* - fields (모든 독립 필드)
|
|
* - customTabs (columnSetting 포함)
|
|
* - unitOptions
|
|
*/
|
|
public function init(): array
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
|
|
// 1. 페이지 목록
|
|
$pages = ItemPage::where('tenant_id', $tenantId)
|
|
->where('is_active', 1)
|
|
->get();
|
|
|
|
// 2. 페이지별 linkedSections 조회 (entity_relationships 기반)
|
|
$pagesWithSections = $pages->map(function ($page) use ($tenantId) {
|
|
$linkedSections = $this->getLinkedSections($tenantId, $page->id);
|
|
|
|
return [
|
|
'id' => $page->id,
|
|
'tenant_id' => $page->tenant_id,
|
|
'group_id' => $page->group_id,
|
|
'page_name' => $page->page_name,
|
|
'item_type' => $page->item_type,
|
|
'absolute_path' => $page->absolute_path,
|
|
'is_active' => $page->is_active,
|
|
'created_by' => $page->created_by,
|
|
'updated_by' => $page->updated_by,
|
|
'created_at' => $page->created_at,
|
|
'updated_at' => $page->updated_at,
|
|
'sections' => $linkedSections,
|
|
];
|
|
});
|
|
|
|
// 3. 모든 독립 섹션 (재사용 가능 목록)
|
|
$sections = ItemSection::where('tenant_id', $tenantId)
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
// entity_relationships 기반이므로 각 섹션별로 관련 엔티티 로드
|
|
$sections->each(fn ($section) => $section->loadRelatedEntities());
|
|
|
|
// 4. 커스텀 탭 (컬럼 설정 포함)
|
|
$customTabs = CustomTab::with('columnSetting')
|
|
->where('tenant_id', $tenantId)
|
|
->orderBy('order_no')
|
|
->get();
|
|
|
|
// 5. 단위 옵션
|
|
$unitOptions = UnitOption::where('tenant_id', $tenantId)->get();
|
|
|
|
// 6. 모든 필드 목록 (재사용 가능 목록)
|
|
$fields = ItemField::where('tenant_id', $tenantId)
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
return [
|
|
'pages' => $pagesWithSections,
|
|
'sections' => $sections,
|
|
'fields' => $fields,
|
|
'customTabs' => $customTabs,
|
|
'unitOptions' => $unitOptions,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 페이지에 연결된 섹션 조회 (entity_relationships 기반)
|
|
*/
|
|
private function getLinkedSections(int $tenantId, int $pageId): array
|
|
{
|
|
// 페이지-섹션 관계 조회
|
|
$relationships = EntityRelationship::where('tenant_id', $tenantId)
|
|
->where('parent_type', EntityRelationship::TYPE_PAGE)
|
|
->where('parent_id', $pageId)
|
|
->where('child_type', EntityRelationship::TYPE_SECTION)
|
|
->orderBy('order_no')
|
|
->get();
|
|
|
|
$sections = [];
|
|
foreach ($relationships as $rel) {
|
|
$section = ItemSection::find($rel->child_id);
|
|
|
|
if ($section) {
|
|
// 섹션에 연결된 필드 (entity_relationships 기반)
|
|
$linkedFields = $this->getLinkedFields($tenantId, $section->id);
|
|
// 섹션에 연결된 BOM 항목 (entity_relationships 기반)
|
|
$linkedBomItems = $this->getLinkedBomItems($tenantId, $section->id);
|
|
|
|
$sectionData = $section->toArray();
|
|
$sectionData['order_no'] = $rel->order_no;
|
|
// 연결 잠금 상태 (이 페이지-섹션 관계의 잠금)
|
|
$sectionData['is_locked'] = (bool) $rel->is_locked;
|
|
|
|
// FK 기반 필드 + 링크 기반 필드 병합
|
|
if (! empty($linkedFields)) {
|
|
$sectionData['fields'] = $linkedFields;
|
|
}
|
|
|
|
// FK 기반 BOM + 링크 기반 BOM 병합
|
|
if (! empty($linkedBomItems)) {
|
|
$sectionData['bom_items'] = $linkedBomItems;
|
|
}
|
|
|
|
$sections[] = $sectionData;
|
|
}
|
|
}
|
|
|
|
return $sections;
|
|
}
|
|
|
|
/**
|
|
* 섹션에 연결된 필드 조회 (entity_relationships 기반)
|
|
*/
|
|
private function getLinkedFields(int $tenantId, int $sectionId): array
|
|
{
|
|
$relationships = EntityRelationship::where('tenant_id', $tenantId)
|
|
->where('parent_type', EntityRelationship::TYPE_SECTION)
|
|
->where('parent_id', $sectionId)
|
|
->where('child_type', EntityRelationship::TYPE_FIELD)
|
|
->orderBy('order_no')
|
|
->get();
|
|
|
|
$fields = [];
|
|
foreach ($relationships as $rel) {
|
|
$field = ItemField::find($rel->child_id);
|
|
if ($field) {
|
|
$fieldData = $field->toArray();
|
|
$fieldData['order_no'] = $rel->order_no;
|
|
// 연결 잠금 상태 (이 섹션-필드 관계의 잠금)
|
|
$fieldData['is_locked'] = (bool) $rel->is_locked;
|
|
$fields[] = $fieldData;
|
|
}
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
|
|
/**
|
|
* 섹션에 연결된 BOM 항목 조회 (entity_relationships 기반)
|
|
*/
|
|
private function getLinkedBomItems(int $tenantId, int $sectionId): array
|
|
{
|
|
$relationships = EntityRelationship::where('tenant_id', $tenantId)
|
|
->where('parent_type', EntityRelationship::TYPE_SECTION)
|
|
->where('parent_id', $sectionId)
|
|
->where('child_type', EntityRelationship::TYPE_BOM)
|
|
->orderBy('order_no')
|
|
->get();
|
|
|
|
$bomItems = [];
|
|
foreach ($relationships as $rel) {
|
|
$bomItem = ItemBomItem::find($rel->child_id);
|
|
if ($bomItem) {
|
|
$bomData = $bomItem->toArray();
|
|
$bomData['order_no'] = $rel->order_no;
|
|
// 연결 잠금 상태 (이 섹션-BOM 관계의 잠금)
|
|
$bomData['is_locked'] = (bool) $rel->is_locked;
|
|
$bomItems[] = $bomData;
|
|
}
|
|
}
|
|
|
|
return $bomItems;
|
|
}
|
|
}
|