actionOfGetData()) { return $this->response()->success([ 'items' => $this->getLotteryTypeStatistics(), ]); } if ($this->actionOfExport()) { return $this->export(); } return $this->response()->success( $this->baseList( $this->baseCRUD() ->headerToolbar( collect($this->baseHeaderToolBar()) ->when(Admin::user()->can('admin.finance.sales_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') ->columnRatio(6), amis()->InputCityControl('region', '区域') ->allowDistrict(false) ->extractValue(false), amis()->SelectControl('store_id', __('finance.ledger.store')) ->source(admin_url('api/stores?region=${region}')) ->searchable() ->labelField('title') ->valueField('id') ->clearable(), ]), ])) ->filterDefaultVisible() ->columns([ amis()->TableColumn('name', '彩种'), amis()->TableColumn('sales', '销量'), amis()->TableColumn('expenditure', '兑奖'), ]) ->affixRow([ ['type' => 'text', 'text' => '合计'], ['type' => 'tpl', 'text' => '${items|pick:real_sales|sum}'], ['type' => 'tpl', 'text' => '${items|pick:real_expenditure|sum}'], ]) ) ); } protected function getLotteryTypeStatistics(): array { /** @var \Illuminate\Database\Eloquent\Collection */ $lotteryTypes = Keyword::where('parent_key', 'lottery_type')->get(); /** @var \Illuminate\Database\Eloquent\Collection */ $statistics = LedgerItem::select([ 'ledger_item_type_id', DB::raw('SUM(sales) as sales'), DB::raw('SUM(expenditure) as expenditure'), ]) ->filter(request()->input(), LedgerItemFilter::class) ->whereIn('ledger_item_type_id', $lotteryTypes->pluck('key')) ->groupBy('ledger_item_type_id') ->get() ->keyBy('ledger_item_type_id'); return $lotteryTypes->map(function ($lotteryType) use ($statistics) { $statistic = $statistics->get($lotteryType->key); return [ 'name' => $lotteryType->name, 'sales' => trim_zeros($statistic->sales ?? '0.00'), 'expenditure' => trim_zeros($statistic->expenditure ?? '0.00'), 'real_sales' => match ($lotteryType->value) { '+' => $statistic->sales ?? '0.00', '-' => -($statistic->sales ?? '0.00'), default => 0, }, 'real_expenditure' => match ($lotteryType->value) { '+' => $statistic->expenditure ?? '0.00', '-' => -($statistic->expenditure ?? '0.00'), default => 0, }, ]; })->all(); } /** * 导出按钮 * * @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->getLotteryTypeStatistics(); try { fastexcel($data)->export(storage_path('app/'.$path), fn ($row) => [ '彩种' => $row['name'], '销量' => $row['sales'], '兑奖' => $row['expenditure'], ]); } catch (\Throwable $e) { admin_abort(__('admin.action_failed')); } return $this->response()->success(compact('path')); } }