diff --git a/app/Admin/Controllers/QuotaV1SendJobController.php b/app/Admin/Controllers/QuotaV1SendJobController.php index 672b45e5..1f10a622 100644 --- a/app/Admin/Controllers/QuotaV1SendJobController.php +++ b/app/Admin/Controllers/QuotaV1SendJobController.php @@ -3,6 +3,8 @@ namespace App\Admin\Controllers; use App\Admin\Actions\Grid\QuotaV1SendJobStart; +use App\Admin\Extensions\Grid\Tools\QuotaV1\Deduction; +use App\Admin\Extensions\Grid\Tools\QuotaV1\Recharge; use App\Admin\Renderable\QuotaV1SendLogTable; use App\Admin\Repositories\QuotaV1SendJob; use App\Models\QuotaV1SendJob as QuotaV1SendJobModel; @@ -25,6 +27,14 @@ class QuotaV1SendJobController extends AdminController $builder = QuotaV1SendJob::with('administrator'); return Grid::make($builder, function (Grid $grid) { $grid->column('id')->sortable(); + $grid->tools(function (Grid\Tools $tools) { + if (Admin::user()->can('dcat.admin.quota_v1_send_jobs.recharge')) { + $tools->append(new Recharge()); + } + if (Admin::user()->can('dcat.admin.quota_v1_send_jobs.deduction')) { + $tools->append(new Deduction()); + } + }); $grid->column('amount')->display(function ($value) { return bcdiv($value, 100, 2); })->prepend('¥'); diff --git a/app/Admin/Extensions/Grid/Tools/QuotaV1/Deduction.php b/app/Admin/Extensions/Grid/Tools/QuotaV1/Deduction.php new file mode 100644 index 00000000..ffdcaa58 --- /dev/null +++ b/app/Admin/Extensions/Grid/Tools/QuotaV1/Deduction.php @@ -0,0 +1,54 @@ +can('dcat.admin.quota_v1_send_jobs.deduction'); + } + + /** + * 按钮样式定义,默认 btn btn-white waves-effect + * + * @var string + */ + protected $style = 'btn btn btn-danger'; + + /** + * 按钮文本 + * + * @return string|void + */ + public function title() + { + return '扣减'; + } + + public function render() + { + $form = QuotaV1Deduction::make(); + return Modal::make() + ->lg() + ->title($this->title()) + ->body($form) + ->button($this->html()); + } + + /** + * 设置请求参数 + * + * @return array|void + */ + public function parameters() + { + return [ + + ]; + } +} diff --git a/app/Admin/Extensions/Grid/Tools/QuotaV1/Recharge.php b/app/Admin/Extensions/Grid/Tools/QuotaV1/Recharge.php new file mode 100644 index 00000000..0b1b71a7 --- /dev/null +++ b/app/Admin/Extensions/Grid/Tools/QuotaV1/Recharge.php @@ -0,0 +1,54 @@ +can('dcat.admin.quota_v1_send_jobs.recharge'); + } + + /** + * 按钮样式定义,默认 btn btn-white waves-effect + * + * @var string + */ + protected $style = 'btn btn btn-warning'; + + /** + * 按钮文本 + * + * @return string|void + */ + public function title() + { + return '增加'; + } + + public function render() + { + $form = QuotaV1Recharge::make(); + return Modal::make() + ->lg() + ->title($this->title()) + ->body($form) + ->button($this->html()); + } + + /** + * 设置请求参数 + * + * @return array|void + */ + public function parameters() + { + return [ + + ]; + } +} diff --git a/app/Admin/Forms/QuotaV1Deduction.php b/app/Admin/Forms/QuotaV1Deduction.php new file mode 100644 index 00000000..d3cf0096 --- /dev/null +++ b/app/Admin/Forms/QuotaV1Deduction.php @@ -0,0 +1,69 @@ +can('dcat.admin.quota_v1_send_jobs.deduction'); + } + + /** + * Handle the form request. + * + * @param array $input + * + * @return mixed + */ + public function handle(array $input) + { + if (($input['change_balance'] ?? 0) <= 0) { + return $this->response()->error('扣减配额必须大于0'); + } + try { + DB::beginTransaction(); + //获取当前操作人; + $adminUser = Admin::user(); + $user = User::findOrFail($input['user_id'] ?? 0); + $quotaV1Service = new QuotaV1Service(); + $quotaV1Service->changeBalance($user, -($input['change_balance'] ?? 0), QuotaV1Log::ACTION_ADMIN_DEDUCTION, '后台扣减', $adminUser); + 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->select('user_id', '用户手机号')->ajax(admin_route('api.users'))->required(); + $this->currency('change_balance', '扣减配额')->symbol('¥')->required(); + $this->confirm('是否确认扣减?', '提交后该动作无法逆转'); + } +} diff --git a/app/Admin/Forms/QuotaV1Recharge.php b/app/Admin/Forms/QuotaV1Recharge.php new file mode 100644 index 00000000..ecd0f1c8 --- /dev/null +++ b/app/Admin/Forms/QuotaV1Recharge.php @@ -0,0 +1,69 @@ +can('dcat.admin.quota_v1_send_jobs.recharge'); + } + + /** + * Handle the form request. + * + * @param array $input + * + * @return mixed + */ + public function handle(array $input) + { + if (($input['change_balance'] ?? 0) <= 0) { + return $this->response()->error('增加配额必须大于0'); + } + try { + DB::beginTransaction(); + //获取当前操作人; + $adminUser = Admin::user(); + $user = User::findOrFail($input['user_id'] ?? 0); + $quotaV1Service = new QuotaV1Service(); + $quotaV1Service->changeBalance($user, $input['change_balance'] ?? 0, QuotaV1Log::ACTION_ADMIN_RECHARGE, '后台增加', $adminUser); + 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->select('user_id', '用户手机号')->ajax(admin_route('api.users'))->required(); + $this->currency('change_balance', '增加配额')->symbol('¥')->required(); + $this->confirm('是否确认增加老配额?', '提交后该动作无法逆转'); + } +} diff --git a/app/Models/QuotaV1Log.php b/app/Models/QuotaV1Log.php new file mode 100644 index 00000000..6271df3d --- /dev/null +++ b/app/Models/QuotaV1Log.php @@ -0,0 +1,14 @@ +userInfo()->lockForUpdate()->first(); + + if ($userInfo === null) { + throw new BizException('系统错误'); + } + + // 变更前余额 + $beforeBalance = $userInfo->quota_v1; + $_changeBalance = abs($changeBalance); + + if ($changeBalance > 0) { + // 收入 + $user->userInfo()->increment('quota_v1', $_changeBalance); + } else { + // 支出 + if ($userInfo->quota_v1 < $_changeBalance) { + throw new BizException('老配额不足'); + } + + $user->userInfo()->decrement('quota_v1', $_changeBalance); + } + + $user->walletLogs()->create([ + 'loggable_id' => $loggable?->id, + 'loggable_type' => $loggable?->getMorphClass(), + 'before_balance' => $beforeBalance, + 'change_balance' => $changeBalance, + 'action' => $action, + 'remarks' => $remarks, + ]); + } +} diff --git a/database/migrations/2022_02_28_161649_create_quota_v1_logs_table.php b/database/migrations/2022_02_28_161649_create_quota_v1_logs_table.php new file mode 100644 index 00000000..2f14c0f9 --- /dev/null +++ b/database/migrations/2022_02_28_161649_create_quota_v1_logs_table.php @@ -0,0 +1,39 @@ +id(); + $table->unsignedBigInteger('user_id')->comment('用户ID'); + $table->nullableMorphs('loggable'); + $table->tinyInteger('action')->comment('操作类型'); + $table->unsignedDecimal('before_balance')->default(0)->comment('变更前的余额'); + $table->unsignedDecimal('change_balance', 12, 3)->default(0)->comment('变动余额'); + $table->string('remarks')->nullable()->comment('备注'); + $table->timestamps(); + + $table->index('user_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('quota_v1_logs'); + } +}