dbmsv4/app/Services/Equipment/ServerService.php
2025-12-01 19:24:35 +09:00

271 lines
11 KiB
PHP

<?php
namespace App\Services\Equipment;
use RuntimeException;
use App\Models\Equipment\ServerModel;
use App\Helpers\Equipment\ServerHelper;
use App\Forms\Equipment\ServerForm;
use App\Entities\Equipment\ServerEntity;
use App\Entities\Customer\ServiceEntity;
use App\Entities\CommonEntity;
use App\DTOs\Equipment\ServerDTO;
class ServerService extends EquipmentService
{
private $_form = null;
private $_helper = null;
public function __construct(ServerModel $model)
{
parent::__construct($model);
$this->addClassPaths('Server');
}
protected function getDTOClass(): string
{
return ServerDTO::class;
}
public function createDTO(array $formDatas): ServerDTO
{
return new ServerDTO($formDatas);
}
public function getEntityClass(): string
{
return ServerEntity::class;
}
public function getFormService(): ServerForm
{
if ($this->_form === null) {
$this->_form = new ServerForm();
$this->_form->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false)
]);
}
return $this->_form;
}
public function getHelper(): ServerHelper
{
if ($this->_helper === null) {
$this->_helper = new ServerHelper();
$this->_helper->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false)
]);
}
return $this->_helper;
}
public function action_init_process(string $action, array $formDatas = []): void
{
$fields = [
"code",
"type",
"switchinfo_uid",
"ip",
"title",
"os",
"price",
"manufactur_at",
"format_at",
];
$filters = [
"clientinfo_uid",
'title',
'type',
'switchinfo_uid',
'ip',
'os',
"status",
];
$indexFilter = $filters;
$batchjobFilters = ['type', 'title', 'switchinfo_uid', 'ip', 'os', 'status'];
switch ($action) {
case 'create':
case 'create_form':
case 'modify':
case 'modify_form':
$fields = [...$fields, 'status'];
break;
case 'view':
$fields = ['clientinfo_uid', ...$fields, 'status', 'created_at'];
break;
case 'index':
case 'download':
$fields = [
'clientinfo_uid',
"code",
"type",
"switchinfo_uid",
"ip",
"title",
"os",
"part",
"price",
"manufactur_at",
"format_at",
'status',
];
break;
}
$this->getFormService()->setFormFields($fields);
$this->getFormService()->setFormRules($action, $fields);
$this->getFormService()->setFormFilters($filters);
$this->getFormService()->setFormOptions($action, $filters, $formDatas);
$this->getFormService()->setIndexFilters($indexFilter);
$this->getFormService()->setBatchjobFilters($batchjobFilters);
}
//기본 기능부분
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(__METHOD__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
}
//서버추가시 서버파트 자동추가용
service('part_ipservice')->attachToServer($entity);
service('part_switchservice')->attachToServer($entity);
service('equipment_serverpartservice')->attachToServer($entity);
return $entity;
}
protected function modify_process($entity, array $formDatas): ServerEntity
{
$entity = parent::modify_process($entity, $formDatas);
if (!$entity instanceof ServerEntity) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 ServerEntity만 가능");
}
return $entity;
}
//List 검색용
//FormFilter 조건절 처리
//검색어조건절처리
//추가기능
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;
}
//총서버금액
public function getCalculatedAmount(int $uid): int
{
$entity = $this->getEntity($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;
}
//List 검색용
//FormFilter 조건절 처리
//검색어조건절처리
public function setSearchWord(string $word): void
{
$this->model->orLike($this->model->getTable() . '.ip', $word, 'both');
parent::setSearchWord($word);
}
//OrderBy 처리
//서비스관련
public function attachToService(ServiceEntity $serviceEntity, array $formDatas = []): void
{
//서버정보 가져오기
$entity = $this->getEntity($serviceEntity->getServerInfoUID());
$formDatas['serviceinfo_uid'] = $serviceEntity->getPK();
$formDatas["clientinfo_uid"] = $serviceEntity->getClientInfoUID();
$formDatas['status'] = $formDatas['status'] ?? STATUS['OCCUPIED'];
$fields = array_keys($formDatas);
$this->getFormService()->setFormFields($fields);
$this->getFormService()->setFormRules('modify', $fields);
parent::modify_process($entity, $formDatas);
}
public function detachFromService(ServiceEntity $serviceEntity, array $formDatas = []): void
{
//서버정보 가져오기
$entity = $this->getEntity($serviceEntity->getServerInfoUID());
$formDatas['serviceinfo_uid'] = NULL;
$formDatas["clientinfo_uid"] = NULL;
$formDatas['status'] = $formDatas['status'] ?? STATUS['AVAILABLE'];
$fields = array_keys($formDatas);
$this->getFormService()->setFormFields($fields);
$this->getFormService()->setFormRules('modify', $fields);
parent::modify_process($entity, $formDatas);
//서버파트정보처리
service('part_ipservice')->detachFromService($entity);
service('part_switchservice')->detachFromService($entity);
service('equipment_serverpartservice')->detachFromService($entity);
}
}