dbmsv2/app/Services/Equipment/ServerPartService.php
2025-09-05 10:02:18 +09:00

246 lines
9.2 KiB
PHP

<?php
namespace App\Services\Equipment;
use App\Entities\Equipment\ServerEntity;
use App\Entities\Equipment\ServerPartEntity;
use App\Helpers\Equipment\ServerPartHelper;
use App\Models\Equipment\ServerPartModel;
use App\Services\Equipment\EquipmentService;
class ServerPartService extends EquipmentService
{
private ?SwitchService $_switchService = null;
private ?PartService $_partService = null;
private ?IPService $_ipService = null;
private ?CSService $_csService = null;
public function __construct()
{
parent::__construct(new ServerPartModel());
$this->addClassName('ServerPart');
}
public function getFormFields(): array
{
return [
"serverinfo_uid",
"type",
"part_uid",
"billing",
"amount",
"cnt",
"extra",
];
}
public function getFormFilters(): array
{
return [
"serverinfo_uid",
"type",
"part_uid",
"billing",
];
}
public function getBatchjobFields(): array
{
return ['billing', 'type'];
}
final public function getSwitchService(): SwitchService
{
if (!$this->_switchService) {
$this->_switchService = new SwitchService();
}
return $this->_switchService;
}
final public function getPartService(): PartService
{
if (!$this->_partService) {
$this->_partService = new PartService();
}
return $this->_partService;
}
final public function getIPService(): IPService
{
if (!$this->_ipService) {
$this->_ipService = new IPService();
}
return $this->_ipService;
}
final public function getCSService(): CSService
{
if (!$this->_csService) {
$this->_csService = new CSService();
}
return $this->_csService;
}
//partEntity 정보 추가
protected function getEntity_process(mixed $entity): ServerPartEntity
{
$partEntity = $this->getPartService()->getEntity($entity->getPartUID());
if ($entity) {
$entity->setPartEntity($partEntity);
}
return $entity;
}
//기본 기능부분
// FieldForm관련용
public function getFormOption(string $field, array $options = []): array
{
switch ($field) {
case 'part_uid':
$options = $this->getPartService()->getEntities();
break;
case 'CPU':
$options = $this->getPartService()->getEntities(['type' => 'CPU']);
break;
case 'RAM':
$options = $this->getPartService()->getEntities(['type' => 'RAM']);
break;
case 'DISK':
$options = $this->getPartService()->getEntities(['type' => 'DISK']);
break;
case 'OS':
$options = $this->getPartService()->getEntities(['type' => 'OS']);
break;
case 'DB':
$options = $this->getPartService()->getEntities(['type' => 'DB']);
break;
case 'SOFTWARE':
$options = $this->getPartService()->getEntities(['type' => 'SOFTWARE']);
break;
case 'SWITCH':
$options = $this->getSwitchService()->getEntities();
break;
case 'IP':
$options = $this->getIPService()->getEntities();
break;
case 'CS':
$options = $this->getCSService()->getEntities();
break;
default:
$options = parent::getFormOption($field, $options);
break;
}
if (!is_array($options)) {
throw new \Exception(__FUNCTION__ . "에서 {$field}의 options 값이 array가 아닙니다.\n" . var_export($options, true));
}
return $options;
}
//서버별생성
public function createByServer(ServerEntity $serverEntity, array $formDatas): ServerEntity
{
foreach (SERVERPART['PARTTYPES'] as $partType) {
if (array_key_exists($partType, $formDatas)) {
$serverPartFormDatas = [];
$serverPartFormDatas["part_uid"] = $formDatas[$partType];
$serverPartFormDatas["serverinfo_uid"] = $serverEntity->getPK();
$serverPartFormDatas["serviceinfo_uid"] = $serverEntity->getServiceInfoUID();
$serverPartFormDatas["type"] = $partType;
$serverPartFormDatas["billing"] = null;
$serverPartFormDatas["amount"] = 0;
$serverPartFormDatas["cnt"] = array_key_exists("{$partType}_cnt", $formDatas) ? $formDatas["{$partType}_cnt"] : 1;
$serverPartFormDatas["extra"] = array_key_exists("{$partType}_extra", $formDatas) ? $formDatas["{$partType}_extra"] : null;
$entity = parent::create($serverPartFormDatas);
//부품정보 정의
switch ($partType) {
case 'SWITCH':
$partService = $this->getSwitchService();
break;
case 'IP':
$partService = $this->getIPService();
break;
case 'CS':
$partService = $this->getCSService();
break;
default:
$partService = $this->getPartService();
break;
}
$partEntity = $partService->getEntity($entity->getPartUID());
if (!$partEntity) {
throw new \Exception(__METHOD__ . "에서 오류:{$partType}{$entity->getPartUID()}에 해닫하는 정보가 없습니다.");
}
$partService->modify($partEntity, [
'serverinfo_uid' => $serverEntity->getPK(),
'serviceinfo_uid' => $serverEntity->getServiceInfoUID(),
]);
$entity->setPartEntity($partEntity);
$serverEntity->addServerPartEntity($partType, $entity);
}
}
return $serverEntity;
}
//서버별삭제
public function deleteByServer(ServerEntity $serverEntity): void
{
//기존 서벼별 부품연결정보 삭제 후
foreach ($this->getEntities(['serverinfo_uid' => $serverEntity->getPK()]) as $entity) {
$this->delete($entity);
}
}
//서비스연결
public function enableService(ServerEntity $serverEntity, ServerPartEntity $entity, array $formDatas): ServerPartEntity
{
//ServerPart 수정
$formDatas["serverinfo_uid"] = $serverEntity->getPK();
$entity = parent::modify($entity, $formDatas);
//각 파트서비스별 수정
switch ($entity->getType()) {
case 'SWITCH':
$partService = $this->getSwitchService();
$formDatas['status'] = STATUS['OCCUPIED'];;
break;
case 'IP':
$partService = $this->getIPService();
$formDatas['status'] = STATUS['OCCUPIED'];;
break;
case 'CS':
$partService = $this->getCSService();
$formDatas['status'] = STATUS['OCCUPIED'];;
break;
default:
$partService = $this->getPartService();
break;
}
$partEntity = $partService->getEntity($entity->getPartUID());
if (!$partEntity) {
throw new \Exception(__METHOD__ . "에서 오류:{$entity->getType()}{$entity->getPartUID()}에 해닫하는 정보가 없습니다.");
}
$partEntity = $partService->modify($partEntity, $formDatas);
//파트설정
$entity->setPartEntity($partEntity);
return $entity;
}
//서비스해지
public function disableService(ServerPartEntity $entity, array $formDatas = []): ServerPartEntity
{
//ServerPart 수정
$entity = parent::modify($entity, $formDatas);
//각 파트서비스별 수정
switch ($entity->getType()) {
case 'SWITCH':
$partService = $this->getSwitchService();
$formDatas['status'] = STATUS['AVAILABLE'];
break;
case 'IP':
$partService = $this->getIPService();
$formDatas['status'] = STATUS['AVAILABLE'];
break;
case 'CS':
$partService = $this->getCSService();
$formDatas['status'] = STATUS['AVAILABLE'];
break;
default:
$partService = $this->getPartService();
break;
}
$partEntity = $partService->getEntity($entity->getPartUID());
if (!$partEntity) {
throw new \Exception(__METHOD__ . "에서 오류:{$entity->getType()}{$entity->getPartUID()}에 해닫하는 정보가 없습니다.");
}
$partEntity = $partService->modify($partEntity, $formDatas);
//파트설정
$entity->setPartEntity($partEntity);
return $entity;
}
}