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

@@ -0,0 +1,191 @@
<?php
namespace App\Models\ItemMaster;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
/**
* EntityRelationship - 엔티티 간 관계를 관리하는 링크 테이블 모델
*
* 지원하는 관계 유형:
* - page-section: 페이지와 섹션 연결
* - page-field: 페이지와 필드 직접 연결
* - section-field: 섹션과 필드 연결
* - section-bom: 섹션과 BOM 항목 연결
*/
class EntityRelationship extends Model
{
use BelongsToTenant, ModelTrait;
protected $fillable = [
'tenant_id',
'group_id',
'parent_type',
'parent_id',
'child_type',
'child_id',
'order_no',
'metadata',
'created_by',
'updated_by',
];
protected $casts = [
'group_id' => 'integer',
'parent_id' => 'integer',
'child_id' => 'integer',
'order_no' => 'integer',
'metadata' => 'array',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
// 엔티티 타입 상수
public const TYPE_PAGE = 'page';
public const TYPE_SECTION = 'section';
public const TYPE_FIELD = 'field';
public const TYPE_BOM = 'bom';
// 그룹 ID 상수
public const GROUP_ITEM_MASTER = 1;
/**
* 부모 엔티티 조회 (Polymorphic)
*/
public function parent()
{
return $this->morphTo('parent', 'parent_type', 'parent_id');
}
/**
* 자식 엔티티 조회 (Polymorphic)
*/
public function child()
{
return $this->morphTo('child', 'child_type', 'child_id');
}
/**
* 부모 타입에 따른 모델 클래스 반환
*/
public static function getModelClass(string $type): ?string
{
return match ($type) {
self::TYPE_PAGE => ItemPage::class,
self::TYPE_SECTION => ItemSection::class,
self::TYPE_FIELD => ItemField::class,
self::TYPE_BOM => ItemBomItem::class,
default => null,
};
}
/**
* 특정 부모의 자식 관계 조회
*/
public static function getChildren(string $parentType, int $parentId, ?string $childType = null)
{
$query = self::where('parent_type', $parentType)
->where('parent_id', $parentId)
->orderBy('order_no');
if ($childType) {
$query->where('child_type', $childType);
}
return $query;
}
/**
* 특정 자식의 부모 관계 조회
*/
public static function getParents(string $childType, int $childId, ?string $parentType = null)
{
$query = self::where('child_type', $childType)
->where('child_id', $childId);
if ($parentType) {
$query->where('parent_type', $parentType);
}
return $query;
}
/**
* 관계 생성 또는 업데이트
*/
public static function link(
int $tenantId,
string $parentType,
int $parentId,
string $childType,
int $childId,
int $orderNo = 0,
?array $metadata = null,
int $groupId = self::GROUP_ITEM_MASTER
): self {
return self::updateOrCreate(
[
'tenant_id' => $tenantId,
'group_id' => $groupId,
'parent_type' => $parentType,
'parent_id' => $parentId,
'child_type' => $childType,
'child_id' => $childId,
],
[
'order_no' => $orderNo,
'metadata' => $metadata,
]
);
}
/**
* 관계 해제
*/
public static function unlink(
int $tenantId,
string $parentType,
int $parentId,
string $childType,
int $childId,
int $groupId = self::GROUP_ITEM_MASTER
): bool {
return self::where([
'tenant_id' => $tenantId,
'group_id' => $groupId,
'parent_type' => $parentType,
'parent_id' => $parentId,
'child_type' => $childType,
'child_id' => $childId,
])->delete() > 0;
}
/**
* 특정 부모의 모든 자식 관계 해제
*/
public static function unlinkAllChildren(
int $tenantId,
string $parentType,
int $parentId,
?string $childType = null,
int $groupId = self::GROUP_ITEM_MASTER
): int {
$query = self::where([
'tenant_id' => $tenantId,
'group_id' => $groupId,
'parent_type' => $parentType,
'parent_id' => $parentId,
]);
if ($childType) {
$query->where('child_type', $childType);
}
return $query->delete();
}
}

View File

@@ -13,6 +13,7 @@ class ItemBomItem extends Model
protected $fillable = [
'tenant_id',
'group_id',
'section_id',
'item_code',
'item_name',
@@ -28,6 +29,7 @@ class ItemBomItem extends Model
];
protected $casts = [
'group_id' => 'integer',
'quantity' => 'decimal:4',
'unit_price' => 'decimal:2',
'total_price' => 'decimal:2',
@@ -42,10 +44,33 @@ class ItemBomItem extends Model
];
/**
* 소속 섹션
* 소속 섹션 (기존 FK 기반 - 하위 호환성)
*/
public function section()
{
return $this->belongsTo(ItemSection::class, 'section_id');
}
/**
* 이 BOM 항목이 연결된 섹션들 조회 (링크 테이블 기반)
*/
public function linkedSections()
{
return ItemSection::whereIn('id', function ($query) {
$query->select('parent_id')
->from('entity_relationships')
->where('parent_type', EntityRelationship::TYPE_SECTION)
->where('child_type', EntityRelationship::TYPE_BOM)
->where('child_id', $this->id);
});
}
/**
* 이 BOM 항목의 모든 부모 관계 목록 조회
*/
public function allParentRelationships()
{
return EntityRelationship::where('child_type', EntityRelationship::TYPE_BOM)
->where('child_id', $this->id);
}
}

