dbmsv2/app/Services/Equipment/ServerPartService.php
2025-09-01 18:41:54 +09:00

133 lines
5.1 KiB
PHP

<?php
namespace App\Services\Equipment;
use App\Entities\Equipment\ServerEntity;
use App\Entities\Equipment\ServerPartEntity;
use App\Models\Equipment\ServerPartModel;
use App\Services\Equipment\EquipmentService;
class ServerPartService extends EquipmentService
{
private ?PartService $_partService = null;
public function __construct()
{
parent::__construct(new ServerPartModel());
$this->addClassName('ServerPart');
}
public function getFormFields(): array
{
return [
"partinfo_uid",
"serverinfo_uid",
"serviceinfo_uid",
"type",
"billing",
"amount",
"cnt",
"extra",
];
}
public function getFormFilters(): array
{
return [
"partinfo_uid",
"serverinfo_uid",
"serviceinfo_uid",
"type"
];
}
public function getBatchjobFields(): array
{
return ['partinfo_uid', "serverinfo_uid", "serviceinfo_uid",];
}
final public function getPartService(): PartService
{
if (!$this->_partService) {
$this->_partService = new PartService();
}
return $this->_partService;
}
//partEntity 정보 추가
protected function getEntity_process(mixed $entity): ServerPartEntity
{
//부품정보 정의
$partEntity = $this->getPartService()->getEntity($entity->getPartInfoUID());
if ($entity) {
$entity->setPartEntity($partEntity);
}
return $entity;
}
//기본 기능부분
// FieldForm관련용
public function getFormOption(string $field, array $options = []): array
{
switch ($field) {
case 'partinfo_uid_CPU':
$options = $this->getPartService()->getEntities(['type' => 'CPU']);
break;
case 'partinfo_uid_RAM':
$options = $this->getPartService()->getEntities(['type' => 'RAM']);
break;
case 'partinfo_uid_DISK':
$options = $this->getPartService()->getEntities(['type' => 'DISK']);
break;
case 'partinfo_uid_OS':
$options = $this->getPartService()->getEntities(['type' => 'OS']);
break;
case 'partinfo_uid_DB':
$options = $this->getPartService()->getEntities(['type' => 'DB']);
break;
case 'partinfo_uid_SOFTWARE':
$options = $this->getPartService()->getEntities(['type' => 'SOFTWARE']);
break;
case 'partinfo_uid': //수정때문에 전체가 필요
$options = $this->getPartService()->getEntities();
break;
default:
$options = parent::getFormOption($field, $options);
break;
}
return $options;
}
//생성
public function createByServer(ServerEntity $serverEntity, array $formDatas): ServerPartEntity
{
foreach (SERVER['PARTTYPES'] as $partType) {
$serverPartFormDatas = [];
$serverPartFormDatas["partinfo_uid"] = $formDatas["partinfo_uid_{$partType}"];
$serverPartFormDatas["serverinfo_uid"] = $serverEntity->getPK();
$serverPartFormDatas["serviceinfo_uid"] = $serverEntity->getServiceInfoUID();
$serverPartFormDatas["type"] = $partType;
$serverPartFormDatas["billing"] = array_key_exists("serverinfopartinfo_uid_{$partType}_billing", $formDatas) ? $formDatas["serverinfopartinfo_uid_{$partType}_billing"] : null;
$serverPartFormDatas["amount"] = array_key_exists("serverinfopartinfo_uid_{$partType}_amount", $formDatas) ? $formDatas["serverinfopartinfo_uid_{$partType}_amount"] : 0;
$serverPartFormDatas["cnt"] = array_key_exists("serverinfopartinfo_uid_{$partType}_cnt", $formDatas) ? $formDatas["serverinfopartinfo_uid_{$partType}_cnt"] : null;
$serverPartFormDatas["extra"] = array_key_exists("serverinfopartinfo_uid_{$partType}_extra", $formDatas) ? $formDatas["serverinfopartinfo_uid_{$partType}_extra"] : null;
$entity = parent::create($serverPartFormDatas);
//부품정보 정의
$partEntity = $this->getPartService()->getEntity($entity->getPartInfoUID());
if ($partEntity) {
$entity->setPartEntity($partEntity);
}
}
return $entity;
}
//수정
public function modifyByServer(ServerEntity $serverEntity, array $formDatas): ServerPartEntity
{
//삭제후생성
$this->deleteByServer($serverEntity);
return $this->createByServer($serverEntity, $formDatas);
}
//삭제
public function deleteByServer(ServerEntity $serverEntity): array
{
//기존 서벼별 부품연결정보 삭제 후
$entities = $this->getEntities(['serverinfo_uid' => $serverEntity->getPK()]);
foreach ($entities as $entity) {
$this->delete($entity);
}
return $entities;
}
}