clqms-be/app/Models/Test/TestMapModel.php
mahdahar 5e0a7f21f5 feat: add TestMapDetail controller, model and migration for test site mapping
- Add TestMapDetailController for managing test mapping details
- Create TestMapDetailModel with CRUD operations
- Add migration for TestSiteID column in testmap table
- Update TestMapController and TestMapModel
- Update routes, seeder and PatVisitModel
- Regenerate API documentation bundle
2026-02-26 16:48:10 +07:00

86 lines
2.5 KiB
PHP

<?php
namespace App\Models\Test;
use App\Models\BaseModel;
class TestMapModel extends BaseModel {
protected $table = 'testmap';
protected $primaryKey = 'TestMapID';
protected $allowedFields = [
'TestSiteID',
'HostType',
'HostID',
'ClientType',
'ClientID',
'CreateDate',
'EndDate'
];
protected $useTimestamps = true;
protected $createdField = 'CreateDate';
protected $updatedField = '';
protected $useSoftDeletes = true;
protected $deletedField = "EndDate";
/**
* Get all test mappings with names
*/
public function getUniqueGroupings() {
$db = $this->db;
$sql = "
SELECT
tm.TestMapID,
tm.TestSiteID,
tm.HostType,
tm.HostID,
CASE
WHEN tm.HostType = 'SITE' THEN s.SiteName
WHEN tm.HostType = 'WORKSTATION' THEN ws.WorkstationName
WHEN tm.HostType = 'INSTRUMENT' THEN el.InstrumentName
ELSE tm.HostID
END as HostName,
tm.ClientType,
tm.ClientID,
CASE
WHEN tm.ClientType = 'SITE' THEN cs.SiteName
WHEN tm.ClientType = 'WORKSTATION' THEN cws.WorkstationName
WHEN tm.ClientType = 'INSTRUMENT' THEN cel.InstrumentName
ELSE tm.ClientID
END as ClientName
FROM testmap tm
LEFT JOIN site s ON tm.HostType = 'SITE' AND s.SiteID = tm.HostID
LEFT JOIN workstation ws ON tm.HostType = 'WORKSTATION' AND ws.WorkstationID = tm.HostID
LEFT JOIN equipmentlist el ON tm.HostType = 'INSTRUMENT' AND el.EID = tm.HostID
LEFT JOIN site cs ON tm.ClientType = 'SITE' AND cs.SiteID = tm.ClientID
LEFT JOIN workstation cws ON tm.ClientType = 'WORKSTATION' AND cws.WorkstationID = tm.ClientID
LEFT JOIN equipmentlist cel ON tm.ClientType = 'INSTRUMENT' AND cel.EID = tm.ClientID
WHERE tm.EndDate IS NULL
";
return $db->query($sql)->getResultArray();
}
/**
* Get test mappings by test site
*/
public function getMappingsByTestSite($testSiteID) {
return $this->where('TestSiteID', $testSiteID)
->where('EndDate IS NULL')
->findAll();
}
/**
* Get test mappings with details by test site
*/
public function getMappingsWithDetailsByTestSite($testSiteID) {
return $this->select('testmap.*, testmapdetail.*')
->join('testmapdetail', 'testmapdetail.TestMapID = testmap.TestMapID', 'left')
->where('testmap.TestSiteID', $testSiteID)
->where('testmap.EndDate IS NULL')
->where('testmapdetail.EndDate IS NULL')
->findAll();
}
}