- 마이그레이션: is_active 컬럼 추가 (기본값 true) - ItemField 모델: fillable, casts에 is_active 추가 - ItemFieldService: store, storeIndependent, clone, update 메서드에 is_active 처리 - FormRequest: is_active 유효성 검사 규칙 추가 - API Flow 테스트 시나리오 추가 (docs/api-flows/) - docs/INDEX.md에 api-flows 섹션 추가 ModelTrait::scopeActive() 메서드 사용을 위한 필수 컬럼
319 lines
11 KiB
PHP
319 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ItemMaster;
|
|
|
|
use App\Models\ItemMaster\EntityRelationship;
|
|
use App\Models\ItemMaster\ItemField;
|
|
use App\Services\Service;
|
|
use App\Traits\LockCheckTrait;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class ItemFieldService extends Service
|
|
{
|
|
use LockCheckTrait;
|
|
|
|
/**
|
|
* 모든 필드 목록 조회
|
|
*
|
|
* 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();
|
|
|
|
// 1. 필드 생성 (field_key 없이)
|
|
$field = ItemField::create([
|
|
'tenant_id' => $tenantId,
|
|
'group_id' => $data['group_id'] ?? 1,
|
|
'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,
|
|
'category' => $data['category'] ?? null,
|
|
'description' => $data['description'] ?? null,
|
|
'is_common' => $data['is_common'] ?? false,
|
|
'is_active' => $data['is_active'] ?? true,
|
|
'is_locked' => $data['is_locked'] ?? false,
|
|
'locked_by' => ($data['is_locked'] ?? false) ? $userId : null,
|
|
'locked_at' => ($data['is_locked'] ?? false) ? now() : null,
|
|
'created_by' => $userId,
|
|
]);
|
|
|
|
// 2. field_key 설정 ({ID}_{field_key} 형식)
|
|
if (! empty($data['field_key'])) {
|
|
$field->update(['field_key' => $field->id.'_'.$data['field_key']]);
|
|
}
|
|
|
|
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'));
|
|
}
|
|
|
|
// 1. 복제 생성 (field_key, is_locked 없이)
|
|
$cloned = ItemField::create([
|
|
'tenant_id' => $tenantId,
|
|
'group_id' => $original->group_id,
|
|
'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,
|
|
'category' => $original->category,
|
|
'description' => $original->description,
|
|
'is_common' => $original->is_common,
|
|
'is_active' => $original->is_active, // 원본의 활성화 상태 복제
|
|
'is_locked' => false, // 복제본은 잠금 해제 상태
|
|
'created_by' => $userId,
|
|
]);
|
|
|
|
// 2. field_key 복제 ({새ID}_{원본key} 형식)
|
|
if ($original->field_key) {
|
|
// 원본 field_key에서 키 부분 추출 (예: "81_itemNum" → "itemNum")
|
|
$originalKey = preg_replace('/^\d+_/', '', $original->field_key);
|
|
$cloned->update(['field_key' => $cloned->id.'_'.$originalKey.'_copy']);
|
|
}
|
|
|
|
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'));
|
|
}
|
|
|
|
// entity_relationships 기반 연결
|
|
$linkedSections = $field->linkedSections()->get();
|
|
$linkedPages = $field->linkedPages()->get();
|
|
|
|
return [
|
|
'field_id' => $id,
|
|
'linked_sections' => $linkedSections,
|
|
'linked_pages' => $linkedPages,
|
|
'total_usage_count' => $linkedSections->count() + $linkedPages->count(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 필드 생성 및 섹션에 연결
|
|
*/
|
|
public function store(int $sectionId, array $data): ItemField
|
|
{
|
|
$tenantId = $this->tenantId();
|
|
$userId = $this->apiUserId();
|
|
|
|
// order_no 자동 계산 (해당 섹션에 연결된 마지막 필드 + 1)
|
|
$maxOrder = EntityRelationship::where('parent_type', EntityRelationship::TYPE_SECTION)
|
|
->where('parent_id', $sectionId)
|
|
->where('child_type', EntityRelationship::TYPE_FIELD)
|
|
->max('order_no');
|
|
|
|
// 1. 필드 생성
|
|
$field = ItemField::create([
|
|
'tenant_id' => $tenantId,
|
|
'group_id' => $data['group_id'] ?? 1,
|
|
'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,
|
|
'is_active' => $data['is_active'] ?? true,
|
|
'is_locked' => $data['is_locked'] ?? false,
|
|
'locked_by' => ($data['is_locked'] ?? false) ? $userId : null,
|
|
'locked_at' => ($data['is_locked'] ?? false) ? now() : null,
|
|
'created_by' => $userId,
|
|
]);
|
|
|
|
// 2. field_key 설정 ({ID}_{field_key} 형식)
|
|
if (! empty($data['field_key'])) {
|
|
$field->update(['field_key' => $field->id.'_'.$data['field_key']]);
|
|
}
|
|
|
|
// 3. 섹션-필드 관계 생성
|
|
EntityRelationship::link(
|
|
$tenantId,
|
|
EntityRelationship::TYPE_SECTION,
|
|
$sectionId,
|
|
EntityRelationship::TYPE_FIELD,
|
|
$field->id,
|
|
($maxOrder ?? -1) + 1,
|
|
null,
|
|
$data['group_id'] ?? 1
|
|
);
|
|
|
|
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'));
|
|
}
|
|
|
|
// 기본 필드 업데이트
|
|
$updateData = [
|
|
'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,
|
|
'category' => $data['category'] ?? $field->category,
|
|
'description' => $data['description'] ?? $field->description,
|
|
'is_common' => $data['is_common'] ?? $field->is_common,
|
|
'is_active' => $data['is_active'] ?? $field->is_active,
|
|
'updated_by' => $userId,
|
|
];
|
|
|
|
// field_key 변경 시 {ID}_{field_key} 형식 유지
|
|
if (array_key_exists('field_key', $data)) {
|
|
$updateData['field_key'] = $data['field_key'] ? $field->id.'_'.$data['field_key'] : null;
|
|
}
|
|
|
|
// is_locked 변경 처리
|
|
if (array_key_exists('is_locked', $data)) {
|
|
$updateData['is_locked'] = $data['is_locked'];
|
|
if ($data['is_locked'] && ! $field->is_locked) {
|
|
// 새로 잠금 설정
|
|
$updateData['locked_by'] = $userId;
|
|
$updateData['locked_at'] = now();
|
|
} elseif (! $data['is_locked'] && $field->is_locked) {
|
|
// 잠금 해제
|
|
$updateData['locked_by'] = null;
|
|
$updateData['locked_at'] = null;
|
|
}
|
|
}
|
|
|
|
$field->update($updateData);
|
|
|
|
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'));
|
|
}
|
|
|
|
// 잠금 체크: 이 필드가 잠금된 연결로 보호되고 있는지 확인
|
|
$this->checkCanDelete(EntityRelationship::TYPE_FIELD, $id);
|
|
|
|
// 1. entity_relationships에서 이 필드의 모든 부모 관계 해제
|
|
// (section→field, page→field 관계에서 이 필드 제거)
|
|
// 주의: 잠금된 연결이 있으면 위에서 예외 발생
|
|
EntityRelationship::where('child_type', EntityRelationship::TYPE_FIELD)
|
|
->where('child_id', $id)
|
|
->where('is_locked', false)
|
|
->delete();
|
|
|
|
// 2. 필드 Soft Delete
|
|
$field->update(['deleted_by' => $userId]);
|
|
$field->delete();
|
|
}
|
|
|
|
/**
|
|
* 필드 순서 변경 (entity_relationships 기반)
|
|
*
|
|
* @param array $items [['id' => 1, 'order_no' => 0], ['id' => 2, 'order_no' => 1], ...]
|
|
*/
|
|
public function reorder(int $sectionId, array $items): void
|
|
{
|
|
foreach ($items as $item) {
|
|
// entity_relationships에서 순서 업데이트
|
|
EntityRelationship::where('parent_type', EntityRelationship::TYPE_SECTION)
|
|
->where('parent_id', $sectionId)
|
|
->where('child_type', EntityRelationship::TYPE_FIELD)
|
|
->where('child_id', $item['id'])
|
|
->update(['order_no' => $item['order_no']]);
|
|
|
|
// 필드 자체의 order_no도 동기화
|
|
ItemField::where('id', $item['id'])
|
|
->update(['order_no' => $item['order_no']]);
|
|
}
|
|
}
|
|
}
|