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

135 lines
5.1 KiB
PHP

<?php
namespace App\Controllers;
use App\Helpers\Response;
use App\Middleware\JwtAuthMiddleware;
use App\Models\Patient;
use App\Models\Queue;
use App\Models\SoapNote;
use App\Services\FhirService;
use App\Services\HisConnector;
/**
* Class HisController
* Hospital Interoperability Controller (HIS / HOSxP Sync, Smart Card Reader, HL7 FHIR)
*
* @package App\Controllers
*/
class HisController
{
private HisConnector $hisConnector;
private FhirService $fhirService;
public function __construct()
{
$this->hisConnector = new HisConnector();
$this->fhirService = new FhirService();
}
/**
* POST /api/v1/his/sync - Sync Patient Data from HIS/HOSxP by HN or CID
*/
public function syncPatient(): void
{
JwtAuthMiddleware::handle();
$input = json_decode(file_get_contents('php://input'), true) ?? $_POST;
$query = $input['query'] ?? '';
$type = $input['type'] ?? 'hn'; // hn or cid
if (empty($query)) {
Response::error('กรุณาระบุหมายเลข HN หรือเลขบัตรประชาชนที่ต้องการดึงข้อมูล', 400);
}
$hisData = $this->hisConnector->getPatientFromHis($query, $type);
if (!$hisData) {
Response::error('ไม่พบข้อมูลผู้ป่วยในระบบ HIS ของโรงพยาบาล', 404);
}
// Save or update to local database
$patientModel = new Patient();
$existing = $patientModel->firstWhere([$type === 'hn' ? 'hn' : 'cid' => $query]);
if ($existing) {
$patientModel->updateHisData((int)$existing['id'], $hisData);
$patientId = (int)$existing['id'];
} else {
$patientId = $patientModel->saveFromSmartCard([
'cid' => $hisData['cid'] ?? $query,
'hn' => $hisData['hn'] ?? $query,
'first_name_th' => $hisData['first_name_th'] ?? 'ไม่ระบุ',
'last_name_th' => $hisData['last_name_th'] ?? 'ไม่ระบุ',
'birth_date' => $hisData['birth_date'] ?? '1980-01-01',
'gender' => $hisData['gender'] ?? 'Male',
'photo_url' => null,
]);
$patientModel->updateHisData($patientId, $hisData);
}
$patient = $patientModel->find($patientId);
Response::success('ดึงข้อมูลผู้ป่วยจาก HIS โรงพยาบาลสำเร็จ', [
'patient' => $patient,
'his_raw' => $hisData,
]);
}
/**
* GET /api/v1/his/smartcard - Read data from Local Smart Card Agent APDU / WebUSB
*/
public function readSmartCard(): void
{
JwtAuthMiddleware::handle();
// จำลองข้อมูลจากการอ่านบัตรประชาชนไทย Smart Card (ผ่าน Local PC/SC Agent)
$mockCardData = [
'cid' => '1100000000009',
'first_name_th' => 'สมศักดิ์',
'last_name_th' => 'บัตรทอง',
'first_name_en' => 'Somsak',
'last_name_en' => 'Batthong',
'birth_date' => '1975-08-12',
'gender' => 'Male',
'address' => '100 ถนนดินสอ แขวงบวรนิเวศ เขตพระนคร กรุงเทพมหานคร 10200',
'photo_base64' => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAAAAAA...',
];
$patientModel = new Patient();
$patientId = $patientModel->saveFromSmartCard($mockCardData);
$patient = $patientModel->find($patientId);
Response::success('อ่านข้อมูลบัตรประชาชนไทยและลงทะเบียนผู้ป่วยสำเร็จ', [
'patient' => $patient,
'card_read_time' => date('Y-m-d H:i:s'),
]);
}
/**
* POST /api/v1/fhir/encounter - Export Encounter & Pain Score to HL7 FHIR Server
*/
public function exportFhir(): void
{
JwtAuthMiddleware::handle();
$input = json_decode(file_get_contents('php://input'), true) ?? $_POST;
$queueId = isset($input['queue_id']) ? (int)$input['queue_id'] : 0;
if (!$queueId) {
Response::error('กรุณาระบุรหัสคิว (queue_id)', 400);
}
$queue = (new Queue())->find($queueId);
$soap = (new SoapNote())->findByQueueId($queueId);
if (!$queue || !$soap) {
Response::error('ไม่พบข้อมูลคิวหรือเวชระเบียน SOAP Note', 404);
}
$bundle = $this->fhirService->exportEncounterBundle($queue, $soap);
Response::success('สร้างและส่งออกทรัพยากร HL7 FHIR R4 Bundle เรียบร้อยแล้ว', [
'fhir_bundle' => $bundle,
'exported_at' => date('c'),
]);
}
}