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

124 lines
3.0 KiB
PHP

<?php
namespace App\Admin\Metrics;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Donut;
use App\Models\Order;
use Carbon\Carbon;
class OrderDevices extends Donut
{
protected $labels = ['线下订单', '小程序订单'];
/**
* 初始化卡片内容
*/
protected function init()
{
parent::init();
$color = Admin::color();
$colors = [$color->primary(), $color->alpha('blue2', 0.5)];
$this->title('订单比例');
$this->subTitle('近30天内');
$this->chartLabels($this->labels);
// 设置图表颜色
$this->chartColors($colors);
}
/**
* 渲染模板
*
* @return string
*/
public function render()
{
$this->fill();
return parent::render();
}
/**
* 写入数据.
*
* @return void
*/
public function fill()
{
$start_time = Carbon::now()->subDays(29)->startOfDay();
$end_time = Carbon::now()->endOfDay();
$mini_count = $this->miniprogramOrderCount($start_time, $end_time);
$admin_count = $this->adminOrderCount($start_time, $end_time);
$all_count = $mini_count+$admin_count;
$mini_rate = number_format(($all_count==0 ? 0:($mini_count/$all_count*100)), 2);
$admin_rate = number_format(($all_count==0 ? 0:($admin_count/$all_count*100)), 2);
$this->withContent($admin_rate, $mini_rate);
// 图表数据
$this->withChart([floatval($admin_rate), floatval($mini_rate)]);
}
/**
* 设置图表数据.
*
* @param array $data
*
* @return $this
*/
public function withChart(array $data)
{
return $this->chart([
'series' => $data
]);
}
/**
* 设置卡片头部内容.
*
* @param mixed $desktop
* @param mixed $mobile
*
* @return $this
*/
protected function withContent($desktop, $mobile)
{
$blue = Admin::color()->alpha('blue2', 0.5);
$style = 'margin-bottom: 8px';
$labelWidth = 120;
return $this->content(
<<<HTML
<div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
<div style="width: {$labelWidth}px">
<i class="fa fa-circle text-primary"></i> {$this->labels[0]}
</div>
<div>{$desktop}%</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style}">
<div style="width: {$labelWidth}px">
<i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
</div>
<div>{$mobile}%</div>
</div>
HTML
);
}
public function miniprogramOrderCount($start_time, $end_time){
return Order::where([
'order_status'=>1,
'pay_status'=>1,
])->where('pay_type', '<', 3)->whereBetween('created_at', [$start_time, $end_time])->count();
}
public function adminOrderCount($start_time, $end_time){
return Order::where([
'order_status'=>1,
'pay_status'=>1,
])->where('pay_type', 3)->whereBetween('created_at', [$start_time, $end_time])->count();
}
}