feat: TenantStatField 모델 및 하이브리드 구조 사용 가이드 추가

[추가된 파일]
- app/Models/Tenants/TenantStatField.php
  - 테넌트별 통계 필드 설정 모델
  - Scopes: forTable, critical, withAggregation, ordered
  - 통계 시스템의 메타 정보 제공

- claudedocs/mes/HYBRID_STRUCTURE_GUIDE.md
  - 하이브리드 구조 사용 가이드 문서
  - 데이터베이스 구조 설명
  - 코드 예제 (Product, ProductComponent 생성)
  - 통계 쿼리 예제
  - 성능 고려사항 및 주의사항

[모델 기능]
- BelongsToTenant, ModelTrait 적용
- aggregation_types JSON 자동 캐스팅
- tenant, target_table, field_key 조합으로 통계 필드 관리

[문서 내용]
- 고정 필드 vs 동적 필드 선택 기준
- attributes JSON 사용법
- 통계 쿼리 예제 (JSON_EXTRACT)
- CategoryField와 연동 방법
- 향후 Virtual Column 최적화 가이드
This commit is contained in:
2025-11-14 11:47:28 +09:00
parent 62d671edcf
commit 4b04d6db87
2 changed files with 520 additions and 0 deletions

View File

@@ -0,0 +1,109 @@
<?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');
}
}