dbmsv4/app/Services/Equipment/ServerService.php
2026-02-04 15:32:47 +09:00

193 lines
7.4 KiB
PHP

<?php
namespace App\Services\Equipment;
use App\DTOs\Equipment\ServerDTO;
use App\Entities\Customer\ServiceEntity;
use App\Entities\Equipment\ServerEntity;
use App\Forms\Equipment\ServerForm;
use App\Helpers\Equipment\ServerHelper;
use App\Models\Equipment\ServerModel;
use RuntimeException;
class ServerService extends EquipmentService
{
protected string $formClass = ServerForm::class;
protected string $helperClass = ServerHelper::class;
public function __construct(ServerModel $model)
{
parent::__construct($model);
$this->addClassPaths('Server');
}
public function getDTOClass(): string
{
return ServerDTO::class;
}
public function createDTO(array $formDatas): ServerDTO
{
return new ServerDTO($formDatas);
}
public function getEntityClass(): string
{
return ServerEntity::class;
}
//총서버금액
final public function getCalculatedAmount(int $uid): int
{
$entity = $this->getEntity($uid);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid} 서버 정보를 찾을수 없습니다.");
}
$serverPartService = service('equipment_serverpartservice');
$caculatedAmount = $entity->getPrice();
foreach ($serverPartService->getEntities(['serverinfo_uid' => $entity->getPK(), 'billing' => PAYMENT['BILLING']['MONTH']]) as $serverPartEntity) {
$caculatedAmount += $serverPartEntity->getCalculatedAmount();
}
return $caculatedAmount;
}
protected function getEntity_process(mixed $entity): ServerEntity
{
return $entity;
}
protected function create_process(array $formDatas): ServerEntity
{
$entity = parent::create_process($formDatas);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
}
if ($entity->getIP()) {
service('part_ipservice')->attachToServer($entity);
}
if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->attachToServer($entity);
}
service('equipment_chassisservice')->attachToServer($entity);
service('equipment_serverpartservice')->attachToServer($entity);
return $entity;
}
protected function modify_process($entity, array $formDatas): ServerEntity
{
if (!array_key_exists('chassisinfo_uid', $formDatas)) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . '에서 오류발생: 샷시정보가 정의되지 않았습니다.');
}
$oldEntity = clone $entity;
$entity = parent::modify_process($entity, $formDatas);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
}
if ($oldEntity->getIP() !== $entity->getIP()) {
service('part_ipservice')->detachFromServer($oldEntity);
if ($entity->getIP() !== null) {
service('part_ipservice')->attachToServer($entity);
}
}
if ($oldEntity->getSwitchInfoUid() !== $entity->getSwitchInfoUid()) {
service('part_switchservice')->detachFromServer($oldEntity);
if ($entity->getSwitchInfoUid() !== null) {
service('part_switchservice')->attachToServer($entity);
}
}
if ($oldEntity->getChassisInfoUid() !== $entity->getChassisInfoUid()) {
service('equipment_chassisservice')->detachFromServer($oldEntity);
if ($entity->getChassisInfoUid() !== null) {
service('equipment_chassisservice')->attachToServer($entity);
}
}
// ✅ 서비스 유지중이면 정상 동기화 (해지 시는 deatchFromService에서 따로 처리)
if ($entity->getServiceInfoUid() !== null) {
$serviceEntity = service('customer_serviceservice')->getEntity($entity->getServiceInfoUid());
if (!$serviceEntity instanceof ServiceEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$entity->getServiceInfoUid()}에 해당하는 서비스정보을 찾을수 없습니다.");
}
service('customer_serviceservice')->recalcAmountAndSyncPayment($serviceEntity);
}
return $entity;
}
public function setSearchWord(string $word): void
{
$this->model->orLike($this->model->getTable() . '.ip', $word, 'both');
$this->model->orLike($this->model->getTable() . '.ilo_ip', $word, 'both');
parent::setSearchWord($word);
}
public function attatchToService(ServiceEntity $serviceEntity, $uid, array $formDatas = []): void
{
$entity = $this->getEntity($uid);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 서버정보을 찾을수 없습니다.");
}
$formDatas['serviceinfo_uid'] = $serviceEntity->getPK();
$formDatas["clientinfo_uid"] = $serviceEntity->getClientInfoUid();
$formDatas['status'] = $formDatas['status'] ?? STATUS['OCCUPIED'];
parent::modify_process($entity, $formDatas);
}
//해지처리 (중요: server.serviceinfo_uid == null 이후에 ServerPart detach 호출)
public function deatchFromService($uid, array $formDatas = []): void
{
$entity = $this->getEntity($uid);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 서버정보을 찾을수 없습니다.");
}
// ✅ 분리 전 서비스 스냅샷
$oldServiceEntity = null;
$serviceUid = $entity->getServiceInfoUid();
if ($serviceUid) {
$svc = service('customer_serviceservice')->getEntity($serviceUid);
if ($svc instanceof ServiceEntity) {
$oldServiceEntity = clone $svc;
}
}
// 파트 detach(네트워크 자원)
if ($entity->getIP()) {
service('part_ipservice')->detachFromServer($entity);
}
if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->detachFromServer($entity);
}
// ✅ 먼저 서버를 서비스에서 분리 (serviceinfo_uid=null)
$formDatas['serviceinfo_uid'] = null;
$formDatas["clientinfo_uid"] = null;
$formDatas["switchinfo_uid"] = null;
$formDatas["ip"] = null;
$formDatas['status'] = $formDatas['status'] ?? STATUS['AVAILABLE'];
$entity = parent::modify_process($entity, $formDatas);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
}
// ✅ 이제 server.serviceinfo_uid == null 상태이므로 정책 발동:
// - MONTH 파트 삭제
// - 월 미납 payment는 삭제/0원수정 금지, status=TERMINATED
// - ONETIME 미납 삭제는 보류
service('equipment_serverpartservice')->detachFromServer($entity, $oldServiceEntity);
}
}