77 lines
1.8 KiB
PHP
77 lines
1.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models\Qualitys;
|
||
|
|
|
||
|
|
use App\Models\Commons\File;
|
||
|
|
use App\Traits\Auditable;
|
||
|
|
use App\Traits\BelongsToTenant;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||
|
|
|
||
|
|
class ChecklistTemplate extends Model
|
||
|
|
{
|
||
|
|
use Auditable, BelongsToTenant, SoftDeletes;
|
||
|
|
|
||
|
|
protected $table = 'checklist_templates';
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'tenant_id',
|
||
|
|
'name',
|
||
|
|
'type',
|
||
|
|
'categories',
|
||
|
|
'options',
|
||
|
|
'created_by',
|
||
|
|
'updated_by',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'categories' => 'array',
|
||
|
|
'options' => 'array',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function creator(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(\App\Models\User::class, 'created_by');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function updater(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(\App\Models\User::class, 'updated_by');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 점검항목별 연결 파일 (files 테이블 polymorphic)
|
||
|
|
* document_type = 'checklist_template', document_id = this.id
|
||
|
|
* field_key = sub_item_id (e.g. 'cat-1-1')
|
||
|
|
*/
|
||
|
|
public function documents(): MorphMany
|
||
|
|
{
|
||
|
|
return $this->morphMany(File::class, 'document', 'document_type', 'document_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 특정 항목의 파일 조회
|
||
|
|
*/
|
||
|
|
public function documentsForItem(string $subItemId)
|
||
|
|
{
|
||
|
|
return $this->documents()->where('field_key', $subItemId);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* categories JSON에서 모든 sub_item_id 추출
|
||
|
|
*/
|
||
|
|
public function getAllSubItemIds(): array
|
||
|
|
{
|
||
|
|
$ids = [];
|
||
|
|
foreach ($this->categories ?? [] as $category) {
|
||
|
|
foreach ($category['subItems'] ?? [] as $subItem) {
|
||
|
|
$ids[] = $subItem['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $ids;
|
||
|
|
}
|
||
|
|
}
|