- CASCADE FK → 독립 엔티티 + entity_relationships 링크 테이블 - 독립 API 10개 추가 (섹션/필드/BOM CRUD, clone, usage) - SectionTemplate 모델 제거 → ItemSection.is_template 통합 - 페이지-섹션, 섹션-필드, 섹션-BOM 링크/언링크 API 14개 추가 - Swagger 문서 업데이트
243 lines
7.8 KiB
PHP
243 lines
7.8 KiB
PHP
<?php
|
|
|
|
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),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 필드 생성
|
|
*/
|
|
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)
|
|
->max('order_no');
|
|
|
|
$field = ItemField::create([
|
|
'tenant_id' => $tenantId,
|
|
'section_id' => $sectionId,
|
|
'field_name' => $data['field_name'],
|
|
'field_type' => $data['field_type'],
|
|
'order_no' => ($maxOrder ?? -1) + 1,
|
|
'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;
|
|
}
|
|
|
|
/**
|
|
* 필드 수정
|
|
*/
|
|
public function update(int $id, array $data): ItemField
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
$userId = $this->apiUserId();
|
|
|
|
$field = ItemField::where('tenant_id', $tenantId)
|
|
->where('id', $id)
|
|
->first();
|
|
|
|
if (! $field) {
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
}
|
|
|
|
$field->update([
|
|
'field_name' => $data['field_name'] ?? $field->field_name,
|
|
'field_type' => $data['field_type'] ?? $field->field_type,
|
|
'is_required' => $data['is_required'] ?? $field->is_required,
|
|
'default_value' => $data['default_value'] ?? $field->default_value,
|
|
'placeholder' => $data['placeholder'] ?? $field->placeholder,
|
|
'display_condition' => $data['display_condition'] ?? $field->display_condition,
|
|
'validation_rules' => $data['validation_rules'] ?? $field->validation_rules,
|
|
'options' => $data['options'] ?? $field->options,
|
|
'properties' => $data['properties'] ?? $field->properties,
|
|
'updated_by' => $userId,
|
|
]);
|
|
|
|
return $field;
|
|
}
|
|
|
|
/**
|
|
* 필드 삭제 (Soft Delete)
|
|
*/
|
|
public function destroy(int $id): void
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
$userId = $this->apiUserId();
|
|
|
|
$field = ItemField::where('tenant_id', $tenantId)
|
|
->where('id', $id)
|
|
->first();
|
|
|
|
if (! $field) {
|
|
throw new NotFoundHttpException(__('error.not_found'));
|
|
}
|
|
|
|
$field->update(['deleted_by' => $userId]);
|
|
$field->delete();
|
|
}
|
|
|
|
/**
|
|
* 필드 순서 변경
|
|
*
|
|
* @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,
|
|
]);
|
|
}
|
|
}
|
|
}
|