5 changed files with 356 additions and 0 deletions
-
84app/Admin/Actions/Tools/GoodsReportExport.php
-
123app/Admin/Controllers/v3/GoodsReportController.php
-
128app/Admin/Repositories/v3/GoodsReport.php
-
2app/Admin/routes.php
-
19resources/lang/zh-CN/goods-report.php
@ -0,0 +1,84 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Admin\Actions\Tools; |
||||
|
|
||||
|
use Dcat\Admin\Actions\Response; |
||||
|
use Dcat\Admin\Traits\HasPermissions; |
||||
|
use Dcat\Admin\Tree\AbstractTool; |
||||
|
use Illuminate\Contracts\Auth\Authenticatable; |
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
use Illuminate\Http\Request; |
||||
|
|
||||
|
class GoodsReportExport extends AbstractTool |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* @return string |
||||
|
*/ |
||||
|
protected $title = '<span class="feather icon-download">导出<span>'; |
||||
|
|
||||
|
/** |
||||
|
* 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; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,123 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Admin\Controllers\v3; |
||||
|
|
||||
|
use App\Admin\Actions\Grid\v3\DataReportOption; |
||||
|
use App\Admin\Actions\Tools\GoodsReportExport; |
||||
|
use App\Admin\Common\Auth; |
||||
|
use App\Admin\Repositories\v3\GoodsReport; |
||||
|
|
||||
|
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; |
||||
|
|
||||
|
use Illuminate\Http\Request; |
||||
|
use Maatwebsite\Excel\Facades\Excel; |
||||
|
|
||||
|
class GoodsReportController extends AdminController |
||||
|
{ |
||||
|
protected $GoodsActivityReport = null; |
||||
|
public $marketId = 0; |
||||
|
public $newParams = []; |
||||
|
public $marketList = []; |
||||
|
public $storeList = []; |
||||
|
public function __construct() |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Make a grid builder. |
||||
|
* |
||||
|
* @return Grid |
||||
|
*/ |
||||
|
protected function grid() |
||||
|
{ |
||||
|
|
||||
|
return Grid::make(new GoodsReport(), function (Grid $grid) { |
||||
|
$marketList = $this->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'); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,128 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Admin\Repositories\v3; |
||||
|
|
||||
|
use App\Models\LanzuOrderGoods as Model; |
||||
|
use Dcat\Admin\Grid\Model as GridModel; |
||||
|
use Dcat\Admin\Repositories\EloquentRepository; |
||||
|
use Illuminate\Support\Facades\DB; |
||||
|
|
||||
|
class GoodsReport extends EloquentRepository |
||||
|
{ |
||||
|
public $params = []; |
||||
|
/** |
||||
|
* Model. |
||||
|
* |
||||
|
* @var string |
||||
|
*/ |
||||
|
protected $eloquentClass = Model::class; |
||||
|
|
||||
|
public function __construct($params = []) |
||||
|
{ |
||||
|
$this->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 ?? []; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
<?php |
||||
|
return [ |
||||
|
'labels' => [ |
||||
|
'GoodsReport' => '普通商品统计报表', |
||||
|
'goods_report' => '普通商品统计报表', |
||||
|
], |
||||
|
'fields' => [ |
||||
|
'goods_id' => 'ID', |
||||
|
'name' => '统计名称', |
||||
|
'cover_img' => '封面图', |
||||
|
'market_id' => '市场', |
||||
|
'store_id' => '店铺', |
||||
|
'price' => '售价', |
||||
|
'original_price' => '原价', |
||||
|
'total' => '统计数量', |
||||
|
], |
||||
|
'options' => [ |
||||
|
], |
||||
|
]; |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue