275 lines
11 KiB
PHP
275 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Customer;
|
|
|
|
use App\Entities\Customer\ClientEntity;
|
|
use App\Entities\Customer\PaymentEntity;
|
|
use App\Entities\Customer\ServiceEntity;
|
|
use App\Entities\Equipment\ServerEntity;
|
|
use App\Entities\UserEntity;
|
|
use App\Helpers\Customer\ServiceHelper;
|
|
use App\Models\Customer\ServiceModel;
|
|
use App\Services\Equipment\ServerService;
|
|
use App\Traits\IPTrait;
|
|
|
|
class ServiceService extends CustomerService
|
|
{
|
|
use IPTrait;
|
|
private ?ServerService $_serverService = null;
|
|
public function __construct()
|
|
{
|
|
parent::__construct(new ServiceModel(), new ServiceHelper());
|
|
$this->addClassName('Service');
|
|
}
|
|
public function getFormFields(): array
|
|
{
|
|
return [
|
|
"site",
|
|
"location",
|
|
"type",
|
|
"clientinfo_uid",
|
|
'serverinfo_uid',
|
|
"start_at",
|
|
"billing_at",
|
|
"amount",
|
|
"status",
|
|
"history",
|
|
];
|
|
}
|
|
public function getFormFilters(): array
|
|
{
|
|
return [
|
|
'site',
|
|
'location',
|
|
'type',
|
|
'clientinfo_uid',
|
|
'serverinfo_uid',
|
|
'status',
|
|
];
|
|
}
|
|
public function getIndexFields(): array
|
|
{
|
|
return [
|
|
'site',
|
|
'location',
|
|
'type',
|
|
'clientinfo_uid',
|
|
'serverinfo_uid',
|
|
'billing_at',
|
|
'amount',
|
|
'status',
|
|
'start_at',
|
|
'updated_at',
|
|
];
|
|
}
|
|
public function getIndexFilters(): array
|
|
{
|
|
return [
|
|
'site',
|
|
'location',
|
|
'type',
|
|
'clientinfo_uid',
|
|
'serverinfo_uid',
|
|
'user_uid', //home의 최신신규서버현황에서 사용
|
|
'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":
|
|
$rule = "required|numeric";
|
|
break;;
|
|
default:
|
|
$rule = parent::getFormRule($action, $field);
|
|
break;
|
|
}
|
|
return $rule;
|
|
}
|
|
protected function getEntity_process(mixed $entity): ServiceEntity
|
|
{
|
|
if (!($entity instanceof ServiceEntity)) {
|
|
throw new \Exception(__METHOD__ . "에서 형식오류:ServiceEntity만 허용됩니다.");
|
|
}
|
|
//관리자정보 정의
|
|
$userEntity = $this->getUserService()->getEntity($entity->getUserUID());
|
|
if (!($userEntity instanceof UserEntity)) {
|
|
throw new \Exception("{$entity->getUserUID()}에 해당하는 관리자정보를 찾을수 없습니다.");
|
|
}
|
|
$entity = $entity->setUserEntity($userEntity);
|
|
//고객정보 정의
|
|
$clientEntity = $this->getClientService()->getEntity($entity->getClientInfoUID());
|
|
if (!($clientEntity instanceof ClientEntity)) {
|
|
throw new \Exception("{$entity->getClientInfoUID()}에 해당하는 고객정보를 찾을수 없습니다.");
|
|
}
|
|
$entity = $entity->setUserEntity($userEntity);
|
|
//서버정보 정의
|
|
$serverEntity = $this->getServerService()->getEntity(['serviceinfo_uid' => $entity->getPK()]);
|
|
if (!($serverEntity instanceof ServerEntity)) {
|
|
$serverEntity = new ServerEntity();
|
|
}
|
|
return $entity->setServerEntity($serverEntity);
|
|
}
|
|
//기본 기능부분
|
|
public function getFormOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
case 'CPU':
|
|
case 'RAM':
|
|
case 'DISK':
|
|
case 'OS':
|
|
case 'DB':
|
|
case 'SOFTWARE':
|
|
case 'SWITCH':
|
|
case 'IP':
|
|
case 'CS':
|
|
$options = $this->getServerService()->getFormOption($field, $options);
|
|
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 $where = []): 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],
|
|
'vpn' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
'event' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
'test' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
'alternative' => ['chiba' => 0, 'tokyo' => 0, 'total' => 0],
|
|
];
|
|
$rows = $this->getModel()->groupBy('type')->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")
|
|
->where($where)
|
|
->get()->getResult();
|
|
foreach ($rows 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 getTotalAmounts($where = []): array
|
|
{
|
|
$rows = $this->getModel()->groupBy('clientinfo_uid')->select("clientinfo_uid,SUM(amount) AS amount")
|
|
->where($where)
|
|
->get()->getResult();
|
|
$amounts = [];
|
|
foreach ($rows as $row) {
|
|
$amounts[$row->clientinfo_uid] = $row->amount;
|
|
}
|
|
return $amounts;
|
|
}
|
|
//다음 달로 결제일을 연장합니다.
|
|
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]);
|
|
}
|
|
//기본기능
|
|
//생성
|
|
public function create(array $formDatas): ServiceEntity
|
|
{
|
|
if (!array_key_exists('serverinfo_uid', $formDatas)) {
|
|
throw new \Exception("서버가 지정되지 않았습니다.");
|
|
}
|
|
$entity = parent::create($formDatas);
|
|
$entity = $entity->setServerEntity($this->getServerService()->setService($entity, $formDatas['serverinfo_uid'], STATUS['OCCUPIED']));
|
|
// 결제정보에 추가한다.
|
|
$paymentDatas = [];
|
|
$paymentDatas['clientinfo_uid'] = $entity->getClientInfoUID();
|
|
$paymentDatas['serviceinfo_uid'] = $entity->getPK();
|
|
$paymentDatas['serverinfo_uid'] = $entity->getServerInfoUID();
|
|
$paymentDatas['title'] = sprintf("[%s] 서비스", $entity->getServerEntity()->getTitle());
|
|
$paymentDatas['amount'] = $entity->getAmount();
|
|
$paymentDatas['billing'] = PAYMENT['BILLING']['MONTH'];
|
|
$paymentDatas['billing_at'] = $entity->getBillingAt();
|
|
$this->getPaymentService()->create($paymentDatas);
|
|
return $entity;
|
|
}
|
|
//수정
|
|
public function modify(mixed $entity, array $formDatas): ServiceEntity
|
|
{
|
|
if (!array_key_exists('serverinfo_uid', $formDatas)) {
|
|
throw new \Exception("신규 서버가 지정되지 않았습니다.");
|
|
}
|
|
//기존서버정보 사용가능으로 설정
|
|
if ($entity->getServerInfoUID() && $entity->getServerInfoUID() !== $formDatas['serverinfo_uid']) {
|
|
$this->getServerService()->setService($entity, $entity->getServerEntity()->getPK(), STATUS['AVAILABLE']);
|
|
}
|
|
//서비스 정보수정
|
|
$entity = parent::modify($entity, $formDatas);
|
|
//신규서버정보 사용중으로 설정
|
|
if ($entity->getServerInfoUID() !== $formDatas['serverinfo_uid']) {
|
|
$entity->setServerEntity($this->getServerService()->setService($entity, $formDatas['serverinfo_uid'], STATUS['OCCUPIED']));
|
|
}
|
|
//수정된 정보중 서버,결제일,청구액이 바뀐경우 결제정보에서 미납인경우 수정해준다.
|
|
if (
|
|
$entity->getServerInfoUID() !== $formDatas['serverinfo_uid'] ||
|
|
$entity->getBillingAt() !== $formDatas['billing_at'] ||
|
|
$entity->getAmount() !== $formDatas['amount']
|
|
) {
|
|
$paymentEntity = $this->getPaymentService()->getEntity(['serviceinfo_uid' => $entity->getPK(), 'status' => STATUS['UNPAID']]);
|
|
if (!($paymentEntity instanceof PaymentEntity)) {
|
|
throw new \Exception("{$entity->getPK()}에 해당하는 미결제정보를 찾을수 없습니다.");
|
|
}
|
|
$paymentDatas = [];
|
|
$paymentDatas['clientinfo_uid'] = $entity->getClientInfoUID();
|
|
$paymentDatas['serviceinfo_uid'] = $entity->getPK();
|
|
$paymentDatas['serverinfo_uid'] = $entity->getServerInfoUID();
|
|
$paymentDatas['title'] = sprintf("[%s] 서비스", $entity->getServerEntity()->getTitle());
|
|
$paymentDatas['amount'] = $entity->getAmount();
|
|
$paymentDatas['billing'] = PAYMENT['BILLING']['MONTH'];
|
|
$paymentDatas['billing_at'] = $entity->getBillingAt();
|
|
$this->getPaymentService()->modify($paymentEntity, $paymentDatas);
|
|
}
|
|
return $entity;
|
|
}
|
|
//삭제
|
|
public function delete(mixed $entity): ServiceEntity
|
|
{
|
|
//기존서버정보 사용가능으로 설정
|
|
$this->getServerService()->setService($entity, $entity->getServerEntity()->getPK(), STATUS['AVAILABLE']);
|
|
return parent::delete($entity);
|
|
}
|
|
//History
|
|
public function history(mixed $entity, array $formDatas): ServiceEntity
|
|
{
|
|
//서비스 정보수정
|
|
return $this->getModel()->modify($entity, $formDatas);
|
|
}
|
|
}
|