FormHelperTrait.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Module\Fund\AdminControllers\Helper;
  3. use App\Module\Fund\Enums\FUND_STATUS;
  4. use App\Module\Fund\Enums\FUND_TYPE;
  5. use App\Module\Fund\Enums\LOG_TYPE;
  6. use App\Module\Fund\Enums\OPERATE_TYPE;
  7. use App\Module\Fund\Services\AccountService;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Form\Field;
  10. /**
  11. * 表单辅助特性
  12. *
  13. * 提供资金模块后台控制器的表单构建功能的具体实现
  14. * 只保留具有复用价值的方法
  15. */
  16. trait FormHelperTrait
  17. {
  18. /**
  19. * 添加资金账户选择
  20. *
  21. * 复用价值:高 - 统一处理资金账户的选择,使用AccountService获取账户描述
  22. *
  23. * @param string $field 字段名
  24. * @param string $label 标签名
  25. * @return Field\Select
  26. */
  27. public function selectFundId(string $field = 'fund_id', string $label = '资金账户'): Field\Select
  28. {
  29. return $this->form->select($field, $label)
  30. ->options(AccountService::getFundsDesc())
  31. ->required();
  32. }
  33. /**
  34. * 添加操作类型选择
  35. *
  36. * 复用价值:高 - 统一处理操作类型的选择,使用枚举类型
  37. *
  38. * @param string $field 字段名
  39. * @param string $label 标签名
  40. * @return Field\Select
  41. */
  42. public function selectOperateType(string $field = 'operate_type', string $label = '操作类型'): Field\Select
  43. {
  44. return $this->form->select($field, $label)
  45. ->options(LOG_TYPE::getValueDescription())
  46. ->required();
  47. }
  48. /**
  49. * 添加资金类型选择
  50. *
  51. * 复用价值:高 - 统一处理资金类型的选择,使用枚举类型
  52. *
  53. * @param string $field 字段名
  54. * @param string $label 标签名
  55. * @return Field\Select
  56. */
  57. public function selectFundType(string $field = 'fund_type', string $label = '资金类型'): Field\Select
  58. {
  59. return $this->form->select($field, $label)
  60. ->options(FUND_TYPE::getValueDescription())
  61. ->required();
  62. }
  63. /**
  64. * 添加金额输入(元转毫)
  65. *
  66. * 复用价值:高 - 统一处理金额的输入,包括单位转换和验证
  67. *
  68. * @param string $field 字段名
  69. * @param string $label 标签名
  70. * @param bool $required 是否必填
  71. * @return Field\Text
  72. */
  73. public function textAmount(string $field = 'amount', string $label = '金额', bool $required = true): Field\Text
  74. {
  75. $field = $this->form->text($field, $label)
  76. ->help('金额,单位为元,系统会自动转换为毫')
  77. ->saving(function ($value) {
  78. return $value * 1000;
  79. });
  80. if ($required) {
  81. $field->required();
  82. }
  83. return $field;
  84. }
  85. /**
  86. * 添加转账表单组
  87. *
  88. * 复用价值:高 - 提供完整的转账表单组,包括来源账户、目标账户、金额等
  89. *
  90. * @return void
  91. */
  92. public function addTransferFields(): void
  93. {
  94. $this->form->divider('转账信息');
  95. $this->form->number('user_id', '来源用户ID')
  96. ->required()
  97. ->min(1)
  98. ->help('来源用户ID,必须是有效的用户');
  99. $this->selectFundId('fund_id', '来源资金账户');
  100. $this->form->number('to_user_id', '目标用户ID')
  101. ->required()
  102. ->min(1)
  103. ->help('目标用户ID,必须是有效的用户');
  104. $this->selectFundId('to_fund_id', '目标资金账户');
  105. $this->textAmount('amount', '转账金额');
  106. $this->form->textarea('remark', '备注')
  107. ->rows(3);
  108. }
  109. /**
  110. * 添加资金操作表单组
  111. *
  112. * 复用价值:高 - 提供完整的资金操作表单组,包括用户、账户、金额、操作类型等
  113. *
  114. * @param bool $isDeduct 是否为扣除操作
  115. * @return void
  116. */
  117. public function addFundOperationFields(bool $isDeduct = false): void
  118. {
  119. $this->form->divider('资金操作信息');
  120. $this->form->number('user_id', '用户ID')
  121. ->required()
  122. ->min(1)
  123. ->help('用户ID,必须是有效的用户');
  124. $this->selectFundId('fund_id', '资金账户');
  125. $amountLabel = $isDeduct ? '扣除金额' : '增加金额';
  126. $this->textAmount('amount', $amountLabel)
  127. ->saving(function ($value) use ($isDeduct) {
  128. return $isDeduct ? -abs($value * 1000) : abs($value * 1000);
  129. });
  130. $this->selectOperateType('operate_type', '操作类型');
  131. $this->form->textarea('remark', '备注')
  132. ->rows(3);
  133. }
  134. }