252 lines
9.3 KiB
PHP
252 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use App\Models\CommonModel;
|
|
|
|
abstract class CommonService
|
|
{
|
|
private $_logService = null;
|
|
protected array $classPaths = [];
|
|
protected $formServiceInstance = null;
|
|
protected function __construct(protected CommonModel $model) {}
|
|
abstract public function getFormService(): mixed;
|
|
final protected function addClassPath(string $className): void
|
|
{
|
|
$this->classPaths[] = $className;
|
|
}
|
|
final public function getClassPath($delimeter = DIRECTORY_SEPARATOR): string
|
|
{
|
|
return implode($delimeter, $this->classPaths);
|
|
}
|
|
final public function getEntity(string|int|array $where, ?string $message = null): mixed
|
|
{
|
|
try {
|
|
$entity = is_array($where) ? $this->model->where($where)->first() : $this->model->find($where);
|
|
if (!$entity) {
|
|
return null;
|
|
}
|
|
if (is_array($entity)) {
|
|
throw new \Exception(__METHOD__ . "에서 결과값 Array 오류발생:\n" . var_export($entity, true));
|
|
}
|
|
return $this->getEntity_process($entity);
|
|
} catch (\Exception $e) {
|
|
$message = sprintf(
|
|
"\n------%s SQL오류-----<BR>\n%s\n%s\n------------------------------\n",
|
|
__FUNCTION__,
|
|
$this->model->getLastQuery(),
|
|
$e->getMessage()
|
|
);
|
|
throw new \Exception($message);
|
|
}
|
|
}
|
|
final public function getEntities(mixed $where = null, array $columns = ['*']): array
|
|
{
|
|
try {
|
|
return $this->getEntities_process($where, $columns);
|
|
} catch (\Exception $e) {
|
|
$message = sprintf(
|
|
"\n------%s SQL오류-----<BR>\n%s\n%s\n------------------------------\n",
|
|
__FUNCTION__,
|
|
$this->model->getLastQuery(),
|
|
$e->getMessage()
|
|
);
|
|
throw new \Exception($message);
|
|
}
|
|
} //
|
|
final public function getLatestPK(): int
|
|
{
|
|
$row = $this->model->selectMax($this->model->getPKField())->get()->getRow();
|
|
return isset($row->uid) ? ((int)$row->uid + 1) : 1;
|
|
}
|
|
//필수함수
|
|
//Form관련
|
|
public function getFormFields(): array
|
|
{
|
|
return $this->getFormService()->getFormFields();
|
|
}
|
|
final public function getFormRules(string $action): array
|
|
{
|
|
return $this->getFormService()->getFormRules($action, $this->getFormFields());
|
|
}
|
|
public function getFormFilters(): array
|
|
{
|
|
return $this->getFormService()->getFormFilters();
|
|
}
|
|
public function getFormOptions(string $action): array
|
|
{
|
|
return $this->getFormService()->getFormOptions(
|
|
$this->getClassPath(),
|
|
$action,
|
|
$this->getFormFilters()
|
|
);
|
|
}
|
|
public function getViewFields(): array
|
|
{
|
|
return $this->getFormService()->getViewFields();
|
|
}
|
|
public function getViewFilters(): array
|
|
{
|
|
return $this->getFormService()->getViewFields();
|
|
}
|
|
public function getIndexFields(): array
|
|
{
|
|
return $this->getFormService()->getIndexFields();
|
|
}
|
|
public function getIndexFilters(): array
|
|
{
|
|
return $this->getFormService()->getIndexFilters();
|
|
}
|
|
public function getBatchjobFields(): array
|
|
{
|
|
return $this->getFormService()->getBatchjobFields();
|
|
}
|
|
//Entity관련
|
|
protected function getEntity_process(mixed $entity): mixed
|
|
{
|
|
return $entity;
|
|
}
|
|
//entities를 가져오는 조건
|
|
protected function getEntities_process(mixed $where = null, array $columns = ['*']): array
|
|
{
|
|
if ($where) {
|
|
$this->model->where($where);
|
|
}
|
|
//출력순서 정의
|
|
$this->setOrderBy();
|
|
$entities = [];
|
|
foreach ($this->model->select(implode(',', $columns))->findAll() as $entity) {
|
|
$entities[$entity->getPK()] = $this->getEntity_process($entity);
|
|
}
|
|
return $entities;
|
|
}
|
|
//Action관련
|
|
//Index용
|
|
final public function getTotalCount(): int
|
|
{
|
|
return $this->model->countAllResults(false);
|
|
}
|
|
//Limit처리
|
|
final public function setLimit(int $per_page): void
|
|
{
|
|
$this->model->limit($per_page);
|
|
}
|
|
//Offset처리
|
|
final public function setOffset(int $offset): void
|
|
{
|
|
$this->model->offset($offset);
|
|
}
|
|
public function index_condition_filterField(string $field, mixed $filter_value): void
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$this->model->where("{$this->model->getTable()}.{$field}", $filter_value);
|
|
break;
|
|
}
|
|
}
|
|
//검색어조건절처리
|
|
public function index_condition_filterWord(string $word): void
|
|
{
|
|
$this->model->orLike($this->model->getTable() . "." . $this->model->getTitleField(), $word, 'both');
|
|
}
|
|
//날자검색
|
|
public function index_condition_filterDate(string $start, string $end): void
|
|
{
|
|
$this->model->where(sprintf("%s.created_at >= '%s 00:00:00'", $this->model->getTable(), $start));
|
|
$this->model->where(sprintf("%s.created_at <= '%s 23:59:59'", $this->model->getTable(), $end));
|
|
}
|
|
//OrderBy 처리
|
|
public function setOrderBy(mixed $field = null, mixed $value = null): void
|
|
{
|
|
if ($field !== null && $value !== null) {
|
|
$this->model->orderBy(sprintf("%s.%s %s", $this->model->getTable(), $field, $value));
|
|
}
|
|
}
|
|
|
|
|
|
// //단일작업
|
|
// protected function toggle_process(mixed $entity, array $formDatas): mixed
|
|
// {
|
|
// return $this->model->modify($entity, $formDatas);
|
|
// }
|
|
// public function toggle(mixed $entity, array $formDatas): mixed
|
|
// {
|
|
// $db = \Config\Database::connect();
|
|
// try {
|
|
// //트랜잭션 도중 DB 오류가 발생하면 DatabaseException을 던지도록 설정
|
|
// $db->transException(true)->transStart();
|
|
// $entity = $this->toggle_process($entity, $formDatas);
|
|
// $db->transComplete();
|
|
// return $entity;
|
|
// } catch (DatabaseException $e) { //DB 오류시 발생
|
|
// throw new RuntimeException(sprintf(
|
|
// "\n----[%s]에서 트랜잭션 실패: DB 오류----\n%s\n%s\n------------------------------\n",
|
|
// __METHOD__,
|
|
// $this->model->getLastQuery(),
|
|
// $e->getMessage()
|
|
// ), $e->getCode(), $e);
|
|
// } catch (\Throwable $e) { // 그 외 다른 종류의 예외 처리
|
|
// $db->transRollback(); // 예외 발생 시 수동으로 롤백
|
|
// throw new RuntimeException($e->getMessage(), 0, $e);
|
|
// }
|
|
// }
|
|
// //일괄처리작업
|
|
// protected function batchjob_process(mixed $entity, array $formDatas): mixed
|
|
// {
|
|
// return $this->model->modify($entity, $formDatas);
|
|
// }
|
|
// public function batchjob(mixed $entity, array $formDatas): mixed
|
|
// {
|
|
// $db = \Config\Database::connect();
|
|
// try {
|
|
// //트랜잭션 도중 DB 오류가 발생하면 DatabaseException을 던지도록 설정
|
|
// $db->transException(true)->transStart();
|
|
// $entity = $this->batchjob_process($entity, $formDatas);
|
|
// $db->transComplete();
|
|
// return $entity;
|
|
// } catch (DatabaseException $e) { //DB 오류시 발생
|
|
// throw new RuntimeException(sprintf(
|
|
// "\n----[%s]에서 트랜잭션 실패: DB 오류----\n%s\n%s\n------------------------------\n",
|
|
// __METHOD__,
|
|
// $this->model->getLastQuery(),
|
|
// $e->getMessage()
|
|
// ), $e->getCode(), $e);
|
|
// } catch (\Throwable $e) { // 그 외 다른 종류의 예외 처리
|
|
// $db->transRollback(); // 예외 발생 시 수동으로 롤백
|
|
// throw new RuntimeException($e->getMessage(), 0, $e);
|
|
// }
|
|
// }
|
|
// //삭제
|
|
// protected function delete_process(string $uid): void
|
|
// {
|
|
// if (!$this->model->delete($uid)) {
|
|
// // delete() 메서드 실패 시 모델의 errors()를 통해 상세 정보 확인
|
|
// $errors = $this->model->errors();
|
|
// throw new RuntimeException("모델 삭제 실패: " . var_export($errors, true));
|
|
// }
|
|
// }
|
|
// public function delete(mixed $entity): mixed
|
|
// {
|
|
// $db = \Config\Database::connect();
|
|
// $db->transStart();
|
|
// try {
|
|
// //트랜잭션 도중 DB 오류가 발생하면 DatabaseException을 던지도록 설정
|
|
// $db->transException(true)->transStart();
|
|
// $this->delete_process($entity->getPK());
|
|
// $db->transComplete();
|
|
// return $entity;
|
|
// } catch (DatabaseException $e) { //DB 오류시 발생
|
|
// throw new RuntimeException(sprintf(
|
|
// "\n----[%s]에서 트랜잭션 실패: DB 오류----\n%s\n%s\n------------------------------\n",
|
|
// __METHOD__,
|
|
// $this->model->getLastQuery(),
|
|
// $e->getMessage()
|
|
// ), $e->getCode(), $e);
|
|
// } catch (\Throwable $e) { // 그 외 다른 종류의 예외 처리
|
|
// $db->transRollback(); // 예외 발생 시 수동으로 롤백
|
|
// throw new RuntimeException($e->getMessage(), 0, $e);
|
|
// }
|
|
// }
|
|
}
|