From 1d16c6d1789b73cb6d25afde370debbdbe968a30 Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Mon, 6 Sep 2021 10:24:14 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E8=B4=A2=E5=8A=A1=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FinanceStatisticsController.php | 41 ++++++++++++------ .../Metrics/Examples/FinanceStatistics.php | 43 ++++++++++++++----- 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/app/AdminAgent/Controllers/FinanceStatisticsController.php b/app/AdminAgent/Controllers/FinanceStatisticsController.php index 78b3929..9a7bbbe 100755 --- a/app/AdminAgent/Controllers/FinanceStatisticsController.php +++ b/app/AdminAgent/Controllers/FinanceStatisticsController.php @@ -3,6 +3,9 @@ namespace App\AdminAgent\Controllers; use App\AdminAgent\Metrics\Examples\FinanceStatistics; +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; @@ -21,7 +24,20 @@ class FinanceStatisticsController extends AdminController } CSS ); - $count = $total = $totalNon = 0; + + //数据 + + //金额 + $price = Order::query()->where('agent_id',Admin::user()->id)->where('status',OrderStatus::SUCCESS)->sum('price'); + + //利润 + $costPrice = OrderProductItem::query()->where('agent_id',Admin::user()->id)->whereHas('order',function ($query) { + $query->where('status',OrderStatus::SUCCESS); + })->sum('price'); + $profit = bcsub($price,$costPrice,2); + + //已完成订单 + $count = Order::query()->where('agent_id',Admin::user()->id)->where('status',OrderStatus::SUCCESS)->count(); return $content ->body( <<body(function (Row $row) use ($count, $total, $totalNon) { + ->body(function (Row $row) use ($price, $profit, $count) { - $row->column(4, function (Column $column) use ($count) { - $column->row(Card::make('金额', function () use ($count) { + $row->column(4, function (Column $column) use ($price) { + $column->row(Card::make('金额', function () use ($price) { return << -

$count

+

$price

