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

157 lines
6.5 KiB
PHP

<?php
namespace App\Admin\Controllers;
use App\Admin\Services\TotalProfitService;
use App\Models\Keyword;
use Slowlyo\OwlAdmin\Controllers\AdminController;
use Slowlyo\OwlAdmin\Services\AdminUserService;
use Slowlyo\OwlAdmin\Support\Excel\AdminExport;
/**
* 提成统计
*/
class TotalProfitController extends AdminController
{
protected string $serviceName = TotalProfitService::class;
protected $adminUserOptions;
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->getAdminUserOptions())->searchable()->name('id')->label(__('total-profit.id'))->clearable()->size('md'),
amisMake()->SelectControl()->options($this->getTypeOptions())->name('type_id')->label(__('total-profit.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-profit.id')),
amisMake()->Column()->name('records_count')->label(__('total-profit.records_count')),
amisMake()->Column()->name('doctor_money')->label(__('total-profit.doctor_money')),
amisMake()->Column()->name('inviter_money')->label(__('total-profit.inviter_money')),
amisMake()->Column()->name('saler_money')->label(__('total-profit.saler_money')),
amisMake()->Column()->name('total_money')->label(__('total-profit.total_money')),
])
->affixRowClassName('text-info-dk')
->affixRow([
['type' => 'text', 'text' => '总计: ${total}'],
['type' => 'text', 'text' => __('total-profit.records_count'). ': ${records_count}'],
['type' => 'text', 'text' => __('total-profit.doctor_money'). ': ${doctor_money}'],
['type' => 'text', 'text' => __('total-profit.inviter_money'). ': ${inviter_money}'],
['type' => 'text', 'text' => __('total-profit.saler_money'). ': ${saler_money}'],
['type' => 'text', 'text' => __('total-profit.total_money'). ': ${total_money}'],
]);
return $this->baseList($crud);
}
public function getAdminUserOptions()
{
if (!$this->adminUserOptions) {
$this->adminUserOptions = AdminUserService::make()->query()->select(['id as value', 'name as label'])->get();
}
return $this->adminUserOptions;
}
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-profit.id'),
__('total-profit.records_count'),
__('total-profit.doctor_money'),
__('total-profit.inviter_money'),
__('total-profit.saler_money'),
__('total-profit.total_money'),
])
->setMap(function ($item) {
$doctor_money = round($item->doctors->sum('doctor_money'), 2, PHP_ROUND_HALF_DOWN);
$inviter_money = round($item->inviters->sum('inviter_money'), 2, PHP_ROUND_HALF_DOWN);
$saler_money = round($item->salers->sum('saler_money'), 2, PHP_ROUND_HALF_DOWN);
$records_count = floor($item->doctors->count() + $item->inviters->count() + $item->salers->count());
$total_money = round($doctor_money + $inviter_money + $saler_money, 2, PHP_ROUND_HALF_DOWN);
return [
$item->name,
$records_count,
$doctor_money,
$inviter_money,
$saler_money,
$total_money,
];
})
->store($path);
return $this->response()->success(compact('path'));
}
}