where('delete_time IS NULL') ->where('diagnosis_id', '>', 0) ->where('creator_id', $adminId) ->where('fulfillment_status', self::FULFILLMENT_COMPLETED) ->where('create_time', '>=', $startTs) ->where('create_time', '<=', $endTs); // 业绩总额 $totalAmount = (clone $baseQuery)->sum('amount'); // 有效订单数 $totalCount = (clone $baseQuery)->count(); // 按日期分组的折线图数据 $dailyData = (clone $baseQuery) ->field("FROM_UNIXTIME(create_time, '%Y-%m-%d') as date_label, SUM(amount) as daily_amount, COUNT(*) as daily_count") ->group('date_label') ->order('date_label', 'asc') ->select() ->toArray(); // 补全日期范围内的空日期 $dateMap = []; foreach ($dailyData as $row) { $dateMap[$row['date_label']] = [ 'amount' => round((float)$row['daily_amount'], 2), 'count' => (int)$row['daily_count'], ]; } $dates = []; $amounts = []; $counts = []; $cursor = strtotime($startDate); $endCursor = strtotime($endDate); while ($cursor <= $endCursor) { $d = date('Y-m-d', $cursor); $dates[] = substr($d, 5); // MM-DD $amounts[] = $dateMap[$d]['amount'] ?? 0; $counts[] = $dateMap[$d]['count'] ?? 0; $cursor = strtotime('+1 day', $cursor); } return [ 'date_range' => [$startDate, $endDate], 'summary' => [ 'total_amount' => round((float)$totalAmount, 2), 'total_count' => (int)$totalCount, ], 'chart' => [ 'dates' => $dates, 'amounts' => $amounts, 'counts' => $counts, ], ]; } }