4 changed files with 555 additions and 0 deletions
-
163app/Admin/Controllers/ProductStatisticsController.php
-
102app/Admin/Controllers/UserStatisticsController.php
-
141app/Admin/Metrics/Examples/ProductStatistics.php
-
149app/Admin/Metrics/Examples/UserStatistics.php
@ -0,0 +1,163 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Admin\Controllers; |
||||
|
|
||||
|
use App\Admin\Actions\Tools\DataReportOption; |
||||
|
use App\Admin\Metrics\Examples\Index\CanteenStatisticsExamples; |
||||
|
use App\AdminAgent\Tools\DataReportDate; |
||||
|
use App\AdminAgent\Metrics\Examples\FinanceStatistics; |
||||
|
use App\AdminAgent\Metrics\Examples\OrderStatistics; |
||||
|
use App\AdminAgent\Metrics\Examples\ProductStatistics; |
||||
|
use App\AdminAgent\Metrics\Examples\UserStatistics; |
||||
|
use App\Common\OrderStatus; |
||||
|
use App\Common\ProductStatus; |
||||
|
use App\Models\AgentProduct; |
||||
|
use App\Models\ImsCjdcMarket; |
||||
|
use App\Models\Order; |
||||
|
use App\Models\OrderProductItem; |
||||
|
use Dcat\Admin\Admin; |
||||
|
use Dcat\Admin\Layout\Column; |
||||
|
use Dcat\Admin\Layout\Content; |
||||
|
use Dcat\Admin\Layout\Row; |
||||
|
use Dcat\Admin\Http\Controllers\AdminController; |
||||
|
use Dcat\Admin\Widgets\Box; |
||||
|
use Dcat\Admin\Widgets\Card; |
||||
|
use Dcat\Admin\Widgets\Dropdown; |
||||
|
use Illuminate\Support\Arr; |
||||
|
use Illuminate\Support\Str; |
||||
|
|
||||
|
class ProductStatisticsController extends AdminController |
||||
|
{ |
||||
|
public function index(Content $content) |
||||
|
{ |
||||
|
Admin::style( |
||||
|
<<<CSS |
||||
|
.col-sm-12.d-flex{ |
||||
|
display: inline-block !important; |
||||
|
} |
||||
|
CSS |
||||
|
); |
||||
|
|
||||
|
//数据
|
||||
|
|
||||
|
//订单
|
||||
|
|
||||
|
|
||||
|
return $content |
||||
|
->body( |
||||
|
<<<HTML |
||||
|
<div class="content-header"> |
||||
|
<section class="content-header breadcrumbs-top"> |
||||
|
<h1 class=" float-left"> |
||||
|
<span class="text-capitalize">商品统计</span> |
||||
|
|
||||
|
</h1> |
||||
|
<div class="clearfix"></div> |
||||
|
|
||||
|
</section> |
||||
|
</div> |
||||
|
HTML |
||||
|
|
||||
|
) |
||||
|
|
||||
|
->body(function (Row $row){ |
||||
|
$row->column(6,function (Column $column){ |
||||
|
$column->row(new \App\Admin\Tools\DataReportDate('data_report')); |
||||
|
}); |
||||
|
|
||||
|
}) |
||||
|
|
||||
|
->body(function (Row $row){ |
||||
|
$row->column(4, function (Column $column) { |
||||
|
$column->row(Card::make('总数', function () { |
||||
|
$dateTime = request()->get('created_at')??null; |
||||
|
$count = AgentProduct::query(); |
||||
|
if (!empty($dateTime)) { |
||||
|
$count->whereBetween('created_at',$dateTime); |
||||
|
} |
||||
|
$count = $count->count(); |
||||
|
return <<<HTML |
||||
|
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px"> |
||||
|
<h2 class="ml-1 font-large-1 text-primary">$count</h2> |
||||
|
</div> |
||||
|
HTML; |
||||
|
})); |
||||
|
}); |
||||
|
|
||||
|
$row->column(4, function (Column $column) { |
||||
|
$column->row(Card::make('上架', function () { |
||||
|
$dateTime = request()->get('created_at')??null; |
||||
|
$profit = AgentProduct::query()->where('status',ProductStatus::ON_SALE); |
||||
|
if (!empty($dateTime)) { |
||||
|
$profit->whereBetween('created_at',$dateTime); |
||||
|
} |
||||
|
$profit = $profit->count(); |
||||
|
return <<<HTML |
||||
|
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px"> |
||||
|
<h2 class="ml-1 font-large-1 text-primary">$profit</h2> |
||||
|
</div> |
||||
|
HTML; |
||||
|
})); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
$row->column(4, function (Column $column) { |
||||
|
$column->row(Card::make('下架', function () { |
||||
|
$unSale = AgentProduct::query() |
||||
|
->where('status',ProductStatus::SOLD_OUT); |
||||
|
$dateTime = request()->get('created_at')??null; |
||||
|
if (!empty($dateTime)) { |
||||
|
$unSale->whereBetween('created_at',$dateTime); |
||||
|
} |
||||
|
$unSale = $unSale->count(); |
||||
|
return <<<HTML |
||||
|
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px"> |
||||
|
<h2 class="ml-1 font-large-1 text-primary">$unSale</h2> |
||||
|
</div> |
||||
|
HTML; |
||||
|
})); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}) |
||||
|
->body(function (Row $row){ |
||||
|
// 构建下拉菜单,当点击菜单时发起请求获取数据重新渲染图表
|
||||
|
$menu = [ |
||||
|
'1' => '日', |
||||
|
'30' => '月', |
||||
|
'365' => '年', |
||||
|
]; |
||||
|
$buttonName = '日'; |
||||
|
if (Arr::exists($menu, \request()->input('time_key', ''))) { |
||||
|
$buttonName = $menu[\request()->input('time_key')]; |
||||
|
} |
||||
|
|
||||
|
$dropdown = Dropdown::make($menu) |
||||
|
->button(current($menu)) |
||||
|
->button($buttonName) |
||||
|
->click() |
||||
|
->map(function ($v, $k) { |
||||
|
$querys = \request()->all(); |
||||
|
$querys['time_key'] = $k; |
||||
|
$queryString = http_build_query($querys); |
||||
|
$str = Str::replaceFirst('admin/','',request()->path()); |
||||
|
$url = admin_url($str.'?'.$queryString); |
||||
|
// 此处设置的 data-xxx 属性会作为post数据发送到后端api
|
||||
|
return "<a class='switch-bar' data-option='{$k}' href='$url'>{$v}</a>"; |
||||
|
}); |
||||
|
|
||||
|
// 传递自定义参数
|
||||
|
|
||||
|
$bar = ProductStatistics::make() |
||||
|
->fetching('$("#my-box").loading()') // 设置loading效果
|
||||
|
->fetched('$("#my-box").loading(false)') // 移除loading效果
|
||||
|
->click('.switch-bar'); // 设置图表点击菜单则重新发起请求,且被点击的目标元素上的 data-xxx 属性会被作为post数据发送到后端API
|
||||
|
|
||||
|
$box = Box::make('成本总额图表', $bar) |
||||
|
->id('my-box') // 设置盒子的ID
|
||||
|
->tool($dropdown); // 设置下拉菜单按钮
|
||||
|
|
||||
|
$row->column(12, $box); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,102 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Admin\Controllers; |
||||
|
|
||||
|
use App\AdminAgent\Metrics\Examples\FinanceStatistics; |
||||
|
use App\AdminAgent\Metrics\Examples\OrderStatistics; |
||||
|
use App\AdminAgent\Metrics\Examples\UserStatistics; |
||||
|
use App\Common\OrderStatus; |
||||
|
use App\Models\Order; |
||||
|
use App\Models\OrderProductItem; |
||||
|
use Dcat\Admin\Admin; |
||||
|
use Dcat\Admin\Layout\Column; |
||||
|
use Dcat\Admin\Layout\Content; |
||||
|
use Dcat\Admin\Layout\Row; |
||||
|
use Dcat\Admin\Http\Controllers\AdminController; |
||||
|
use Dcat\Admin\Widgets\Box; |
||||
|
use Dcat\Admin\Widgets\Card; |
||||
|
use Dcat\Admin\Widgets\Dropdown; |
||||
|
use Illuminate\Support\Arr; |
||||
|
use Illuminate\Support\Str; |
||||
|
|
||||
|
class UserStatisticsController extends AdminController |
||||
|
{ |
||||
|
public function index(Content $content) |
||||
|
{ |
||||
|
Admin::style( |
||||
|
<<<CSS |
||||
|
.col-sm-12.d-flex{ |
||||
|
display: inline-block !important; |
||||
|
} |
||||
|
CSS |
||||
|
); |
||||
|
|
||||
|
//数据
|
||||
|
|
||||
|
//订单
|
||||
|
|
||||
|
|
||||
|
return $content |
||||
|
->body( |
||||
|
<<<HTML |
||||
|
<div class="content-header"> |
||||
|
<section class="content-header breadcrumbs-top"> |
||||
|
<h1 class=" float-left"> |
||||
|
<span class="text-capitalize">用户统计</span> |
||||
|
|
||||
|
</h1> |
||||
|
<div class="clearfix"></div> |
||||
|
|
||||
|
</section> |
||||
|
</div> |
||||
|
HTML |
||||
|
|
||||
|
) |
||||
|
->body(function (Row $row) { |
||||
|
$row->column(6, function (Column $column) { |
||||
|
$column->row(new \App\Admin\Tools\DataReportDate('data_report')); |
||||
|
}); |
||||
|
|
||||
|
}) |
||||
|
->body(function (Row $row) { |
||||
|
// 构建下拉菜单,当点击菜单时发起请求获取数据重新渲染图表
|
||||
|
$menu = [ |
||||
|
'1' => '日', |
||||
|
'30' => '月', |
||||
|
'365' => '年', |
||||
|
]; |
||||
|
$buttonName = '日'; |
||||
|
if (Arr::exists($menu, \request()->input('time_key', ''))) { |
||||
|
$buttonName = $menu[\request()->input('time_key')]; |
||||
|
} |
||||
|
|
||||
|
$dropdown = Dropdown::make($menu) |
||||
|
->button(current($menu)) |
||||
|
->button($buttonName) |
||||
|
->click() |
||||
|
->map(function ($v, $k) { |
||||
|
$querys = \request()->all(); |
||||
|
$querys['time_key'] = $k; |
||||
|
$queryString = http_build_query($querys); |
||||
|
$str = Str::replaceFirst('admin/', '', request()->path()); |
||||
|
$url = admin_url($str . '?' . $queryString); |
||||
|
// 此处设置的 data-xxx 属性会作为post数据发送到后端api
|
||||
|
return "<a class='switch-bar' data-option='{$k}' href='$url'>{$v}</a>"; |
||||
|
}); |
||||
|
|
||||
|
// 传递自定义参数
|
||||
|
|
||||
|
$bar = UserStatistics::make() |
||||
|
->fetching('$("#my-box").loading()') // 设置loading效果
|
||||
|
->fetched('$("#my-box").loading(false)') // 移除loading效果
|
||||
|
->click('.switch-bar'); // 设置图表点击菜单则重新发起请求,且被点击的目标元素上的 data-xxx 属性会被作为post数据发送到后端API
|
||||
|
|
||||
|
$box = Box::make('成本总额图表', $bar) |
||||
|
->id('my-box') // 设置盒子的ID
|
||||
|
->tool($dropdown); // 设置下拉菜单按钮
|
||||
|
|
||||
|
$row->column(12, $box); |
||||
|
}); |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,141 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Admin\Metrics\Examples; |
||||
|
|
||||
|
use App\Models\Order; |
||||
|
use Dcat\Admin\Admin; |
||||
|
use Dcat\Admin\Widgets\ApexCharts\Chart; |
||||
|
use Illuminate\Support\Arr; |
||||
|
use Illuminate\Support\Facades\DB; |
||||
|
|
||||
|
class ProductStatistics extends Chart |
||||
|
{ |
||||
|
public function __construct() |
||||
|
{ |
||||
|
parent::__construct(); |
||||
|
$this->setUpOptions(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 初始化图表配置 |
||||
|
*/ |
||||
|
protected function setUpOptions() |
||||
|
{ |
||||
|
$this->options([ |
||||
|
'chart' => [ |
||||
|
//'width' => '180%',
|
||||
|
'type' => 'bar', |
||||
|
'events' => [ |
||||
|
], |
||||
|
'toolbar' => ['show' => false], |
||||
|
], |
||||
|
'plotOptions' => [ |
||||
|
'bar' => [ |
||||
|
//'columnWidth' => '45%',
|
||||
|
'distributed' => true, |
||||
|
] |
||||
|
], |
||||
|
'dataLabels' => [ |
||||
|
'enabled' => false |
||||
|
], |
||||
|
'legend' => [ |
||||
|
'show' => false |
||||
|
], |
||||
|
'xaxis' => [ |
||||
|
//'categories' =>
|
||||
|
// [75, 125, 225, 175, 125, 75, 25]
|
||||
|
//,
|
||||
|
'labels' => [ |
||||
|
'show' => true, |
||||
|
'style' => [ |
||||
|
'fontSize' => '12px' |
||||
|
] |
||||
|
], |
||||
|
], |
||||
|
'yaxis' => [ |
||||
|
'show' => true |
||||
|
], |
||||
|
'tooltip' => [ |
||||
|
'x' => ['show' => true], |
||||
|
], |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 处理图表数据 |
||||
|
*/ |
||||
|
protected function buildData() |
||||
|
{ |
||||
|
$query = Order::query() |
||||
|
->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(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,149 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Admin\Metrics\Examples; |
||||
|
|
||||
|
use App\Common\OrderStatus; |
||||
|
use App\Models\Order; |
||||
|
use App\Models\User; |
||||
|
use Dcat\Admin\Admin; |
||||
|
use Dcat\Admin\Widgets\ApexCharts\Chart; |
||||
|
use Dcat\Admin\Widgets\Metrics\Bar; |
||||
|
use Illuminate\Http\Request; |
||||
|
use Illuminate\Support\Arr; |
||||
|
use Illuminate\Support\Facades\DB; |
||||
|
|
||||
|
class UserStatistics extends Chart |
||||
|
{ |
||||
|
public function __construct() |
||||
|
{ |
||||
|
parent::__construct(); |
||||
|
$this->setUpOptions(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 初始化图表配置 |
||||
|
*/ |
||||
|
protected function setUpOptions() |
||||
|
{ |
||||
|
$this->options([ |
||||
|
'chart' => [ |
||||
|
//'width' => '180%',
|
||||
|
'type' => 'bar', |
||||
|
'events' => [ |
||||
|
], |
||||
|
'toolbar' => ['show' => false], |
||||
|
], |
||||
|
'plotOptions' => [ |
||||
|
'bar' => [ |
||||
|
//'columnWidth' => '45%',
|
||||
|
'distributed' => true, |
||||
|
] |
||||
|
], |
||||
|
'dataLabels' => [ |
||||
|
'enabled' => false |
||||
|
], |
||||
|
'legend' => [ |
||||
|
'show' => false |
||||
|
], |
||||
|
'xaxis' => [ |
||||
|
//'categories' =>
|
||||
|
// [75, 125, 225, 175, 125, 75, 25]
|
||||
|
//,
|
||||
|
'labels' => [ |
||||
|
'show' => true, |
||||
|
'style' => [ |
||||
|
'fontSize' => '12px' |
||||
|
] |
||||
|
], |
||||
|
], |
||||
|
'yaxis' => [ |
||||
|
'show' => true |
||||
|
], |
||||
|
'tooltip' => [ |
||||
|
'x' => ['show' => true], |
||||
|
], |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 处理图表数据 |
||||
|
*/ |
||||
|
protected function buildData() |
||||
|
{ |
||||
|
$query = User::query() |
||||
|
->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")); |
||||
|
} |
||||
|
$users = $query->groupBy('statistics_time') |
||||
|
->orderBy('statistics_time') |
||||
|
->get(); |
||||
|
|
||||
|
$userNum = 0; |
||||
|
$userArr = []; |
||||
|
|
||||
|
foreach ($users as $user) { |
||||
|
$userNum += $user->sum_price; |
||||
|
array_push($userArr,$userNum); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
$this->withData([ |
||||
|
[ |
||||
|
'name' => '用户数', |
||||
|
'data' => $userArr |
||||
|
], |
||||
|
] |
||||
|
); |
||||
|
$this->withCategories( |
||||
|
Arr::pluck($users,'statistics_time') |
||||
|
); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
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(); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue