1
0
Fork 0
medical-record-server/app/Admin/Controllers/TotalPatientController.php

146 lines
5.7 KiB
PHP

<?php
namespace App\Admin\Controllers;
use Slowlyo\OwlAdmin\Controllers\AdminController;
use App\Models\{Keyword, Patient, PatientRecord};
use Illuminate\Support\Facades\DB;
use App\Admin\Services\TotalPatientService;
use Slowlyo\OwlAdmin\Support\Excel\AdminExport;
/**
* 客户统计
*/
class TotalPatientController extends AdminController
{
protected string $serviceName = TotalPatientService::class;
protected $patientOptions;
protected $typeOptions;
public function list()
{
$crud = $this->baseCRUD()
->filterTogglable(false)
->columnsTogglable(false)
->alwaysShowPagination()
->headerToolbar([
$this->exportAction(),
])
->filter($this->baseFilter()->actions()->body([
amisMake()->SelectControl()->options($this->getPatientOptions())->searchable()->name('id')->label(__('total-record.name'))->size('md')->multiple()->clearable(),
amisMake()->SelectControl()->options($this->getTypeOptions())->name('type_id')->label(__('patient-record.type_id'))->size('md')->clearable(),
amisMake()->DateRangeControl()->name('treat_range')->label(__('total-record.treat_at'))->clearable()->size('md'),
// amisMake()->Button()->label(__('admin.reset'))->actionType('clear-and-submit'),
amisMake()->Component()->setType('submit')->label(__('admin.search'))->level('primary'),
]))
->columns([
amisMake()->Column()->name('name')->label(__('total-record.name')),
amisMake()->Column()->name('type.name')->label(__('total-record.type_id')),
amisMake()->Column()->name('records_count')->label(__('total-record.records_count')),
amisMake()->Column()->name('origin_price')->label(__('total-record.origin_price')),
amisMake()->Column()->name('sell_price')->label(__('total-record.sell_price')),
])
->affixRowClassName('text-info-dk')
->affixRow([
['type' => 'text', 'text' => '总计: ${total}', 'colSpan' => 2],
['type' => 'text', 'text' => __('total-record.records_count').': ${records_count}'],
['type' => 'text', 'text' => __('total-record.origin_price') . ': ${origin_price}'],
['type' => 'text', 'text' => __('total-record.sell_price') . ': ${sell_price}'],
]);
return $this->baseList($crud);
}
public function getPatientOptions()
{
if (!$this->patientOptions) {
$this->patientOptions = Patient::select(['id as value', 'name as label'])->sort()->get();
}
return $this->patientOptions;
}
public function getTypeOptions()
{
if (!$this->typeOptions) {
$this->typeOptions = Keyword::where('type_key', 'treat_type')->select(['id as value', 'name as label'])->get();
}
return $this->typeOptions;
}
protected function exportAction($disableSelectedItem = false)
{
$event = fn($script) => ['click' => ['actions' => [['actionType' => 'custom', 'script' => $script]]]];
$downloadPath = '/' . admin_url('_download_export', true);
$exportPath = $this->getExportPath();
$doAction = <<<JS
doAction([
{ actionType: "ajax", args: { api: { url: url.toString(), method: "get" } } },
{
actionType: "custom",
expression: "\${event.data.responseResult.responseStatus === 0}",
script: "window.open('{$downloadPath}?path='+event.data.responseResult.responseData.path)"
}
])
JS;
$buttons = [
amisMake()->VanillaAction()->label(__('admin.export.all'))->onEvent(
$event(<<<JS
let url = new URL("{$exportPath}", window.location.origin)
let param = window.location.href.split('?')[1]
if (param) {
url = url + '&' + param
}
{$doAction}
JS
)
),
];
return amisMake()
->DropdownButton()
->label(__('admin.export.title'))
->set('icon', 'fa-solid fa-download')
->buttons($buttons)
->align('right')
->closeOnClick();
}
protected function export()
{
// 默认在 storage/app/ 下
$path = sprintf('%s-%s.xlsx', $this->exportFileName(), date('YmdHis'));
// 导出本页和导出选中项都是通过 _ids 查询
$ids = request()->input('_ids');
// listQuery() 为列表查询条件,与获取列表数据一致
$query = $this->service->listQuery()
->when($ids, fn($query) => $query->whereIn($this->service->primaryKey(), explode(',', $ids)));
// 此处使用 laravel-excel 导出,可自行修改
AdminExport::make($query)
->setHeadings([
__('total-record.name'),
__('total-record.type_id'),
__('total-record.records_count'),
__('total-record.origin_price'),
__('total-record.sell_price'),
])
->setMap(fn($row) => [
$row->name,
$row->type?->name,
$row->records->count(),
round($row->records->sum('sell_price'), 2, PHP_ROUND_HALF_DOWN),
round($row->records->sum('origin_price'), 2, PHP_ROUND_HALF_DOWN),
])
->store($path);
return $this->response()->success(compact('path'));
}
}