order-food-admin/app/Admin/Metrics/NewOrders.php

144 lines
4.3 KiB
PHP

<?php
namespace App\Admin\Metrics;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use App\Models\Order;
use Carbon\Carbon;
class NewOrders extends Line
{
/**
* 初始化卡片内容
*
* @return void
*/
protected function init()
{
parent::init();
$this->title('支付订单统计');
$this->dropdown([
'1' => '今日',
'7' => '7天内',
'30' => '30天内',
]);
}
/**
* 处理请求
*
* @param Request $request
*
* @return mixed|void
*/
public function handle(Request $request)
{
switch ($request->get('option')) {
case '1':
// 卡片内容
$start_time = Carbon::now()->startOfDay();
$end_time = Carbon::now()->endOfDay();
$this->withContent($this->getOrderCount($start_time, $end_time).'单');
$start_time = Carbon::now()->subDays(7)->startOfDay();
// 图表数据
$this->withChart($this->getOrderGenerator($start_time, $end_time, 6));
break;
case '7':
// 卡片内容
$start_time = Carbon::now()->subDays(6)->startOfDay();
$end_time = Carbon::now()->endOfDay();
$this->withContent($this->getOrderCount($start_time, $end_time).'单');
// 图表数据
$this->withChart($this->getOrderGenerator($start_time, $end_time, 6));
break;
case '30':
// 卡片内容
$start_time = Carbon::now()->subDays(29)->startOfDay();
$end_time = Carbon::now()->endOfDay();
$this->withContent($this->getOrderCount($start_time, $end_time).'单');
// 图表数据
$this->withChart($this->getOrderGenerator($start_time, $end_time, 29));
break;
default:
// 卡片内容
$start_time = Carbon::now()->startOfDay();
$end_time = Carbon::now()->endOfDay();
$this->withContent($this->getOrderCount($start_time, $end_time).'单');
$start_time = Carbon::now()->subDays(6)->startOfDay();
// 图表数据
$this->withChart($this->getOrderGenerator($start_time, $end_time, 6));
}
}
/**
* 设置图表数据.
*
* @param array $data
*
* @return $this
*/
public function withChart(array $data)
{
return $this->chart([
'series' => [
[
'name' => $this->title,
'data' => $data,
],
],
]);
}
/**
* 设置卡片内容.
*
* @param string $content
*
* @return $this
*/
public function withContent($content)
{
return $this->content(
<<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
<h2 class="ml-1 font-lg-1">{$content}</h2>
<span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
);
}
/**
* 获取指定时间段用户数量
*/
public function getOrderCount($start_time, $end_time){
return Order::where([
'order_status'=>1,
'pay_status'=>1
])->whereBetween('created_at', [$start_time, $end_time])->count();
}
public function getOrderGenerator($start_time, $end_time, $days, $groupBy='d'){
$list = Order::selectRaw("COUNT(DISTINCT id) AS orderNum, DATE_FORMAT(created_at,'%Y-%m-%d') dDay")
->where([
'order_status'=>1,
'pay_status'=>1
])
->whereBetween('created_at', [$start_time, $end_time])
->groupBy('dDay')->pluck('orderNum','dDay')->toArray();
for($i = 0; $i< $days; $i++){
$time = Carbon::now()->subDay($i)->toDateString();
if(!isset($list[Carbon::now()->subDay($i)->toDateString()])){
$list[$time] = 0;
}
}
ksort($list);
// dd($list);
return array_values($list);
}
}