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,172 @@
namespace App\Services\ItemMaster;
use App\Models\ItemMaster\EntityRelationship;
use App\Models\ItemMaster\ItemBomItem;
use App\Models\ItemMaster\ItemField;
use App\Models\ItemMaster\ItemSection;
use App\Services\Service;
use Illuminate\Database\Eloquent\Collection;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ItemSectionService extends Service
{
/**
* 독립 섹션 목록 조회
*
* GET /api/v1/item-master/sections
*/
public function index(?bool $isTemplate = null): Collection
{
$tenantId = $this->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),
];
}
/**
* 섹션 생성
*/