dbmsv2/app/Controllers/Admin/Equipment/ServerController.php
2025-08-27 17:54:08 +09:00

114 lines
4.4 KiB
PHP

<?php
namespace App\Controllers\Admin\Equipment;
use App\Entities\Equipment\IPEntity;
use App\Entities\Equipment\ServerEntity;
use App\Entities\Equipment\ServerPartEntity;
use App\Helpers\Equipment\ServerHelper;
use App\Services\Equipment\ServerService;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class ServerController extends EquipmentController
{
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->content_title = lang("{$this->getService()->getClassName()}.title");
$this->class_path .= $this->getService()->getClassName();
$this->uri_path .= strtolower($this->getService()->getClassName('/')) . '/';
// $this->view_path .= strtolower($this->getService()->getClassName()) . DIRECTORY_SEPARATOR;
$this->serverpartinfo_cnt_range = array_combine(range(0, 10), range(0, 10));
$this->serverpartinfo_extra_options = array_merge(["" => "RAID 선택"], lang("{$this->getService()->getClassName()}.EXTRAS"));
}
public function getService(): ServerService
{
if (!$this->_service) {
$this->_service = new ServerService();
}
return $this->_service;
}
public function getHelper(): ServerHelper
{
if (!$this->_helper) {
$this->_helper = new ServerHelper();
}
return $this->_helper;
}
//Index,FieldForm관련
//생성
protected function create_form_process(): void
{
$format = env("Server.Prefix.code.format", false);
if (!$format) {
throw new \Exception(__METHOD__ . "에서 code의 format[Server.Prefix.code.format]이 정의되지 않았습니다.");
}
$this->setFieldDefaultValue('code', $this->getService()->getLastestCode($format, (int)env("Server.Default.code", 0)));
parent::create_form_process();
}
//추가 파트정보 생성
private function getFormDatasForServerPartInfo(ServerEntity $entity, string $partType): array
{
$formDatas = [
"partinfo_uid" => $this->request->getPost(["serverpartinfo_{$partType}_uid"]),
"serverinfo_uid" => $entity->getPK(),
"serviceinfo_uid" => $entity->getServiceInfoUID(),
"type" => $partType,
"billing" => ServerPartENtity::DEFAULT_BILLING,
"amount" => $this->request->getPost("amount") ?? 0,
"cnt" => $this->request->getPost("serverpartinfo_{$partType}_uid_cnt") ?? 1,
"extra" => $this->request->getPost("serverpartinfo_{$partType}_uid_extra") ?? ""
];
return $formDatas;
}
private function getFormDatasForIPInfo(ServerEntity $entity, string $status): array
{
$formDatas = [
"serverinfo_uid" => $entity->getPK(),
"clientinfo_uid" => $entity->getClientInfoUID(),
"serviceinfo_uid" => $entity->getServiceInfoUID(),
"status" => $status
];
return $formDatas;
}
private function createIpInfo(ServerEntity $entity): IPEntity
{
//기존 Entity 가져오기
$ipinfo_uid = $this->request->getPost("ipinfo_uid");
$ipEntity = $this->getService()->getIPService()->getEntity($ipinfo_uid);
if (!$ipEntity) {
throw new \Exception("{$ipinfo_uid}에 대한 정보를 찾을수 없습니다.");
}
return $this->getService()->getIPService()->modify($ipEntity, $this->getFormDatasForIPInfo($entity, $ipEntity::STATUS_OCCUPIED));
}
protected function create_process(array $formDatas): ServerEntity
{
//코드 패턴체크
$pattern = env("Server.Prefix.code.pattern", false);
if (!$pattern) {
throw new \Exception(__METHOD__ . "에서 code의 prefix[Server.Prefix.code.pattern]가 정의되지 않았습니다.");
}
if (!array_key_exists('code', $formDatas)) {
throw new \Exception("Server코드가 정의되지 않았습니다");
}
if (!preg_match($pattern, $formDatas['code'])) {
throw new \Exception("Server코드[{$formDatas['code']}의 형식이 맞지않습니다");
}
$entity = parent::create_process($formDatas);
//ServerPart정보 생성 후 ServerEntity에 설정
foreach (SERVER['PARTTYPES'] as $partType) {
$serverPartEnty = $this->getService()->getServerPartService()->create(
$this->getFormDatasForServerPartInfo($entity, $partType)
);
$entity->setServerPartEntity($partType, $serverPartEnty);
}
//IP정보 속성 수정후 ServerEntity에 설정
$entity->setIPEntities([$this->createIpInfo($entity)]);
return $entity;
}
}