Files
sam-api/app/Models/SystemFieldDefinition.php

102 lines
2.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* 시스템 필드 정의 모델
*
* 소스 테이블(items, tenants, users) 시스템 필드를 정의합니다.
* 테넌트별 item_fields 시딩 참조하는 마스터 데이터입니다.
*/
class SystemFieldDefinition extends Model
{
protected $fillable = [
'source_table',
'source_table_label',
'field_key',
'field_name',
'field_type',
'order_no',
'is_required',
'is_seed_default',
'default_value',
'options',
'is_active',
];
protected $casts = [
'order_no' => 'integer',
'is_required' => 'boolean',
'is_seed_default' => 'boolean',
'is_active' => 'boolean',
'options' => 'array',
];
/**
* 소스 테이블 목록 조회
*/
public static function getSourceTables(): Collection
{
return self::query()
->select('source_table', 'source_table_label')
->where('is_active', true)
->groupBy('source_table', 'source_table_label')
->orderBy('source_table')
->get();
}
/**
* 특정 소스 테이블의 필드 목록 조회
*/
public static function getFieldsFor(string $sourceTable, bool $onlyActive = true): Collection
{
$query = self::query()
->where('source_table', $sourceTable)
->orderBy('order_no');
if ($onlyActive) {
$query->where('is_active', true);
}
return $query->get();
}
/**
* 특정 소스 테이블의 기본 시딩 대상 필드 목록 조회
*/
public static function getSeedDefaultFieldsFor(string $sourceTable): Collection
{
return self::query()
->where('source_table', $sourceTable)
->where('is_active', true)
->where('is_seed_default', true)
->orderBy('order_no')
->get();
}
/**
* 특정 소스 테이블의 필드 조회
*/
public static function getFieldCountFor(string $sourceTable): int
{
return self::query()
->where('source_table', $sourceTable)
->where('is_active', true)
->count();
}
/**
* 모든 소스 테이블의 시스템 필드 목록
*/
public static function getAllSystemFieldKeys(string $sourceTable): array
{
return self::query()
->where('source_table', $sourceTable)
->pluck('field_key')
->toArray();
}
}