Files
sam-api/app/Services/ItemMaster/SectionTemplateService.php
hskwon efa2a84d2c feat(item-master): 잠금 기능 추가 및 FK 레거시 코드 정리
## 잠금 기능 (Lock Feature)
- entity_relationships 테이블에 is_locked, locked_by, locked_at 컬럼 추가
- EntityRelationship 모델에 잠금 관련 헬퍼 메서드 추가
- LockCheckTrait 생성 (destroy 시 잠금 체크 공통 로직)
- 각 Service의 destroy() 메서드에 잠금 체크 적용
- API 응답에 is_locked 필드 포함
- 한국어 에러 메시지 추가

## FK 레거시 코드 정리
- ItemMasterSeeder: entity_relationships 기반으로 전환
- ItemPage 모델: FK 기반 sections() 관계 제거
- ItemSectionService: clone() 메서드 FK 제거
- SectionTemplateService: page_id 컬럼 참조 제거
- EntityRelationship::link() 파라미터 순서 통일

## 기타
- Swagger 스키마에 is_locked 속성 추가
- 프론트엔드 가이드 문서 추가
2025-11-27 15:51:00 +09:00

158 lines
4.8 KiB
PHP

<?php
namespace App\Services\ItemMaster;
use App\Models\ItemMaster\EntityRelationship;
use App\Models\ItemMaster\ItemPage;
use App\Models\ItemMaster\ItemSection;
use App\Services\Service;
use Illuminate\Database\Eloquent\Collection;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* SectionTemplateService
*
* 섹션 관리 서비스 (참조 방식)
* - 섹션은 독립 엔티티로 생성
* - 페이지와는 entity_relationships로 링크 연결
*/
class SectionTemplateService extends Service
{
/**
* 섹션 목록 (독립 섹션 전체)
*/
public function index(): Collection
{
$tenantId = $this->tenantId();
return ItemSection::where('tenant_id', $tenantId)
->with(['fields', 'bomItems'])
->orderBy('created_at', 'desc')
->get();
}
/**
* 독립 섹션 생성 (page_id가 있으면 링크도 연결)
*
* @param array $data ['page_id'(optional), 'title', 'type', 'description', 'is_default']
*/
public function store(array $data): ItemSection
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$pageId = $data['page_id'] ?? null;
// page_id가 있으면 페이지 존재 확인
if ($pageId) {
$page = ItemPage::where('tenant_id', $tenantId)->find($pageId);
if (! $page) {
throw new NotFoundHttpException(__('error.page_not_found'));
}
}
// 1. 독립 섹션 생성
$section = ItemSection::create([
'tenant_id' => $tenantId,
'group_id' => 1,
'title' => $data['title'],
'type' => $data['type'],
'order_no' => 0,
'is_template' => false,
'is_default' => $data['is_default'] ?? false,
'description' => $data['description'] ?? null,
'created_by' => $userId,
]);
// 2. page_id가 있으면 링크 연결
if ($pageId) {
$maxOrderNo = EntityRelationship::where('tenant_id', $tenantId)
->where('parent_type', EntityRelationship::TYPE_PAGE)
->where('parent_id', $pageId)
->where('child_type', EntityRelationship::TYPE_SECTION)
->max('order_no') ?? -1;
EntityRelationship::link(
$tenantId,
EntityRelationship::TYPE_PAGE,
$pageId,
EntityRelationship::TYPE_SECTION,
$section->id,
$maxOrderNo + 1
);
}
return $section->load(['fields', 'bomItems']);
}
/**
* 섹션 수정
*/
public function update(int $id, array $data): ItemSection
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$section = ItemSection::where('tenant_id', $tenantId)
->where('id', $id)
->first();
if (! $section) {
throw new NotFoundHttpException(__('error.section_not_found'));
}
$section->update([
'title' => $data['title'] ?? $section->title,
'type' => $data['type'] ?? $section->type,
'description' => $data['description'] ?? $section->description,
'is_default' => $data['is_default'] ?? $section->is_default,
'updated_by' => $userId,
]);
return $section->fresh()->load(['fields', 'bomItems']);
}
/**
* 섹션 삭제 (Soft Delete) + 링크 해제
*/
public function destroy(int $id): void
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$section = ItemSection::where('tenant_id', $tenantId)
->where('id', $id)
->first();
if (! $section) {
throw new NotFoundHttpException(__('error.section_not_found'));
}
// 1. 모든 부모 링크 해제 (페이지-섹션 관계)
EntityRelationship::where('tenant_id', $tenantId)
->where('child_type', EntityRelationship::TYPE_SECTION)
->where('child_id', $id)
->delete();
// 2. 모든 자식 링크 해제 (섹션-필드, 섹션-BOM 관계)
EntityRelationship::where('tenant_id', $tenantId)
->where('parent_type', EntityRelationship::TYPE_SECTION)
->where('parent_id', $id)
->delete();
// 3. 섹션 Soft Delete
$section->update(['deleted_by' => $userId]);
$section->delete();
// 4. 하위 필드/BOM도 Soft Delete
foreach ($section->fields as $field) {
$field->update(['deleted_by' => $userId]);
$field->delete();
}
foreach ($section->bomItems as $bomItem) {
$bomItem->update(['deleted_by' => $userId]);
$bomItem->delete();
}
}
}