247 lines
8.3 KiB
PHP
247 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Customer;
|
|
|
|
use App\Traits\IPTrait;
|
|
use App\Services\Equipment\IPService;
|
|
use App\Services\Equipment\CSService;
|
|
use App\Models\Customer\ServiceModel;
|
|
use App\Entities\Customer\ServiceEntity;
|
|
|
|
class ServiceService extends CustomerService
|
|
{
|
|
use IPTrait;
|
|
private ?IPService $_ipService = null;
|
|
private ?CSService $_csService = null;
|
|
public function __construct()
|
|
{
|
|
parent::__construct(new ServiceModel());
|
|
$this->addClassName('Service');
|
|
}
|
|
public function getFormFields(): array
|
|
{
|
|
return [
|
|
"site",
|
|
"location",
|
|
"type",
|
|
"clientinfo_uid",
|
|
"switchinfo_uid",
|
|
"serverinfo_uid",
|
|
"ipinfo_uid",
|
|
"csinfo_uid",
|
|
"billing_at",
|
|
"amount",
|
|
"start_at",
|
|
"status",
|
|
"history",
|
|
];
|
|
}
|
|
public function getFormFilters(): array
|
|
{
|
|
return [
|
|
'site',
|
|
'location',
|
|
'type',
|
|
'clientinfo_uid',
|
|
"switchinfo_uid",
|
|
'serverinfo_uid',
|
|
"ipinfo_uid",
|
|
"csinfo_uid",
|
|
'status',
|
|
];
|
|
}
|
|
public function getIndexFields(): array
|
|
{
|
|
return [
|
|
'site',
|
|
'location',
|
|
'type',
|
|
'clientinfo_uid',
|
|
"switchinfo_uid",
|
|
'serverinfo_uid',
|
|
'billing_at',
|
|
'amount',
|
|
'status',
|
|
'user_uid',
|
|
'start_at',
|
|
'updated_at',
|
|
];
|
|
}
|
|
public function getIndexFilters(): array
|
|
{
|
|
return [
|
|
'site',
|
|
'location',
|
|
'type',
|
|
'clientinfo_uid',
|
|
"switchinfo_uid",
|
|
'serverinfo_uid',
|
|
'user_uid',
|
|
'status',
|
|
];
|
|
}
|
|
public function getBatchjobFields(): array
|
|
{
|
|
return ['site', 'location', 'type', 'clientinfo_uid', 'status'];
|
|
}
|
|
public function getFormRule(string $action, string $field): string
|
|
{
|
|
if (is_array($field)) {
|
|
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
|
|
}
|
|
switch ($field) {
|
|
case "serverinfo_uid":
|
|
case "switchinfo_uid":
|
|
$rule = "required|numeric";
|
|
break;;
|
|
default:
|
|
$rule = parent::getFormRule($action, $field);
|
|
break;
|
|
}
|
|
return $rule;
|
|
}
|
|
protected function getEntity_process(mixed $entity): ServiceEntity
|
|
{
|
|
//서버정보 정의
|
|
$serverEntityy = $this->getServerService()->getEntity(['serviceinfo_uid' => $entity->getPK()]);
|
|
if ($serverEntityy !== null) {
|
|
$entity->setServerEntity($serverEntityy);
|
|
}
|
|
return $entity;
|
|
}
|
|
final public function getIPService(): IPService
|
|
{
|
|
if (!$this->_ipService) {
|
|
$this->_ipService = new IPService();
|
|
}
|
|
return $this->_ipService;
|
|
}
|
|
final public function getCSService(): CSService
|
|
{
|
|
if (!$this->_csService) {
|
|
$this->_csService = new CSService();
|
|
}
|
|
return $this->_csService;
|
|
}
|
|
//기본 기능부분
|
|
public function getFormOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
case 'ipinfo_uid': //수정때문에 전체가 필요
|
|
$options = $this->getIPService()->getEntities();
|
|
break;
|
|
case 'csinfo_uid': //수정때문에 전체가 필요
|
|
$options = $this->getCSService()->getEntities();
|
|
break;
|
|
default:
|
|
$options = parent::getFormOption($field, $options);
|
|
break;
|
|
}
|
|
return $options;
|
|
}
|
|
//FieldForm관련용
|
|
//interval을 기준으로 최근 신규 서비스정보 가져오기
|
|
final public function getEntitiesByNewService(int $interval, string $status = ServiceEntity::DEFAULT_STATUS): array
|
|
{
|
|
return $this->getEntities(sprintf("start_at >= NOW()-INTERVAL {$interval} DAY AND status = '%s'", $status));
|
|
}
|
|
//서비스 방식에 따른 서비스별 Count
|
|
final public function getTotalCountsByType(): array
|
|
{
|
|
$totalCounts = [
|
|
'chiba_total' => 0,
|
|
'tokyo_total' => 0,
|
|
'all_total' => 0,
|
|
'normal' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
'defence' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
'dedicated' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
'alternative' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
'test' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
];
|
|
$sql = "SELECT type,
|
|
COUNT(CASE WHEN location = 'chiba' THEN 1 END) AS chiba,
|
|
COUNT(CASE WHEN location = 'tokyo' THEN 1 END) AS tokyo,
|
|
COUNT(CASE WHEN location IN ('chiba', 'tokyo') THEN 1 END) AS total
|
|
FROM serviceinfo GROUP BY type";
|
|
foreach ($this->getModel()->query($sql)->getResult() as $row) {
|
|
$totalCounts[$row->type] = [
|
|
'chiba' => $row->chiba,
|
|
'tokyo' => $row->tokyo,
|
|
'total' => $row->total,
|
|
];
|
|
$totalCounts['chiba_total'] += $row->chiba;
|
|
$totalCounts['tokyo_total'] += $row->tokyo;
|
|
}
|
|
$totalCounts['all_total'] = $totalCounts['chiba_total'] + $totalCounts['tokyo_total'];
|
|
return $totalCounts;
|
|
}
|
|
//다음 달로 결제일을 연장합니다.
|
|
final public function extendBillingAt(string $billing_at, string $status): bool
|
|
{
|
|
$sql = "UPDATE serviceinfo SET billing_at =
|
|
IF(DAY(billing_at) > DAY(LAST_DAY(billing_at)),
|
|
LAST_DAY(DATE_ADD(billing_at, INTERVAL 1 MONTH)),
|
|
DATE_ADD(billing_at, INTERVAL 1 MONTH)
|
|
) WHERE billing_at = ? AND status = ?";
|
|
return $this->getModel()->query($sql, [$billing_at, $status]);
|
|
}
|
|
|
|
//다른장치 설정용
|
|
private function disableService(ServiceEntity $entity): ServiceEntity
|
|
{
|
|
//기존 Server정보와 다른경우 사용가능상태로 변경
|
|
$serverEntity = $entity->getServerEntity();
|
|
if ($serverEntity !== null) {
|
|
$serverEntity = $this->getServerService()->disableService($serverEntity);
|
|
$entity->setServerEntity(null);
|
|
}
|
|
return $entity;
|
|
}
|
|
private function enableService(ServiceEntity $entity, array $formDatas): ServiceEntity
|
|
{
|
|
//서버경우 서비스중으로 설정
|
|
$serverEntity = $this->getServerService()->getEntity($formDatas['serverinfo_uid']);
|
|
if (!$serverEntity) {
|
|
throw new \Exception("{$formDatas['serverinfo_uid']}에 대한 서버정보를 찾을수 없습니다.");
|
|
}
|
|
$serverEntity = $this->getServerService()->enableService($entity, $serverEntity, $formDatas);
|
|
$entity->setServerEntity($serverEntity);
|
|
return $entity;
|
|
}
|
|
//생성
|
|
public function create(array $formDatas): ServiceEntity
|
|
{
|
|
$entity = parent::create($formDatas);
|
|
if (!array_key_exists('serverinfo_uid', $formDatas)) {
|
|
throw new \Exception("서버가 지정되지 않았습니다.");
|
|
}
|
|
if (!array_key_exists('switchinfo_uid', $formDatas)) {
|
|
throw new \Exception("스위치정보가 지정되지 않았습니다.");
|
|
}
|
|
//신규정보 Enable
|
|
return $this->enableService($entity, $formDatas);
|
|
}
|
|
//수정
|
|
public function modify(mixed $entity, array $formDatas): ServiceEntity
|
|
{
|
|
if (!array_key_exists('serverinfo_uid', $formDatas)) {
|
|
throw new \Exception("서버가 지정되지 않았습니다.");
|
|
}
|
|
if (!array_key_exists('switchinfo_uid', $formDatas)) {
|
|
throw new \Exception("스위치정보가 지정되지 않았습니다.");
|
|
}
|
|
//기존정보 Disable
|
|
$entity = $this->disableService($entity);
|
|
//신규정보 Enable
|
|
$this->enableService($entity, $formDatas);
|
|
return parent::modify($entity, $formDatas);
|
|
}
|
|
//삭제
|
|
public function delete(mixed $entity): ServiceEntity
|
|
{
|
|
//기존정보 Disable
|
|
$entity = $this->disableService($entity);
|
|
return parent::delete($entity);
|
|
}
|
|
}
|