dbmsv2/app/Services/Equipment/ServerPartService.php
2025-09-05 17:33:23 +09:00

234 lines
8.2 KiB
PHP

<?php
namespace App\Services\Equipment;
use App\Entities\Equipment\PartEntity;
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
{
switch ($entity->getType()) {
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 ($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;
}
private function action_process(ServerPartEntity $entity, array $formDatas): mixed
{
//Type에 따른 부품서비스 정의
switch ($entity->getType()) {
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__ . "에서 오류:{$entity->getPartUID()}에 해닫하는 {$entity->getType()}정보가 없습니다.");
}
switch ($entity->getType()) {
case 'SWITCH':
case 'CS':
$partEntity = $partService->modify($partEntity, $formDatas);
break;
case 'IP':
//기존IP 사용자로 고객정보 설정
$formDatas['old_clientinfo_uid'] = $partEntity->getClientInfoUID();
$partEntity = $partService->modify($partEntity, $formDatas);
break;
}
return $partEntity;
}
//부품연결정보생성
public function create(array $formDatas): ServerPartEntity
{
$entity = parent::create($formDatas);
//서버정보가져오기
$serverEntity = $this->getServerService()->getEntity($entity->getServerInfoUID());
if (!$serverEntity) {
throw new \Exception(__METHOD__ . "에서 오류:{$entity->getServerInfoUID()}에 해닫하는 서버정보가 없습니다.");
}
//부품연결정보에 부품정보 정의
$partFormDatas = [
'clientinfo_uid' => $serverEntity->getClientInfoUID(),
'serviceinfo_uid' => $serverEntity->getServiceInfoUID(),
'serverinfo_uid' => $serverEntity->getPK(),
'status' => STATUS['OCCUPIED']
];
$entity->setPartEntity($this->action_process($entity, $partFormDatas));
return $entity;
}
//수정
public function modify(mixed $entity, array $formDatas): ServerPartEntity
{
$entity = parent::modify($entity, $formDatas);
if (!array_key_exists('status', $formDatas)) {
throw new \Exception(__METHOD__ . "에서 상태정보가 정의되지 않았습니다.");
}
//부품정보에 서버정보 설정 및 서비스,고객정보 정의
$partFormDatas = [
'status' => $formDatas['status'],
];
if ($partFormDatas['status'] === STATUS['OCCUPIED']) {
$partFormDatas['clientinfo_uid'] = $entity->getClientInfoUID();
$partFormDatas['serviceinfo_uid'] = $entity->getServiceInfoUID();
$partFormDatas['serverinfo_uid'] = $entity->getServerInfoUID();
$entity->setPartEntity($this->action_process($entity, $partFormDatas));
} else {
$partFormDatas['clientinfo_uid'] = null;
$partFormDatas['serviceinfo_uid'] = null;
$partFormDatas['serverinfo_uid'] = null;
$entity->setPartEntity($this->action_process($entity, $partFormDatas));
}
return $entity;
}
//삭제
public function delete(mixed $entity): ServerPartEntity
{
$partFormDatas = [
'status' => STATUS['AVAIABLE'],
];
$partFormDatas['clientinfo_uid'] = null;
$partFormDatas['serviceinfo_uid'] = null;
$partFormDatas['serverinfo_uid'] = null;
$this->action_process($entity, $partFormDatas);
return parent::delete($entity);
}
}