title('每日转入/转出统计'); $this->dropdown([ '7' => '最近 7 天', '14' => '最近 14 天', '30' => '最近 30 天', '90' => '最近 90 天', ]); } /** * 处理请求 * * @param Request $request * @return mixed|void */ public function handle(Request $request) { $days = (int)$request->get('option', 7); $chartData = $this->getTransferData($days); // 计算总交易数量作为卡片内容 $totalTransfers = array_sum($chartData['data']['transfer_in']) + array_sum($chartData['data']['transfer_out']); // 卡片内容 $this->withContent(number_format($totalTransfers)); // 图表数据 $this->withChart($chartData['series'], $chartData['categories']); } /** * 获取转账统计数据 * * @param int $days * @return array */ protected function getTransferData(int $days): array { // 生成日期范围 $endDate = Carbon::today(); $startDate = $endDate->copy()->subDays($days - 1); $dates = []; for ($date = $startDate->copy(); $date->lte($endDate); $date->addDay()) { $dates[] = $date->format('Y-m-d'); } // 查询转账数据 - 按日期和类型分组统计 $transferData = DB::table('transfer_orders') ->select([ DB::raw('DATE(created_at) as date'), 'type', DB::raw('COUNT(*) as count'), DB::raw('SUM(amount) as total_amount'), DB::raw('SUM(actual_amount) as total_actual_amount') ]) ->where('status', TransferStatus::COMPLETED->value) // 只统计已完成的订单 ->where('created_at', '>=', $startDate) ->where('created_at', '<=', $endDate->endOfDay()) ->groupBy(['date', 'type']) ->orderBy('date') ->get() ->groupBy(['date', 'type']); // 构建图表数据 $transferInCounts = []; $transferOutCounts = []; $transferInAmounts = []; $transferOutAmounts = []; foreach ($dates as $date) { $dayData = $transferData->get($date, collect()); // 转入数据 $transferInData = $dayData->get(TransferType::IN->value, collect())->first(); $transferInCounts[] = $transferInData ? (int)$transferInData->count : 0; $transferInAmounts[] = $transferInData ? (float)$transferInData->total_actual_amount : 0; // 转出数据 $transferOutData = $dayData->get(TransferType::OUT->value, collect())->first(); $transferOutCounts[] = $transferOutData ? (int)$transferOutData->count : 0; $transferOutAmounts[] = $transferOutData ? (float)$transferOutData->total_actual_amount : 0; } // 构建多线图表数据 $series = [ [ 'name' => '转入笔数', 'data' => $transferInCounts, 'color' => '#28a745', // 绿色 ], [ 'name' => '转出笔数', 'data' => $transferOutCounts, 'color' => '#dc3545', // 红色 ], [ 'name' => '转入金额', 'data' => $transferInAmounts, 'color' => '#17a2b8', // 蓝色 'yAxis' => 1, // 使用第二个Y轴 ], [ 'name' => '转出金额', 'data' => $transferOutAmounts, 'color' => '#fd7e14', // 橙色 'yAxis' => 1, // 使用第二个Y轴 ], ]; return [ 'series' => $series, 'categories' => $dates, 'data' => [ 'transfer_in' => $transferInCounts, 'transfer_out' => $transferOutCounts, 'transfer_in_amounts' => $transferInAmounts, 'transfer_out_amounts' => $transferOutAmounts, ], ]; } /** * 设置图表数据 * * @param array $series * @param array $categories * @return $this */ public function withChart(array $series, array $categories = []) { return $this->chart([ 'series' => $series, 'xaxis' => [ 'categories' => $categories, 'title' => [ 'text' => '日期' ] ], 'yaxis' => [ [ 'title' => [ 'text' => '交易笔数' ], 'labels' => [ 'formatter' => 'function(val) { return val.toFixed(0); }' ] ], [ 'opposite' => true, 'title' => [ 'text' => '交易金额' ], 'labels' => [ 'formatter' => 'function(val) { return val.toFixed(2); }' ] ] ], 'tooltip' => [ 'shared' => true, 'intersect' => false, ], 'legend' => [ 'position' => 'top', 'horizontalAlign' => 'left' ] ]); } }