Files
sam-api/app/Models/Tenants/TenantStatField.php

110 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Models\Tenants;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
/**
* 테넌트별 통계 필드 설정 모델
*
* 테넌트가 통계를 원하는 필드를 선언하는 메타 정보 테이블
*
* 사용 용도:
* - 통계 시스템에서 어떤 필드의 통계를 계산할지 결정
* - aggregation_types에 따라 평균, 합계, 최소, 최대 집계
* - is_critical 플래그로 중요 통계 식별
*
* @property int $id
* @property int $tenant_id
* @property string $target_table
* @property string $field_key
* @property string $field_name
* @property string $field_type
* @property array|null $aggregation_types
* @property bool $is_critical
* @property int $display_order
* @property string|null $description
* @property int|null $created_by
* @property int|null $updated_by
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*/
class TenantStatField extends Model
{
use BelongsToTenant, ModelTrait;
protected $table = 'tenant_stat_fields';
protected $fillable = [
'tenant_id',
'target_table',
'field_key',
'field_name',
'field_type',
'aggregation_types',
'is_critical',
'display_order',
'description',
'created_by',
'updated_by',
];
protected $casts = [
'aggregation_types' => 'array',
'is_critical' => 'boolean',
'display_order' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
// ============================================
// Relationships
// ============================================
/**
* 소속 테넌트
*/
public function tenant()
{
return $this->belongsTo(Tenant::class, 'tenant_id');
}
// ============================================
// Query Scopes
// ============================================
/**
* 특정 테이블의 통계 필드만
*/
public function scopeForTable($query, string $targetTable)
{
return $query->where('target_table', $targetTable);
}
/**
* 중요 필드만 (일일 통계 대상)
*/
public function scopeCritical($query)
{
return $query->where('is_critical', true);
}
/**
* 특정 집계 함수를 포함하는 필드
*/
public function scopeWithAggregation($query, string $aggregationType)
{
return $query->whereJsonContains('aggregation_types', $aggregationType);
}
/**
* 표시 순서대로 정렬
*/
public function scopeOrdered($query)
{
return $query->orderBy('display_order')->orderBy('field_name');
}
}