Files
sam-api/app/Services/ItemMaster/SectionTemplateService.php
hskwon bccfa19791 feat: Item Master 하이브리드 구조 전환 및 독립 API 추가
- CASCADE FK → 독립 엔티티 + entity_relationships 링크 테이블
- 독립 API 10개 추가 (섹션/필드/BOM CRUD, clone, usage)
- SectionTemplate 모델 제거 → ItemSection.is_template 통합
- 페이지-섹션, 섹션-필드, 섹션-BOM 링크/언링크 API 14개 추가
- Swagger 문서 업데이트
2025-11-26 14:09:31 +09:00

116 lines
3.1 KiB
PHP

<?php
namespace App\Services\ItemMaster;
use App\Models\ItemMaster\ItemSection;
use App\Services\Service;
use Illuminate\Database\Eloquent\Collection;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* SectionTemplateService
*
* 섹션 템플릿 관리 서비스
* 내부적으로 ItemSection (is_template=true) 사용
*/
class SectionTemplateService extends Service
{
/**
* 섹션 템플릿 목록
*/
public function index(): Collection
{
$tenantId = $this->tenantId();
return ItemSection::templates()
->where('tenant_id', $tenantId)
->with(['fields', 'bomItems'])
->orderBy('created_at', 'desc')
->get();
}
/**
* 섹션 템플릿 생성
*/
public function store(array $data): ItemSection
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$template = ItemSection::create([
'tenant_id' => $tenantId,
'group_id' => 1,
'page_id' => null,
'title' => $data['title'],
'type' => $data['type'],
'order_no' => 0,
'is_template' => true,
'is_default' => $data['is_default'] ?? false,
'description' => $data['description'] ?? null,
'created_by' => $userId,
]);
return $template->load(['fields', 'bomItems']);
}
/**
* 섹션 템플릿 수정
*/
public function update(int $id, array $data): ItemSection
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$template = ItemSection::templates()
->where('tenant_id', $tenantId)
->where('id', $id)
->first();
if (! $template) {
throw new NotFoundHttpException(__('error.section_not_found'));
}
$template->update([
'title' => $data['title'] ?? $template->title,
'type' => $data['type'] ?? $template->type,
'description' => $data['description'] ?? $template->description,
'is_default' => $data['is_default'] ?? $template->is_default,
'updated_by' => $userId,
]);
return $template->fresh()->load(['fields', 'bomItems']);
}
/**
* 섹션 템플릿 삭제
*/
public function destroy(int $id): void
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$template = ItemSection::templates()
->where('tenant_id', $tenantId)
->where('id', $id)
->first();
if (! $template) {
throw new NotFoundHttpException(__('error.section_not_found'));
}
$template->update(['deleted_by' => $userId]);
$template->delete();
// 하위 필드/BOM도 Soft Delete
foreach ($template->fields as $field) {
$field->update(['deleted_by' => $userId]);
$field->delete();
}
foreach ($template->bomItems as $bomItem) {
$bomItem->update(['deleted_by' => $userId]);
$bomItem->delete();
}
}
}