统计中心-》添加部门统计逻辑
This commit is contained in:
@@ -20,4 +20,13 @@ class StatisticsController extends BaseAdminController
|
||||
{
|
||||
return $this->dataLists(new StatisticsLists());
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 部门统计列表
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function deptLists()
|
||||
{
|
||||
return $this->dataLists(new StatisticsLists('dept'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,17 @@ use app\common\model\auth\Admin;
|
||||
*/
|
||||
class StatisticsLists extends BaseAdminDataLists
|
||||
{
|
||||
protected $type = 'doctor';
|
||||
|
||||
/**
|
||||
* @param string $type 统计类型:doctor(医生统计) 或 dept(部门统计)
|
||||
*/
|
||||
public function __construct($type = 'doctor')
|
||||
{
|
||||
parent::__construct();
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 搜索条件
|
||||
* @return array
|
||||
@@ -28,6 +39,10 @@ class StatisticsLists extends BaseAdminDataLists
|
||||
*/
|
||||
public function lists(): array
|
||||
{
|
||||
if ($this->type === 'dept') {
|
||||
return $this->getDeptStatistics();
|
||||
}
|
||||
|
||||
$doctorId = $this->params['doctor_id'] ?? '';
|
||||
$timeType = $this->params['time_type'] ?? 'today';
|
||||
$startDate = $this->params['start_date'] ?? '';
|
||||
@@ -377,6 +392,68 @@ class StatisticsLists extends BaseAdminDataLists
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取部门统计数据
|
||||
* @return array
|
||||
*/
|
||||
private function getDeptStatistics()
|
||||
{
|
||||
$deptId = $this->params['dept_id'] ?? '';
|
||||
$timeType = $this->params['time_type'] ?? 'today';
|
||||
$startDate = $this->params['start_date'] ?? '';
|
||||
$endDate = $this->params['end_date'] ?? '';
|
||||
|
||||
// 计算时间范围
|
||||
list($startDate, $endDate, $timeRangeText) = $this->getTimeRange($timeType, $startDate, $endDate);
|
||||
|
||||
// 查询部门统计数据
|
||||
$deptStatsQuery = \think\facade\Db::name('doctor_appointment')
|
||||
->alias('apt')
|
||||
->leftJoin('zyt_admin_dept ad', 'apt.assistant_id = ad.admin_id')
|
||||
->leftJoin('zyt_dept dept', 'ad.dept_id = dept.id')
|
||||
->field([
|
||||
'dept.id as dept_id',
|
||||
'dept.name as dept_name',
|
||||
'COUNT(*) as total_count',
|
||||
'SUM(CASE WHEN apt.status = 1 THEN 1 ELSE 0 END) as registered_count',
|
||||
'SUM(CASE WHEN apt.status = 3 THEN 1 ELSE 0 END) as completed_count',
|
||||
'SUM(CASE WHEN apt.status = 2 THEN 1 ELSE 0 END) as canceled_count',
|
||||
'SUM(CASE WHEN apt.status = 4 THEN 1 ELSE 0 END) as expired_count'
|
||||
])
|
||||
->where('apt.appointment_date', '>=', $startDate)
|
||||
->where('apt.appointment_date', '<=', $endDate)
|
||||
->whereNotNull('dept.id');
|
||||
|
||||
if ($deptId) {
|
||||
$deptStatsQuery->where('dept.id', $deptId);
|
||||
}
|
||||
|
||||
$deptStats = $deptStatsQuery->group('dept.id')->select()->toArray();
|
||||
|
||||
// 组装结果
|
||||
$result = [];
|
||||
foreach ($deptStats as $stat) {
|
||||
// 计算完成率
|
||||
$completionRate = $stat['total_count'] > 0
|
||||
? round(($stat['completed_count'] / $stat['total_count']) * 100, 2)
|
||||
: 0;
|
||||
|
||||
$result[] = [
|
||||
'dept_id' => $stat['dept_id'],
|
||||
'dept_name' => $stat['dept_name'],
|
||||
'total_count' => (int)$stat['total_count'] ?? 0,
|
||||
'registered_count' => (int)$stat['registered_count'] ?? 0,
|
||||
'completed_count' => (int)$stat['completed_count'] ?? 0,
|
||||
'canceled_count' => (int)$stat['canceled_count'] ?? 0,
|
||||
'expired_count' => (int)$stat['expired_count'] ?? 0,
|
||||
'completion_rate' => $completionRate,
|
||||
'time_range' => $timeRangeText
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取时间范围
|
||||
* @param string $timeType
|
||||
|
||||
Reference in New Issue
Block a user