| 
						 | 
						<?php
namespace App\AdminAgent\Metrics\Examples;
use App\Common\OrderStatus;use App\Models\Order;use Carbon\Carbon;use Dcat\Admin\Admin;use Dcat\Admin\Widgets\Metrics\RadialBar;use Illuminate\Http\Request;use Illuminate\Support\Arr;use Illuminate\Support\Facades\DB;
class OrderExamples extends RadialBar{	/**	 * 初始化卡片内容	 */	protected function init()	{		parent::init();
		$this->title('订单统计');		$this->height(400);		$this->chartHeight(322);	}
	/**	 * 处理请求	 *	 * @param Request $request	 *	 * @return mixed|void	 */	public function handle(Request $request)	{		//总订单数
		$count = Order::query()->where('agent_id',Admin::user()->id)->count();		// 卡片内容
		$this->withContent($count);		// 卡片底部
		//今日订单
		$countToday = Order::query()->where('agent_id',Admin::user()->id)->whereDate('created_at',Carbon::today())->count();		$countYesterday = Order::query()->where('agent_id',Admin::user()->id)->whereDate('created_at',Carbon::yesterday())->count();		$price = Order::query()->where('agent_id',Admin::user()->id)->where('status',OrderStatus::SUCCESS)->whereDate('created_at',Carbon::today())->sum('price');		$this->withFooter($countToday, $countYesterday, $price);		$order = Order::query()			->select('*')			->addSelect(DB::raw("COUNT(id) as count_id,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"))			->where('agent_id',Admin::user()->id)			->limit(30)			->orderBy('created_at', 'asc')			->groupBy('statistics_time')			->get()			->toArray();
		$categories = Arr::pluck($order,'statistics_time');		// 图表数据
		$data = Arr::pluck($order,'count_id');		$this->withChart($data,$categories);
	}
	/**	 * 设置图表数据.	 *	 * @param int $data	 *	 * @return $this	 */	public function withChart(array $data,array $categories)	{		return $this->chart([			'series' => [				[					'name' => '订单量',					'data' => $data				]			],			'chart' => [				'type' => 'line',				'zoom' => [					'enabled' => false				],				'toolbar' => [					'show' => false				],			],			'colors' => [				Admin::color()->green(),			],			'dataLabels' => [				'enabled' => false			],			'stroke' => [				'curve' => 'smooth'			],			'legend' => [				'position' =>'top',				//'horizontalAlign' => 'right'
			],			'fill' => [				'opacity' => 1,				'type' => 'solid',			],			'xaxis' => [				'categories' => $categories,			]		]);	}
	/**	 * 卡片内容	 *	 * @param string $content	 *	 * @return $this	 */	public function withContent($content)	{		return $this->content(			<<<HTML<div class="d-flex flex-column flex-wrap text-left p-1">    <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>    <small>总订单数</small></div>HTML		);	}
	/**	 * 卡片底部内容.	 *	 * @param string $new	 * @param string $open	 * @param string $response	 *	 * @return $this	 */	public function withFooter($new, $open, $response)	{		return $this->footer(			<<<HTML<div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">    <div class="text-center">        <p>今日订单</p>        <span class="font-lg-1">{$new}</span>    </div>    <div class="text-center">        <p>昨日订单</p>        <span class="font-lg-1">{$open}</span>    </div>    <div class="text-center">        <p>今日成交(元)</p>        <span class="font-lg-1">{$response}</span>    </div></div>HTML		);	}}
  |