dbmsv4/app/Services/Equipment/ServerService.php
2026-02-04 11:18:22 +09:00

266 lines
12 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 getTotalServiceCount(array $where = []): array
{
$totalCounts = [
'chiba_summary' => 0,
'tokyo_summary' => 0,
'all_summary' => 0,
'normal' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'defence' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'dedicated' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'alternative' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'vpn' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'event' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
'test' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0],
];
$builder = $this->model->select("serverinfo.type,
COUNT(CASE WHEN serviceinfo.location = 'chiba' THEN 1 END) AS chiba,
COUNT(CASE WHEN serviceinfo.location = 'tokyo' THEN 1 END) AS tokyo,
COUNT(CASE WHEN serviceinfo.location IN ('chiba', 'tokyo') THEN 1 END) AS summary")
->join('serviceinfo', 'serviceinfo.uid = serverinfo.serviceinfo_uid')
->where($where)
->groupBy('serverinfo.type')
->builder();
// echo $builder->getCompiledSelect(false) . "<BR>"; //초기화 없이 SQL만 보고 싶을 때: getCompiledSelect(false) ← 꼭 false!
// dd($rows);
foreach ($builder->get()->getResult() as $row) {
$totalCounts[$row->type]['chiba'] = $row->chiba;
$totalCounts[$row->type]['tokyo'] = $row->tokyo;
$totalCounts[$row->type]['summary'] += $row->summary;
$totalCounts['chiba_summary'] += $row->chiba;
$totalCounts['tokyo_summary'] += $row->tokyo;
$totalCounts['all_summary'] = $totalCounts['chiba_summary'] + $totalCounts['tokyo_summary'];
}
// dd($totalCounts);
return $totalCounts;
}
//전체 검색어에 따른 서버정보를 검색 후 해당하는 서비스리스트를 가져온다.
final public function getSearchServices(string $keyword): array
{
$builder = $this->model->distinct()->select('serverinfo.serviceinfo_uid AS serviceinfo_uid')
->join('clientinfo', 'clientinfo.uid = serverinfo.clientinfo_uid')
->join('serverpartinfo', 'serverpartinfo.clientinfo_uid = clientinfo.uid', 'left')
->groupStart()
->like('clientinfo.name', $keyword, 'both', null, true) // escape=true
->orLike('serverinfo.code', $keyword, 'both', null, true)
->orLike('serverinfo.ip', $keyword, 'both', null, true)
->orLike('serverinfo.title', $keyword, 'both', null, true)
->orLike('serverpartinfo.title', $keyword, 'both', null, true)
->groupEnd()
->builder();
// echo $builder->getCompiledSelect(false); //초기화 없이 SQL만 보고 싶을 때: getCompiledSelect(false) ← 꼭 false!
$rows = $builder->get()->getResult();
if (!count($rows)) {
return [];
}
return $rows;
}
//서버 Title별 카운트수
final public function getStockCount(): array
{
$builder = $this->model->select('title,COUNT(*) AS cnt')->groupBy('title')->builder();
// echo $builder->getCompiledSelect(false); //초기화 없이 SQL만 보고 싶을 때: getCompiledSelect(false) ← 꼭 false!
// dd($builder->get()->getResult());
$rows = [];
foreach ($builder->get()->getResult() as $row) {
$rows[$row->title] = $row->cnt;
}
return $rows;
}
//총서버금액
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();
//해당 서비스(서버) 관련 결제방식(Billing)이 Month인 ServerPart찾아서 월청구액에 합산한다.
foreach ($serverPartService->getEntities(['serverinfo_uid' => $entity->getPK(), 'billing' => PAYMENT['BILLING']['MONTH']]) as $serverPartEntity) {
$caculatedAmount += $serverPartEntity->getCalculatedAmount(); //단가*Cnt
}
return $caculatedAmount;
}
//기본 기능부분
protected function getEntity_process(mixed $entity): ServerEntity
{
return $entity;
}
protected function action_process_fieldhook(string $field, $value, array $formDatas): array
{
switch ($field) {
default:
$formDatas = parent::action_process_fieldhook($field, $value, $formDatas);
break;
}
return $formDatas;
}
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만 가능");
}
//새로운 IP 추가 (IP가 정의 되어 있으면)
if ($entity->getIP()) {
service('part_ipservice')->attachToServer($entity);
}
//새로운 Switch 추가 (Switch가 정의 되어 있으면)
if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->attachToServer($entity);
}
//새로운 Chassis 추가 (Chassis가 정의 되어 있으면)
service('equipment_chassisservice')->attachToServer($entity);
//새로운 ServerPart 추가 (ServerPart가 정의 되어 있으면)
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만 가능");
}
//IP변경
if ($oldEntity->getIP() !== $entity->getIP()) {
//기존 IP 제거
service('part_ipservice')->detachFromServer($oldEntity);
//새로운 IP 추가 (IP가 정의 되어 있으면)
if ($entity->getIP()) {
service('part_ipservice')->attachToServer($entity);
}
}
//SWITCH변경
if ($oldEntity->getSwitchInfoUid() !== $entity->getSwitchInfoUid()) {
service('part_switchservice')->detachFromServer($oldEntity);
//새로운 Switch 추가 (Switch가 정의 되어 있으면)
if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->attachToServer($entity);
}
}
//샷시변경
if ($oldEntity->getChassisInfoUid() !== $entity->getChassisInfoUid()) {
service('equipment_chassisservice')->detachFromServer($oldEntity);
//새로운 Chassis 추가 (Chassis가 정의 되어 있으면)
if ($entity->getChassisInfoUid()) {
service('equipment_chassisservice')->attachToServer($entity);
}
}
//서비스변경
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')->updateAmount($serviceEntity);
}
return $entity;
}
//List 검색용
//FormFilter 조건절 처리
//검색어조건절처리
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);
}
//OrderBy 처리
//서비스관련
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);
}
public function modifyByService(ServiceENtity $oldServiceEntity, ServiceEntity $serviceEntity): void
{
//기존 메인서버 정보
$oldEntity = $this->getEntity($oldServiceEntity->getServerInfoUid());
if (!$oldEntity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$oldServiceEntity->getServerInfoUid()}에 해당하는 서비스의 기존 메인 서버정보을 찾을수 없습니다.");
}
//신규 메인서버 정보
$entity = $this->getEntity($serviceEntity->getServerInfoUid());
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$serviceEntity->getServerInfoUid()}에 해당하는 서비스의 신규 메인 서버정보을 찾을수 없습니다.");
}
//신규 메인서버는 기존 메인서버의 서버타입을 전달받아 설정 메인서버로 추가
$this->attatchToService($serviceEntity, $entity->getPK(), ['type' => $oldEntity->getType()]);
//기존서버는 대체서버로 변경 대체서버로 유지시킴
parent::modify_process($oldEntity, ['type' => 'alternative']);
}
//해지처리
public function deatchFromService($uid, array $formDatas = []): void
{
$entity = $this->getEntity($uid);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 서버정보을 찾을수 없습니다.");
}
//서버파트정보 해제
if ($entity->getIP()) {
service('part_ipservice')->detachFromServer($entity);
}
if ($entity->getSwitchInfoUid()) {
service('part_switchservice')->detachFromServer($entity);
}
service('equipment_serverpartservice')->detachFromServer($entity);
//서버정보 초기화
$formDatas['serviceinfo_uid'] = null;
$formDatas["clientinfo_uid"] = null;
$formDatas["switchinfo_uid"] = null;
$formDatas["ip"] = null;
$formDatas["switchinfo_uid"] = null;
$formDatas['status'] = $formDatas['status'] ?? STATUS['AVAILABLE'];
parent::modify_process($entity, $formDatas);
// throw new RuntimeException(var_export($formDatas, true) . var_export($entity, true));
}
}