From dd6dd1e0308de72bd721b5156a18594b520d6f8d Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Wed, 21 Oct 2020 15:13:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B4=BB=E5=8A=A8=E5=95=86=E5=93=81=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1-=E8=AE=A1=E5=88=92=E6=B7=BB=E5=8A=A0=E5=9B=BE?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v3/GoodsActivityReportController.php | 45 +++-- ...oodsReport.php => GoodsActivityReport.php} | 11 +- .../Charts/OrderGoodsActivityColumnChart.php | 186 ++++++++++++++++++ app/Admin/routes.php | 2 +- 4 files changed, 219 insertions(+), 25 deletions(-) rename app/Admin/Repositories/v3/{GoodsReport.php => GoodsActivityReport.php} (82%) create mode 100644 app/Admin/Widgets/Charts/OrderGoodsActivityColumnChart.php diff --git a/app/Admin/Controllers/v3/GoodsActivityReportController.php b/app/Admin/Controllers/v3/GoodsActivityReportController.php index 6428a07..6439235 100644 --- a/app/Admin/Controllers/v3/GoodsActivityReportController.php +++ b/app/Admin/Controllers/v3/GoodsActivityReportController.php @@ -6,16 +6,27 @@ use App\Admin\Actions\Grid\v3\OrderReportLastMonth; use App\Admin\Actions\Grid\v3\OrderReportLastWeek; use App\Admin\Actions\Grid\v3\OrderReportToday; use App\Admin\Actions\Grid\v3\OrderReportYesterday; -use App\Admin\Repositories\v3\GoodsReport; + +use App\Admin\Repositories\v3\GoodsActivityReport; +use App\Admin\Widgets\Charts\OrderGoodsActivityColumnChart; use Dcat\Admin\Grid; use Dcat\Admin\Controllers\AdminController; use App\Models\v3\Market as MarketModel; use App\Models\v3\Store as StoreModel; use Dcat\Admin\Grid\Filter; +use Dcat\Admin\Layout\Content; +use Dcat\Admin\Layout\Row; class GoodsActivityReportController extends AdminController { - // public static $searchTime = []; + public $data = []; + public $repositories = null; + public function __construct() + { + $this->option = $this->id = 7; + $this->repositories = new GoodsActivityReport(); + $this->data = $this->repositories->data; + } /** * Make a grid builder. * @@ -23,7 +34,7 @@ class GoodsActivityReportController extends AdminController */ protected function grid() { - return Grid::make(new GoodsReport(), function (Grid $grid) { + return Grid::make($this->repositories, function (Grid $grid) { $marketList = MarketModel::getMarketArray(); $storeList = StoreModel::getStoreArray(); @@ -44,9 +55,7 @@ class GoodsActivityReportController extends AdminController })->width('12%'); // $grid->column('price'); // $grid->column('original_price'); - $grid->column('total','销量')->as(function(){ - return 0; - }); + $grid->column('total','销量'); $grid->filter(function (Filter $filter) use($marketList,$storeList) { // 更改为 panel 布局 @@ -80,19 +89,17 @@ class GoodsActivityReportController extends AdminController } /** - * 获取一个时间段内的日期 + * 页面 */ - public function periodDateArr($start_time,$end_time){ - $start_time = strtotime($start_time); - $end_time = strtotime($end_time); - $i=0; - $arr = []; - while ($start_time<=$end_time){ - $arr[date('Y-m-d',$start_time)]=date('Y年m月d日',$start_time); - $start_time = strtotime('+1 day',$start_time); - $i++; - } - - return array_reverse($arr); + public function index(Content $content) + { + return $content->title('活动商品统计') + ->body(function(Row $row){ + $row->column(12,$this->grid()); + }) + ->body(function(Row $row){ + $list = $this->data;dd($list); + $row->column(12,new OrderGoodsActivityColumnChart($list)); + }); } } diff --git a/app/Admin/Repositories/v3/GoodsReport.php b/app/Admin/Repositories/v3/GoodsActivityReport.php similarity index 82% rename from app/Admin/Repositories/v3/GoodsReport.php rename to app/Admin/Repositories/v3/GoodsActivityReport.php index 6c97b52..36c93e5 100644 --- a/app/Admin/Repositories/v3/GoodsReport.php +++ b/app/Admin/Repositories/v3/GoodsActivityReport.php @@ -6,10 +6,11 @@ use App\Models\LanzuOrderGoods as Model; use Dcat\Admin\Grid\Model as GridModel; use Dcat\Admin\Repositories\EloquentRepository; use Illuminate\Support\Facades\DB; +use App\Admin\Controllers\v3\GoodsActivityController; - -class GoodsReport extends EloquentRepository +class GoodsActivityReport extends EloquentRepository { + public $data = []; /** * Model. * @@ -34,7 +35,7 @@ class GoodsReport extends EloquentRepository $startTime = request()->input('start_time'); $endTime = request()->input('end_time'); - $orderGoodsActivity = Model::select(DB::raw('COUNT(lanzu_order_goods.id) as total,lanzu_order_goods.goods_id,lanzu_order_goods.name,lanzu_order_goods.cover_img,lanzu_order_main.market_id,lanzu_order.store_id')) + $orderGoodsActivity = Model::select(DB::raw('SUM(lanzu_order_goods.number) as total,lanzu_order_goods.goods_id,lanzu_order_goods.name,lanzu_order_goods.cover_img,lanzu_order_main.market_id,lanzu_order.store_id')) ->join('lanzu_order','lanzu_order_goods.order_id','=','lanzu_order.id') ->join('lanzu_order_main','lanzu_order.order_main_id','=','lanzu_order_main.global_order_id') ->where('lanzu_order_goods.activity_type',2) @@ -74,9 +75,9 @@ class GoodsReport extends EloquentRepository // $orderGoodsActivity->where([['lanzu_order_goods.created_at','>=',strtotime($todayStart)]]); // $orderGoodsActivity->where([['lanzu_order_goods.created_at','<=',strtotime($todayEnd)]]); // } - - $list = $orderGoodsActivity->groupBy('goods_id','lanzu_order_goods.name','cover_img','market_id','store_id')->paginate($perPage); + $list = $orderGoodsActivity->orderBy('total','desc')->groupBy('goods_id','lanzu_order_goods.name','cover_img','market_id','store_id')->paginate($perPage); $list = $list->toArray(); + $this->data = $list['data'] ?? []; return $model->makePaginator( $list['total'] ?? 0,$list['data'] ?? [] diff --git a/app/Admin/Widgets/Charts/OrderGoodsActivityColumnChart.php b/app/Admin/Widgets/Charts/OrderGoodsActivityColumnChart.php new file mode 100644 index 0000000..d4f02ae --- /dev/null +++ b/app/Admin/Widgets/Charts/OrderGoodsActivityColumnChart.php @@ -0,0 +1,186 @@ +option = $this->id = 7; + $this->data = $data; + parent::__construct($containerSelector, $options); + + $this->setUpOptions(); + } + + /** + * 初始化图表配置 + */ + protected function setUpOptions() + { + $color = Admin::color(); + + $this->options([ + 'chart' => [ + 'type' => 'bar', + 'height'=> 350, + ], + 'colors' => [ + '#33b2df', '#546E7A', '#d4526e', '#13d8aa', '#A5978B', '#2b908f', '#f9a3a4', '#90ee7e', '#f48024', '#69d2e7' + ,'#33b2df', '#546E7A', '#d4526e', '#13d8aa', '#A5978B', '#2b908f', '#f9a3a4', '#90ee7e', '#f48024', '#69d2e7' + ], + 'plotOptions'=> [ + 'bar'=> [ + 'distributed' =>false, // 柱状图颜色 + 'horizontal' => false, // 竖直 + 'columnWidth' => '50%', // 柱状宽 + 'dataLabels' => [ + 'position' => 'bottom' + ] + ] + ], + 'dataLabels' => [ + 'enabled'=> true, + ], + 'xaxis' => [ + 'categories' => $this->categories, + ], + 'yaxis' => [ + 'axisBorder' => [ + 'show' => true, + 'color' => $color->primary() + ], + 'labels' => [ + 'show' => true, + 'style' => [ + 'colors' => $color->primary() + ] + ], + 'title' => [ + 'text' => "销量", + 'align' => 'center', + 'style' => [ + 'color' => $color->primary() + ] + ] + ] + ]); + $number = 10; + // $this->chartOption( + // 'dataLabels.formatter', + // // 这个值最后段代码会作为JS代码执行 + // JavaScript::make("function () { return {$number}; }") + // ); + } + + /** + * 处理请求 + * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求 + * + * @param Request $request + * @return mixed|void + */ + public function handle(Request $request) + { + + switch ((int) $request->get('option')) { + case 30: + // 你的数据查询逻辑 + $data = [ + [ + 'name' => '活动商品', + 'data' => [54, 45, 41, 64, 22, 46, 52, 10, 9, 11, 54, 45, 41, 64, 22, 46, 52, 10, 9, 11] + ] + ]; + break; + case 28: + // 你的数据查询逻辑 + $data = [ + [ + 'name' => '活动商品', + 'data' => [44, 55, 41, 64, 22, 56, 52, 12, 52, 12, 44, 55, 41, 64, 22, 56, 52, 12, 52, 12] + ] + ]; + break; + case 7: + default: + // 你的数据查询逻辑 + $data = [ + [ + 'name' => '活动商品', + 'data' => [54, 45, 41, 64, 22, 46, 52, 10, 9, 11] + ] + ]; + break; + } + + $this->withData($data); + } + /** + * 处理图表数据 + */ + protected function buildData() + { + // 执行你的数据查询逻辑 + $data = [ + [ + 'name' => '活动商品', + 'data' => [44, 55, 41, 64, 22, 56, 52, 12, 52, 12] + ] + ]; + $categories = $this->categories; + + $this->withData($data); + $this->withCategories($categories); + } + + /** + * 这里返回需要异步传递到 handler 方法的参数 + * + * @return array + */ + public function parameters(): array + { + return [ + 'id' => $this->id, + 'option' => $this->option, + ]; + } + + /** + * 设置图表数据 + * + * @param array $data + * + * @return $this + */ + public function withData(array $data) + { + return $this->option('series', $data); + } + + /** + * 渲染图表 + * + * @return string + */ + public function render() + { + $this->buildData(); + + return parent::render(); + } + +} \ No newline at end of file diff --git a/app/Admin/routes.php b/app/Admin/routes.php index d8e1001..52a25ff 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -88,6 +88,6 @@ Route::group([ // 订单统计 $router->resource('/order_report', 'v3\OrderReportController'); // 活动商品统计 - $router->resource('/goods_activity_report', 'v3\GoodsActivityReportController'); + $router->any('/goods_activity_report', 'v3\GoodsActivityReportController@index'); });