dbmsv4/app/Services/Equipment/ServerPartService.php
2025-11-26 14:52:50 +09:00

182 lines
7.0 KiB
PHP

<?php
namespace App\Services\Equipment;
use App\DTOs\Equipment\ServerPartDTO;
use App\Entities\Equipment\ServerEntity;
use App\Entities\Equipment\ServerPartEntity;
use App\Entities\Part\PartEntity;
use App\Forms\Equipment\ServerPartForm;
use App\Helpers\Equipment\ServerPartHelper;
use App\Models\Equipment\ServerPartModel;
use App\Services\Part\PartService;
use RuntimeException;
class ServerPartService extends EquipmentService
{
private $_form = null;
private $_helper = null;
public function __construct(ServerPartModel $model)
{
parent::__construct($model);
$this->addClassPaths('ServerPart');
}
protected function getDTOClass(): string
{
return ServerPartDTO::class;
}
public function createDTO(array $formDatas): ServerPartDTO
{
return new ServerPartDTO($formDatas);
}
public function getEntityClass(): string
{
return ServerPartEntity::class;
}
public function getFormService(): ServerPartForm
{
if ($this->_form === null) {
$this->_form = new ServerPartForm();
$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(): ServerPartHelper
{
if ($this->_helper === null) {
$this->_helper = new ServerPartHelper();
$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 = [
"serverinfo_uid",
"type",
"billing",
"part_uid",
"cnt",
"extra",
"amount",
];
$filters = [
"serverinfo_uid",
"type",
"part_uid",
"billing",
];
$indexFilter = [
"serverinfo_uid",
"type",
"billing",
];
$batchjobFilters = ['billing', 'type'];
switch ($action) {
case 'create':
case 'create_form':
case 'modify':
case 'modify_form':
break;
case 'view':
$fields = [...$fields, 'created_at'];
break;
case 'index':
case 'download':
$fields = [...$fields, 'created_at'];
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);
}
//각 파트별 서비스
private function getPartService(string $type): PartService
{
return service('part_' . strtolower($type) . 'service');
}
//기본 기능부분
protected function getEntity_process(mixed $entity): ServerPartEntity
{
return $entity;
}
protected function create_process(array $formDatas): ServerPartEntity
{
if (!array_key_exists('type', $formDatas)) {
throw new \Exception(__METHOD__ . "에서 오류발생:부품형식이 지정되지 않았습니다.");
}
//각 파트정보 가져오기
$partEntity = $this->getPartService($formDatas['type'])->getEntity($formDatas['part_uid']);
//서버파트정보 Title 설정용
$formDatas['title'] = $partEntity->getTitle();
$entity = parent::create_process($formDatas);
if (!$entity instanceof ServerPartEntity) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 ServerPartEntity만 가능");
}
return $entity;
}
protected function modify_process($uid, array $formDatas): ServerPartEntity
{
if (!array_key_exists('type', $formDatas)) {
throw new \Exception(__METHOD__ . "에서 오류발생:부품형식이 지정되지 않았습니다.");
}
//각 파트정보 가져오기
$partEntity = $this->getPartService($formDatas['type'])->getEntity($formDatas['part_uid']);
//서버파트정보 Title 설정용
$formDatas['title'] = $partEntity->getTitle();
$entity = parent::modify_process($uid, $formDatas);
if (!$entity instanceof ServerPartEntity) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 ServerPartEntity만 가능");
}
return $entity;
}
//List 검색용
//FormFilter 조건절 처리
//검색어조건절처리
//서버추가시 서버파트 자동추가용
public function attachToServer(ServerEntity $serverEntity): void
{
//*서버의 Title 대소문자구분 필요->서버의 Title로 구분해서 기본부품 추가
foreach (SERVERPART['SERVER_PARTTYPES'] as $parttype) {
//해당 server_type의 정의된 상수값이 있으면
if (array_key_exists($serverEntity->getTitle(), SERVERPART[$parttype])) {
foreach (SERVERPART[$parttype][$serverEntity->getTitle()] as $part) {
//해당 파트정보 가져오기
$partEntity = $this->getPartService($parttype)->getEntity($part['UID']);
if (!$partEntity instanceof PartEntity) {
throw new \Exception(__METHOD__ . "에서 오류발생: {$part['UID']} 서버 정보를 찾을수 없습니다.");
}
//서버파트정보 생성
$formDatas = [];
$formDatas['serverinfo_uid'] = $serverEntity->getPK();
$formDatas["part_uid"] = $partEntity->getPK();
$formDatas['billing'] = PAYMENT['BILLING']['BASE'];
$formDatas['type'] = $parttype;
$formDatas['title'] = $partEntity->getTitle(); //파트 제목
$formDatas['amount'] = $partEntity->getPrice(); //파트 금액
$formDatas['cnt'] = $part["CNT"];
$formDatas['extra'] = $part["EXTRA"];
$entity = $this->model->create($formDatas);
$this->getPartService($entity->getType())->attachToServerPart($entity);
}
}
}
}
}