This commit is contained in:
hant 2025-05-06 18:15:03 +08:00
parent 69cd09a63e
commit f3a692243b

View File

@ -41,16 +41,99 @@ class Orderplan extends Backend
parent::_initialize(); parent::_initialize();
$this->model = new \app\admin\model\Order; $this->model = new \app\admin\model\Order;
//$this->view->assign("collectList", $this->model->getCollectList());
//$this->view->assign("dispatchTypeList", $this->model->getDispatchTypeList());
} }
public function dashboard(){ public function dashboard()
{
$top = $this->getTopTotal();
$lines = $this->getLine();
dd($lines);
return $this->fetch('orderplan/index'); 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,
],
];
}
} }