refactor(item-master): 독립 엔티티 아키텍처 적용 및 Swagger 보완

- FK 컬럼 제거: item_sections.page_id, item_fields.section_id, item_bom_items.section_id
- entity_relationships 테이블로 전환하여 독립 엔티티 구조 확립
- ItemMasterField 관련 파일 삭제 (Controller, Service, Model, Requests)
- destroy 메서드 독립 엔티티 아키텍처 적용 (관계 링크만 삭제)
- Swagger 스키마에서 FK 참조 제거
- FormRequest 및 Swagger에 group_id(계층번호) 필드 추가
This commit is contained in:
2025-11-27 10:28:51 +09:00
parent 1d2dadc7da
commit 9588945922
18 changed files with 361 additions and 550 deletions

View File

@@ -11,7 +11,7 @@
class ItemFieldService extends Service
{
/**
* 독립 필드 목록 조회
* 모든 필드 목록 조회
*
* GET /api/v1/item-master/fields
*/
@@ -37,7 +37,6 @@ public function storeIndependent(array $data): ItemField
$field = ItemField::create([
'tenant_id' => $tenantId,
'group_id' => $data['group_id'] ?? 1,
'section_id' => null,
'field_name' => $data['field_name'],
'field_type' => $data['field_type'],
'order_no' => 0,
@@ -48,6 +47,9 @@ public function storeIndependent(array $data): ItemField
'validation_rules' => $data['validation_rules'] ?? null,
'options' => $data['options'] ?? null,
'properties' => $data['properties'] ?? null,
'category' => $data['category'] ?? null,
'description' => $data['description'] ?? null,
'is_common' => $data['is_common'] ?? false,
'created_by' => $userId,
]);
@@ -75,7 +77,6 @@ public function clone(int $id): ItemField
$cloned = ItemField::create([
'tenant_id' => $tenantId,
'group_id' => $original->group_id,
'section_id' => null,
'field_name' => $original->field_name.' (복사본)',
'field_type' => $original->field_type,
'order_no' => 0,
@@ -86,6 +87,9 @@ public function clone(int $id): ItemField
'validation_rules' => $original->validation_rules,
'options' => $original->options,
'properties' => $original->properties,
'category' => $original->category,
'description' => $original->description,
'is_common' => $original->is_common,
'created_by' => $userId,
]);
@@ -109,48 +113,36 @@ public function getUsage(int $id): array
throw new NotFoundHttpException(__('error.field_not_found'));
}
// 1. 기존 FK 기반 연결 (section_id)
$directSection = $field->section;
// 2. entity_relationships 기반 연결 - 섹션
$linkedSectionIds = EntityRelationship::where('child_type', EntityRelationship::TYPE_FIELD)
->where('child_id', $id)
->where('parent_type', EntityRelationship::TYPE_SECTION)
->pluck('parent_id')
->toArray();
// 3. entity_relationships 기반 연결 - 페이지 (직접 연결)
$linkedPageIds = EntityRelationship::where('child_type', EntityRelationship::TYPE_FIELD)
->where('child_id', $id)
->where('parent_type', EntityRelationship::TYPE_PAGE)
->pluck('parent_id')
->toArray();
// entity_relationships 기반 연결
$linkedSections = $field->linkedSections()->get();
$linkedPages = $field->linkedPages()->get();
return [
'field_id' => $id,
'direct_section' => $directSection,
'linked_sections' => $field->linkedSections()->get(),
'linked_pages' => $field->linkedPages()->get(),
'total_usage_count' => ($directSection ? 1 : 0) + count($linkedSectionIds) + count($linkedPageIds),
'linked_sections' => $linkedSections,
'linked_pages' => $linkedPages,
'total_usage_count' => $linkedSections->count() + $linkedPages->count(),
];
}
/**
* 필드 생성
* 필드 생성 및 섹션에 연결
*/
public function store(int $sectionId, array $data): ItemField
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
// order_no 자동 계산 (해당 섹션 마지막 필드 + 1)
$maxOrder = ItemField::where('tenant_id', $tenantId)
->where('section_id', $sectionId)
// order_no 자동 계산 (해당 섹션에 연결된 마지막 필드 + 1)
$maxOrder = EntityRelationship::where('parent_type', EntityRelationship::TYPE_SECTION)
->where('parent_id', $sectionId)
->where('child_type', EntityRelationship::TYPE_FIELD)
->max('order_no');
// 필드 생성
$field = ItemField::create([
'tenant_id' => $tenantId,
'section_id' => $sectionId,
'group_id' => $data['group_id'] ?? 1,
'field_name' => $data['field_name'],
'field_type' => $data['field_type'],
'order_no' => ($maxOrder ?? -1) + 1,
@@ -164,6 +156,17 @@ public function store(int $sectionId, array $data): ItemField
'created_by' => $userId,
]);
// 섹션-필드 관계 생성
EntityRelationship::link(
EntityRelationship::TYPE_SECTION,
$sectionId,
EntityRelationship::TYPE_FIELD,
$field->id,
$tenantId,
$data['group_id'] ?? 1,
($maxOrder ?? -1) + 1
);
return $field;
}
@@ -193,6 +196,9 @@ public function update(int $id, array $data): ItemField
'validation_rules' => $data['validation_rules'] ?? $field->validation_rules,
'options' => $data['options'] ?? $field->options,
'properties' => $data['properties'] ?? $field->properties,
'category' => $data['category'] ?? $field->category,
'description' => $data['description'] ?? $field->description,
'is_common' => $data['is_common'] ?? $field->is_common,
'updated_by' => $userId,
]);
@@ -201,6 +207,8 @@ public function update(int $id, array $data): ItemField
/**
* 필드 삭제 (Soft Delete)
*
* 독립 엔티티 아키텍처: 필드 삭제 시 모든 부모 관계도 해제
*/
public function destroy(int $id): void
{
@@ -215,28 +223,35 @@ public function destroy(int $id): void
throw new NotFoundHttpException(__('error.not_found'));
}
// 1. entity_relationships에서 이 필드의 모든 부모 관계 해제
// (section→field, page→field 관계에서 이 필드 제거)
EntityRelationship::where('child_type', EntityRelationship::TYPE_FIELD)
->where('child_id', $id)
->delete();
// 2. 필드 Soft Delete
$field->update(['deleted_by' => $userId]);
$field->delete();
}
/**
* 필드 순서 변경
* 필드 순서 변경 (entity_relationships 기반)
*
* @param array $items [['id' => 1, 'order_no' => 0], ['id' => 2, 'order_no' => 1], ...]
*/
public function reorder(int $sectionId, array $items): void
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
foreach ($items as $item) {
ItemField::where('tenant_id', $tenantId)
->where('section_id', $sectionId)
->where('id', $item['id'])
->update([
'order_no' => $item['order_no'],
'updated_by' => $userId,
]);
// entity_relationships에서 순서 업데이트
EntityRelationship::where('parent_type', EntityRelationship::TYPE_SECTION)
->where('parent_id', $sectionId)
->where('child_type', EntityRelationship::TYPE_FIELD)
->where('child_id', $item['id'])
->update(['order_no' => $item['order_no']]);
// 필드 자체의 order_no도 동기화
ItemField::where('id', $item['id'])
->update(['order_no' => $item['order_no']]);
}
}
}