addClassName('Server'); } public function getFormFields(): array { return [ "code", "type", "title", "price", "manufactur_at", ]; } public function getFormFilters(): array { return [ "type", "title", "status" ]; } public function getIndexFields(): array { return [ 'clientinfo_uid', 'serviceinfo_uid', "type", 'title', 'price', 'manufactur_at', "format_at", 'status', ]; } public function getIndexFilters(): array { return [ 'clientinfo_uid', 'serviceinfo_uid', 'type', "title", '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 getServerPartService(): ServerPartService { if (!$this->_serverPartService) { $this->_serverPartService = new ServerPartService(); } return $this->_serverPartService; } //partEntity 정보 추가 protected function getEntity_process(mixed $entity): ServerEntity { if (!$entity instanceof ServerEntity) { throw new \Exception(__METHOD__ . "에서 형식오류:ServerEntity만 허용됩니다."); } return $entity; } 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], 'vpn' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], 'event' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], 'test' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], 'alternative' => ['chiba' => 0, 'tokyo' => 0, 'summary' => 0], ]; $rows = $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') // JOIN 조건 ->where($where) ->groupBy('serverinfo.type') ->get() ->getResult(); foreach ($rows 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; } //기본 기능부분 //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 setFormData(string $field, array $requestDatas, array $formDatas): array { switch ($field) { case 'CPU': case 'RAM': $formDatas[$field] = $requestDatas[$field] ?? null; $formDatas["{$field}_cnt"] = $requestDatas["{$field}_cnt"] ?? null; break; case 'DISK': $formDatas[$field] = $requestDatas[$field] ?? null; $formDatas["{$field}_cnt"] = $requestDatas["{$field}_cnt"] ?? null; $formDatas["{$field}_extra"] = $requestDatas["{$field}_extra"] ?? null; break; default: $formDatas = parent::setFormData($field, $requestDatas, $formDatas); } return $formDatas; } public function create(array $formDatas): ServerEntity { $entity = parent::create($formDatas); return $entity; } //수정 public function modify(mixed $entity, array $formDatas): ServerEntity { //수정전 정보 $oldEntity = $entity; //서버정보 수정 $entity = parent::modify($entity, $formDatas); //서비스가 연결되어 있고 대체서버가 아니면, 서비스정보수정(청구액수정) if ($entity->getServiceInfoUID() !== null && $entity->getType() !== "alternative") { $entity = $this->getServiceService()->changeServer($oldEntity, $entity, []); } $entity = $this->getServerPartService()->changeServer($oldEntity, $entity, []); return $entity; } //삭제 public function delete(mixed $entity): ServerEntity { //서비스중인지 확인 if ($entity->getServiceInfoUID() !== null || $entity->getStatus() === STATUS['OCCUPIED']) { throw new \Exception("서비스중이 서버는 삭제하실수 없습니다."); } //선처리작업 $entity = $this->getServerPartService()->unsetServer($entity, []);; return parent::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); } //서비스관련 작업 public function setService(ServiceEntity $serviceEntity, array $serviceDatas): ServiceEntity { if (!array_key_exists('serverinfo_uid', $serviceDatas)) { throw new \Exception("서버정보가 없습니다."); } $entity = $this->getEntity($serviceDatas['serverinfo_uid']); if (!$entity instanceof ServerEntity) { throw new \Exception("{$serviceDatas['serverinfo_uid']}에 대한 서버정보를 찾을수 없습니다."); } $entity = $this->getEntity($serviceDatas['serverinfo_uid']); if ($entity->getStatus() != STATUS['AVAILABLE']) { throw new \Exception("{$entity->getCustomTitle()} 서버는 사용가능 서버가 아닙니다."); } $formDatas = []; $formDatas['clientinfo_uid'] = $serviceEntity->getClientInfoUID(); $formDatas['serviceinfo_uid'] = $serviceEntity->getPK(); $formDatas['status'] = STATUS['OCCUPIED']; //대체서버용인지 확인 if (array_key_exists('type', $serviceDatas) && $serviceDatas['type'] === 'alternative') { $formDatas['type'] = $serviceDatas['type']; } //필수정보처리 후 서버정보등록 후 서비스정보 Entity에 서버정보 설정 return $serviceEntity->setServerEntity(parent::modify($entity, $formDatas)); } public function changeService(ServiceEntity $oldServiceEntity, ServiceEntity $serviceEntity, array $serviceDatas): ServiceEntity { if (!array_key_exists('serverinfo_uid', $serviceDatas)) { throw new \Exception("신규로 지정할 서버정보가 없습니다."); } //기존 서버 해지(uynsetService사용) $this->unsetService($serviceEntity, ['serverinfo_uid' => $serviceEntity->getServerEntity()->getPK()]); //신규서버 설정(setService사용) return $this->setService($serviceEntity, $serviceDatas); } public function unsetService(ServiceEntity $serviceEntity, array $serviceDatas): ServiceEntity { if (!array_key_exists('serverinfo_uid', $serviceDatas)) { throw new \Exception("서비스를 해지할 서버정보가 없습니다."); } if ($serviceEntity->getServerEntity()->getPK() === $serviceDatas['serverinfo_uid']) { throw new \Exception("Main으로 설정된 서버는 해지하실 수 없습니다."); } $entity = $this->getEntity($serviceDatas['serverinfo_uid']); if (!$entity instanceof ServerEntity) { throw new \Exception("{$serviceDatas['serverinfo_uid']}에 대한 서버정보를 찾을수 없습니다."); } //서버정보설정 $formDatas = []; $formDatas['clientinfo_uid'] = null; $formDatas['serviceinfo_uid'] = null; $formDatas['format_at'] = date("Y-m-d"); $formDatas['status'] = STATUS['AVAILABLE']; $entity = parent::modify($entity, $formDatas); //서버파트정보 회수처리 호출 $this->getServerPartService()->unsetServer($entity, []); return $serviceEntity; } //서버파트관련 작업 }