feat: Item Master 하이브리드 구조 전환 및 독립 API 추가

- CASCADE FK → 독립 엔티티 + entity_relationships 링크 테이블
- 독립 API 10개 추가 (섹션/필드/BOM CRUD, clone, usage)
- SectionTemplate 모델 제거 → ItemSection.is_template 통합
- 페이지-섹션, 섹션-필드, 섹션-BOM 링크/언링크 API 14개 추가
- Swagger 문서 업데이트
This commit is contained in:
2025-11-26 14:09:31 +09:00
parent 3fefb8ce26
commit bccfa19791
38 changed files with 5888 additions and 92 deletions

View File

@@ -2,12 +2,139 @@
namespace App\Services\ItemMaster;
use App\Models\ItemMaster\EntityRelationship;
use App\Models\ItemMaster\ItemField;
use App\Services\Service;
use Illuminate\Database\Eloquent\Collection;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ItemFieldService extends Service
{
/**
* 독립 필드 목록 조회
*
* GET /api/v1/item-master/fields
*/
public function index(): Collection
{
$tenantId = $this->tenantId();
return ItemField::where('tenant_id', $tenantId)
->orderBy('created_at', 'desc')
->get();
}
/**
* 독립 필드 생성 (섹션 연결 없음)
*
* POST /api/v1/item-master/fields
*/
public function storeIndependent(array $data): ItemField
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$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,
'is_required' => $data['is_required'] ?? false,
'default_value' => $data['default_value'] ?? null,
'placeholder' => $data['placeholder'] ?? null,
'display_condition' => $data['display_condition'] ?? null,
'validation_rules' => $data['validation_rules'] ?? null,
'options' => $data['options'] ?? null,
'properties' => $data['properties'] ?? null,
'created_by' => $userId,
]);
return $field;
}
/**
* 필드 복제
*
* POST /api/v1/item-master/fields/{id}/clone
*/
public function clone(int $id): ItemField
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$original = ItemField::where('tenant_id', $tenantId)
->where('id', $id)
->first();
if (! $original) {
throw new NotFoundHttpException(__('error.field_not_found'));
}
$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,
'is_required' => $original->is_required,
'default_value' => $original->default_value,
'placeholder' => $original->placeholder,
'display_condition' => $original->display_condition,
'validation_rules' => $original->validation_rules,
'options' => $original->options,
'properties' => $original->properties,
'created_by' => $userId,
]);
return $cloned;
}
/**
* 필드 사용처 조회 (어떤 섹션/페이지에 연결되어 있는지)
*
* GET /api/v1/item-master/fields/{id}/usage
*/
public function getUsage(int $id): array
{
$tenantId = $this->tenantId();
$field = ItemField::where('tenant_id', $tenantId)
->where('id', $id)
->first();
if (! $field) {
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();
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),
];
}
/**
* 필드 생성
*/