156 lines
4.2 KiB
PHP
156 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Metrics;
|
|
|
|
use App\Models\Order;
|
|
use Carbon\Carbon;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Widgets\Metrics\Bar;
|
|
use Illuminate\Http\Request;
|
|
|
|
class Orders extends Bar
|
|
{
|
|
/**
|
|
* 初始化卡片内容
|
|
*/
|
|
protected function init()
|
|
{
|
|
parent::init();
|
|
|
|
$color = Admin::color();
|
|
|
|
$dark35 = $color->dark35();
|
|
|
|
// 卡片内容宽度
|
|
$this->contentWidth(4, 8);
|
|
$this->chartOption('chart.width', '400');
|
|
// 标题
|
|
$this->title('订单');
|
|
// 设置下拉选项
|
|
$this->dropdown([
|
|
'7' => '最近7天',
|
|
'30' => '最近30天',
|
|
]);
|
|
$this->chart->style('float: left;');
|
|
// 设置图表颜色
|
|
$this->chartColors([
|
|
$dark35,
|
|
$dark35,
|
|
$dark35,
|
|
$dark35,
|
|
$dark35,
|
|
$dark35,
|
|
$color->primary(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 处理请求
|
|
*
|
|
* @param Request $request
|
|
*
|
|
* @return mixed|void
|
|
*/
|
|
public function handle(Request $request)
|
|
{
|
|
switch ($request->get('option')) {
|
|
case '30':
|
|
$start_time = Carbon::now()->subDays(29)->startOfDay();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
// 卡片内容
|
|
$this->withContent($this->getOrderCount($start_time, $end_time).'单', '+5.2%');
|
|
|
|
// 图表数据
|
|
$this->withChart($this->getOrderGenerator($start_time, $end_time, 30));
|
|
break;
|
|
case '7':
|
|
default:
|
|
$start_time = Carbon::now()->subDays(6)->startOfDay();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
// 卡片内容
|
|
$this->withContent($this->getOrderCount($start_time, $end_time).'单', '+5.2%');
|
|
|
|
// 图表数据
|
|
$this->withChart($this->getOrderGenerator($start_time, $end_time, 7));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置图表数据.
|
|
*
|
|
* @param array $data
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function withChart(array $data)
|
|
{
|
|
// dd(array_values($data));
|
|
return $this->chart([
|
|
'series' => [
|
|
[
|
|
'name' => $this->title,
|
|
'data' => array_values($data),
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取指定时间段用户数量
|
|
*/
|
|
public function getOrderCount($start_time, $end_time)
|
|
{
|
|
return Order::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")
|
|
->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);
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 设置卡片内容.
|
|
*
|
|
* @param string $title
|
|
* @param string $value
|
|
* @param string $style
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function withContent($title, $value, $style = 'success')
|
|
{
|
|
// 根据选项显示
|
|
$label = strtolower(
|
|
$this->dropdown[request()->option] ?? 'last 7 days'
|
|
);
|
|
|
|
$minHeight = '183px';
|
|
|
|
return $this->content(
|
|
<<<HTML
|
|
<div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
|
|
<div class="text-left">
|
|
<h1 class="font-lg-2 mt-2 mb-0">{$title}</h1>
|
|
<!-- <h5 class="font-medium-2" style="margin-top: 10px;">
|
|
<span class="text-{$style}">{$value} </span>
|
|
<span>vs {$label}</span>
|
|
</h5> -->
|
|
</div>
|
|
|
|
<a href="/admin/orders" class="btn btn-primary shadow waves-effect waves-light">查看详情 <i class="feather icon-chevrons-right"></i></a>
|
|
</div>
|
|
HTML
|
|
);
|
|
}
|
|
}
|