|
|
|
@ -2,94 +2,26 @@ |
|
|
|
|
|
|
|
namespace App\AdminAgent\Metrics\Examples; |
|
|
|
|
|
|
|
use App\Common\DataTime; |
|
|
|
use App\Common\OrderStatus; |
|
|
|
use App\Models\Order; |
|
|
|
use App\Models\User; |
|
|
|
use Dcat\Admin\Admin; |
|
|
|
use Dcat\Admin\Widgets\Metrics\Bar; |
|
|
|
use Illuminate\Http\Request; |
|
|
|
use Dcat\Admin\Widgets\ApexCharts\Chart; |
|
|
|
use Illuminate\Support\Arr; |
|
|
|
use Illuminate\Support\Facades\DB; |
|
|
|
|
|
|
|
class ProductStatistics extends Bar |
|
|
|
class ProductStatistics extends Chart |
|
|
|
{ |
|
|
|
/** |
|
|
|
* 初始化卡片内容 |
|
|
|
*/ |
|
|
|
protected function init() |
|
|
|
{ |
|
|
|
parent::init(); |
|
|
|
public function __construct() |
|
|
|
{ |
|
|
|
parent::__construct(); |
|
|
|
$this->setUpOptions(); |
|
|
|
} |
|
|
|
|
|
|
|
$color = Admin::color(); |
|
|
|
// 卡片内容宽度
|
|
|
|
$this->contentWidth(0, 12); |
|
|
|
// 标题
|
|
|
|
//$this->title('财务统计');
|
|
|
|
$this->chartHeight = 500; |
|
|
|
// 设置下拉选项
|
|
|
|
$this->dropdown([ |
|
|
|
'1' => '日', |
|
|
|
'30' => '月', |
|
|
|
'365' => '年', |
|
|
|
]); |
|
|
|
// 设置图表颜色
|
|
|
|
$this->chartColors([ |
|
|
|
$color->green(), |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 处理请求 |
|
|
|
* |
|
|
|
* @param Request $request |
|
|
|
* |
|
|
|
* @return mixed|void |
|
|
|
*/ |
|
|
|
public function handle(Request $request) |
|
|
|
{ |
|
|
|
$query = Order::query() |
|
|
|
->where('agent_id',Admin::user()->id) |
|
|
|
->select('*'); |
|
|
|
switch ($request->get('option')) { |
|
|
|
case '1': |
|
|
|
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); |
|
|
|
break; |
|
|
|
case '30': |
|
|
|
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time")); |
|
|
|
break; |
|
|
|
case '365': |
|
|
|
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time")); |
|
|
|
break; |
|
|
|
default: |
|
|
|
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); |
|
|
|
} |
|
|
|
$order = $query->groupBy('statistics_time') |
|
|
|
->orderBy('statistics_time') |
|
|
|
->get(); |
|
|
|
|
|
|
|
$this->withChart( |
|
|
|
Arr::pluck($order,'sum_price') |
|
|
|
); |
|
|
|
$this->chartLabels( |
|
|
|
Arr::pluck($order,'statistics_time') |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 设置图表数据. |
|
|
|
* |
|
|
|
* @param array $data |
|
|
|
* |
|
|
|
* @return $this |
|
|
|
*/ |
|
|
|
public function withChart(array $data) |
|
|
|
{ |
|
|
|
return $this->chart([ |
|
|
|
'series' => [[ |
|
|
|
'name' => '销量', |
|
|
|
'data' => $data |
|
|
|
]], |
|
|
|
/** |
|
|
|
* 初始化图表配置 |
|
|
|
*/ |
|
|
|
protected function setUpOptions() |
|
|
|
{ |
|
|
|
$this->options([ |
|
|
|
'chart' => [ |
|
|
|
//'width' => '180%',
|
|
|
|
'type' => 'bar', |
|
|
|
@ -97,7 +29,6 @@ class ProductStatistics extends Bar |
|
|
|
], |
|
|
|
'toolbar' => ['show' => false], |
|
|
|
], |
|
|
|
'colors' => $this->colors, |
|
|
|
'plotOptions' => [ |
|
|
|
'bar' => [ |
|
|
|
//'columnWidth' => '45%',
|
|
|
|
@ -117,7 +48,6 @@ class ProductStatistics extends Bar |
|
|
|
'labels' => [ |
|
|
|
'show' => true, |
|
|
|
'style' => [ |
|
|
|
'colors' => $this->colors, |
|
|
|
'fontSize' => '12px' |
|
|
|
] |
|
|
|
], |
|
|
|
@ -128,7 +58,85 @@ class ProductStatistics extends Bar |
|
|
|
'tooltip' => [ |
|
|
|
'x' => ['show' => true], |
|
|
|
], |
|
|
|
|
|
|
|
]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 处理图表数据 |
|
|
|
*/ |
|
|
|
protected function buildData() |
|
|
|
{ |
|
|
|
$query = Order::query() |
|
|
|
->where('agent_id', Admin::user()->id) |
|
|
|
->select('*'); |
|
|
|
$dateTime = request('created_at', 0); |
|
|
|
if ($dateTime) { |
|
|
|
$query->whereBetween('created_at',$dateTime); |
|
|
|
} |
|
|
|
switch (request('time_key', 0)) { |
|
|
|
case '1': |
|
|
|
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); |
|
|
|
break; |
|
|
|
case '30': |
|
|
|
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time")); |
|
|
|
break; |
|
|
|
case '365': |
|
|
|
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time")); |
|
|
|
break; |
|
|
|
default: |
|
|
|
$query->addSelect(DB::raw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); |
|
|
|
} |
|
|
|
$order = $query->groupBy('statistics_time') |
|
|
|
->orderBy('statistics_time') |
|
|
|
->get() |
|
|
|
->toArray(); |
|
|
|
|
|
|
|
$this->withData([ |
|
|
|
[ |
|
|
|
'name' => '销量', |
|
|
|
'data' => Arr::pluck($order,'sum_price') |
|
|
|
], |
|
|
|
] |
|
|
|
); |
|
|
|
$this->withCategories( |
|
|
|
Arr::pluck($order,'statistics_time') |
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 设置图表数据 |
|
|
|
* |
|
|
|
* @param array $data |
|
|
|
* |
|
|
|
* @return \App\AdminAgent\Metrics\Examples\ProductStatistics |
|
|
|
*/ |
|
|
|
public function withData(array $data) |
|
|
|
{ |
|
|
|
return $this->option('series', $data); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 设置图表类别. |
|
|
|
* |
|
|
|
* @param array $data |
|
|
|
* |
|
|
|
* @return $this |
|
|
|
*/ |
|
|
|
public function withCategories(array $data) |
|
|
|
{ |
|
|
|
return $this->option('xaxis.categories', $data); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 渲染图表 |
|
|
|
* |
|
|
|
* @return string |
|
|
|
*/ |
|
|
|
public function render() |
|
|
|
{ |
|
|
|
$this->buildData(); |
|
|
|
|
|
|
|
return parent::render(); |
|
|
|
} |
|
|
|
} |