56 lines
1.2 KiB
PHP
56 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace plugin\admin\app\repository;
|
|
|
|
use plugin\admin\app\model\OpmMwDepartment;
|
|
use plugin\admin\app\repository\exception\EntityNotFoundException;
|
|
|
|
class DepartmentRepository extends BaseRepository
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->model = new OpmMwDepartment;
|
|
}
|
|
|
|
public static function new(): DepartmentRepository
|
|
{
|
|
return new self();
|
|
}
|
|
|
|
/**
|
|
* 只查询消毒灭菌科室 (type=1)
|
|
*/
|
|
public function scopedQuery()
|
|
{
|
|
return parent::scopedQuery()->where('type', 1);
|
|
}
|
|
|
|
/**
|
|
* 根据ID查找科室
|
|
* @throws EntityNotFoundException
|
|
*/
|
|
public function findOrFail(int $id): OpmMwDepartment
|
|
{
|
|
$dept = $this->scopedQuery()->find($id);
|
|
if (!$dept) {
|
|
throw new EntityNotFoundException('科室不存在');
|
|
}
|
|
return $dept;
|
|
}
|
|
|
|
/**
|
|
* 获取指定医院下的科室选项
|
|
*/
|
|
public function getDepartmentsByHospital(int $hospitalId): array
|
|
{
|
|
return $this->scopedQuery()
|
|
->where('organ_id', $hospitalId)
|
|
->get()
|
|
->map(fn($item) => [
|
|
'id' => $item->id,
|
|
'name' => $item->dept_name,
|
|
])
|
|
->toArray();
|
|
}
|
|
}
|