分离扣费部分,增加扣费请求
This commit is contained in:
parent
9e6e5151af
commit
c333e3bc38
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Host;
|
||||
use Cache;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HostController extends Controller
|
||||
@ -33,34 +34,59 @@ public function store(Request $request)
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @param Host $host
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
public function show(Host $host)
|
||||
{
|
||||
|
||||
return $this->success($host);
|
||||
//
|
||||
|
||||
// dd($host->cost());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @param Host $host
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
public function update(Request $request, Host $host)
|
||||
{
|
||||
//
|
||||
$request->validate([
|
||||
'status' => 'sometimes|in:stopped,running,suspended,error',
|
||||
'managed_price' => 'sometimes|numeric|nullable',
|
||||
|
||||
// 如果是立即扣费
|
||||
'cost_once' => 'sometimes|boolean|nullable',
|
||||
]);
|
||||
|
||||
// if has cost_once
|
||||
if ($request->has('cost_once')) {
|
||||
$host->cost($request->cost_once);
|
||||
|
||||
return $this->updated($request->cost_once);
|
||||
}
|
||||
|
||||
$host->update($request->all());
|
||||
|
||||
return $this->updated($host);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @param Host $host
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
public function destroy(Host $host)
|
||||
{
|
||||
//
|
||||
$host->delete();
|
||||
|
||||
return $this->deleted($host);
|
||||
}
|
||||
}
|
||||
|
@ -35,40 +35,42 @@ public function __construct()
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->cache = Cache::tags(['users']);
|
||||
// $this->cache = new Cache();
|
||||
|
||||
// chunk hosts and load user
|
||||
Host::active()->with('user')->chunk(100, function ($hosts) {
|
||||
foreach ($hosts as $host) {
|
||||
|
||||
$this->cache_key = 'user_' . $host->user_id;
|
||||
$host->cost();
|
||||
|
||||
// if cache has user
|
||||
// $this->cache_key = 'user_' . $host->user_id;
|
||||
|
||||
if ($this->cache->has($this->cache_key)) {
|
||||
// if user is not instances of Model
|
||||
$user = $this->cache->get($this->cache_key);
|
||||
// // if cache has user
|
||||
|
||||
if ($user instanceof 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());
|
||||
}
|
||||
// if ($this->cache->has($this->cache_key)) {
|
||||
// // if user is not instances of Model
|
||||
// $user = $this->cache->get($this->cache_key);
|
||||
|
||||
// Log::debug($user);
|
||||
// if ($user instanceof 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());
|
||||
// }
|
||||
|
||||
if ($host->managed_price) {
|
||||
$host->price = $host->managed_price;
|
||||
}
|
||||
// // Log::debug($user);
|
||||
|
||||
// if ($host->managed_price) {
|
||||
// $host->price = $host->managed_price;
|
||||
// }
|
||||
|
||||
|
||||
$this->user->drops -= $host->price;
|
||||
// $this->user->drops -= $host->price;
|
||||
|
||||
// update cache
|
||||
$this->cache->put($this->cache_key, $this->user, now()->addDay());
|
||||
// // update cache
|
||||
// $this->cache->put($this->cache_key, $this->user, now()->addDay());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
70
app/Jobs/Remote/Host.php
Normal file
70
app/Jobs/Remote/Host.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Remote;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use App\Models\Host as HostModel;
|
||||
|
||||
class Host implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public HostModel $host;
|
||||
public string $type;
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(HostModel $host, $type = 'post')
|
||||
{
|
||||
//
|
||||
$this->host = $host;
|
||||
$this->host->load(['module']);
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
//
|
||||
|
||||
$http = Http::remote($this->host->module->api_token, $this->host->module->url);
|
||||
|
||||
switch ($this->type) {
|
||||
case 'put':
|
||||
$response = $http->put('hosts/' . $this->host->id, $this->host->toArray());
|
||||
break;
|
||||
case 'post':
|
||||
$response = $http->post('hosts', $this->host->toArray());
|
||||
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
$response = $http->delete('hosts/' . $this->host->id);
|
||||
|
||||
// if success
|
||||
if ($response->successful()) {
|
||||
$this->host->delete();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$response->successful()) {
|
||||
$this->host->status = 'error';
|
||||
}
|
||||
|
||||
$this->host->save();
|
||||
}
|
||||
}
|
@ -34,18 +34,15 @@ public function __construct()
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
//
|
||||
$this->cache = Cache::tags(['users']);
|
||||
|
||||
Host::active()->chunk(100, function ($hosts) {
|
||||
foreach ($hosts as $host) {
|
||||
$this->cache_key = 'user_' . $host->user_id;
|
||||
|
||||
// if cache has user
|
||||
|
||||
if ($this->cache->has($this->cache_key)) {
|
||||
if (Cache::has($this->cache_key)) {
|
||||
// if user is not instances of Model
|
||||
$user = $this->cache->get($this->cache_key);
|
||||
$user = Cache::get($this->cache_key);
|
||||
if ($user instanceof User) {
|
||||
$this->await($this->cache_key, function () use ($user, $host) {
|
||||
$user->save();
|
||||
|
@ -2,12 +2,13 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Exceptions\CommonException;
|
||||
use App\Models\Module\Module;
|
||||
use App\Exceptions\CommonException;
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class Host extends Model
|
||||
{
|
||||
@ -31,17 +32,20 @@ class Host extends Model
|
||||
|
||||
|
||||
// user
|
||||
public function user() {
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// module
|
||||
public function module() {
|
||||
public function module()
|
||||
{
|
||||
return $this->belongsTo(Module::class);
|
||||
}
|
||||
|
||||
// workOrders
|
||||
public function workOrders() {
|
||||
public function workOrders()
|
||||
{
|
||||
return $this->hasMany(WorkOrder::class);
|
||||
}
|
||||
|
||||
@ -52,11 +56,13 @@ public function workOrders() {
|
||||
|
||||
|
||||
// scope
|
||||
public function scopeActive($query) {
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where('status', 'running')->where('price', '!=', 0);
|
||||
}
|
||||
|
||||
public function scopeThisUser($query, $module = null) {
|
||||
public function scopeThisUser($query, $module = null)
|
||||
{
|
||||
if ($module) {
|
||||
return $query->where('user_id', auth()->id())->where('module_id', $module);
|
||||
} else {
|
||||
@ -64,6 +70,46 @@ public function scopeThisUser($query, $module = null) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// cost
|
||||
public function cost($price = null)
|
||||
{
|
||||
|
||||
$cache_key = 'user_' . $this->user_id;
|
||||
|
||||
// if cache has user
|
||||
|
||||
if (Cache::has($cache_key)) {
|
||||
// if user is not instances of Model
|
||||
$user = Cache::get($cache_key);
|
||||
|
||||
if (!($user instanceof User)) {
|
||||
$user = Cache::put($cache_key, $this->user, now()->addDay());
|
||||
}
|
||||
} else {
|
||||
$user = Cache::put($cache_key, $this->user, now()->addDay());
|
||||
}
|
||||
|
||||
|
||||
// Log::debug($user);
|
||||
|
||||
if ($price !== null) {
|
||||
$this->managed_price = $price;
|
||||
}
|
||||
|
||||
if ($this->managed_price) {
|
||||
$this->price = $this->managed_price;
|
||||
}
|
||||
|
||||
|
||||
$user->drops -= $this->price;
|
||||
|
||||
// update cache
|
||||
Cache::put($cache_key, $user, now()->addDay());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// on create
|
||||
protected static function boot()
|
||||
{
|
||||
@ -85,5 +131,17 @@ protected static function boot()
|
||||
// add to queue
|
||||
|
||||
});
|
||||
|
||||
// when Updated
|
||||
static::updated(function ($model) {
|
||||
dispatch(new \App\Jobs\Remote\Host($model, 'put'));
|
||||
});
|
||||
|
||||
// when delete
|
||||
static::deleting(function ($model) {
|
||||
return false;
|
||||
|
||||
dispatch(new \App\Jobs\Remote\Host($model, 'delete'));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
Route::apiResource('modules', Remote\ModuleController::class)->only(['index']);
|
||||
Route::apiResource('servers', Remote\ServerController::class);
|
||||
Route::apiResource('hosts', Remote\Host\HostController::class);
|
||||
Route::patch('hosts/{host}/drops', [Remote\Host\DropController::class, 'update']);
|
||||
// Route::patch('hosts/{host}', [Remote\Host\DropController::class, 'update']);
|
||||
Route::apiResource('hosts.tasks', Remote\Host\TaskController::class);
|
||||
|
||||
Route::apiResource('work-orders', Remote\WorkOrder\WorkOrderController::class);
|
||||
|
Loading…
Reference in New Issue
Block a user