FundController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Module\Fund\AdminControllers;
  3. use App\Module\Fund\Admin\Actions\Circulation;
  4. use App\Module\Fund\Admin\Actions\CirculationForm;
  5. use App\Module\Fund\Admin\Actions\FundAdminAction;
  6. use App\Module\Fund\Admin\Actions\FundLogViewAction;
  7. use App\Module\Fund\Admin\Actions\FundStatisticsAction;
  8. use App\Module\Fund\Admin\Actions\FundTransferAction;
  9. use App\Module\Fund\AdminControllers\Helper\FilterHelper;
  10. use App\Module\Fund\AdminControllers\Helper\FormHelper;
  11. use App\Module\Fund\AdminControllers\Helper\GridHelper;
  12. use App\Module\Fund\AdminControllers\Helper\ShowHelper;
  13. use App\Module\Fund\Repositorys\FundLogRepository;
  14. use App\Module\Fund\Repositorys\FundRepository;
  15. use App\Module\Fund\Services\AccountService;
  16. use Spatie\RouteAttributes\Attributes\Get;
  17. use Spatie\RouteAttributes\Attributes\Resource;
  18. use UCore\DcatAdmin\AdminController;
  19. use Dcat\Admin\Form;
  20. use Dcat\Admin\Grid;
  21. use Dcat\Admin\Layout\Content;
  22. use Dcat\Admin\Show;
  23. use App\Module\Fund\Models\FundModel;
  24. use App\Module\Fund\Repositorys\FundRepository as Fund;
  25. /**
  26. * 资金账户
  27. */
  28. #[Resource('fund-accounts', names: 'dcat.admin.fund-accounts')]
  29. class FundController extends AdminController
  30. {
  31. /**
  32. * 页面标题
  33. *
  34. * @var string
  35. */
  36. protected $title = '资金账户';
  37. /**
  38. * 账户服务
  39. *
  40. * @var AccountService
  41. */
  42. protected $service;
  43. /**
  44. * 构造函数
  45. */
  46. public function __construct()
  47. {
  48. $this->service = new AccountService();
  49. }
  50. /**
  51. * 列表页面
  52. *
  53. * @return Grid
  54. */
  55. protected function grid()
  56. {
  57. return Grid::make(new Fund(['user']), function (Grid $grid) {
  58. // 使用GridHelper
  59. $helper = new GridHelper($grid, $this);
  60. // 使用高复用价值的列方法
  61. $grid->column('id', 'ID')->sortable();
  62. $helper->columnUserFund(); // 组合列,显示用户ID和资金账户
  63. $helper->columnBalance(); // 格式化余额显示
  64. $helper->columnTimestamp('create_time', '创建时间'); // 格式化时间戳
  65. $helper->columnTimestamp('update_time', '更新时间');
  66. // 禁用不需要的功能
  67. $grid->disableCreateButton();
  68. $grid->disableDeleteButton();
  69. // 筛选器
  70. $grid->filter(function (Grid\Filter $filter) {
  71. // 使用FilterHelper
  72. $helper = new FilterHelper($filter, $this);
  73. // 直接实现资金筛选组
  74. $helper->equalId(); // 添加ID筛选
  75. $filter->equal('user_id', '用户ID');
  76. $helper->equalFundId();
  77. $helper->betweenAmount('balance', '余额范围');
  78. $helper->betweenTimestamp('create_time', '创建时间');
  79. });
  80. // 添加操作按钮
  81. $grid->actions(function (Grid\Displayers\Actions $actions) {
  82. $actions->append(new FundAdminAction());
  83. $actions->append(new Circulation());
  84. $actions->append(new FundLogViewAction());
  85. $actions->append(new FundTransferAction());
  86. $actions->append(new FundStatisticsAction());
  87. });
  88. });
  89. }
  90. /**
  91. * 详情页面
  92. *
  93. * @param mixed $id
  94. * @return Show
  95. */
  96. protected function detail($id)
  97. {
  98. return Show::make($id, new FundRepository(), function (Show $show) {
  99. // 使用ShowHelper
  100. $helper = new ShowHelper($show, $this);
  101. // 直接实现资金账户详情面板
  102. $show->divider('账户信息');
  103. $show->field('user_id', '用户ID');
  104. $helper->fieldFundId();
  105. $helper->fieldBalance();
  106. $helper->fieldStatus();
  107. $show->field('fund_type', '资金类型')->as(function ($value) {
  108. return \App\Module\Fund\Enums\FUND_TYPE::getName($value);
  109. });
  110. $helper->fieldTimestamp('create_time', '创建时间');
  111. $helper->fieldTimestamp('update_time', '更新时间');
  112. // todo 显示关联的资金日志
  113. });
  114. }
  115. /**
  116. * 表单页面
  117. *
  118. * @return Form
  119. */
  120. protected function form()
  121. {
  122. return Form::make(new Fund(), function (Form $form) {
  123. // 使用FormHelper
  124. $helper = new FormHelper($form, $this);
  125. $form->display('id', 'ID');
  126. // 使用高复用价值的表单方法
  127. $form->number('user_id', '用户ID')
  128. ->required()
  129. ->min(1)
  130. ->help('用户ID,必须是有效的用户');
  131. $helper->selectFundId(); // 资金账户选择,使用AccountService
  132. $helper->selectFundType(); // 资金类型选择,使用枚举
  133. $form->display('create_time', '创建时间');
  134. $form->display('update_time', '更新时间');
  135. // 保存前回调
  136. $form->saving(function (Form $form) {
  137. // 检查用户是否存在
  138. if ($form->isCreating()) {
  139. $userId = $form->user_id;
  140. $user = \App\Module\User\Models\User::find($userId);
  141. if (!$user) {
  142. return $form->response()->error('用户ID不存在');
  143. }
  144. }
  145. });
  146. });
  147. }
  148. }