Files
sam-manage/app/Models/Archives/ArchivedRecord.php

105 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Models\Archives;
use App\Models\Tenants\Tenant;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ArchivedRecord extends Model
{
protected $table = 'archived_records';
protected $fillable = [
'batch_id',
'batch_description',
'record_type',
'tenant_id',
'original_id',
'main_data',
'schema_version',
'deleted_by',
'deleted_at',
'notes',
];
protected $casts = [
'main_data' => 'array',
'deleted_at' => 'datetime',
'original_id' => 'integer',
'tenant_id' => 'integer',
'deleted_by' => 'integer',
];
/**
* 관계: 대상 테넌트
* NOTE: 삭제된 테넌트를 참조할 있어 FK 없음
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class, 'tenant_id');
}
/**
* 관계: 삭제한 사용자
*/
public function deletedByUser(): BelongsTo
{
return $this->belongsTo(User::class, 'deleted_by');
}
/**
* 관계: 연관 테이블 데이터
*/
public function relations(): HasMany
{
return $this->hasMany(ArchivedRecordRelation::class, 'archived_record_id');
}
/**
* Scope: 테넌트 타입만
*/
public function scopeTenant($query)
{
return $query->where('record_type', 'tenant');
}
/**
* Scope: 사용자 타입만
*/
public function scopeUser($query)
{
return $query->where('record_type', 'user');
}
/**
* 레코드 타입 레이블
*/
public function getRecordTypeLabelAttribute(): string
{
return match ($this->record_type) {
'tenant' => '테넌트',
'user' => '사용자',
default => $this->record_type,
};
}
/**
* 메인 데이터 요약
*/
public function getMainDataSummaryAttribute(): string
{
if (empty($this->main_data)) {
return '-';
}
$data = $this->main_data;
$name = $data['name'] ?? $data['title'] ?? '-';
$id = $data['id'] ?? $this->original_id;
return "{$name} (ID: {$id})";
}
}