From 807c466b21ee5331ebe97a41c5485b26a941ef3d Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Mon, 6 Sep 2021 21:54:49 +0800 Subject: [PATCH] =?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 | 157 ++++++++++++++++++ .../Metrics/Examples/FinanceStatistics.php | 145 ++++++++++++++++ app/AdminSupplier/Tools/DataReportDate.php | 111 +++++++++++++ app/AdminSupplier/routes.php | 4 + 4 files changed, 417 insertions(+) create mode 100755 app/AdminSupplier/Controllers/FinanceStatisticsController.php create mode 100644 app/AdminSupplier/Metrics/Examples/FinanceStatistics.php create mode 100644 app/AdminSupplier/Tools/DataReportDate.php diff --git a/app/AdminSupplier/Controllers/FinanceStatisticsController.php b/app/AdminSupplier/Controllers/FinanceStatisticsController.php new file mode 100755 index 0000000..779a9d8 --- /dev/null +++ b/app/AdminSupplier/Controllers/FinanceStatisticsController.php @@ -0,0 +1,157 @@ +where('supplier_id',Admin::user()->id)->whereHas('order',function ($query) { + $query->complete(); + }); + //利润 + $costPrice = OrderProductItem::query()->where('supplier_id',Admin::user()->id)->whereHas('order',function ($query) { + $query->complete(); + }); + + //已完成订单 + $count = OrderProductItem::query()->where('supplier_id',Admin::user()->id)->whereHas('order',function ($query) { + $query->complete(); + }); + + $dateTime = request('created_at', 0); + if ($dateTime) { + $price->whereBetween('created_at',$dateTime); + $costPrice->whereBetween('created_at',$dateTime); + $count->whereBetween('created_at',$dateTime); + } + + $price = $price->sum('price'); + $costPrice = $costPrice->sum('price'); + $profit = bcsub($price,$costPrice,2); + $count = $count->count(); + return $content + ->body( + << + + + HTML + + ) + ->body(function (Row $row){ + $row->column(6,function (Column $column){ + $column->row(new \App\AdminSupplier\Tools\DataReportDate('data_report')); + }); + + }) + ->body(function (Row $row) use ($price, $profit, $count) { + + $row->column(4, function (Column $column) use ($price) { + $column->row(Card::make('金额', function () use ($price) { + return << +

$price

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

$profit

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

$count

+ +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-agent/','',request()->path()); + $url = admin_url($str.'?'.$queryString); + // 此处设置的 data-xxx 属性会作为post数据发送到后端api + return "{$v}"; + }); + + // 传递自定义参数 + + $bar = FinanceStatistics::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); + }); + } +} diff --git a/app/AdminSupplier/Metrics/Examples/FinanceStatistics.php b/app/AdminSupplier/Metrics/Examples/FinanceStatistics.php new file mode 100644 index 0000000..3ad7227 --- /dev/null +++ b/app/AdminSupplier/Metrics/Examples/FinanceStatistics.php @@ -0,0 +1,145 @@ +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 = OrderProductItem::query() + ->where('supplier_id',Admin::user()->id) + ->whereHas('order',function ($query) { + $query->complete(); + }) + ->select('*'); + + $dateTime = request('created_at', 0); + if ($dateTime) { + $query->whereBetween('created_at',$dateTime); + } + + switch (request('time_key', 0)) { + 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: + $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->withData([ + [ + 'name' => '金额', + 'data' => Arr::pluck($order,'sum_price') + ], + ] + ); + $this->withCategories( + Arr::pluck($order,'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(); + } +} diff --git a/app/AdminSupplier/Tools/DataReportDate.php b/app/AdminSupplier/Tools/DataReportDate.php new file mode 100644 index 0000000..593ac06 --- /dev/null +++ b/app/AdminSupplier/Tools/DataReportDate.php @@ -0,0 +1,111 @@ +route = $route; + parent::__construct(); + } + + protected function script() + { + $url = $path = request()->url(); + $fullUrl = request()->fullUrl(); + $date = request()->input('created_at'); + $start = $date?$date['start']:''; + $end = $date?$date['end']:''; + return << Date.now(); + }, + shortcuts: [{ + text: '今天', + onClick(picker) { + picker.\$emit('pick', new Date()); + } + }, { + text: '昨天', + onClick(picker) { + const date = new Date(); + date.setTime(date.getTime() - 3600 * 1000 * 24); + picker.\$emit('pick', date); + } + }, { + text: '一周前', + onClick(picker) { + const date = new Date(); + date.setTime(date.getTime() - 3600 * 1000 * 24 * 7); + picker.\$emit('pick', date); + } + }] + }, + }, + methods:{ + doSearch(){ + + var url = this.url; + if (this.start){ + url = url+"?type=date&option=0&created_at[start]="+this.start; + } + if (this.end){ + if (!this.start){ + this.\$message({message:'请选择起始时间',type:"error"}) + return false; + } + url = url+"&created_at[end]="+this.end; + }else { + if (this.start){ + this.\$message({message:'请选择截止时间',type:"error"}) + return false; + } + } + + if (this.start&&this.end&&(this.start>this.end)){ + this.\$message({message:'截止时间不能小于起始时间',type:"error"}) + return false; + } + + window.location.href=url; + }, + reset(){ + window.location.href=this.path; + } + } + + }); + +JS; + + } + + public function render() + { + Admin::js(LinkUrl::VUE_JS); + Admin::js(LinkUrl::ELEMENT_UI_JS); + Admin::css(LinkUrl::ELEMENT_UI_CSS); + Admin::css('css/data_report.css'); + Admin::script($this->script()); + return view('admin.tools.data_report_date'); + } +} diff --git a/app/AdminSupplier/routes.php b/app/AdminSupplier/routes.php index 9cc76b4..7f7b9c9 100644 --- a/app/AdminSupplier/routes.php +++ b/app/AdminSupplier/routes.php @@ -25,4 +25,8 @@ Route::group([ $router->resource('demand_product', 'DemandProductController'); $router->resource('my_demand_product', 'MyDemandProductController'); $router->resource('my_bidding_product', 'MyBiddingProductController'); + + $router->resource('finance_statistics', 'FinanceStatisticsController'); + $router->resource('order_statistics', 'OrderStatisticsController'); + $router->resource('product_statistics', 'ProductStatisticsController'); });