feat: improve admin master data management
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
@@ -12,6 +14,8 @@ class CodeDetailModel extends Model
|
||||
protected $useTimestamps = false;
|
||||
protected $allowedFields = [
|
||||
'cd_ck_idx',
|
||||
'cd_source',
|
||||
'cd_lg_idx',
|
||||
'cd_code',
|
||||
'cd_name',
|
||||
'cd_sort',
|
||||
@@ -20,14 +24,50 @@ class CodeDetailModel extends Model
|
||||
];
|
||||
|
||||
/**
|
||||
* 특정 코드 종류의 세부코드 목록
|
||||
* 목록 조회: 플랫폼(0) + (선택) 해당 지자체 행
|
||||
*
|
||||
* @param int|null $effectiveLgIdx null 또는 1 미만이면 플랫폼 공통만
|
||||
*/
|
||||
public function getByKind(int $ckIdx, bool $activeOnly = false): array
|
||||
public function filterByTenantScope(?int $effectiveLgIdx): self
|
||||
{
|
||||
$builder = $this->where('cd_ck_idx', $ckIdx);
|
||||
if ($activeOnly) {
|
||||
$builder->where('cd_state', 1);
|
||||
if ($effectiveLgIdx === null || $effectiveLgIdx < 1) {
|
||||
return $this->where('cd_lg_idx', 0);
|
||||
}
|
||||
return $builder->orderBy('cd_sort', 'ASC')->findAll();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
@@ -11,7 +13,34 @@ class SalesAgencyModel extends Model
|
||||
protected $returnType = 'object';
|
||||
protected $useTimestamps = false;
|
||||
protected $allowedFields = [
|
||||
'sa_lg_idx', 'sa_name', 'sa_biz_no', 'sa_rep_name',
|
||||
'sa_tel', 'sa_addr', 'sa_state', 'sa_regdate',
|
||||
'sa_lg_idx',
|
||||
'sa_kind',
|
||||
'sa_code',
|
||||
'sa_name',
|
||||
'sa_regdate',
|
||||
];
|
||||
|
||||
/** sales_agency 테이블에 sa_kind, sa_code 컬럼이 있는지(마이그레이션 적용 여부). */
|
||||
public function hasKindCodeColumns(): bool
|
||||
{
|
||||
static $cache = null;
|
||||
if ($cache === null) {
|
||||
$cols = db_connect()->getFieldNames($this->table);
|
||||
$cache = in_array('sa_kind', $cols, true) && in_array('sa_code', $cols, true);
|
||||
}
|
||||
|
||||
return $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 신규 스키마면 구분·코드 순, 아니면 명·PK 순(옛 DB 호환).
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function orderForDisplay()
|
||||
{
|
||||
return $this->hasKindCodeColumns()
|
||||
? $this->orderBy('sa_kind', 'ASC')->orderBy('sa_code', 'ASC')
|
||||
: $this->orderBy('sa_name', 'ASC')->orderBy('sa_idx', 'ASC');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user