Files
jongryangje/app/Models/CodeDetailModel.php
2026-04-08 00:19:00 +09:00

74 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use CodeIgniter\Model;
class CodeDetailModel extends Model
{
protected $table = 'code_detail';
protected $primaryKey = 'cd_idx';
protected $returnType = 'object';
protected $useTimestamps = false;
protected $allowedFields = [
'cd_ck_idx',
'cd_source',
'cd_lg_idx',
'cd_code',
'cd_name',
'cd_sort',
'cd_state',
'cd_regdate',
];
/**
* 목록 조회: 플랫폼(0) + (선택) 해당 지자체 행
*
* @param int|null $effectiveLgIdx null 또는 1 미만이면 플랫폼 공통만
*/
public function filterByTenantScope(?int $effectiveLgIdx): self
{
if ($effectiveLgIdx === null || $effectiveLgIdx < 1) {
return $this->where('cd_lg_idx', 0);
}
return $this->groupStart()
->where('cd_lg_idx', 0)
->orWhere('cd_lg_idx', $effectiveLgIdx)
->groupEnd();
}
/**
* 특정 코드 종류의 세부코드 목록
*
* @param int|null $effectiveLgIdx 테넌트 범위 (null=플랫폼만)
*/
public function getByKind(int $ckIdx, bool $activeOnly = false, ?int $effectiveLgIdx = null): array
{
$this->where('cd_ck_idx', $ckIdx);
$this->filterByTenantScope($effectiveLgIdx);
if ($activeOnly) {
$this->where('cd_state', 1);
}
return $this->orderBy('cd_sort', 'ASC')->orderBy('cd_idx', 'ASC')->findAll();
}
/**
* 동일 세부코드값: 지자체 전용이 있으면 우선, 없으면 플랫폼
*/
public function findResolvedByKindAndCode(int $ckIdx, string $code, ?int $effectiveLgIdx): ?object
{
if ($effectiveLgIdx !== null && $effectiveLgIdx > 0) {
$local = $this->where('cd_ck_idx', $ckIdx)->where('cd_code', $code)->where('cd_lg_idx', $effectiveLgIdx)->first();
if ($local !== null) {
return $local;
}
}
return $this->where('cd_ck_idx', $ckIdx)->where('cd_code', $code)->where('cd_lg_idx', 0)->first();
}
}