dbmsv3/app/Processors/Equipment/ServerPartV1Processor.php
2025-10-28 13:41:54 +09:00

190 lines
7.8 KiB
PHP

<?php
namespace App\Processors\Equipment;
use App\Entities\Equipment\ServerEntity;
use App\Entities\Equipment\ServerPartEntity;
use App\Services\Customer\ServiceService;
use App\Services\Equipment\ServerPartService;
use App\Services\Equipment\ServerService;
use App\Services\MyLogService;
use App\Services\Part\IPService;
use App\Services\Part\SWITCHService;
use App\Services\PaymentService;
use CodeIgniter\Database\BaseConnection;
use RuntimeException;
class ServerPartV1Processor
{
public function __construct(
private BaseConnection $db,
private ServerPartService $service,
private ServiceService $serviceService,
private ServerService $serverService,
private PaymentService $paymentService,
private IPService $ipService,
private SWITCHService $switchService,
private MyLogService $logService,
) {}
public function create(array $formDatas): ServerPartEntity
{
$this->db->transStart();
//서버정보가져오기
if (!array_key_exists('serverinfo_uid', $formDatas)) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서버 정보가 지정되지 않았습니다.");
}
$serverEntity = $this->serverService->getEntity($formDatas['serverinfo_uid']);
if (!$serverEntity instanceof ServerEntity) {
throw new \Exception(__METHOD__ . "에서 오류발생: {$formDatas['serverinfo_uid']} 서버 정보를 찾을수 없습니다.");
}
//서비스정의확인(기본은 제외)
if ($formDatas['billing'] !== PAYMENT['BILLING']['BASE'] && $serverEntity->getServiceInfoUID() === null) {
throw new \Exception(__METHOD__ . "에서 오류발생:서비스가 지정되어야 작업이 가능합니다.");
}
// 1) 생성작업
$formDatas["clientinfo_uid"] = $serverEntity->getClientInfoUID();
$formDatas["serviceinfo_uid"] = $serverEntity->getServiceInfoUID();
$formDatas["serverinfo_uid"] = $serverEntity->getPK();
$entity = $this->service->getModel()->create($formDatas);
// 2) Type에 따른 각 파트 정의
$this->service->getPartService($entity->getType())->attachToServerPart($entity);
// 3) 과금정의
switch ($entity->getBilling()) {
case PAYMENT['BILLING']['MONTH']: //월별과금일때만 처리
if ($entity->getServiceInfoUID() === null) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서비스정보가 정의된 후에만 가능합니다.");
}
//서비스 금액만 재계산변경
$this->serviceService->setAmount($entity->getServiceInfoUID());
break;
case PAYMENT['BILLING']['ONETIME']: //일회성일때만 처리
$paymentEntity = $this->paymentService->createForServerPart($entity);
//결제정보PK정의
$entity = $this->service->getModel()->modify($entity, ['payment_uid' => $paymentEntity->getPK()]);
break;
case PAYMENT['BILLING']['BASE']: //기본처리
//아무처리 않함
break;
default:
throw new \Exception(__METHOD__ . "에서 오류발생:{$entity->getBilling()}은 지정되지 않은 결제방식입니다.");
// break;
}
// 4) 로그
$this->logService->getModel()->create(formDatas: [
'title' => "{$entity->getTitle()} 서버파트 추가",
'status' => $entity->getStatus(),
]);
$this->db->transComplete();
if ($this->db->transStatus() === false) {
throw new RuntimeException('트랜잭션 실패');
}
return $entity;
}
public function modify(ServerPartEntity $entity, array $formDatas): ServerPartEntity
{
$this->db->transStart();
//서버정보가져오기
if (!array_key_exists('serverinfo_uid', $formDatas)) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서버 정보가 지정되지 않았습니다.");
}
// 1) 수정전 정보
//파트값이 정의되어 있으면
$formDatas['part_uid'] = null; //null이면 기존정보 유지
$isPartChanged = false;
if (array_key_exists('part_uid', $formDatas) && !$formDatas['part_uid']) {
//기존파트값과 다르면
if ($entity->getPartUID() != $formDatas['part_uid']) {
//기존파트값이 있으면 해지
if ($entity->getPartUID() != null) {
$this->service->getPartService($entity->getType())->detachFromServerPart($entity);
}
$isPartChanged = true;
}
}
// 2) 서버연결정보 수정
$serverEntity = $this->serverService->getEntity($formDatas['serverinfo_uid']);
if (!$serverEntity instanceof ServerEntity) {
throw new \Exception(__METHOD__ . "에서 오류발생: {$formDatas['serverinfo_uid']} 서버 정보를 찾을수 없습니다.");
}
$formDatas["clientinfo_uid"] = $serverEntity->getClientInfoUID();
$formDatas["serviceinfo_uid"] = $serverEntity->getServiceInfoUID();
$formDatas["serverinfo_uid"] = $serverEntity->getPK();
$entity = $this->service->getModel()->modify($entity, $formDatas);
// 3) Type에 따른 각 파트 정의
if ($isPartChanged) { //파트가 바뀐경우 추가작업
$this->service->getPartService($entity->getType())->attachToServerPart($entity);
}
// 3) 과금정의
switch ($entity->getBilling()) {
case PAYMENT['BILLING']['MONTH']: //월별과금일때만 처리
if ($entity->getServiceInfoUID() === null) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서비스정보가 정의된 후에만 가능합니다.");
}
//서비스 금액만 재계산변경
$this->serviceService->setAmount($entity->getServiceInfoUID());
break;
case PAYMENT['BILLING']['ONETIME']: //일회성일때만 처리
$this->paymentService->updateForServerPart($entity);
break;
case PAYMENT['BILLING']['BASE']: //기본처리
//아무처리 않함
break;
default:
throw new \Exception(__METHOD__ . "에서 오류발생:{$entity->getBilling()}은 지정되지 않은 결제방식입니다.");
// break;
}
// 4) 로그
$this->logService->getModel()->create(formDatas: [
'title' => "{$entity->getTitle()} 서버파트 수정",
'status' => $entity->getStatus(),
]);
$this->db->transComplete();
if ($this->db->transStatus() === false) {
throw new RuntimeException('트랜잭션 실패');
}
return $entity;
}
public function delete(ServerPartEntity $entity): ServerPartEntity
{
$this->db->transStart();
// 1) 기존파트값이 있으면 해지
if ($entity->getPartUID() != null) {
$this->service->getPartService($entity->getType())->detachFromServerPart($entity);
}
// 2) 서버파트정보 삭제
$this->service->getModel()->delete($entity);
// 3) 과금처리
switch ($entity->getBilling()) {
case PAYMENT['BILLING']['MONTH']: //월별과금일때만 처리
if ($entity->getServiceInfoUID() !== null) { //서비스가 정의되어 있으면
//서비스 금액만 재계산변경
$this->serviceService->setAmount($entity->getServiceInfoUID());
}
break;
case PAYMENT['BILLING']['ONETIME']: //일회성일때만 처리
if ($entity->getPaymentUID() !== null) { //결제정보가 정의되어 있으면
$this->paymentService->unlinkFromServerPart($entity);
}
break;
case PAYMENT['BILLING']['BASE']: //기본처리
//아무처리 않함
break;
default:
throw new \Exception(__METHOD__ . "에서 오류발생:{$entity->getBilling()}은 지정되지 않은 결제방식입니다.");
// break;
}
// 4) 로그
$this->logService->getModel()->create(formDatas: [
'title' => "{$entity->getTitle()} 서버파트 삭제",
'status' => $entity->getStatus(),
]);
$this->db->transComplete();
if ($this->db->transStatus() === false) {
throw new RuntimeException('트랜잭션 실패');
}
return $entity;
}
}