修复 计费系统

This commit is contained in:
iVampireSP.com 2022-08-15 21:20:42 +08:00
parent 10bd7f8373
commit 90b357591e
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
7 changed files with 45 additions and 36 deletions

View File

@ -37,7 +37,7 @@ public function store(Request $request, Host $host)
{ {
$request->validate([ $request->validate([
'name' => 'required|max:255', 'name' => 'required|max:255',
'provider_module_id' => 'required|integer|exists:provider_modules,id', 'module_id' => 'required|string|exists:modules,id',
'price' => 'required|numeric', 'price' => 'required|numeric',
]); ]);
@ -53,7 +53,7 @@ public function store(Request $request, Host $host)
$data = [ $data = [
'name' => $request->name, 'name' => $request->name,
'provider_module_id' => $request->provider_module_id, 'module_id' => $request->module_id,
'user_id' => $user_id, 'user_id' => $user_id,
'price' => $request->price, 'price' => $request->price,
'configuration' => $request->configuration ?? [], 'configuration' => $request->configuration ?? [],

View File

@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers\Remote\Host;
use App\Http\Controllers\Controller;
use App\Models\User\Host;
use Illuminate\Http\Request;
class DropController extends Controller
{
public function update(Request $request, Host $host) {
$request->validate([
'managed_price' => 'sometimes|numeric|max:1000|nullable',
]);
$host->managed_price = $request->managed_price;
$host->save();
return $this->updated($host);
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace App\Http\Controllers\Remote\User;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
class DropController extends Controller
{
public function destroy(Request $request, User $user) {
$request->validate([
'amount' => 'required|numeric|max:1000',
]);
return $this->success();
}
}

View File

@ -12,6 +12,7 @@
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class HostCost implements ShouldQueue class HostCost implements ShouldQueue
{ {
@ -27,7 +28,6 @@ class HostCost implements ShouldQueue
public function __construct() public function __construct()
{ {
// //
$this->cache = Cache::tags(['users']);
} }
/** /**
@ -37,6 +37,8 @@ public function __construct()
*/ */
public function handle() public function handle()
{ {
$this->cache = Cache::tags(['users']);
// chunk hosts and load user // chunk hosts and load user
Host::active()->with('user')->chunk(100, function ($hosts) { Host::active()->with('user')->chunk(100, function ($hosts) {
foreach ($hosts as $host) { foreach ($hosts as $host) {
@ -48,17 +50,23 @@ public function handle()
if ($this->cache->has($this->cache_key)) { if ($this->cache->has($this->cache_key)) {
// if user is not instances of Model // if user is not instances of Model
$user = $this->cache->get($this->cache_key); $user = $this->cache->get($this->cache_key);
if (!($user instanceof User)) {
$this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay()); if ($user instanceof User) {
} else {
$this->user = $user; $this->user = $user;
} else {
$this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay());
} }
} else {
$this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay());
} }
// Log::debug($user);
if ($host->managed_price) { if ($host->managed_price) {
$host->price = $host->managed_price; $host->price = $host->managed_price;
} }
$this->user->drops -= $host->price; $this->user->drops -= $host->price;
// update cache // update cache

View File

@ -26,7 +26,6 @@ class UserSave implements ShouldQueue
public function __construct() public function __construct()
{ {
// //
$this->cache = Cache::tags(['users']);
} }
/** /**
@ -37,6 +36,8 @@ public function __construct()
public function handle() public function handle()
{ {
// //
$this->cache = Cache::tags(['users']);
Host::active()->chunk(100, function ($hosts) { Host::active()->chunk(100, function ($hosts) {
foreach ($hosts as $host) { foreach ($hosts as $host) {
$this->cache_key = 'user_' . $host->user_id; $this->cache_key = 'user_' . $host->user_id;

View File

@ -4,7 +4,6 @@
use App\Models\User; use App\Models\User;
use App\Models\Module\Module; use App\Models\Module\Module;
// use App\Models\Module\ProviderModule;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -18,7 +17,7 @@ class Host extends Model
protected $fillable = [ protected $fillable = [
'name', 'name',
'provider_module_id', 'module_id',
'user_id', 'user_id',
'price', 'price',
'configuration', 'configuration',
@ -36,10 +35,10 @@ public function user() {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
// // provider module // module
// public function provider_module() { public function module() {
// return $this->belongsTo(ProviderModule::class); return $this->belongsTo(Module::class);
// } }
// workorders // workorders
public function workorders() { public function workorders() {
@ -63,8 +62,8 @@ protected static function boot()
parent::boot(); parent::boot();
static::creating(function ($model) { static::creating(function ($model) {
$model->load(['provider_module']); // $model->load('module');
$model->provider_module->load(['provider', 'module']); // $model->module->load(['provider', 'module']);
// add to queue // add to queue

View File

@ -6,7 +6,7 @@
Route::name('remote.')->middleware(['api'])->group(function () { Route::name('remote.')->middleware(['api'])->group(function () {
Route::apiResource('modules', Remote\ModuleController::class)->only(['index']); Route::apiResource('modules', Remote\ModuleController::class)->only(['index']);
Route::apiResource('servers', Remote\ServerController::class); Route::apiResource('servers', Remote\ServerController::class);
Route::delete('users/{user}/drops', [Remote\User\DropController::class, 'destroy']); Route::patch('hosts/{host}/drops', [Remote\Host\DropController::class, 'update']);
Route::apiResource('users.tasks', Remote\User\TaskController::class); Route::apiResource('users.tasks', Remote\User\TaskController::class);
// Route::apiResource('users', Controllers\User\UserController::class); // Route::apiResource('users', Controllers\User\UserController::class);
}); });