Files
sam-api/app/Services/ItemMaster/ItemFieldService.php

116 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\ItemMaster;
use App\Models\ItemMaster\ItemField;
use App\Services\Service;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ItemFieldService extends Service
{
/**
* 필드 생성
*/
public function store(int $sectionId, array $data): ItemField
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
// order_no 자동 계산 (해당 섹션의 마지막 필드 + 1)
$maxOrder = ItemField::where('tenant_id', $tenantId)
->where('section_id', $sectionId)
->max('order_no');
$field = ItemField::create([
'tenant_id' => $tenantId,
'section_id' => $sectionId,
'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,
'created_by' => $userId,
]);
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'));
}
$field->update([
'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,
'updated_by' => $userId,
]);
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'));
}
$field->update(['deleted_by' => $userId]);
$field->delete();
}
/**
* 필드 순서 변경
*
* @param array $items [['id' => 1, 'order_no' => 0], ['id' => 2, 'order_no' => 1], ...]
*/
public function reorder(int $sectionId, array $items): void
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
foreach ($items as $item) {
ItemField::where('tenant_id', $tenantId)
->where('section_id', $sectionId)
->where('id', $item['id'])
->update([
'order_no' => $item['order_no'],
'updated_by' => $userId,
]);
}
}
}