| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace App\Module\Fund\AdminControllers;
- use App\Module\Fund\Admin\Actions\Circulation;
- use App\Module\Fund\Admin\Actions\FundAdminAction;
- use App\Module\Fund\AdminControllers\Helper\FilterHelper;
- use App\Module\Fund\AdminControllers\Helper\FormHelper;
- use App\Module\Fund\AdminControllers\Helper\GridHelper;
- use App\Module\Fund\AdminControllers\Helper\ShowHelper;
- use App\Module\Fund\Repositorys\FundLogRepository;
- use App\Module\Fund\Repositorys\FundRepository;
- use App\Module\Fund\Services\AccountService;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Show;
- use App\Module\Fund\Models\FundModel;
- use App\Module\Fund\Repositorys\FundRepository as Fund;
- /**
- * 资金账户
- */
- #[Resource('fund-accounts', names: 'dcat.admin.fund-accounts')]
- class FundController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '资金账户';
- /**
- * 账户服务
- *
- * @var AccountService
- */
- protected $service;
- /**
- * 构造函数
- */
- public function __construct()
- {
- $this->service = new AccountService();
- }
- /**
- * 列表页面
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new Fund(['user']), function (Grid $grid) {
- // 使用GridHelper
- $helper = new GridHelper($grid, $this);
- // 使用高复用价值的列方法
- $grid->column('id', 'ID')->sortable();
- $helper->columnUserFund(); // 组合列,显示用户ID和资金账户
- $helper->columnBalance(); // 格式化余额显示
- $helper->columnTimestamp('create_time', '创建时间'); // 格式化时间戳
- $helper->columnTimestamp('update_time', '更新时间');
- // 禁用不需要的功能
- $grid->disableCreateButton();
- $grid->disableDeleteButton();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- // 使用FilterHelper
- $helper = new FilterHelper($filter, $this);
- // 直接实现资金筛选组
- $helper->equalId(); // 添加ID筛选
- $filter->equal('user_id', '用户ID');
- $helper->equalFundId();
- $helper->betweenAmount('balance', '余额范围');
- $helper->betweenTimestamp('create_time', '创建时间');
- });
- // 添加操作按钮
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->append(new FundAdminAction());
- $actions->append(new Circulation());
- });
- });
- }
- /**
- * 详情页面
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new FundRepository(), function (Show $show) {
- // 使用ShowHelper
- $helper = new ShowHelper($show, $this);
- // 直接实现资金账户详情面板
- $show->divider('账户信息');
- $show->field('user_id', '用户ID');
- $helper->fieldFundId();
- $helper->fieldBalance();
- $helper->fieldStatus();
- $show->field('fund_type', '资金类型')->as(function ($value) {
- return \App\Module\Fund\Enums\FUND_TYPE::getName($value);
- });
- $helper->fieldTimestamp('create_time', '创建时间');
- $helper->fieldTimestamp('update_time', '更新时间');
- // todo 显示关联的资金日志
- });
- }
- /**
- * 表单页面
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new Fund(), function (Form $form) {
- // 使用FormHelper
- $helper = new FormHelper($form, $this);
- $form->display('id', 'ID');
- // 使用高复用价值的表单方法
- $form->number('user_id', '用户ID')
- ->required()
- ->min(1)
- ->help('用户ID,必须是有效的用户');
- $helper->selectFundId(); // 资金账户选择,使用AccountService
- $helper->textBalance(); // 余额输入,自动单位转换
- $helper->radioStatus(); // 状态选择,使用枚举
- $helper->selectFundType(); // 资金类型选择,使用枚举
- $form->display('create_time', '创建时间');
- $form->display('update_time', '更新时间');
- // 保存前回调
- $form->saving(function (Form $form) {
- // 检查用户是否存在
- if ($form->isCreating()) {
- $userId = $form->user_id;
- $user = \App\Module\User\Models\User::find($userId);
- if (!$user) {
- return $form->response()->error('用户ID不存在');
- }
- }
- });
- });
- }
- public function circulation(Content $content)
- {
- return $content
- ->title('流转')
- ->body(new CirculationForm());
- }
- }
|