添加老配额增减
parent
610080c092
commit
62427335fd
|
|
@ -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('¥');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Extensions\Grid\Tools\QuotaV1;
|
||||
|
||||
use App\Admin\Forms\QuotaV1Deduction;
|
||||
use Dcat\Admin\Grid\Tools\AbstractTool;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
|
||||
class Deduction extends AbstractTool
|
||||
{
|
||||
protected function authorize($user): bool
|
||||
{
|
||||
return $user->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 [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Extensions\Grid\Tools\QuotaV1;
|
||||
|
||||
use App\Admin\Forms\QuotaV1Recharge;
|
||||
use Dcat\Admin\Grid\Tools\AbstractTool;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
|
||||
class Recharge extends AbstractTool
|
||||
{
|
||||
protected function authorize($user): bool
|
||||
{
|
||||
return $user->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 [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Forms;
|
||||
|
||||
use App\Models\QuotaV1Log;
|
||||
use App\Models\User;
|
||||
use App\Services\QuotaV1Service;
|
||||
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 QuotaV1Deduction extends Form implements LazyRenderable
|
||||
{
|
||||
use LazyWidget;
|
||||
|
||||
/**
|
||||
* @param Model|Authenticatable|HasPermissions|null $user
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorize($user): bool
|
||||
{
|
||||
return $user->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('是否确认扣减?', '提交后该动作无法逆转');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Forms;
|
||||
|
||||
use App\Models\QuotaV1Log;
|
||||
use App\Models\User;
|
||||
use App\Services\QuotaV1Service;
|
||||
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 QuotaV1Recharge extends Form implements LazyRenderable
|
||||
{
|
||||
use LazyWidget;
|
||||
|
||||
/**
|
||||
* @param Model|Authenticatable|HasPermissions|null $user
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorize($user): bool
|
||||
{
|
||||
return $user->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('是否确认增加老配额?', '提交后该动作无法逆转');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class QuotaV1Log extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const ACTION_ADMIN_RECHARGE = 7;
|
||||
public const ACTION_ADMIN_DEDUCTION = 8;
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\User;
|
||||
|
||||
class QuotaV1Service
|
||||
{
|
||||
/**
|
||||
* 变老配额余额
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @param int $changeBalance
|
||||
* @param int $action
|
||||
* @param string|null $remarks
|
||||
* @param mixed $loggable
|
||||
* @return void
|
||||
*/
|
||||
public function changeBalance(User $user, int $changeBalance, int $action, ?string $remarks = null, $loggable = null)
|
||||
{
|
||||
if ($changeBalance === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$userInfo = $user->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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateQuotaV1LogsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('quota_v1_logs', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue