generated from liutk/owl-admin-base
137 lines
5.4 KiB
PHP
137 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers\Finance;
|
|
|
|
use App\Admin\Controllers\AdminController;
|
|
use App\Services\StatisticService;
|
|
use Illuminate\Support\Arr;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
|
|
class StoreStatisticController extends AdminController
|
|
{
|
|
public function index()
|
|
{
|
|
if ($this->actionOfGetData()) {
|
|
return $this->response()->success([
|
|
'items' => $this->getStoreRanking(),
|
|
]);
|
|
}
|
|
|
|
if ($this->actionOfExport()) {
|
|
return $this->export();
|
|
}
|
|
|
|
return $this->response()->success(
|
|
$this->baseList(
|
|
$this->baseCRUD()
|
|
->headerToolbar(
|
|
collect($this->baseHeaderToolBar())
|
|
->when(Admin::user()->can('admin.finance.store_statistics.export'), fn ($collection) => $collection->push($this->exportAction(true)))
|
|
)
|
|
->footerToolbar([])
|
|
->bulkActions([])
|
|
->filter($this->baseFilter()->body([
|
|
amis()->GroupControl()->mode('horizontal')->body([
|
|
amis()->DateRangeControl('date_range', '日期')
|
|
->valueFormat('YYYY-MM-DD'),
|
|
amis()->InputCityControl('region', '区域')
|
|
->allowDistrict(false)
|
|
->extractValue(false),
|
|
amis()->SelectControl('store_id', __('finance.ledger.store'))
|
|
->source(admin_url('api/stores?region=${region}'))
|
|
->labelField('title')
|
|
->valueField('id')
|
|
->clearable(),
|
|
]),
|
|
]))
|
|
->filterDefaultVisible()
|
|
->columns([
|
|
amis()->TableColumn('ranking', '排序'),
|
|
amis()->TableColumn('store.title', '门店'),
|
|
amis()->TableColumn('sales', '收入')->sortable(),
|
|
amis()->TableColumn('expenditure', '支出')->sortable(),
|
|
amis()->TableColumn('new_customers', '累计人数')->sortable(),
|
|
])
|
|
)
|
|
);
|
|
}
|
|
|
|
protected function getStoreRanking(): array
|
|
{
|
|
$input = Arr::except(request()->input(), ['orderBy', 'orderDir']);
|
|
|
|
$orderBy = request()->input('orderBy') ?: 'sales';
|
|
$orderDir = request()->input('orderDir') ?: 'desc';
|
|
|
|
$input['sort'] = ($orderDir === 'desc' ? '-' : '').$orderBy;
|
|
|
|
return (new StatisticService())->storeRanking($input);
|
|
}
|
|
|
|
/**
|
|
* 导出按钮
|
|
*
|
|
* @param bool $disableSelectedItem
|
|
*
|
|
* @return \Slowlyo\OwlAdmin\Renderers\Service
|
|
*/
|
|
protected function exportAction($disableSelectedItem = false)
|
|
{
|
|
// 下载路径
|
|
$downloadPath = admin_url('_download_export', true);
|
|
// 导出接口地址
|
|
$exportPath = $this->getExportPath();
|
|
// 按钮点击事件
|
|
$event = fn($script) => ['click' => ['actions' => [['actionType' => 'custom', 'script' => $script]]]];
|
|
// 导出处理动作
|
|
$doAction = "doAction([{actionType:'setValue',componentId:'export-action',args:{value:{showExportLoading:true}}},{actionType:'ajax',args:{api:{url:url.toString(),method:'get'}}},{actionType:'setValue',componentId:'export-action',args:{value:{showExportLoading:false}}},{actionType:'custom',expression:'\${event.data.responseResult.responseStatus===0}',script:'window.open(\'{$downloadPath}?path=\'+event.data.responseResult.responseData.path)'}])";
|
|
// 按钮
|
|
$button = amis()->Button()
|
|
->label(__('admin.export.title'))
|
|
->icon('fa-solid fa-download')
|
|
->onEvent(
|
|
$event("let data=event.data;let params=Object.keys(data).filter(key=>key!=='page' && key!=='__super').reduce((obj,key)=>{obj[key]=data[key];return obj;},{});let url=new URL('{$exportPath}',window.location.origin);Object.keys(params).forEach(key=>url.searchParams.append(key,params[key]));{$doAction}")
|
|
);
|
|
|
|
return amis()->Service()
|
|
->id('export-action')
|
|
->set('align', 'right')
|
|
->set('data', ['showExportLoading' => false])
|
|
->body(
|
|
amis()->Spinner()->set('showOn', '${showExportLoading}')->overlay()->body($button)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 导出
|
|
*
|
|
* @return JsonResponse|JsonResource
|
|
*/
|
|
protected function export()
|
|
{
|
|
admin_abort_if(!class_exists('\Rap2hpoutre\FastExcel\FastExcel'), __('admin.export.please_install_laravel_excel'));
|
|
|
|
// 默认在 storage/app/ 下
|
|
$path = sprintf('门店统计-%s.xlsx', date('YmdHis'));
|
|
|
|
$data = $this->getStoreRanking();
|
|
|
|
try {
|
|
fastexcel($data)->export(storage_path('app/' . $path), function($row) {
|
|
return [
|
|
'排序' => $row['ranking'],
|
|
'门店' => Arr::get($row, 'store.title'),
|
|
'收入' => $row['sales'],
|
|
'支出' => $row['expenditure'],
|
|
'new_customers' => $row['new_customers'],
|
|
];
|
|
});
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
admin_abort(__('admin.action_failed'));
|
|
}
|
|
|
|
return $this->response()->success(compact('path'));
|
|
}
|
|
}
|