157 lines
4.8 KiB
PHP
157 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Metrics;
|
|
|
|
use Dcat\Admin\Widgets\Metrics\Round;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Order;
|
|
use Carbon\Carbon;
|
|
|
|
class ProductOrders extends Round
|
|
{
|
|
protected $labels = ['完成订单', '支付订单', '取消订单'];
|
|
/**
|
|
* 初始化卡片内容
|
|
*/
|
|
protected function init()
|
|
{
|
|
parent::init();
|
|
|
|
$this->title('订单情况');
|
|
$this->chartLabels($this->labels);
|
|
$this->dropdown([
|
|
'7' => '7天内',
|
|
'30' => '30天内',
|
|
'365' => '今年',
|
|
// '365' => 'Last Year',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 处理请求
|
|
*
|
|
* @param Request $request
|
|
*
|
|
* @return mixed|void
|
|
*/
|
|
public function handle(Request $request)
|
|
{
|
|
switch ($request->get('option')) {
|
|
case '365':
|
|
$start_time = Carbon::now()->startOfYear();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
case '30':
|
|
$start_time = Carbon::now()->subDays(29)->startOfDay();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
case '7':
|
|
$start_time = Carbon::now()->subDays(6)->startOfDay();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
default:
|
|
$start_time = Carbon::now()->subDays(6)->startOfDay();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
}
|
|
$finished_count = $this->finishedOrderCount($start_time, $end_time);
|
|
$payed_count = $this->payedOrderCount($start_time, $end_time);
|
|
$canceled_count = $this->canceledOrderCount($start_time, $end_time);
|
|
// 卡片内容
|
|
$this->withContent($finished_count, $payed_count, $canceled_count);
|
|
|
|
$all_count = $this->orderCount($start_time, $end_time);
|
|
$finished_rate = number_format(($all_count==0 ? 0:($finished_count/$all_count*100)), 2);
|
|
$payed_rate = number_format(($all_count==0 ? 0:($payed_count/$all_count*100)), 2);
|
|
$canceled_rate = number_format(($all_count==0 ? 0:($canceled_count/$all_count*100)), 2);
|
|
// 图表数据
|
|
$this->withChart([$finished_rate, $payed_rate, $canceled_rate]);
|
|
|
|
// 总数
|
|
$this->chartTotal('总数', $all_count);
|
|
}
|
|
|
|
/**
|
|
* 设置图表数据.
|
|
*
|
|
* @param array $data
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function withChart(array $data)
|
|
{
|
|
return $this->chart([
|
|
'series' => $data,
|
|
]);
|
|
}
|
|
|
|
public function orderCount($start_time, $end_time){
|
|
return Order::whereBetween('created_at', [$start_time, $end_time])->count();
|
|
}
|
|
|
|
public function finishedOrderCount($start_time, $end_time){
|
|
return Order::where([
|
|
'order_status'=>1,
|
|
'pay_status'=>1,
|
|
'shipping_status'=>1
|
|
])->whereBetween('created_at', [$start_time, $end_time])->count();
|
|
}
|
|
|
|
public function payedOrderCount($start_time, $end_time){
|
|
return Order::where([
|
|
'order_status'=>1,
|
|
'pay_status'=>1,
|
|
])->whereBetween('created_at', [$start_time, $end_time])->count();
|
|
}
|
|
|
|
public function canceledOrderCount($start_time, $end_time){
|
|
return Order::where([
|
|
'order_status'=>-1,
|
|
])->whereBetween('created_at', [$start_time, $end_time])->count();
|
|
}
|
|
|
|
/**
|
|
* 卡片内容.
|
|
*
|
|
* @param int $finished
|
|
* @param int $pending
|
|
* @param int $rejected
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function withContent($finished, $pending, $rejected)
|
|
{
|
|
return $this->content(
|
|
<<<HTML
|
|
<div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
|
|
<div class="chart-info d-flex justify-content-between mb-1 mt-2" >
|
|
<div class="series-info d-flex align-items-center">
|
|
<i class="fa fa-circle-o text-bold-700 text-primary"></i>
|
|
<span class="text-bold-600 ml-50">{$this->labels[0]}</span>
|
|
</div>
|
|
<div class="product-result">
|
|
<span>{$finished}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="chart-info d-flex justify-content-between mb-1">
|
|
<div class="series-info d-flex align-items-center">
|
|
<i class="fa fa-circle-o text-bold-700 text-warning"></i>
|
|
<span class="text-bold-600 ml-50">{$this->labels[1]}</span>
|
|
</div>
|
|
<div class="product-result">
|
|
<span>{$pending}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="chart-info d-flex justify-content-between mb-1">
|
|
<div class="series-info d-flex align-items-center">
|
|
<i class="fa fa-circle-o text-bold-700 text-danger"></i>
|
|
<span class="text-bold-600 ml-50">{$this->labels[2]}</span>
|
|
</div>
|
|
<div class="product-result">
|
|
<span>{$rejected}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
HTML
|
|
);
|
|
}
|
|
}
|