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

141 lines
3.9 KiB
PHP
Raw 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',
];
/**
* 소스 테이블 목록 조회 (라벨 포함)
* - 등록된 모든 테이블 포함 (_meta_ 플레이스홀더 포함)
*/
public static function getSourceTables(): Collection
{
return self::query()
->select('source_table', 'source_table_label')
->groupBy('source_table', 'source_table_label')
->orderBy('source_table')
->get();
}
/**
* 소스 테이블 목록 (key => label 형태)
* - 등록된 모든 테이블 포함 (드롭다운용)
*/
public static function getSourceTableOptions(): array
{
return self::getSourceTables()
->pluck('source_table_label', 'source_table')
->toArray();
}
/**
* 활성 필드가 있는 소스 테이블 목록 조회
* - 시딩 상태 조회 등에서 사용
*/
public static function getActiveSourceTables(): 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();
}
/**
* 특정 소스 테이블의 기본 시딩 대상 필드 조회
* - 시딩 상태 비교용 (is_seed_default=true인 필드만 카운트)
*/
public static function getFieldCountFor(string $sourceTable): int
{
return self::query()
->where('source_table', $sourceTable)
->where('is_active', true)
->where('is_seed_default', true)
->count();
}
/**
* 특정 소스 테이블의 전체 필드 조회
* - 제외된 필드 포함 전체 카운트
*/
public static function getTotalFieldCountFor(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();
}
}