Files
gravity/บริหารจัดการคิวนวดแผนไทย/app/Models/Therapist.php
T
2026-09-16 23:20:08 +07:00

80 lines
2.4 KiB
PHP

<?php
namespace App\Models;
/**
* Class Therapist
* Therapist & Workload Model (Smart Queue Balancer Support)
*
* @package App\Models
*/
class Therapist extends Model
{
protected string $table = 'therapists';
/**
* Get all therapists with User profile details
*/
public function getWithProfiles(int $branchId = null): array
{
$sql = "SELECT t.*, u.national_id, u.title, u.first_name, u.last_name, u.phone, u.email
FROM `{$this->table}` t
JOIN `users` u ON t.user_id = u.id
WHERE t.is_active = 1 ";
$params = [];
if ($branchId !== null) {
$sql .= " AND t.branch_id = :bid ";
$params['bid'] = $branchId;
}
$sql .= " ORDER BY t.current_workload_score ASC, t.id ASC";
$stmt = self::getDB()->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll();
}
/**
* Get Available Therapists matching specific skill in a branch
*/
public function getAvailableBySkill(int $branchId, string $skillName): array
{
$sql = "SELECT t.*, u.title, u.first_name, u.last_name
FROM `{$this->table}` t
JOIN `users` u ON t.user_id = u.id
WHERE t.branch_id = :bid
AND t.is_available = 1
AND t.is_active = 1
AND JSON_CONTAINS(t.skills, :skill)
ORDER BY t.current_workload_score ASC";
$stmt = self::getDB()->prepare($sql);
$stmt->execute([
'bid' => $branchId,
'skill' => json_encode($skillName, JSON_UNESCAPED_UNICODE),
]);
return $stmt->fetchAll();
}
/**
* Update Availability Status
*/
public function setAvailability(int $therapistId, bool $available): bool
{
return $this->update($therapistId, ['is_available' => $available ? 1 : 0]);
}
/**
* Get Daily Workload Summary from View
*/
public function getWorkloadSummary(int $branchId = null): array
{
$sql = "SELECT * FROM `vw_therapist_workload_summary`";
$params = [];
if ($branchId !== null) {
$sql .= " WHERE branch_id = :bid";
$params['bid'] = $branchId;
}
$stmt = self::getDB()->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll();
}
}