dbms init...1

This commit is contained in:
최준흠 2025-04-30 19:48:46 +09:00
parent 1f788b9175
commit 7dd2a85b16
12 changed files with 97 additions and 86 deletions

View File

@ -24,7 +24,7 @@ class MyLogController extends AdminController
protected function getServiceClass(): Service protected function getServiceClass(): Service
{ {
if ($this->service === null) { if ($this->service === null) {
$this->service = new Service($this->request); $this->service = new Service();
} }
return $this->service; return $this->service;
} }
@ -67,11 +67,11 @@ class MyLogController extends AdminController
//View관련 //View관련
protected function view_init(string $action, $fields = []): void protected function view_process($uid): mixed
{ {
$fields = [ $fields = [
'fields' => ['user_uid', 'class_name', 'method_name', $this->getService()->getModel()::TITLE, 'created_at', 'status', 'content'], 'fields' => ['user_uid', 'class_name', 'method_name', $this->getService()->getModel()::TITLE, 'created_at', 'status', 'content'],
]; ];
parent::view_init($action, $fields); return parent::view_process($uid);
} }
} }

View File

@ -3,13 +3,15 @@
namespace App\Controllers; namespace App\Controllers;
use App\Controllers\BaseController; use App\Controllers\BaseController;
use App\Services\MyLogService;
use CodeIgniter\HTTP\DownloadResponse; use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Validation; use CodeIgniter\Validation\Validation;
use PDOException;
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Html; use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
@ -265,24 +267,28 @@ abstract class CommonController extends BaseController
} }
} }
//일괄처리작업 //일괄처리작업
protected function batchjob_process(mixed $uid): mixed protected function batchjob_process(string $uids): array
{ {
//데이터 검증 $temps = [];
$this->formDatas = $this->doValidate($this->action, $this->fields); foreach (explode(",", $uids) as $uid) {
//자신정보정의
$this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid);
$entity = $this->getService()->getEntity(); $entity = $this->getService()->getEntity();
if (!$entity) { if (!$entity) {
throw new \Exception(__FUNCTION__ . " => {$uid} 정보를 찾을수 없습니다."); throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
} }
return $this->getService()->modify($entity, $this->formDatas); $temps[] = $entity;
}
$entities = [];
foreach ($temps as $entity) {
$entities[] = $this->getService()->modify($entity, $this->formDatas);
}
return $entities;
} }
final public function batchjob(): RedirectResponse final public function batchjob(): RedirectResponse
{ {
$this->init(__FUNCTION__);
//Transaction Start //Transaction Start
$this->getService()->getModel()->transStart(); $this->getService()->getModel()->transStart();
try { try {
$this->init(__FUNCTION__);
//변경할 UIDS //변경할 UIDS
$uids = $this->request->getVar('batchjob_uids'); $uids = $this->request->getVar('batchjob_uids');
if (!$uids) { if (!$uids) {
@ -295,13 +301,9 @@ abstract class CommonController extends BaseController
$fields[$field] = $field; $fields[$field] = $field;
} }
} }
$this->fields = $fields; //데이터 검증
$entities = []; $this->formDatas = $this->doValidate($this->action, $fields);
foreach (explode(",", $uids) as $uid) { $this->entities = $this->batchjob_process($uids);
$entities[$uid] = $this->batchjob_process($uid);
}
$this->entities = $entities;
$this->getService()->getModel()->transCommit();
return $this->getResultPageByActon($this->action); return $this->getResultPageByActon($this->action);
} catch (\Exception $e) { } catch (\Exception $e) {
//Transaction Rollback //Transaction Rollback
@ -311,21 +313,25 @@ abstract class CommonController extends BaseController
} }
//삭제,일괄삭제 공통사용 //삭제,일괄삭제 공통사용
protected function delete_process(mixed $uid): mixed protected function delete_process(mixed $entity): bool
{ {
//자신정보정의 $result = $this->getService()->delete($entity);
$this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); if (!$result) {
$entity = $this->getService()->getModel()->getEntity(); throw new \Exception("[$entity->getTitle()] 삭제를 실패하였습니다.");
if ($entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
} }
return $this->getService()->delete($entity); return $result;
} }
public function delete(mixed $uid): RedirectResponse|string public function delete(mixed $uid): RedirectResponse|string
{ {
//Transaction Start //Transaction Start
$this->getService()->getModel()->transStart(); $this->getService()->getModel()->transStart();
try { try {
$this->init(__FUNCTION__);
$this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid);
$entity = $this->getService()->getEntity();
if ($entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
$this->entity = $this->delete_process($uid); $this->entity = $this->delete_process($uid);
$this->getService()->getModel()->transCommit(); $this->getService()->getModel()->transCommit();
return $this->getResultPageByActon($this->action); return $this->getResultPageByActon($this->action);
@ -336,26 +342,46 @@ abstract class CommonController extends BaseController
} }
} }
//일괄삭제 //일괄삭제
protected function batchjob_delete_process(string $uids): void
{
$entities = [];
foreach (explode(",", $uids) as $uid) {
$this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid);
$entity = $this->getService()->getEntity();
if (!$entity) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
$entities[] = $entity;
}
foreach ($entities as $entity) {
try {
if ($this->getService()->delete($entity)) {
MyLogService::debug("[$entity->getTitle()]" . MESSAGES["DELETED"]);
}
} catch (PDOException $e) {
MyLogService::debug("[$entity->getTitle()]" . MESSAGES["FAILED"] . ":" . $e->getMessage());
}
}
}
final public function batchjob_delete(): RedirectResponse|string final public function batchjob_delete(): RedirectResponse|string
{ {
//Transaction Start //Transaction Start
$this->getService()->getModel()->transStart(); $this->getService()->getModel()->transStart();
try { try {
$this->init(__FUNCTION__);
//변경할 UIDS //변경할 UIDS
$uids = $this->request->getVar('batchjob_uids'); $uids = $this->request->getVar('batchjob_uids');
if (!$uids) { if (!$uids) {
throw new \Exception("적용할 리스트를 선택하셔야합니다."); throw new \Exception("적용할 리스트를 선택하셔야합니다.");
} }
$entities = []; $this->batchjob_delete_process($uids);
foreach (explode(",", $uids) as $uid) { MyLogService::save($this->getService(), __FUNCTION__, $this->myauth, MESSAGES["SUCCESS"]);
$entities[$uid] = $this->delete_process($uid);
}
$this->entities = $entities;
$this->getService()->getModel()->transCommit(); $this->getService()->getModel()->transCommit();
return $this->getResultPageByActon($this->action); return $this->getResultPageByActon($this->action);
} catch (\Exception $e) { } catch (\Exception $e) {
//Transaction Rollback //Transaction Rollback
$this->getService()->getModel()->transRollback(); $this->getService()->getModel()->transRollback();
MyLogService::save($this->getService(), __FUNCTION__, $this->myauth, MESSAGES["FAILED"]);
return redirect()->back()->withInput()->with('error', $e->getMessage()); return redirect()->back()->withInput()->with('error', $e->getMessage());
} }
} }
@ -523,7 +549,7 @@ abstract class CommonController extends BaseController
throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다."); throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다.");
} }
$this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid);
$this->entity = $this->getService()->getModel()->getEntity(); $this->entity = $this->getService()->getEntity();
if ($this->entity === null) { if ($this->entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다."); throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
} }

