dbmsv3/app/Services/Equipment/ServerService.php
2025-10-31 12:42:55 +09:00

294 lines
11 KiB
PHP

<?php
namespace App\Services\Equipment;
use App\Entities\Customer\ServiceEntity;
use App\Entities\Equipment\ServerEntity;
use App\Helpers\Equipment\ServerHelper;
use App\Interfaces\Equipment\ServerInterface;
use App\Models\Equipment\ServerModel;
use App\Services\Customer\ServiceService;
use App\Services\Equipment\EquipmentService;
use App\Services\Equipment\ServerPartService;
use App\Services\Part\IPService;
use App\Services\Part\SWITCHService;
use App\Processors\Equipment\ServerV1Processor as Proceessor;
class ServerService extends EquipmentService implements ServerInterface
{
private ?ServiceService $_serviceService = null;
private ?ServerPartService $_serverPartService = null;
private ?IPService $_ipService = null;
private ?SWITCHService $_switchService = null;
public function __construct()
{
parent::__construct(new ServerModel(), new ServerHelper());
$this->addClassName('Server');
}
public function getFormFields(): array
{
return [
"code",
"type",
"switch",
"ip",
"os",
"title",
"price",
"manufactur_at",
];
}
public function getFormFilters(): array
{
return [
"type",
"title",
"os",
"status"
];
}
public function getIndexFields(): array
{
return [
'clientinfo_uid',
'serviceinfo_uid',
"type",
"switch",
"ip",
"os",
'title',
'price',
'manufactur_at',
"format_at",
'status',
];
}
public function getIndexFilters(): array
{
return [
'clientinfo_uid',
'serviceinfo_uid',
'title',
'type',
"os",
'status'
];
}
public function getBatchjobFields(): array
{
return ['clientinfo_uid', 'status'];
}
final public function getServiceService(): ServiceService
{
if ($this->_serviceService === null) {
$this->_serviceService = new ServiceService();
}
return $this->_serviceService;
}
final public function getIPService(): IPService
{
if (!$this->_ipService) {
$this->_ipService = new IPService();
}
return $this->_ipService;
}
final public function getSWITCHService(): SWITCHService
{
if (!$this->_switchService) {
$this->_switchService = new SWITCHService();
}
return $this->_switchService;
}
final public function getServerPartService(): ServerPartService
{
if (!$this->_serverPartService) {
$this->_serverPartService = new ServerPartService();
}
return $this->_serverPartService;
}
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->getModel()->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->getModel()->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->getModel()->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(ServerEntity $entity): int
{
$caculatedAmount = $entity->getPrice();
//해당 서비스(서버) 관련 결제방식(Billing)이 Month인 ServerPart 전체를 다시 검사하여 월청구액을 합산한다.
foreach ($this->getServerPartService()->getEntities(['serverinfo_uid' => $entity->getPK()]) as $serverPartEntity) {
if ($serverPartEntity->getBilling() === PAYMENT['BILLING']['MONTH']) { //월비용일때만 적용
$caculatedAmount += $serverPartEntity->getTotalAmount(); //단가*Cnt
}
}
return $caculatedAmount;
}
//결제관련처리
public function setAmount(ServerEntity $entity): ServerEntity
{
if ($entity->getServiceInfoUID() === null) {
throw new \Exception(__METHOD__ . "에서 오류발생: 서비스정보가 정의된 후에만 가능합니다.");
}
//서비스정보 반영
$serviceEntity = $this->getServiceService()->getEntity($entity->getServiceInfoUID());
if (!$serviceEntity instanceof ServiceEntity) {
throw new \Exception(__METHOD__ . "에서 오류발생: {$entity->getServiceInfoUID()} 서비스 정보를 찾을수 없습니다.");
}
$this->getServiceService()->setAmount($serviceEntity, $this->getCalculatedAmount($entity));
return $entity;
}
//Service관련
public function attachToService(ServiceEntity $serviceEntity, $serverinfo_uid, array $formDatas = []): ServerEntity
{
$entity = $this->getEntity($serverinfo_uid);
if (!$entity instanceof ServerEntity) {
throw new \Exception("[{$serverinfo_uid}]에 대한 서버정보를 찾을 수 없습니다.");
}
$formDatas['clientinfo_uid'] = $serviceEntity->getClientInfoUID();
$formDatas['serviceinfo_uid'] = $serviceEntity->getPK();
$formDatas['status'] = STATUS['OCCUPIED'];
return $this->getModel()->modify($entity, $formDatas);
}
public function detachFromService($serverinfo_uid): ServerEntity
{
$entity = $this->getEntity($serverinfo_uid);
if (!$entity instanceof ServerEntity) {
throw new \Exception("[{$serverinfo_uid}]에 대한 서버정보를 찾을 수 없습니다.");
}
$entity = $this->getModel()->modify($entity, [
'clientinfo_uid' => null,
'serviceinfo_uid' => null,
'format_at' => date("Y-m-d"),
'status' => STATUS['AVAILABLE'],
]);
//서버파트 삭제(기본제외)
$this->getServerPartService()->detachFromServer($entity);
return $entity;
}
//기본 기능부분
//FieldForm관련용
public function getFormOption(string $field, $options = []): array
{
switch ($field) {
case 'serviceinfo_uid':
$options = $this->getServiceService()->getEntities();
break;
default:
$options = parent::getFormOption($field, $options);
break;
}
return $options;
}
public function create(array $formDatas): ServerEntity
{
$processor = new Proceessor(
\Config\Database::connect(),
$this,
$this->getServiceService(),
$this->getServerPartService(),
$this->getIPService(),
$this->getSWITCHService(),
$this->getMylogService()
);
return $processor->create($formDatas);
}
//수정
public function modify(mixed $entity, array $formDatas): ServerEntity
{
$processor = new Proceessor(
\Config\Database::connect(),
$this,
$this->getServiceService(),
$this->getServerPartService(),
$this->getIPService(),
$this->getSWITCHService(),
$this->getMylogService()
);
return $processor->modify($entity, $formDatas);
}
//삭제
public function delete(mixed $entity): ServerEntity
{
$processor = new Proceessor(
\Config\Database::connect(),
$this,
$this->getServiceService(),
$this->getServerPartService(),
$this->getIPService(),
$this->getSWITCHService(),
$this->getMylogService()
);
return $processor->delete($entity);
}
//List 검색용
//OrderBy 처리
public function setOrderBy(mixed $field = null, mixed $value = null): void
{
$this->getModel()->orderBy("code ASC,title ASC");
parent::setOrderBy($field, $value);
}
}