更新
This commit is contained in:
@@ -7,6 +7,7 @@ use app\common\model\DiagnosisViewRecord;
|
||||
use app\common\model\doctor\Appointment;
|
||||
use app\common\model\tcm\Prescription;
|
||||
use app\common\model\auth\AdminRole;
|
||||
use app\common\lists\ListsExtendInterface;
|
||||
use app\common\lists\ListsSearchInterface;
|
||||
|
||||
/**
|
||||
@@ -14,7 +15,7 @@ use app\common\lists\ListsSearchInterface;
|
||||
* Class AppointmentLists
|
||||
* @package app\adminapi\lists\doctor
|
||||
*/
|
||||
class AppointmentLists extends BaseAdminDataLists implements ListsSearchInterface
|
||||
class AppointmentLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
|
||||
{
|
||||
/**
|
||||
* @notes 设置搜索条件
|
||||
@@ -147,45 +148,31 @@ class AppointmentLists extends BaseAdminDataLists implements ListsSearchInterfac
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取数量
|
||||
* @return int
|
||||
* 列表 count / Tab 角标统计共用的过滤条件(不依赖 lists() 对 searchWhere 的副作用)
|
||||
* @param mixed $query
|
||||
* @param bool $applyStatusFilter 为 false 时按状态分组统计各 Tab 数量
|
||||
*/
|
||||
public function count(): int
|
||||
private function applyAppointmentCountFilters($query, bool $applyStatusFilter): void
|
||||
{
|
||||
// 获取当前管理员的角色ID
|
||||
$roleIds = AdminRole::where('admin_id', $this->adminId)->column('role_id');
|
||||
|
||||
// 处理患者姓名搜索
|
||||
|
||||
if (!empty($this->params['patient_name'])) {
|
||||
$this->searchWhere[] = ['u.patient_name', 'like', '%' . $this->params['patient_name'] . '%'];
|
||||
$query->where('u.patient_name', 'like', '%' . $this->params['patient_name'] . '%');
|
||||
}
|
||||
|
||||
// 处理医生姓名搜索
|
||||
if (!empty($this->params['doctor_name'])) {
|
||||
$this->searchWhere[] = ['ad.name', 'like', '%' . $this->params['doctor_name'] . '%'];
|
||||
$query->where('ad.name', 'like', '%' . $this->params['doctor_name'] . '%');
|
||||
}
|
||||
|
||||
// 处理状态搜索
|
||||
if (isset($this->params['status']) && $this->params['status'] !== '') {
|
||||
$this->searchWhere[] = ['a.status', '=', $this->params['status']];
|
||||
if ($applyStatusFilter && isset($this->params['status']) && $this->params['status'] !== '') {
|
||||
$query->where('a.status', '=', $this->params['status']);
|
||||
}
|
||||
|
||||
// 处理日期范围搜索
|
||||
if (!empty($this->params['start_date']) && !empty($this->params['end_date'])) {
|
||||
$this->searchWhere[] = ['a.appointment_date', 'between', [$this->params['start_date'], $this->params['end_date']]];
|
||||
$query->whereBetween('a.appointment_date', [$this->params['start_date'], $this->params['end_date']]);
|
||||
} elseif (!empty($this->params['start_date'])) {
|
||||
$this->searchWhere[] = ['a.appointment_date', '>=', $this->params['start_date']];
|
||||
$query->where('a.appointment_date', '>=', $this->params['start_date']);
|
||||
} elseif (!empty($this->params['end_date'])) {
|
||||
$this->searchWhere[] = ['a.appointment_date', '<=', $this->params['end_date']];
|
||||
$query->where('a.appointment_date', '<=', $this->params['end_date']);
|
||||
}
|
||||
|
||||
// 构建查询
|
||||
$query = Appointment::alias('a')
|
||||
->leftJoin('tcm_diagnosis u', 'a.patient_id = u.id')
|
||||
->leftJoin('admin ad', 'a.doctor_id = ad.id')
|
||||
->where($this->searchWhere);
|
||||
|
||||
// 是否确认诊单
|
||||
if (isset($this->params['diagnosis_confirmed']) && $this->params['diagnosis_confirmed'] !== '') {
|
||||
$confirmed = (int)$this->params['diagnosis_confirmed'];
|
||||
$tbl = (new DiagnosisViewRecord())->getTable();
|
||||
@@ -196,17 +183,57 @@ class AppointmentLists extends BaseAdminDataLists implements ListsSearchInterfac
|
||||
$query->whereNotExists($subSql);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是医生角色(role_id=1),只显示挂自己号的预约
|
||||
|
||||
if (in_array(1, $roleIds)) {
|
||||
$query->where('a.doctor_id', $this->adminId);
|
||||
}
|
||||
|
||||
// 如果是医助角色(role_id=2),只显示自己添加的患者的预约
|
||||
if (in_array(2, $roleIds)) {
|
||||
$query->where('u.assistant_id', $this->adminId);
|
||||
}
|
||||
}
|
||||
|
||||
return $query->count();
|
||||
/**
|
||||
* @notes 获取数量
|
||||
* @return int
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
$query = Appointment::alias('a')
|
||||
->leftJoin('tcm_diagnosis u', 'a.patient_id = u.id')
|
||||
->leftJoin('admin ad', 'a.doctor_id = ad.id');
|
||||
$this->applyAppointmentCountFilters($query, true);
|
||||
|
||||
return (int)$query->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 扩展字段:按需返回各状态数量(一次 GROUP BY,替代前端 4 次列表请求)
|
||||
* @return array
|
||||
*/
|
||||
public function extend(): array
|
||||
{
|
||||
if (empty($this->params['include_status_counts'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$query = Appointment::alias('a')
|
||||
->leftJoin('tcm_diagnosis u', 'a.patient_id = u.id')
|
||||
->leftJoin('admin ad', 'a.doctor_id = ad.id');
|
||||
$this->applyAppointmentCountFilters($query, false);
|
||||
|
||||
$rows = $query->field('a.status, COUNT(*) AS cnt')
|
||||
->group('a.status')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$out = [1 => 0, 2 => 0, 3 => 0, 4 => 0];
|
||||
foreach ($rows as $r) {
|
||||
$s = (int)($r['status'] ?? 0);
|
||||
if (array_key_exists($s, $out)) {
|
||||
$out[$s] = (int)($r['cnt'] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
return ['status_count' => $out];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user