diff --git a/app/Admin/Actions/Tools/GoodsReportExport.php b/app/Admin/Actions/Tools/GoodsReportExport.php new file mode 100644 index 0000000..a517241 --- /dev/null +++ b/app/Admin/Actions/Tools/GoodsReportExport.php @@ -0,0 +1,84 @@ +导出'; + + /** + * Handle the action request. + * + * @param Request $request + * + * @return Response + */ + public function handle(Request $request) + { + $url = '/goods_report_export?'; + $name = $request->get('name', ''); + $marketId = $request->get('market_id',0); + $storeId = $request->get('store_id',0); + $startTime = $request->get('start_time',''); + $endTime = $request->get('end_time',''); + + if(!empty($name)){ + $url .= '&name='.$name; + } + if(!empty($marketId)){ + $url .= '&market_id='.$marketId; + } + if(!empty($storeId)){ + $url .= '&store_id='.$storeId; + } + if(!empty($startTime)){ + $url .= '&start_time='.$startTime; + } + + if(!empty($endTime)){ + $url .= '&end_time='.$endTime; + } + return $this->response() + ->success('导出中~') + ->redirect($url); + } + public function parameters() + { + return [ + 'page' => request()->input('page', 1), + 'name' => request()->input('name', ''), + 'market_id' => request()->input('market_id',0), + 'store_id' => request()->input('store_id',0), + 'start_time' => request()->input('start_time',''), + 'end_time' => request()->input('end_time',''), + ]; + } + /** + * @return string|array|void + */ + public function confirm() + { + return '确定导出数据吗?'; + } + + /** + * @param Model|Authenticatable|HasPermissions|null $user + * + * @return bool + */ + protected function authorize($user): bool + { + return true; + } +} diff --git a/app/Admin/Controllers/v3/GoodsReportController.php b/app/Admin/Controllers/v3/GoodsReportController.php new file mode 100644 index 0000000..133c9fc --- /dev/null +++ b/app/Admin/Controllers/v3/GoodsReportController.php @@ -0,0 +1,123 @@ +marketList; + $storeList = $this->storeList; + $grid->column('goods_id')->sortable(); + $grid->column('cover_img')->image('',50); + $grid->column('name','商品名称'); + + $grid->column('market_id')->display(function($marketId){ + $item = MarketModel::getMarketInfo($marketId,'name'); + return $item->name ?? ''; + }); + $grid->column('store_id')->display(function($storeId){ + $item = StoreModel::getStoreInfo($storeId,'name'); + return $item->name ?? ''; + })->width('12%'); + $grid->column('price'); + $grid->column('original_price'); + $grid->column('total','销量'); + + $grid->filter(function (Filter $filter) use($marketList,$storeList) { + // 更改为 panel 布局 + $filter->panel(); + $filter->equal('start_time','开始时间')->date()->width(2); + $filter->equal('end_time','结束时间')->date()->width(2); + $filter->equal('name','商品名称')->width(3); + if(!$this->marketId){ + $filter->equal('market_id','市场')->select($marketList)->width(2); + } + $filter->equal('store_id','店铺')->select($storeList)->width(3); + }); + + $grid->tools([ + new DataReportOption('today','goods_report','今日'), + new DataReportOption('yesterday','goods_report','昨日'), + new DataReportOption('this_week','goods_report','本周'), + new DataReportOption('last_week','goods_report','上周'), + new DataReportOption('this_month','goods_report','本月'), + new DataReportOption('last_month','goods_report','上月'), + new GoodsReportExport() + ]); + + // 每页1条 + $grid->paginate(10); + $grid->disableCreateButton(); + $grid->disableBatchActions(); + $grid->disableBatchDelete(); + + $grid->toolsWithOutline(); + + $grid->disableDeleteButton(); + $grid->disableEditButton(); + $grid->disableQuickEditButton(); + $grid->disableViewButton(); + $grid->disableActions(); + + $grid->disableRowSelector(); + }); + } + + /** + * 数据导出 + * @return \Symfony\Component\HttpFoundation\BinaryFileResponse + */ + public function export(Request $request) + { + $this->marketId = Auth::getMarket(); + $params = [ + 'page' => request()->input('page', 1), + 'name' => request()->input('name', ''), + 'market_id' => $this->marketId ? $this->marketId : request()->input('market_id',0), + 'store_id' => request()->input('store_id',0), + 'start_time' => request()->input('start_time',''), + 'end_time' => request()->input('end_time',''), + ]; + + $name = date('Y-m-d-His',time()); + $data = new \App\Admin\Actions\Exporter\GoodsActivitySales($params); + if(empty($data)){ + return $this->error('没有数据!'); + } + return Excel::download($data, $name.'.xlsx'); + } +} diff --git a/app/Admin/Repositories/v3/GoodsReport.php b/app/Admin/Repositories/v3/GoodsReport.php new file mode 100644 index 0000000..a837331 --- /dev/null +++ b/app/Admin/Repositories/v3/GoodsReport.php @@ -0,0 +1,128 @@ +params = $params; + } + + /** + * 获取统计列表数据 + */ + public function get(GridModel $model) + { + // 获取当前页数 + $currentPage = $model->getCurrentPage(); + // 获取每页显示行数 + $perPage = $model->getPerPage(); + + $where = []; + if(!empty($this->params) && isset($this->params['market_id']) && !empty($this->params['market_id'])){ + $where = ['market_id'=>$this->params['market_id']]; + } + + $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,$where); + $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'] ?? [] + ); + + } + + public function getDataModel($selects,$params = []) + { + $time = date('Y-m-d',time()); + // 获取筛选参数 + $name = $params['name'] ?? request()->input('name', ''); + $marketId = $params['market_id'] ?? request()->input('market_id',0); + $storeId = $params['store_id'] ?? request()->input('store_id',0); + + $startTime = $params['start_time'] ?? request()->input('start_time',''); + $endTime = $params['end_time'] ?? request()->input('end_time',''); + + $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',1) + ->where('lanzu_order_goods.status',1) + ->whereIn('lanzu_order_main.state',[4,5,10,11]); + + if($name){ + $orderGoodsActivity->where('lanzu_order_goods.name','like',"%$name%"); + } + if($marketId){ + $orderGoodsActivity->where('market_id',$marketId); + } + if($storeId){ + $orderGoodsActivity->where('store_id',$storeId); + } + if($startTime){ + $date = $startTime; + $startTime = $startTime.' 00:00:00'; + + $orderGoodsActivity->where('lanzu_order_goods.created_at','>=',strtotime($startTime)); + }else{ + $date = '2020-06-01'; + } + if($endTime){ + $date = $date.' 至 '.$endTime; + $endTime = $endTime.' 23:59:59'; + + $orderGoodsActivity->where('lanzu_order_goods.created_at','<=',strtotime($endTime)); + }else{ + $date .= ' 至 '.$time; + } + // if(empty($startTime) && empty($endTime)){ + // $date = $time; + // $todayStart = $time.' 00:00:00'; + // $todayEnd = $time.' 23:59:59'; + // $orderGoodsActivity->where([['lanzu_order_goods.created_at','>=',strtotime($todayStart)]]); + // $orderGoodsActivity->where([['lanzu_order_goods.created_at','<=',strtotime($todayEnd)]]); + // } + + return $orderGoodsActivity; + } + + /** + * 获取总数 + */ + public function getCountData($params = []) + { + $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,$params); + $total = $orderGoodsActivity->orderBy('dtime','asc')->groupBy('dtime')->get()->toArray(); + + return $total ?? []; + } + + /** + * 分市场获取 + */ + public function getMarketData($params = []) + { + $selects = "SUM(lanzu_order_goods.number) as total,SUM((original_price-price)*number) as subsidy_total,market_id"; + $orderGoodsActivity = $this->getDataModel($selects,$params); + $total = $orderGoodsActivity->groupBy('market_id')->get()->toArray(); + + return $total ?? []; + } +} diff --git a/app/Admin/routes.php b/app/Admin/routes.php index db8ebcb..0d8419a 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -91,5 +91,7 @@ Route::group([ // 活动商品统计 $router->any('/goods_activity_report', 'v3\GoodsActivityReportController@index'); $router->any('/goods_activity_export', 'v3\GoodsActivityReportController@export'); + //普通商品统计 + $router->resource('/goods_report', 'v3\GoodsReportController'); }); diff --git a/resources/lang/zh-CN/goods-report.php b/resources/lang/zh-CN/goods-report.php new file mode 100644 index 0000000..bbf5f73 --- /dev/null +++ b/resources/lang/zh-CN/goods-report.php @@ -0,0 +1,19 @@ + [ + 'GoodsReport' => '普通商品统计报表', + 'goods_report' => '普通商品统计报表', + ], + 'fields' => [ + 'goods_id' => 'ID', + 'name' => '统计名称', + 'cover_img' => '封面图', + 'market_id' => '市场', + 'store_id' => '店铺', + 'price' => '售价', + 'original_price' => '原价', + 'total' => '统计数量', + ], + 'options' => [ + ], +];