View File

@ -25,7 +25,7 @@ abstract class CommonEntity extends Entity
} }
final public function getTitle(): string final public function getTitle(): string
{ {
$field = constant("static::Title"); $field = constant("static::TITLE");
return $this->$field; return $this->$field;
} }
final public function getUpdatedAt(): string final public function getUpdatedAt(): string

View File

@ -8,7 +8,7 @@ use App\Models\MyLogModel as Model;
class MyLogEntity extends Entity class MyLogEntity extends Entity
{ {
const PK = Model::PK; const PK = Model::PK;
const Title = Model::TITLE; const TITLE = Model::TITLE;
public function __toString(): string public function __toString(): string
{ {
return "{$this->getPK()}:{$this->getTitle()}}"; return "{$this->getPK()}:{$this->getTitle()}}";

View File

@ -8,7 +8,7 @@ use App\Models\UserModel as Model;
class UserEntity extends CommonEntity class UserEntity extends CommonEntity
{ {
const PK = Model::PK; const PK = Model::PK;
const Title = Model::TITLE; const TITLE = Model::TITLE;
public function __toString(): string public function __toString(): string
{ {
return "{$this->getPK()}:{$this->getID()}:{$this->getTitle()},{$this->getRole()}}"; return "{$this->getPK()}:{$this->getID()}:{$this->getTitle()},{$this->getRole()}}";

View File

@ -8,15 +8,11 @@ use App\Models\UserSNSModel as Model;
class UserSNSEntity extends CommonEntity class UserSNSEntity extends CommonEntity
{ {
const PK = Model::PK; const PK = Model::PK;
const Title = Model::TITLE; const TITLE = Model::TITLE;
public function __toString(): string public function __toString(): string
{ {
return "{$this->getPK()}|{$this->getID()}|{$this->getTitle()}"; return "{$this->getPK()}|{$this->getID()}|{$this->getTitle()}";
} }
public function setTitle(string $title): void
{
$this->attributes[Model::TITLE] = $title;
}
//Common Function //Common Function
public function getParent(): int|null public function getParent(): int|null
{ {

View File

@ -151,16 +151,15 @@ abstract class CommonModel extends Model
// log_message("debug", $this->getLastQuery()); // log_message("debug", $this->getLastQuery());
return $entity; return $entity;
} catch (\Exception $e) { } catch (\Exception $e) {
$message = sprintf( throw new \Exception(sprintf(
"\n------%s SQL오류-----\n%s\n%s\n------------------------------\n", "\n------%s SQL오류-----\n%s\n%s\n------------------------------\n",
__FUNCTION__, __FUNCTION__,
$this->getLastQuery(), $this->getLastQuery(),
$e->getMessage() $e->getMessage()
); ));
throw new \Exception($message);
} }
} }
public function create(array $formDatas, $entity): mixed public function create(array $formDatas, mixed $entity): mixed
{ {
// Field에 맞는 Validation Rule 재정의 // Field에 맞는 Validation Rule 재정의
$this->setValidationRules($this->getFieldRules('create', $this->allowedFields)); $this->setValidationRules($this->getFieldRules('create', $this->allowedFields));
@ -178,13 +177,11 @@ abstract class CommonModel extends Model
// log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " DB 작업 완료"); // log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " DB 작업 완료");
return $entity; return $entity;
} }
public function modify($entity, array $formDatas): mixed public function modify(mixed $entity, array $formDatas): mixed
{ {
// Field에 맞는 Validation Rule 재정의 // Field에 맞는 Validation Rule 재정의
$this->setValidationRules($this->getFieldRules('modify', $this->allowedFields)); $this->setValidationRules($this->getFieldRules('modify', $this->allowedFields));
// 저장하기 전에 데이터 값 변경이 필요한 Field // 저장하기 전에 데이터 값 변경이 필요한 Field
echo var_dump($formDatas);
exit;
foreach (array_keys($formDatas) as $field) { foreach (array_keys($formDatas) as $field) {
$entity->$field = $this->convertEntityData($field, $formDatas); $entity->$field = $this->convertEntityData($field, $formDatas);
} }

View File

@ -19,7 +19,7 @@ class MyLogModel extends CommonModel
"method_name", "method_name",
"title", "title",
"content", "content",
"created_at", "status"
]; ];
public function __construct() public function __construct()
{ {

View File

@ -50,7 +50,7 @@ class GoogleService extends AuthService
$userSNS_entity = $this->getMySocket($access_code)->getUserSNSEntity(); $userSNS_entity = $this->getMySocket($access_code)->getUserSNSEntity();
// local db 사용와의 연결 확인 // local db 사용와의 연결 확인
$this->getModel()->where($this->getModel()::PK, $userSNS_entity->getParent()); $this->getModel()->where($this->getModel()::PK, $userSNS_entity->getParent());
$entity = $this->getModel()->getEntity(); $entity = $this->getEntity();
if ($entity === null) { if ($entity === null) {
throw new PageNotFoundException("회원[{$userSNS_entity->getTitle()}]님은 아직 로컬사용자 연결이 이루어지지 않았습니다."); throw new PageNotFoundException("회원[{$userSNS_entity->getTitle()}]님은 아직 로컬사용자 연결이 이루어지지 않았습니다.");
} }

View File

@ -5,53 +5,45 @@ namespace App\Services;
use App\Entities\MyLogEntity as Entity; use App\Entities\MyLogEntity as Entity;
use App\Models\MyLogModel as Model; use App\Models\MyLogModel as Model;
use App\Services\Auth\AuthService; use App\Services\Auth\AuthService;
use CodeIgniter\HTTP\IncomingRequest;
class MyLogService extends CommonService class MyLogService
{ {
private static $_model = null;
private static $_logBuffers = []; private static $_logBuffers = [];
public function __construct(IncomingRequest $request) public function __construct() {}
static public function getModel(): Model
{ {
parent::__construct($request); if (self::$_model === null) {
self::$_model = new Model();
} }
final public function getClassName(): string return self::$_model;
{
return "MyLog";
}
final public function getClassPath(): string
{
return $this->getClassName();
}
public function getModelClass(): string
{
return Model::class;
}
public function getEntityClass(): string
{
return Entity::class;
} }
static public function log(string $message, string $level = "info"): void static public function log(string $message, string $level = "info"): void
{ {
self::$_logBuffers[$level] = sprintf("%s[%s]: %s", date("H:i:s"), $level, $message); self::$_logBuffers[] = sprintf("%s[%s]: %s", date("H:i:s"), $level, $message);
log_message($level, $message); log_message($level, $message);
} }
public function info(string $message): void static public function info(string $message): void
{ {
self::log($message, 'info'); self::log($message, 'info');
} }
public function error(string $message): void static public function error(string $message): void
{ {
self::log($message, 'error'); self::log($message, 'error');
} }
public function warning(string $message): void static public function warning(string $message): void
{ {
self::log($message, 'warning'); self::log($message, 'warning');
} }
public function debug(string $message): void static public function debug(string $message): void
{ {
self::log($message, 'debug'); self::log($message, 'debug');
} }
public function save($service, string $method, AuthService $myauth, string $title): Entity static public function dump()
{
return implode("\n", self::$_logBuffers);
}
static public function save($service, string $method, AuthService $myauth, string $title): Entity
{ {
$formDatas = [ $formDatas = [
'user_uid' => $myauth->getUIDByAuthInfo(), 'user_uid' => $myauth->getUIDByAuthInfo(),
@ -61,6 +53,6 @@ class MyLogService extends CommonService
'content' => implode("\n", self::$_logBuffers), 'content' => implode("\n", self::$_logBuffers),
]; ];
self::$_logBuffers = []; self::$_logBuffers = [];
return $this->getModel()->create($formDatas, new Entity()); return self::getModel()->create($formDatas, new Entity());
} }
} }

View File

@ -59,8 +59,8 @@ class UserService extends CommonService
// die(var_export($formDatas, true)); // die(var_export($formDatas, true));
return $this->getModel()->modify($entity, $formDatas); return $this->getModel()->modify($entity, $formDatas);
} }
public function delete(Entity $entity): void public function delete(Entity $entity): bool
{ {
$this->getModel()->delete($entity); return $this->getModel()->delete($entity->getPK());
} }
} }

View File

@ -13,21 +13,21 @@ trait MylogTrait
switch ($action) { switch ($action) {
case 'create': case 'create':
foreach ($formDatas as $field => $value) { foreach ($formDatas as $field => $value) {
MyLogService::add("info", "{$field}:{$entity->$field}"); MyLogService::info("{$field}:{$entity->$field}");
} }
MyLogService::add("info", "{$entity->getTitle()}를 생성하였습니다."); MyLogService::log("{$entity->getTitle()}를 생성하였습니다.");
break; break;
case 'modify': case 'modify':
foreach ($formDatas as $field => $value) { foreach ($formDatas as $field => $value) {
MyLogService::add("info", "{$field}:{$entity->$field}=>{$value}"); MyLogService::log("{$field}:{$entity->$field}=>{$value}");
} }
MyLogService::add("info", "{$entity->getTitle()}를 수정하였습니다."); MyLogService::log("{$entity->getTitle()}를 수정하였습니다.");
break; break;
case 'delete': case 'delete':
foreach ($this->getModel()->getFields() as $field) { foreach ($this->getModel()->getFields() as $field) {
MyLogService::add("info", "{$field}:{$entity->$field}"); MyLogService::log("{$field}:{$entity->$field}");
} }
MyLogService::add("info", "{$entity->getTitle()}를 삭제하였습니다."); MyLogService::log("{$entity->getTitle()}를 삭제하였습니다.");
break; break;
} }
} }