Реализация смены статуса

Добавлен move метод
Правка в поиске
This commit is contained in:
brusnitsyn
2025-12-07 22:33:19 +09:00
parent 2e1b5a3d0e
commit 945b53c578
10 changed files with 142 additions and 17 deletions

View File

@@ -2,8 +2,10 @@
namespace App\Http\Controllers;
use App\Http\Resources\ArchiveHistoryResource;
use App\Models\ArchiveHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
class ArchiveHistoryController extends Controller
{
@@ -18,4 +20,34 @@ class ArchiveHistoryController extends Controller
return response()->json($archiveHistory);
}
public function move(Request $request)
{
$data = $request->validate([
'issue_at' => 'nullable|numeric:',
'return_at' => 'nullable|numeric:',
'org_id' => 'required|numeric',
'employee_name' => 'nullable|string',
'employee_post' => 'nullable|string',
'comment' => 'nullable|string',
'has_lost' => 'boolean',
'historyable_id' => 'nullable|numeric',
]);
// Находим связанную модель ??
$historyable = SttMedicalHistory::findOrFail($data['historyable_id']);
// Преобразуем timestamp в дату, если пришли числа
if (isset($data['issue_at']) && is_numeric($data['issue_at'])) {
$data['issue_at'] = Carbon::createFromTimestampMs($data['issue_at'])->format('Y-m-d');
}
if (isset($data['return_at']) && is_numeric($data['return_at'])) {
$data['return_at'] = Carbon::createFromTimestampMs($data['return_at'])->format('Y-m-d');
}
$archiveHistory = ArchiveHistory::create($data);
return response()->json(ArchiveHistoryResource::make($archiveHistory));
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Http\Resources\ArchiveHistoryResource;
use App\Http\Resources\ArchiveInfoResource;
use App\Models\SI\SttMedicalHistory;
use Illuminate\Http\Request;
@@ -19,6 +20,10 @@ class MedicalHistoryController extends Controller
$archiveJournal = ArchiveHistoryResource::collection($patient->archiveHistory);
$archiveInfo = $patient->archiveInfo;
if (!empty($archiveInfo)) {
$archiveInfo = ArchiveInfoResource::make($patient->archiveInfo);
}
$patientInfo = [
'info' => $patient,
'journal' => $archiveJournal,

View File

@@ -20,7 +20,8 @@ class SttMedicalHistoryResource extends JsonResource
'fullname' => $this->getFullNameAttribute(),
'daterecipient' => Carbon::parse($this->daterecipient)->format('d.m.Y'),
'dateextract' => Carbon::parse($this->dateextract)->format('d.m.Y'),
'narhiv' => $this->narhiv,
'card_num' => $this->archiveInfo->num ?? null,
'status' => $this->archiveInfo->status ?? null,
'datearhiv' => Carbon::parse($this->datearhiv)->format('d.m.Y'),
'statgod' => $this->statgod,
'enp' => $this->enp,

View File

@@ -6,6 +6,51 @@ use Illuminate\Database\Eloquent\Model;
class ArchiveHistory extends Model
{
protected static function booted()
{
static::created(function ($archiveHistory) {
$archiveHistory->updateArchiveInfoStatus();
});
static::updated(function ($archiveHistory) {
$archiveHistory->updateArchiveInfoStatus();
});
}
public function updateArchiveInfoStatus()
{
// Получаем связанную модель через морф
$historyable = $this->historyable;
if (!$historyable) {
return;
}
// Проверяем, есть ли у модели архивная информация
if (method_exists($historyable, 'archiveInfo') && $historyable->archiveInfo) {
$historyable->archiveInfo->update([
'status_id' => $this->determineStatusId()
]);
}
}
public function determineStatusId()
{
if ($this->has_lost) {
return 4; // Утерян
}
if ($this->issue_at && !$this->return_at) {
return 3; // Выдан
}
if ($this->return_at) {
return 2; // В архиве
}
return 2; // По умолчанию
}
protected $fillable = [
'historyable_type',
'historyable_id',

View File

@@ -18,4 +18,9 @@ class ArchiveInfo extends Model
{
return $this->morphTo();
}
public function status()
{
return $this->belongsTo(ArchiveStatus::class);
}
}

View File

@@ -43,12 +43,19 @@ class SttMedicalHistory extends Model
{
return $query->where(function($q) use ($searchText) {
if (is_numeric($searchText)) {
$q->where('medcardnum', 'ILIKE', "$searchText%");
$q->where('medcardnum', 'ILIKE', "$searchText");
} else {
// Ищем по всем частям ФИО
$q->where('family', 'ILIKE', "%$searchText%")
->orWhere('name', 'ILIKE', "%$searchText%")
->orWhere('ot', 'ILIKE', "%$searchText%");
// Ищем в объединенном ФИО и в отдельных полях
$searchPattern = "%{$searchText}%";
$q->where(function($subQ) use ($searchPattern) {
// Поиск в объединенной строке
$subQ->whereRaw("CONCAT(family, ' ', name, ' ', COALESCE(ot, '')) ILIKE ?", [$searchPattern])
// И дополнительно в отдельных полях для точности
->orWhere('family', 'ILIKE', $searchPattern)
->orWhere('name', 'ILIKE', $searchPattern)
->orWhere('ot', 'ILIKE', $searchPattern);
});
}
});
}