diff --git a/app/Admin/Controllers/v3/GoodsActivityReportController.php b/app/Admin/Controllers/v3/GoodsActivityReportController.php index a0e4042..bb37090 100644 --- a/app/Admin/Controllers/v3/GoodsActivityReportController.php +++ b/app/Admin/Controllers/v3/GoodsActivityReportController.php @@ -17,6 +17,12 @@ use Dcat\Admin\Layout\Row; class GoodsActivityReportController extends AdminController { + public $totalData = []; + protected $GoodsActivityReport = null; + public function __construct() + { + $this->GoodsActivityReport = new GoodsActivityReport(); + } /** * Make a grid builder. @@ -25,7 +31,7 @@ class GoodsActivityReportController extends AdminController */ protected function grid() { - return Grid::make(new GoodsActivityReport(), function (Grid $grid) { + return Grid::make($this->GoodsActivityReport, function (Grid $grid) { $marketList = MarketModel::getMarketArray(); $storeList = StoreModel::getStoreArray(); @@ -44,9 +50,10 @@ class GoodsActivityReportController extends AdminController $item = StoreModel::getStoreInfo($storeId,'name'); return empty($item) ? '' : $item->name; })->width('12%'); - // $grid->column('price'); - // $grid->column('original_price'); + $grid->column('price'); + $grid->column('original_price'); $grid->column('total','销量'); + $grid->column('subsidy_total','补贴'); $grid->filter(function (Filter $filter) use($marketList,$storeList) { // 更改为 panel 布局 @@ -92,13 +99,11 @@ class GoodsActivityReportController extends AdminController { return $content->title('活动商品统计') ->body(function(Row $row){ - $row->column(4,new OrderGoodsActivityTotalChart()); + $row->column(6,new OrderGoodsActivityTotalChart($this->totalData)); + // $row->column(6,new OrderGoodsActivityColumnChart()); }) ->body(function(Row $row){ $row->column(12,$this->grid()); - }) - ->body(function(Row $row){ - // $row->column(12,new OrderGoodsActivityColumnChart()); }); } } diff --git a/app/Admin/Repositories/v3/GoodsActivityReport.php b/app/Admin/Repositories/v3/GoodsActivityReport.php index d2f480f..c9491e7 100644 --- a/app/Admin/Repositories/v3/GoodsActivityReport.php +++ b/app/Admin/Repositories/v3/GoodsActivityReport.php @@ -26,7 +26,11 @@ class GoodsActivityReport extends EloquentRepository $currentPage = $model->getCurrentPage(); // 获取每页显示行数 $perPage = $model->getPerPage(); - $list = $this->getData($perPage); + + $selects = 'SUM(number) as total,SUM((original_price-price)*number) as subsidy_total,price,original_price,lanzu_order_goods.goods_id,lanzu_order_goods.name,lanzu_order_goods.cover_img,lanzu_order_main.market_id,lanzu_order.store_id'; + $orderGoodsActivity = $this->getDataModel($selects, $perPage); + $list = $orderGoodsActivity->orderBy('total','desc')->groupBy('goods_id','lanzu_order_goods.name','cover_img','market_id','store_id','price','original_price')->paginate($perPage); + $list = $list->toArray(); return $model->makePaginator( $list['total'] ?? 0,$list['data'] ?? [] @@ -34,7 +38,7 @@ class GoodsActivityReport extends EloquentRepository } - public function getData($perPage = 10) + public function getDataModel($selects ,$perPage = 10) { // 获取筛选参数 $name = request()->input('name', ''); @@ -44,7 +48,7 @@ class GoodsActivityReport extends EloquentRepository $startTime = request()->input('start_time'); $endTime = request()->input('end_time'); - $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')) + $orderGoodsActivity = Model::select(DB::raw($selects)) ->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) @@ -83,8 +87,19 @@ class GoodsActivityReport extends EloquentRepository // $orderGoodsActivity->where([['lanzu_order_goods.created_at','>=',strtotime($todayStart)]]); // $orderGoodsActivity->where([['lanzu_order_goods.created_at','<=',strtotime($todayEnd)]]); // } - $list = $orderGoodsActivity->orderBy('total','desc')->groupBy('goods_id','lanzu_order_goods.name','cover_img','market_id','store_id')->paginate($perPage); - $list = $list->toArray(); - return $list; + + return $orderGoodsActivity; + } + + /** + * 获取总数 + */ + public function getCountData() + { + $selects = "SUM(lanzu_order_goods.number) as total,SUM((original_price-price)*number) as subsidy_total,FROM_UNIXTIME(lanzu_order_goods.created_at,'%Y-%m-%d') as dtime"; + $orderGoodsActivity = $this->getDataModel($selects); + $total = $orderGoodsActivity->groupBy('dtime')->get()->toArray(); + + return $total ?? []; } } diff --git a/app/Admin/Widgets/Charts/OrderGoodsActivityTotalChart.php b/app/Admin/Widgets/Charts/OrderGoodsActivityTotalChart.php index 3592a17..6bc2d70 100644 --- a/app/Admin/Widgets/Charts/OrderGoodsActivityTotalChart.php +++ b/app/Admin/Widgets/Charts/OrderGoodsActivityTotalChart.php @@ -2,33 +2,99 @@ namespace App\Admin\Widgets\Charts; -use Dcat\Admin\Widgets\Metrics\Card; +use App\Admin\Repositories\v3\GoodsActivityReport; +use Dcat\Admin\Admin; +use Dcat\Admin\Support\JavaScript; +use Dcat\Admin\Widgets\Metrics\Bar; use Illuminate\Http\Request; -class OrderGoodsActivityTotalChart extends Card +class OrderGoodsActivityTotalChart extends Bar { /** * 活动商品总数 */ + protected $categories = []; protected $data = []; + protected $total = []; protected $id; protected $option; + protected $GoodsActivityReport = null; + protected $valueData = []; public function __construct($data = []) { + $this->GoodsActivityReport = new GoodsActivityReport(); $this->option = $this->id = 7; - $this->data = $data; + + // 分页的时候不重复查询数据 + $currentPage = request()->input('page', 1); + if($currentPage == 1){ + $data = $this->GoodsActivityReport->getCountData(); + if(!empty($data) && is_array($data)){ + $total = 0; + $subsidyTotal = 0; + foreach($data as $key => $value){ + if($key < 10){ + $this->data[] = $value['total']; + $this->categories[] = $value['dtime']; + + $this->valueData[] = $value['subsidy_total']; + } + $total += $value['total']; + $subsidyTotal += $value['subsidy_total']; + } + $this->total['number_total'] = $total; + $this->total['subsidy_total'] = $subsidyTotal; + } + } + parent::__construct(); } protected function init() { parent::init(); - + $color = Admin::color(); // 设置标题 - $this->title('销售总数量'); + $this->title(''); // 设置下拉菜单 // $this->dropdown([]); + + // $this->chartBarColumnWidth('50%'); + // $this->style('display:block'); + + // 设置图表颜色 + $this->chartColors([$color->primary()]); + + $this->chartOption( + 'tooltip.x', + ['show' => true] + ); + + // $this->chartOption( + // 'tooltip.y.formatter', + // JavaScript::make("function (params) { + // return params; + // }") + // ); + $this->chartOption( + 'tooltip.trigger', + 'axis' + ); + $this->chartOption( + 'tooltip.formatter', + JavaScript::make("function (params) { + return 'ff'; + }") + ); + $this->chartOption( + 'xaxis.type', + 'category' + ); + $this->chartOption( + 'xaxis.categories', + $this->categories + ); } /** @@ -42,9 +108,17 @@ class OrderGoodsActivityTotalChart extends Card { // dd($request); // 数据查询逻辑 - $data = ['total' => $this->data['total'] ?? 0 ]; - - $this->withData($data); + $data = $this->data; + $categories = $this->categories; + $chartData = [ + [ + 'name' => '销量', + 'data' => $data, + ] + ]; + $this->withContent($this->total); + $this->withChart($chartData); + $this->withCategories($categories); } /** @@ -59,26 +133,47 @@ class OrderGoodsActivityTotalChart extends Card 'option' => $this->option, ]; } - - public function withData($data) + /** + * 设置图表数据. + * + * @param array $data + * + * @return $this + */ + public function withChart(array $data) { - $this->data = $data; + return $this->chart([ + 'series' => $data, + ]); } - /** + /** * 渲染卡片内容. * * @return string */ - public function renderContent() + public function withContent($total = []) { - $total = $this->data['total'] ?? 0 ; - - return << -

{$total}

+ $numberTotal = $this->total['number_total'] ?? 0; + $subsidyTotal = $this->total['subsidy_total'] ?? 0;//d-flex + return $this->content( + << +
+
+
销售总数量(单)
+
{$numberTotal}
+
+
+
+
+
总补贴金额(元)
+
{$subsidyTotal}
+
+
-HTML; +HTML + ); } } \ No newline at end of file