HTML; })); }); - $row->column(4, function (Column $column) use ($total) { - $column->row(Card::make('利润', function () use ($total) { + $row->column(4, function (Column $column) use ($profit) { + $column->row(Card::make('利润', function () use ($profit) { return << -

$total

+

$profit

HTML; })); }); - $row->column(4, function (Column $column) use ($totalNon) { - $column->row(Card::make('已完成订单', function () use ($totalNon) { + $row->column(4, function (Column $column) use ($count) { + $column->row(Card::make('已完成订单', function () use ($count) { return << -

$totalNon

+

$count

HTML; })); @@ -74,7 +90,8 @@ HTML; }) ->body(function (Row $row){ - $row->column(12,new FinanceStatistics()); + $row->column(12,new FinanceStatistics() + ); }); } } diff --git a/app/AdminAgent/Metrics/Examples/FinanceStatistics.php b/app/AdminAgent/Metrics/Examples/FinanceStatistics.php index fb7b1f5..72ebb02 100644 --- a/app/AdminAgent/Metrics/Examples/FinanceStatistics.php +++ b/app/AdminAgent/Metrics/Examples/FinanceStatistics.php @@ -2,9 +2,13 @@ namespace App\AdminAgent\Metrics\Examples; +use App\Common\OrderStatus; +use App\Models\Order; use Dcat\Admin\Admin; use Dcat\Admin\Widgets\Metrics\Bar; use Illuminate\Http\Request; +use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; class FinanceStatistics extends Bar { @@ -23,7 +27,7 @@ class FinanceStatistics extends Bar $this->chartHeight = 500; // 设置下拉选项 $this->dropdown([ - '7' => '日', + '1' => '日', '30' => '月', '365' => '年', ]); @@ -42,16 +46,33 @@ class FinanceStatistics extends Bar */ public function handle(Request $request) { + $query = Order::query() + ->where('agent_id',Admin::user()->id) + ->where('status',OrderStatus::SUCCESS) + ->select('*'); switch ($request->get('option')) { - case '7': + case '1': + $query->addSelect(DB::raw("sum(price) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); + break; + case '30': + $query->addSelect(DB::raw("sum(price) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time")); + break; + case '365': + $query->addSelect(DB::raw("sum(price) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time")); + break; default: - - - // 图表数据 - $this->withChart( - [75, 125, 225, 175, 125, 75, 25] - ); + $query->addSelect(DB::raw("sum(price) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); } + $order = $query->groupBy('statistics_time') + ->orderBy('created_at') + ->get() + ->toArray(); + $this->withChart( + Arr::pluck($order,'sum_price') + ); + $this->chartLabels( + Arr::pluck($order,'statistics_time') + ); } /** @@ -89,9 +110,9 @@ class FinanceStatistics extends Bar 'show' => false ], 'xaxis' => [ - 'categories' => - [75, 125, 225, 175, 125, 75, 25] - , + //'categories' => + // [75, 125, 225, 175, 125, 75, 25] + //, 'labels' => [ 'show' => true, 'style' => [ From f1ca0c962c736cf7596b1b2b9c1f9f51d7a8f252 Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Mon, 6 Sep 2021 10:38:24 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E8=B4=A2=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/AdminAgent/Controllers/FinanceStatisticsController.php | 6 +++--- app/AdminAgent/Metrics/Examples/FinanceStatistics.php | 2 +- app/Models/Order.php | 6 ++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/AdminAgent/Controllers/FinanceStatisticsController.php b/app/AdminAgent/Controllers/FinanceStatisticsController.php index 9a7bbbe..f433aaa 100755 --- a/app/AdminAgent/Controllers/FinanceStatisticsController.php +++ b/app/AdminAgent/Controllers/FinanceStatisticsController.php @@ -28,16 +28,16 @@ class FinanceStatisticsController extends AdminController //数据 //金额 - $price = Order::query()->where('agent_id',Admin::user()->id)->where('status',OrderStatus::SUCCESS)->sum('price'); + $price = Order::query()->where('agent_id',Admin::user()->id)->complete()->sum('price'); //利润 $costPrice = OrderProductItem::query()->where('agent_id',Admin::user()->id)->whereHas('order',function ($query) { - $query->where('status',OrderStatus::SUCCESS); + $query->complete(); })->sum('price'); $profit = bcsub($price,$costPrice,2); //已完成订单 - $count = Order::query()->where('agent_id',Admin::user()->id)->where('status',OrderStatus::SUCCESS)->count(); + $count = Order::query()->where('agent_id',Admin::user()->id)->complete()->count(); return $content ->body( <<where('agent_id',Admin::user()->id) - ->where('status',OrderStatus::SUCCESS) + ->complete() ->select('*'); switch ($request->get('option')) { case '1': diff --git a/app/Models/Order.php b/app/Models/Order.php index d6ccb67..c631c0a 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Common\OrderStatus; use App\Common\PayType; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; @@ -13,6 +14,11 @@ class Order extends BaseModel use HasFactory, SoftDeletes; protected $guarded = ['created_at', 'updated_at']; //不可批量赋值的属性 + public function scopeComplete($query) + { + return $query->where('status',OrderStatus::SUCCESS); + } + public function getCouponIdAttribute($value) { return explode(',', $value); From 716336759933072c1fb164f3a54ed834f7b362a0 Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Mon, 6 Sep 2021 11:57:09 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/OrderStatisticsController.php | 84 +++++++++++ .../Metrics/Examples/OrderStatistics.php | 133 ++++++++++++++++++ app/AdminAgent/routes.php | 1 + 3 files changed, 218 insertions(+) create mode 100755 app/AdminAgent/Controllers/OrderStatisticsController.php create mode 100644 app/AdminAgent/Metrics/Examples/OrderStatistics.php diff --git a/app/AdminAgent/Controllers/OrderStatisticsController.php b/app/AdminAgent/Controllers/OrderStatisticsController.php new file mode 100755 index 0000000..14d7fd5 --- /dev/null +++ b/app/AdminAgent/Controllers/OrderStatisticsController.php @@ -0,0 +1,84 @@ +body( + << + + + HTML + + ) + ->body(function (Row $row) { + $orders = Order::query()->where('agent_id',Admin::user()->id)->select('*')->selectRaw("count(id) as count_id")->groupBy('status')->get(); + $count = Order::query()->where('agent_id',Admin::user()->id)->count(); + + $row->column(3, function (Column $column) use ($count) { + $column->row(Card::make('总数', function () use ($count) { + return << +

$count

+ +HTML; + })); + }); + + $arr = OrderStatus::array(); + foreach ($orders as $order) { + $row->column(3, function (Column $column) use ($order,$arr) { + $column->row(Card::make($arr[$order->status], function () use ($order) { + return << +

$order->count_id

+ +HTML; + })); + }); + } + + }) + ->body(function (Row $row){ + $row->column(12,new OrderStatistics() + ); + }); + } +} diff --git a/app/AdminAgent/Metrics/Examples/OrderStatistics.php b/app/AdminAgent/Metrics/Examples/OrderStatistics.php new file mode 100644 index 0000000..09326cb --- /dev/null +++ b/app/AdminAgent/Metrics/Examples/OrderStatistics.php @@ -0,0 +1,133 @@ +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->selectRaw("count(id) as sum_price,date(created_at) AS statistics_time"); + break; + case '30': + $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"); + break; + case '365': + $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"); + break; + default: + $query->selectRaw("count(id) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"); + } + $order = $query->groupBy('statistics_time') + ->orderBy('created_at') + ->get() + ->toArray(); + $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 + ]], + 'chart' => [ + //'width' => '180%', + 'type' => 'bar', + 'events' => [ + ], + 'toolbar' => ['show' => false], + ], + 'colors' => $this->colors, + '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' => [ + 'colors' => $this->colors, + 'fontSize' => '12px' + ] + ], + ], + 'yaxis' => [ + 'show' => true + ], + 'tooltip' => [ + 'x' => ['show' => true], + ], + + ]); + } +} diff --git a/app/AdminAgent/routes.php b/app/AdminAgent/routes.php index c375c7d..b01d436 100644 --- a/app/AdminAgent/routes.php +++ b/app/AdminAgent/routes.php @@ -38,4 +38,5 @@ Route::group([ $router->resource('channel/list', 'ChannelController'); $router->resource('group_order/list', 'GroupOrderController'); $router->resource('finance_statistics', 'FinanceStatisticsController'); + $router->resource('order_statistics', 'OrderStatisticsController'); }); From c3269d6860626454f04bb76c7fc80b0d8eaaa94e Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Mon, 6 Sep 2021 14:32:02 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/UserStatisticsController.php | 57 +++++++ .../Metrics/Examples/UserStatistics.php | 142 ++++++++++++++++++ app/AdminAgent/routes.php | 1 + 3 files changed, 200 insertions(+) create mode 100755 app/AdminAgent/Controllers/UserStatisticsController.php create mode 100644 app/AdminAgent/Metrics/Examples/UserStatistics.php diff --git a/app/AdminAgent/Controllers/UserStatisticsController.php b/app/AdminAgent/Controllers/UserStatisticsController.php new file mode 100755 index 0000000..11d00bd --- /dev/null +++ b/app/AdminAgent/Controllers/UserStatisticsController.php @@ -0,0 +1,57 @@ +body( + << + + + HTML + + ) + ->body(function (Row $row){ + $row->column(12,new UserStatistics() + ); + }); + } +} diff --git a/app/AdminAgent/Metrics/Examples/UserStatistics.php b/app/AdminAgent/Metrics/Examples/UserStatistics.php new file mode 100644 index 0000000..c5b708e --- /dev/null +++ b/app/AdminAgent/Metrics/Examples/UserStatistics.php @@ -0,0 +1,142 @@ +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 = User::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")); + } + $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->withChart( + $userArr + ); + $this->chartLabels( + Arr::pluck($users,'statistics_time') + ); + } + + /** + * 设置图表数据. + * + * @param array $data + * + * @return $this + */ + public function withChart(array $data) + { + return $this->chart([ + 'series' => [[ + 'name' => '用户数', + 'data' => $data + ]], + 'chart' => [ + //'width' => '180%', + 'type' => 'bar', + 'events' => [ + ], + 'toolbar' => ['show' => false], + ], + 'colors' => $this->colors, + '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' => [ + 'colors' => $this->colors, + 'fontSize' => '12px' + ] + ], + ], + 'yaxis' => [ + 'show' => true + ], + 'tooltip' => [ + 'x' => ['show' => true], + ], + + ]); + } +} diff --git a/app/AdminAgent/routes.php b/app/AdminAgent/routes.php index b01d436..99fdbeb 100644 --- a/app/AdminAgent/routes.php +++ b/app/AdminAgent/routes.php @@ -39,4 +39,5 @@ Route::group([ $router->resource('group_order/list', 'GroupOrderController'); $router->resource('finance_statistics', 'FinanceStatisticsController'); $router->resource('order_statistics', 'OrderStatisticsController'); + $router->resource('user_statistics', 'UserStatisticsController'); }); From e4a50063249e6783f9c827586eb73cca3e259726 Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Mon, 6 Sep 2021 14:54:10 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=95=86=E5=93=81=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProductStatisticsController.php | 102 +++++++++++++ .../Metrics/Examples/ProductStatistics.php | 134 ++++++++++++++++++ app/AdminAgent/routes.php | 1 + 3 files changed, 237 insertions(+) create mode 100755 app/AdminAgent/Controllers/ProductStatisticsController.php create mode 100644 app/AdminAgent/Metrics/Examples/ProductStatistics.php diff --git a/app/AdminAgent/Controllers/ProductStatisticsController.php b/app/AdminAgent/Controllers/ProductStatisticsController.php new file mode 100755 index 0000000..412df87 --- /dev/null +++ b/app/AdminAgent/Controllers/ProductStatisticsController.php @@ -0,0 +1,102 @@ +body( + << + + + HTML + + ) + + ->body(function (Row $row){ + + $row->column(4, function (Column $column) { + $column->row(Card::make('总数', function () { + $count = AgentProduct::query()->where('agent_id',Admin::user()->id)->count(); + return << +

$count

+ +HTML; + })); + }); + + $row->column(4, function (Column $column) { + $column->row(Card::make('上架', function () { + $profit = AgentProduct::query()->where('agent_id',Admin::user()->id)->where('status',ProductStatus::ON_SALE)->count(); + return << +

$profit

+ +HTML; + })); + + }); + + $row->column(4, function (Column $column) { + $column->row(Card::make('下架', function () { + $unSale = AgentProduct::query() + ->where('agent_id',Admin::user()->id) + ->where('status',ProductStatus::SOLD_OUT) + ->count(); + return << +

$unSale

+ +HTML; + })); + + }); + + }) + ->body(function (Row $row){ + $row->column(12,new ProductStatistics() + ); + }); + } +} diff --git a/app/AdminAgent/Metrics/Examples/ProductStatistics.php b/app/AdminAgent/Metrics/Examples/ProductStatistics.php new file mode 100644 index 0000000..947d08d --- /dev/null +++ b/app/AdminAgent/Metrics/Examples/ProductStatistics.php @@ -0,0 +1,134 @@ +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 + ]], + 'chart' => [ + //'width' => '180%', + 'type' => 'bar', + 'events' => [ + ], + 'toolbar' => ['show' => false], + ], + 'colors' => $this->colors, + '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' => [ + 'colors' => $this->colors, + 'fontSize' => '12px' + ] + ], + ], + 'yaxis' => [ + 'show' => true + ], + 'tooltip' => [ + 'x' => ['show' => true], + ], + + ]); + } +} diff --git a/app/AdminAgent/routes.php b/app/AdminAgent/routes.php index 99fdbeb..eee2007 100644 --- a/app/AdminAgent/routes.php +++ b/app/AdminAgent/routes.php @@ -40,4 +40,5 @@ Route::group([ $router->resource('finance_statistics', 'FinanceStatisticsController'); $router->resource('order_statistics', 'OrderStatisticsController'); $router->resource('user_statistics', 'UserStatisticsController'); + $router->resource('product_statistics', 'ProductStatisticsController'); });