View File

@@ -13,6 +13,7 @@ class ItemField extends Model
protected $fillable = [
'tenant_id',
'group_id',
'section_id',
'field_name',
'field_type',
@@ -30,6 +31,7 @@ class ItemField extends Model
];
protected $casts = [
'group_id' => 'integer',
'order_no' => 'integer',
'is_required' => 'boolean',
'display_condition' => 'array',
@@ -47,10 +49,47 @@ class ItemField extends Model
];
/**
* 소속 섹션
* 소속 섹션 (기존 FK 기반 - 하위 호환성)
*/
public function section()
{
return $this->belongsTo(ItemSection::class, 'section_id');
}
/**
* 이 필드가 연결된 섹션들 조회 (링크 테이블 기반)
*/
public function linkedSections()
{
return ItemSection::whereIn('id', function ($query) {
$query->select('parent_id')
->from('entity_relationships')
->where('parent_type', EntityRelationship::TYPE_SECTION)
->where('child_type', EntityRelationship::TYPE_FIELD)
->where('child_id', $this->id);
});
}
/**
* 이 필드가 직접 연결된 페이지들 조회 (링크 테이블 기반)
*/
public function linkedPages()
{
return ItemPage::whereIn('id', function ($query) {
$query->select('parent_id')
->from('entity_relationships')
->where('parent_type', EntityRelationship::TYPE_PAGE)
->where('child_type', EntityRelationship::TYPE_FIELD)
->where('child_id', $this->id);
});
}
/**
* 이 필드의 모든 부모 관계 목록 조회
*/
public function allParentRelationships()
{
return EntityRelationship::where('child_type', EntityRelationship::TYPE_FIELD)
->where('child_id', $this->id);
}
}

View File

@@ -13,6 +13,7 @@ class ItemMasterField extends Model
protected $fillable = [
'tenant_id',
'group_id',
'field_name',
'field_type',
'category',
@@ -28,6 +29,7 @@ class ItemMasterField extends Model
];
protected $casts = [
'group_id' => 'integer',
'is_common' => 'boolean',
'options' => 'array',
'validation_rules' => 'array',
@@ -41,4 +43,4 @@ class ItemMasterField extends Model
'deleted_by',
'deleted_at',
];
}
}

View File

@@ -13,6 +13,7 @@ class ItemPage extends Model
protected $fillable = [
'tenant_id',
'group_id',
'page_name',
'item_type',
'absolute_path',
@@ -23,6 +24,7 @@ class ItemPage extends Model
];
protected $casts = [
'group_id' => 'integer',
'is_active' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
@@ -35,10 +37,70 @@ class ItemPage extends Model
];
/**
* 페이지의 섹션 목록
* 페이지의 섹션 목록 (기존 FK 기반 - 하위 호환성)
*/
public function sections()
{
return $this->hasMany(ItemSection::class, 'page_id')->orderBy('order_no');
}
}
/**
* 페이지와 연결된 섹션 관계 목록 (링크 테이블 기반)
*/
public function sectionRelationships()
{
return $this->hasMany(EntityRelationship::class, 'parent_id')
->where('parent_type', EntityRelationship::TYPE_PAGE)
->where('child_type', EntityRelationship::TYPE_SECTION)
->orderBy('order_no');
}
/**
* 페이지와 직접 연결된 필드 관계 목록 (링크 테이블 기반)
*/
public function fieldRelationships()
{
return $this->hasMany(EntityRelationship::class, 'parent_id')
->where('parent_type', EntityRelationship::TYPE_PAGE)
->where('child_type', EntityRelationship::TYPE_FIELD)
->orderBy('order_no');
}
/**
* 페이지에 연결된 섹션들 조회 (링크 테이블 기반)
*/
public function linkedSections()
{
return ItemSection::whereIn('id', function ($query) {
$query->select('child_id')
->from('entity_relationships')
->where('parent_type', EntityRelationship::TYPE_PAGE)
->where('parent_id', $this->id)
->where('child_type', EntityRelationship::TYPE_SECTION);
});
}
/**
* 페이지에 직접 연결된 필드들 조회 (링크 테이블 기반)
*/
public function linkedFields()
{
return ItemField::whereIn('id', function ($query) {
$query->select('child_id')
->from('entity_relationships')
->where('parent_type', EntityRelationship::TYPE_PAGE)
->where('parent_id', $this->id)
->where('child_type', EntityRelationship::TYPE_FIELD);
});
}
/**
* 페이지의 모든 관계 목록 조회 (섹션 + 직접 연결된 필드)
*/
public function allRelationships()
{
return $this->hasMany(EntityRelationship::class, 'parent_id')
->where('parent_type', EntityRelationship::TYPE_PAGE)
->orderBy('order_no');
}
}

View File

@@ -13,29 +13,52 @@ class ItemSection extends Model
protected $fillable = [
'tenant_id',
'group_id',
'page_id',
'title',
'type',
'order_no',
'is_template',
'is_default',
'description',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'group_id' => 'integer',
'order_no' => 'integer',
'is_template' => 'boolean',
'is_default' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
/**
* 템플릿만 조회하는 스코프
*/
public function scopeTemplates($query)
{
return $query->where('is_template', true);
}
/**
* 일반 섹션만 조회하는 스코프 (템플릿 제외)
*/
public function scopeNonTemplates($query)
{
return $query->where('is_template', false);
}
protected $hidden = [
'deleted_by',
'deleted_at',
];
/**
* 소속 페이지
* 소속 페이지 (기존 FK 기반 - 하위 호환성)
*/
public function page()
{
@@ -43,7 +66,7 @@ public function page()
}
/**
* 섹션의 필드 목록
* 섹션의 필드 목록 (기존 FK 기반 - 하위 호환성)
*/
public function fields()
{
@@ -51,10 +74,93 @@ public function fields()
}
/**
* 섹션의 BOM 항목 목록
* 섹션의 BOM 항목 목록 (기존 FK 기반 - 하위 호환성)
*/
public function bomItems()
{
return $this->hasMany(ItemBomItem::class, 'section_id');
}
/**
* 섹션과 연결된 필드 관계 목록 (링크 테이블 기반)
*/
public function fieldRelationships()
{
return $this->hasMany(EntityRelationship::class, 'parent_id')
->where('parent_type', EntityRelationship::TYPE_SECTION)
->where('child_type', EntityRelationship::TYPE_FIELD)
->orderBy('order_no');
}
/**
* 섹션과 연결된 BOM 관계 목록 (링크 테이블 기반)
*/
public function bomRelationships()
{
return $this->hasMany(EntityRelationship::class, 'parent_id')
->where('parent_type', EntityRelationship::TYPE_SECTION)
->where('child_type', EntityRelationship::TYPE_BOM)
->orderBy('order_no');
}
/**
* 섹션에 연결된 필드들 조회 (링크 테이블 기반)
*/
public function linkedFields()
{
return ItemField::whereIn('id', function ($query) {
$query->select('child_id')
->from('entity_relationships')
->where('parent_type', EntityRelationship::TYPE_SECTION)
->where('parent_id', $this->id)
->where('child_type', EntityRelationship::TYPE_FIELD);
});
}
/**
* 섹션에 연결된 BOM 항목들 조회 (링크 테이블 기반)
*/
public function linkedBomItems()
{
return ItemBomItem::whereIn('id', function ($query) {
$query->select('child_id')
->from('entity_relationships')
->where('parent_type', EntityRelationship::TYPE_SECTION)
->where('parent_id', $this->id)
->where('child_type', EntityRelationship::TYPE_BOM);
});
}
/**
* 이 섹션이 연결된 페이지들 조회 (링크 테이블 기반)
*/
public function linkedPages()
{
return ItemPage::whereIn('id', function ($query) {
$query->select('parent_id')
->from('entity_relationships')
->where('parent_type', EntityRelationship::TYPE_PAGE)
->where('child_type', EntityRelationship::TYPE_SECTION)
->where('child_id', $this->id);
});
}
/**
* 섹션의 모든 자식 관계 목록 조회 (필드 + BOM)
*/
public function allChildRelationships()
{
return $this->hasMany(EntityRelationship::class, 'parent_id')
->where('parent_type', EntityRelationship::TYPE_SECTION)
->orderBy('order_no');
}
/**
* 섹션의 모든 부모 관계 목록 조회
*/
public function allParentRelationships()
{
return EntityRelationship::where('child_type', EntityRelationship::TYPE_SECTION)
->where('child_id', $this->id);
}
}

View File

@@ -1,36 +0,0 @@
<?php
namespace App\Models\ItemMaster;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class SectionTemplate extends Model
{
use BelongsToTenant, ModelTrait, SoftDeletes;
protected $fillable = [
'tenant_id',
'title',
'type',
'description',
'is_default',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'is_default' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
protected $hidden = [
'deleted_by',
'deleted_at',
];
}