model = new \app\admin\model\Order; } public function dashboard() { $top = $this->getTopTotal(); $lines = $this->getLine(); dd($lines); return $this->fetch('orderplan/index'); } private function getTopTotal() { $build = new \app\admin\model\Order(); $res = $build->field([ 'sum(total) total', 'count(id) count', 'sum(performance) performance' ])->limit(1)->select(); $res = $res[0] ?? false; if ($res){ return $res->toArray(); } return [ 'total' => 0, 'count' => 0, 'performance' => 0, ]; } private function getLine() { $build = new \app\admin\model\Order(); $start = now()->modify('-14 days')->format('Y-m-d'); $end_at = now()->format('Y-m-d'); $res = $build->field([ 'DATE(create_time) day', 'sum(total) total', 'count(id) count', 'sum(performance) performance' ])->group(' DATE(create_time)') ->select(); $data = []; foreach ($res as $re){ $re = $re->getData(); $data []= $re; } return $this->prepareEchartsBarData($data,$start,$end_at); } private function prepareEchartsBarData(array $data, string $startDate, string $endDate): array { // 将原始数据用日期作为键索引 $indexed = []; foreach ($data as $item) { $indexed[$item['day']] = $item; } $start = strtotime($startDate); $end = strtotime($endDate); $xAxis = []; $series_total = []; $series_count = []; $series_performance = []; $rate = []; for ($date = $start; $date <= $end; $date += 86400) { $day = date('Y-m-d', $date); $xAxis[] = $day; $total = isset($indexed[$day]) ? (float)$indexed[$day]['total'] : 0; $count = isset($indexed[$day]) ? (int)$indexed[$day]['count'] : 0; $performance = isset($indexed[$day]) ? (float)$indexed[$day]['performance'] : 0; $series_total[] = $total; $series_count[] = $count; $series_performance[] = $performance; $rate[] = $total ? number_format($performance / $total * 100,2) : '0'; } return [ 'xAxis' => $xAxis, 'series' => [ 'total' => $series_total, 'count' => $series_count, 'performance' => $series_performance, 'rate' => $rate, ], ]; } }