radfusion/app/DAL/Studies/WorklistBase.php
2025-01-03 20:46:25 +06:00

184 lines
4.8 KiB
PHP

<?php
namespace App\DAL\Studies;
use App\Models\Enums\ReportStatus;
use App\Models\Enums\StudyLevelStatus;
use App\Models\Study;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
abstract class WorklistBase implements IUserStudyLister
{
private ?int $pageSize = null;
private ?string $sortColumn = null;
private ?string $sortDir = null;
private ?string $searchTerm = null;
private ?int $radiologist_id = null;
private ?StudyLevelStatus $studyStatus = null;
private ?ReportStatus $reportStatus = null;
private ?bool $locked = null;
private ?bool $archived = null;
public function setRadiologist(int $radiologist_id): void
{
$this->radiologist_id = $radiologist_id;
}
public function setStudyStatus(StudyLevelStatus $status): void
{
$this->studyStatus = $status;
}
private function applyRadiologist(Builder $query): Builder
{
if ($this->radiologist_id != null) {
$rad = $this->radiologist_id;
$query = $query->where(function ($query) use ($rad) {
$query->orWhere('assigned_physician_id', '=', $rad);
$query->orWhere('reading_physician_id', '=', $rad);
});
}
return $query;
}
protected function getStudiesQuery(): Builder
{
if ($this->archived === null) {
return Study::active();
}
return Study::query();
}
private function applyStudyStatus(Builder $query): Builder
{
if ($this->studyStatus != null) {
$query = $query->where('study_status', '=', $this->studyStatus->value);
}
return $query;
}
private function applyReportStatus(Builder $query): Builder
{
if ($this->reportStatus != null) {
$query = $query->where('report_status', '=', $this->reportStatus->value);
}
return $query;
}
public function setReportStatus(ReportStatus $status): void
{
$this->reportStatus = $status;
}
protected static function reportCompleteQuery(Builder $query): Builder
{
return $query->where('report_status', '=', ReportStatus::Signed->value);
}
protected function applySort(Builder $query): Builder
{
if ($this->sortColumn) {
return $query->orderBy($this->sortColumn, $this->sortDir ?? 'ASC');
}
return $query
->orderByDesc('study_priority')
->orderByDesc('received_at');
}
abstract protected function buildQuery(?int $user_id = null): Builder;
public function get(?int $user_id = null): LengthAwarePaginator
{
$query = $this->buildQuery($user_id);
$query = $this->applySort($query);
$query = $this->applySearch($query);
$query = $this->applyRadiologist($query);
$query = $this->applyStudyStatus($query);
$query = $this->applyReportStatus($query);
$query = $this->applyArchived($query);
$query = $this->applyLocked($query);
return $query
->paginate($this->getPageSize($user_id));
}
public function setArchived(bool $archived): void
{
$this->archived = $archived;
}
public function setLocked(bool $locked): void
{
$this->locked = $locked;
}
public function setPageSize(int $size): void
{
$this->pageSize = $size;
}
public function setSortColumn(string $column, string $dir): void
{
$this->sortColumn = $column;
$this->sortDir = $dir;
}
public function setSearchTerm(string $search): void
{
$this->searchTerm = $search;
}
private function getPageSize(?int $user_id = null): int
{
return $this->pageSize ?? $this->getPageSize($user_id);
}
private function applySearch(Builder $query): Builder
{
if (filled($this->searchTerm)) {
$search = "%{$this->searchTerm}%";
$query = $query->where(function ($query) use ($search) {
$query->orWhere('patient_name', 'like', $search);
$query->orWhere('patient_id', 'like', $search);
$query->orWhere('accession_number', 'like', $search);
$query->orWhere('study_description', 'like', $search);
$query->orWhere('body_part_examined', 'like', $search);
});
}
return $query;
}
private function applyArchived(Builder $query): Builder
{
if ($this->archived != null) {
$query = $query->where('archived', $this->archived);
}
return $query;
}
private function applyLocked(Builder $query): Builder
{
if ($this->locked != null) {
$query = $query->where('locked', $this->locked);
}
return $query;
}
}