6
0
Fork 0
jiqu-library-server/app/Admin/Forms/DealerWalletChange.php

91 lines
2.4 KiB
PHP

<?php
namespace App\Admin\Forms;
use App\Enums\DealerWalletAction;
use App\Models\Dealer;
use App\Services\Dealer\WalletService;
use Dcat\Admin\Admin;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;
use Throwable;
class DealerWalletChange extends Form implements LazyRenderable
{
use LazyWidget;
/**
* @param Model|Authenticatable|HasPermissions|null $user
*
* @return bool
*/
protected function authorize($user): bool
{
return $user->can('dcat.admin.dealers.wallet_change');
}
/**
* Handle the form request.
*
* @param array $input
*
* @return mixed
*/
public function handle(array $input)
{
if (bccomp($input['change_balance'], '0', 2) <= 0) {
return $this->response()->error('金额必须大于0');
}
$dealer = Dealer::findOrFail($this->payload['id']);
$action = DealerWalletAction::from($input['action']);
try {
DB::beginTransaction();
(new WalletService())->changeBalance(
$dealer->user,
match ($action) {
DealerWalletAction::Recharge => bcmul($input['change_balance'], '1', 2),
DealerWalletAction::Deduct => bcmul($input['change_balance'], '-1', 2),
},
$action,
$input['remarks'],
Admin::user()
);
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
return $this->response()->error('操作失败:'.$th->getMessage());
}
return $this->response()
->success(__('admin.update_succeeded'))
->refresh();
}
/**
* Build a form here.
*/
public function form()
{
$this->radio('action', '操作')
->options([
DealerWalletAction::Recharge->value => '充值',
DealerWalletAction::Deduct->value => '扣除',
])
->default(DealerWalletAction::Recharge->value)
->required();
$this->currency('change_balance', '金额')->symbol('¥')->required();
$this->textarea('remarks', '备注')->required();
$this->confirm('是否确认充值?', '提交后该动作无法逆转');
}
}