FundController.php 5.2 KB

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