87 lines
2.2 KiB
PHP
87 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Enums\DealerLvl;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\Dealer;
|
|
use App\Models\UserInfo;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class DealerEditLvl 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.edit_lvl');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$id = $this->payload['id'] ?? 0;
|
|
$dealer = Dealer::findOrFail($id);
|
|
if ($dealer?->lvl->value >= $input['lvl']) {
|
|
throw new BizException('请选择大于当前的等级');
|
|
}
|
|
try {
|
|
DB::beginTransaction();
|
|
//执行自己升级
|
|
$dealer->upgrade(DealerLvl::from($input['lvl']), '后台修改等级');
|
|
//执行上级尝试升级
|
|
foreach ($dealer->getDealers() as $parentDealer) {
|
|
$parentDealer->attemptUpgrade();
|
|
}
|
|
// $dealer->update([
|
|
// 'lvl'=>$input['lvl'],
|
|
// ]);
|
|
|
|
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()
|
|
{
|
|
// dd(UserInfo::$agentLevelTexts);
|
|
$this->select('lvl', '经销商级别')->options(DealerLvl::texts())
|
|
->help('请选择大于当前的身份')
|
|
->required();
|
|
}
|
|
|
|
public function default()
|
|
{
|
|
$id = $this->payload['id'] ?? 0;
|
|
$dealer = Dealer::findOrFail($id);
|
|
return [
|
|
'lvl' => $dealer->lvl->value,
|
|
];
|
|
}
|
|
}
|