| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Module\Fund\AdminControllers\Helper;
- use App\Module\Fund\Enums\FUND_STATUS;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\Fund\Enums\LOG_TYPE;
- use App\Module\Fund\Enums\OPERATE_TYPE;
- use App\Module\Fund\Services\AccountService;
- use Dcat\Admin\Form;
- use Dcat\Admin\Form\Field;
- /**
- * 表单辅助特性
- *
- * 提供资金模块后台控制器的表单构建功能的具体实现
- * 只保留具有复用价值的方法
- */
- trait FormHelperTrait
- {
- /**
- * 添加资金账户选择
- *
- * 复用价值:高 - 统一处理资金账户的选择,使用AccountService获取账户描述
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectFundId(string $field = 'fund_id', string $label = '资金账户'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(AccountService::getFundsDesc())
- ->required();
- }
- /**
- * 添加操作类型选择
- *
- * 复用价值:高 - 统一处理操作类型的选择,使用枚举类型
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectOperateType(string $field = 'operate_type', string $label = '操作类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(LOG_TYPE::getValueDescription())
- ->required();
- }
- /**
- * 添加资金类型选择
- *
- * 复用价值:高 - 统一处理资金类型的选择,使用枚举类型
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectFundType(string $field = 'fund_type', string $label = '资金类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(FUND_TYPE::getValueDescription())
- ->required();
- }
- /**
- * 添加金额输入(元转毫)
- *
- * 复用价值:高 - 统一处理金额的输入,包括单位转换和验证
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @param bool $required 是否必填
- * @return Field\Text
- */
- public function textAmount(string $field = 'amount', string $label = '金额', bool $required = true): Field\Text
- {
- $field = $this->form->text($field, $label)
- ->help('金额,单位为元,系统会自动转换为毫')
- ->saving(function ($value) {
- return $value * 1000;
- });
- if ($required) {
- $field->required();
- }
- return $field;
- }
- /**
- * 添加转账表单组
- *
- * 复用价值:高 - 提供完整的转账表单组,包括来源账户、目标账户、金额等
- *
- * @return void
- */
- public function addTransferFields(): void
- {
- $this->form->divider('转账信息');
- $this->form->number('user_id', '来源用户ID')
- ->required()
- ->min(1)
- ->help('来源用户ID,必须是有效的用户');
- $this->selectFundId('fund_id', '来源资金账户');
- $this->form->number('to_user_id', '目标用户ID')
- ->required()
- ->min(1)
- ->help('目标用户ID,必须是有效的用户');
- $this->selectFundId('to_fund_id', '目标资金账户');
- $this->textAmount('amount', '转账金额');
- $this->form->textarea('remark', '备注')
- ->rows(3);
- }
- /**
- * 添加资金操作表单组
- *
- * 复用价值:高 - 提供完整的资金操作表单组,包括用户、账户、金额、操作类型等
- *
- * @param bool $isDeduct 是否为扣除操作
- * @return void
- */
- public function addFundOperationFields(bool $isDeduct = false): void
- {
- $this->form->divider('资金操作信息');
- $this->form->number('user_id', '用户ID')
- ->required()
- ->min(1)
- ->help('用户ID,必须是有效的用户');
- $this->selectFundId('fund_id', '资金账户');
- $amountLabel = $isDeduct ? '扣除金额' : '增加金额';
- $this->textAmount('amount', $amountLabel)
- ->saving(function ($value) use ($isDeduct) {
- return $isDeduct ? -abs($value * 1000) : abs($value * 1000);
- });
- $this->selectOperateType('operate_type', '操作类型');
- $this->form->textarea('remark', '备注')
- ->rows(3);
- }
- }
|