dbmsv4/app/Services/Customer/Wallet/WalletService.php
2025-12-12 11:46:28 +09:00

78 lines
3.4 KiB
PHP

<?php
namespace App\Services\Customer\Wallet;
use App\Entities\Customer\ClientEntity;
use App\Entities\PaymentEntity;
use App\Models\CommonModel;
use App\Services\Customer\CustomerService;
use RuntimeException;
abstract class WalletService extends CustomerService
{
protected function __construct(CommonModel $model)
{
parent::__construct($model);
$this->addClassPaths('Wallet');
}
//기본기능
//생성
protected function create_process(array $formDatas): object
{
if (!array_key_exists('clientinfo_uid', $formDatas)) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 고객정보가 정의되지 않았습니다.");
}
if (!array_key_exists('status', $formDatas)) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 상태정보가 정의되지 않았습니다.");
}
//고객 정보
$clientEntity = service('customer_clientservice')->getEntity($formDatas['clientinfo_uid']);
if (!$clientEntity instanceof ClientEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['clientinfo_uid']}에 해당하는 고객정보를 찾을수 없습니다.");
}
switch ($formDatas['status']) {
case STATUS['DEPOSIT']:
$formDatas = $this->deposit_process($clientEntity, $formDatas);
break;
case STATUS['WITHDRAWAL']:
$formDatas = $this->withdrawal_process($clientEntity, $formDatas);
break;
default:
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 정의되지 않은 상태정보입니다.");
}
$entity = parent::create_process($formDatas);
if (!$entity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 고객의 Wallet정보 생성에 실패하였습니다.");
};
return $entity;
}
//입금,쿠폰 충전 처리
protected function deposit_process(ClientEntity $clientEntity, array $formDatas): array
{
$formDatas['issue_at'] = array_key_exists('issue_at', $formDatas) ? $formDatas['issue_at'] : date('Y-m-d');
return $formDatas;
}
//출금,쿠폰 사용 처리
protected function withdrawal_process(ClientEntity $clientEntity, array $formDatas): array
{
$formDatas['issue_at'] = array_key_exists('issue_at', $formDatas) ? $formDatas['issue_at'] : date('Y-m-d');
return $formDatas;
}
//결제관련 출금 처리
final public function withdrawalByPayment(PaymentEntity $paymentEntity): void
{
//고객 정보
$clientEntity = service('customer_clientservice')->getEntity($paymentEntity->getClientInfoUID());
if (!$clientEntity instanceof ClientEntity) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$paymentEntity->getClientInfoUID()}에 해당하는 고객정보를 찾을수 없습니다.");
}
$formDatas = [
'clientinfo_uid' => $paymentEntity->getClientInfoUID(),
'title' => $paymentEntity->getTitle(),
'amount' => $paymentEntity->getAmount(),
'status' => STATUS['WITHDRAWAL'],
];
$this->withdrawal_process($clientEntity, $formDatas);
}
}