dbms/app/Models/Customer/ServiceModel.php
2025-06-12 19:09:31 +09:00

90 lines
2.7 KiB
PHP

<?php
namespace App\Models\Customer;
use App\Entities\Customer\ServiceEntity;
class ServiceModel extends CustomerModel
{
const TABLE = "serviceinfo";
const PK = "uid";
const TITLE = "title";
protected $table = self::TABLE;
protected $primaryKey = self::PK;
protected $returnType = ServiceEntity::class;
protected $allowedFields = [
"clientinfo_uid",
"ownerinfo_uid",
"title",
"type",
"location",
"switch",
"code",
"raid",
"billing_at",
"start_at",
"status",
"updated_at"
];
public function __construct()
{
parent::__construct();
}
public function getFormFieldRule(string $action, string $field): string
{
if (is_array($field)) {
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
}
switch ($field) {
case "clientinfo_uid":
case "ownerinfo_uid":
$rule = "required|numeric";
break;
case "title":
case "type":
case "location":
case "switch":
case "code":
case "status":
$rule = "required|trim|string";
break;
case "billing_at":
case "start_at":
$rule = "required|valid_date";
break;
case "raid":
$rule = "if_exist|trim|string";
break;
default:
$rule = parent::getFormFieldRule($action, $field);
break;
}
return $rule;
}
//다음 달로 결제일을 연장합니다.
public function extendPaymentDate(int $uid): mixed
{
$sql = "UPDATE ? 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 uid = ? AND status = ?";
$sql = $this->setQuery($sql)
->setParameter(1, $this->getTableName())
->setParameter(2, $uid)
->setParameter(3, DEFAULTS['STATUS'])
->getSQL();
if (!$sql) {
throw new \Exception("SQL문이 생성되지 않았습니다.");
}
if (!$this->isValidQuery($sql)) {
throw new \Exception("SQL문이 유효하지 않습니다: " . $sql);
}
// 쿼리 실행
if (!$this->execute($sql)) {
throw new \Exception("SQL문 실행에 실패했습니다: " . $sql);
}
return $this->query($sql);
}
}