dbmsv2/app/Services/Equipment/ServerPartService.php
2025-09-17 15:04:01 +09:00

250 lines
8.8 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\Customer\PaymentService;
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;
private ?PaymentService $_paymentService = null;
public function __construct()
{
parent::__construct(new ServerPartModel(), new ServerPartHelper());
$this->addClassName('ServerPart');
}
public function getFormFields(): array
{
return [
"serverinfo_uid",
"type",
"billing",
"part_uid",
"cnt",
"extra",
"amount",
];
}
public function getFormFilters(): array
{
return [
"serverinfo_uid",
"type",
"part_uid",
"billing",
];
}
public function getIndexFields(): array
{
return [
"serverinfo_uid",
"type",
"part_uid",
"billing",
"amount",
"cnt",
"extra",
];
}
public function getIndexFilters(): 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;
}
final public function getPaymentService(): PaymentService
{
if (!$this->_paymentService) {
$this->_paymentService = new PaymentService();
}
return $this->_paymentService;
}
//partEntity 정보 추가
protected function getEntity_process(mixed $entity): ServerPartEntity
{
if (!($entity instanceof ServerPartEntity)) {
throw new \Exception(__METHOD__ . "에서 형식오류:ServicePartEntity만 허용됩니다.");
}
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;
}
return $entity->setPartEntity($partService->getEntity($entity->getPartUID()));
}
//기본 기능부분
// FieldForm관련용
public function getFormOption(string $field, array $options = []): array
{
switch ($field) {
case 'part_uid':
$partOptions = [];
foreach (SERVERPART['ALL_PARTTYPES'] as $partType) {
switch ($partType) {
case 'CPU':
case 'RAM':
case 'DISK':
case 'OS':
case 'DB':
case 'SOFTWARE':
$partOptions[$partType] = $this->getPartService()->getEntities(['type' => $partType]);
break;
case 'SWITCH':
$partOptions[$partType] = $this->getSwitchService()->getEntities();
break;
case 'IP':
$partOptions[$partType] = $this->getIPService()->getEntities();
break;
case 'CS':
$partOptions[$partType] = $this->getCSService()->getEntities();
break;
}
}
$options = $partOptions;
// dd($options);
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 setServerPart(ServerPartEntity $entity, mixed $part_uid, string $status): mixed
{
//Type에 따른 부품서비스 정의
switch ($entity->getType()) {
case 'SWITCH':
$partEntity = $this->getSwitchService()->setServerPart($entity, $part_uid, $status);
break;
case 'IP':
$partEntity = $this->getIPService()->setServerPart($entity, $part_uid, $status);
break;
case 'CS':
$partEntity = $this->getCSService()->setServerPart($entity, $part_uid, $status);
break;
default: //CPU/RAM/DISK/OS/DB/SOFTWARE
$partEntity = $this->getPartService()->setServerPart($entity, $part_uid, $status);
break;
}
return $partEntity;
}
//부품연결정보생성
public function create(array $formDatas): ServerPartEntity
{
//서버정보가져오기
$serverEntity = null;
if (array_key_exists('serverEntity', $formDatas)) {
$serverEntity = $formDatas['serverEntity'];
} else {
if (!array_key_exists('serverinfo_uid', $formDatas)) {
throw new \Exception("서버 정보가 지정되지 않았습니다.");
}
$serverEntity = $this->getServerService()->getEntity($formDatas['serverinfo_uid']);
}
if (!($serverEntity instanceof ServerEntity)) {
throw new \Exception("서버 정보가 지정되지 않았습니다.");
}
$formDatas["clientinfo_uid"] = $serverEntity->getClientInfoUID();
$formDatas["serviceinfo_uid"] = $serverEntity->getServiceInfoUID();
$formDatas["serverinfo_uid"] = $serverEntity->getPK();
$entity = parent::create($formDatas);
//부품연결정보에 부품정보 정의
$entity = $entity->setPartEntity(
$this->setServerPart($entity, $formDatas['part_uid'], STATUS['OCCUPIED'])
);
//일회성인경우 결제정보에 추가한다.
$this->getPaymentService()->setServerPart($entity, 0, "");
return $entity;
}
//수정
public function modify(mixed $entity, array $formDatas): ServerPartEntity
{
//서버정보가져오기
if (!array_key_exists('serverinfo_uid', $formDatas)) {
throw new \Exception("서버 정보가 지정되지 않았습니다.");
}
$serverEntity = $this->getServerService()->getEntity($formDatas['serverinfo_uid']);
if (!($serverEntity instanceof ServerEntity)) {
throw new \Exception("서버 정보가 지정되지 않았습니다.");
}
$formDatas["clientinfo_uid"] = $serverEntity->getClientInfoUID();
$formDatas["serviceinfo_uid"] = $serverEntity->getServiceInfoUID();
$formDatas["serverinfo_uid"] = $serverEntity->getPK();
//기존 Part_UID와 신규 Part_UID가 다르면 기존 Part정보 사용가능으로 변경
if ($entity->getPartUID() != $formDatas['part_uid']) {
$this->setServerPart($entity, $entity->getPartUID(), STATUS['AVAILABLE']);
}
//기존 정보변경
$entity = parent::modify($entity, $formDatas);
//부품연결정보에 부품정보 정의
return $entity->setPartEntity(
$this->setServerPart($entity, $formDatas['part_uid'], STATUS['OCCUPIED'])
);
}
//삭제
public function delete(mixed $entity): ServerPartEntity
{
//부품연결정보에 부품정보 정의
$this->setServerPart($entity, $entity->getPartUID(), STATUS['AVAILABLE']);
return parent::delete($entity);
}
}