新增功能

This commit is contained in:
Your Name
2026-03-14 15:39:34 +08:00
parent 4ddee40675
commit 4a853ba237
27 changed files with 2906 additions and 74 deletions
@@ -427,4 +427,213 @@ class OrderLogic
return [];
}
}
private static $orderTypeNames = [
0 => '退款统计',
1 => '挂号费',
2 => '问诊费',
3 => '药品费用',
4 => '首付费用',
5 => '尾款费用',
6 => '其他费用',
];
/**
* @notes 订单统计(按订单类型或退款)
* @param array $params order_type(0退款,1挂号费,2问诊费,3药品费用,4首付,5尾款,6其他), days(0今天,7,30)
* @return array
*/
public static function orderStats(array $params = [])
{
$orderType = isset($params['order_type']) ? (int)$params['order_type'] : 1;
if (!array_key_exists($orderType, self::$orderTypeNames)) {
$orderType = 1;
}
$days = isset($params['days']) ? (int)$params['days'] : 7;
$endTime = !empty($params['end_time']) ? strtotime($params['end_time']) : time();
if ($days === 0) {
$startTime = strtotime(date('Y-m-d'));
} else {
$days = $days > 0 ? min($days, 90) : 7;
$startTime = strtotime("-{$days} days", $endTime);
}
$startStr = date('Y-m-d H:i:s', $startTime);
$endStr = date('Y-m-d H:i:s', $endTime);
$depts = \app\common\model\dept\Dept::where('status', 1)
->whereNull('delete_time')
->order('sort desc, id asc')
->column('name', 'id');
$query = Order::where('create_time', 'between', [$startStr, $endStr])
->whereNull('delete_time');
if ($orderType === 0) {
$query->where('status', 4);
} else {
$query->where('order_type', $orderType)->where('status', 2);
}
$orderRows = $query
->field('creator_id, count(*) as cnt, sum(amount) as total_amount')
->group('creator_id')
->select()
->toArray();
$creatorIds = array_unique(array_column($orderRows, 'creator_id'));
$creatorIds = array_filter($creatorIds);
if (empty($creatorIds)) {
return [
'order_type' => $orderType,
'order_type_name' => self::$orderTypeNames[$orderType],
'date_range' => [date('Y-m-d', $startTime), date('Y-m-d', $endTime)],
'today_total' => 0,
'today_amount' => '0.00',
'today_ranking' => [],
'departments' => [],
'ranking' => [],
'chart' => ['xAxis' => [], 'series' => []],
];
}
$counts = [];
$amounts = [];
foreach ($orderRows as $row) {
$counts[$row['creator_id']] = (int)$row['cnt'];
$amounts[$row['creator_id']] = round((float)$row['total_amount'], 2);
}
$adminDepts = \app\common\model\auth\AdminDept::whereIn('admin_id', $creatorIds)
->select()
->toArray();
$adminToDepts = [];
foreach ($adminDepts as $ad) {
$adminId = $ad['admin_id'];
if (!isset($adminToDepts[$adminId])) {
$adminToDepts[$adminId] = [];
}
$adminToDepts[$adminId][] = (int)$ad['dept_id'];
}
$admins = \app\common\model\auth\Admin::whereIn('id', $creatorIds)
->whereNull('delete_time')
->column('name', 'id');
$todayStart = strtotime(date('Y-m-d'));
$todayEnd = time();
$todayStr = date('Y-m-d H:i:s', $todayStart);
$todayEndStr = date('Y-m-d H:i:s', $todayEnd);
$todayQuery = Order::where('create_time', 'between', [$todayStr, $todayEndStr])
->whereNull('delete_time');
if ($orderType === 0) {
$todayQuery->where('status', 4);
} else {
$todayQuery->where('order_type', $orderType)->where('status', 2);
}
$todayRows = $todayQuery
->field('creator_id, count(*) as cnt, sum(amount) as total_amount')
->group('creator_id')
->select()
->toArray();
$todayCounts = [];
$todayAmounts = [];
foreach ($todayRows as $row) {
$todayCounts[$row['creator_id']] = (int)$row['cnt'];
$todayAmounts[$row['creator_id']] = round((float)$row['total_amount'], 2);
}
$todayTotal = array_sum($todayCounts);
$todayAmount = array_sum($todayAmounts);
$todayRanking = [];
foreach ($creatorIds as $cid) {
$cnt = (int)($todayCounts[$cid] ?? 0);
$amt = (float)($todayAmounts[$cid] ?? 0);
if ($cnt > 0 || $amt > 0) {
$todayRanking[] = [
'id' => (int)$cid,
'name' => $admins[$cid] ?? '未知',
'count' => $cnt,
'amount' => number_format($amt, 2, '.', ''),
];
}
}
usort($todayRanking, fn($a, $b) => $b['count'] <=> $a['count']);
$deptData = [];
$assignedIds = [];
foreach ($depts as $deptId => $deptName) {
$members = [];
foreach ($adminToDepts as $adminId => $deptIdsArr) {
if (!in_array((int)$deptId, $deptIdsArr)) {
continue;
}
$assignedIds[] = $adminId;
$members[] = [
'id' => (int)$adminId,
'name' => $admins[$adminId] ?? '未知',
'count' => (int)($counts[$adminId] ?? 0),
'amount' => number_format((float)($amounts[$adminId] ?? 0), 2, '.', ''),
];
}
$deptTotal = array_sum(array_column($members, 'count'));
$deptAmount = array_sum(array_map(fn($m) => (float)$m['amount'], $members));
$deptData[] = [
'dept_id' => (int)$deptId,
'dept_name' => $deptName,
'members' => $members,
'total' => $deptTotal,
'amount' => number_format($deptAmount, 2, '.', ''),
];
}
$unassignedIds = array_diff($creatorIds, array_unique($assignedIds));
if (!empty($unassignedIds)) {
$members = [];
foreach ($unassignedIds as $adminId) {
$members[] = [
'id' => (int)$adminId,
'name' => $admins[$adminId] ?? '未知',
'count' => (int)($counts[$adminId] ?? 0),
'amount' => number_format((float)($amounts[$adminId] ?? 0), 2, '.', ''),
];
}
$deptData[] = [
'dept_id' => 0,
'dept_name' => '未分配部门',
'members' => $members,
'total' => array_sum(array_column($members, 'count')),
'amount' => number_format(array_sum(array_map(fn($m) => (float)$m['amount'], $members)), 2, '.', ''),
];
}
$ranking = [];
foreach ($creatorIds as $adminId) {
$ranking[] = [
'id' => (int)$adminId,
'name' => $admins[$adminId] ?? '未知',
'count' => (int)($counts[$adminId] ?? 0),
'amount' => number_format((float)($amounts[$adminId] ?? 0), 2, '.', ''),
];
}
usort($ranking, fn($a, $b) => $b['count'] <=> $a['count']);
$chartNames = [];
$chartData = [];
foreach ($ranking as $i => $r) {
$chartNames[] = ($i + 1) . '.' . $r['name'];
$chartData[] = $r['count'];
}
return [
'order_type' => $orderType,
'order_type_name' => self::$orderTypeNames[$orderType],
'date_range' => [date('Y-m-d', $startTime), date('Y-m-d', $endTime)],
'today_total' => $todayTotal,
'today_amount' => number_format($todayAmount, 2, '.', ''),
'today_ranking' => $todayRanking,
'departments' => $deptData,
'ranking' => $ranking,
'chart' => [
'xAxis' => $chartNames,
'series' => [['name' => '单数', 'data' => $chartData]],
],
];
}
}