优化 import

This commit is contained in:
iVampireSP.com 2022-11-19 12:38:26 +08:00
parent 042468ba23
commit 869d92e88b
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
15 changed files with 143 additions and 131 deletions

View File

@ -2,7 +2,6 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Models\Host;
use App\Models\User; use App\Models\User;
use Illuminate\Console\Command; use Illuminate\Console\Command;

View File

@ -37,7 +37,8 @@ public function create()
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
*
* @return Response * @return Response
*/ */
public function store(Request $request) public function store(Request $request)
@ -48,7 +49,8 @@ public function store(Request $request)
/** /**
* Display the specified resource. * Display the specified resource.
* *
* @param \App\Models\Host $host * @param \App\Models\Host $host
*
* @return Response * @return Response
*/ */
public function show(Host $host) public function show(Host $host)
@ -59,7 +61,8 @@ public function show(Host $host)
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
* @param \App\Models\Host $host * @param \App\Models\Host $host
*
* @return Response * @return Response
*/ */
public function edit(Host $host): View public function edit(Host $host): View

View File

@ -33,7 +33,8 @@ public function create()
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function store(Request $request) public function store(Request $request)
@ -44,7 +45,8 @@ public function store(Request $request)
/** /**
* Display the specified resource. * Display the specified resource.
* *
* @param \App\Models\Module $module * @param \App\Models\Module $module
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function show(Module $module) public function show(Module $module)
@ -55,7 +57,8 @@ public function show(Module $module)
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
* @param \App\Models\Module $module * @param \App\Models\Module $module
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function edit(Module $module) public function edit(Module $module)
@ -66,8 +69,9 @@ public function edit(Module $module)
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \App\Models\Module $module * @param \App\Models\Module $module
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function update(Request $request, Module $module) public function update(Request $request, Module $module)
@ -78,7 +82,8 @@ public function update(Request $request, Module $module)
/** /**
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
* @param \App\Models\Module $module * @param \App\Models\Module $module
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function destroy(Module $module) public function destroy(Module $module)

View File

@ -31,7 +31,8 @@ public function create()
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function store(Request $request) public function store(Request $request)
@ -42,7 +43,8 @@ public function store(Request $request)
/** /**
* Display the specified resource. * Display the specified resource.
* *
* @param \App\Models\WorkOrder\Reply $reply * @param \App\Models\WorkOrder\Reply $reply
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function show(Reply $reply) public function show(Reply $reply)
@ -53,7 +55,8 @@ public function show(Reply $reply)
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
* @param \App\Models\WorkOrder\Reply $reply * @param \App\Models\WorkOrder\Reply $reply
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function edit(Reply $reply) public function edit(Reply $reply)
@ -64,8 +67,9 @@ public function edit(Reply $reply)
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \App\Models\WorkOrder\Reply $reply * @param \App\Models\WorkOrder\Reply $reply
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function update(Request $request, Reply $reply) public function update(Request $request, Reply $reply)
@ -76,7 +80,8 @@ public function update(Request $request, Reply $reply)
/** /**
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
* @param \App\Models\WorkOrder\Reply $reply * @param \App\Models\WorkOrder\Reply $reply
*
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function destroy(Reply $reply) public function destroy(Reply $reply)

View File

@ -22,6 +22,7 @@ public function index(WorkOrder $workOrder): View
$workOrders = $workOrder->with('user')->paginate(100); $workOrders = $workOrder->with('user')->paginate(100);
return view('admin.work-orders.index', compact('workOrders')); return view('admin.work-orders.index', compact('workOrders'));
} }
/** /**
* Show the form for creating a new resource. * Show the form for creating a new resource.
* *
@ -35,7 +36,8 @@ public function create()
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
*
* @return Response * @return Response
*/ */
public function store(Request $request) public function store(Request $request)
@ -46,7 +48,8 @@ public function store(Request $request)
/** /**
* Display the specified resource. * Display the specified resource.
* *
* @param \App\Models\WorkOrder\WorkOrder $workOrder * @param \App\Models\WorkOrder\WorkOrder $workOrder
*
* @return Response * @return Response
*/ */
public function show(WorkOrder $workOrder): View public function show(WorkOrder $workOrder): View
@ -99,7 +102,8 @@ public function update(Request $request, WorkOrder $workOrder): RedirectResponse
/** /**
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
* @param \App\Models\WorkOrder\WorkOrder $workOrder * @param \App\Models\WorkOrder\WorkOrder $workOrder
*
* @return Response * @return Response
*/ */
public function destroy(WorkOrder $workOrder): RedirectResponse public function destroy(WorkOrder $workOrder): RedirectResponse

View File

@ -4,9 +4,7 @@
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Module; use App\Models\Module;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class ModuleController extends Controller class ModuleController extends Controller

View File

@ -110,7 +110,7 @@ public function notify(Request $request): View|JsonResponse
// if ($balance->paid_at !== null) { // if ($balance->paid_at !== null) {
// // return $this->success('订单已支付'); // // return $this->success('订单已支付');
// return view('balances.process', compact('balance')); // return view('balances.process', compact('balance'));
// } // }
return view('balances.process', compact('balance')); return view('balances.process', compact('balance'));

View File

@ -4,7 +4,6 @@
use App\Models\Module; use App\Models\Module;
use App\Notifications\ModuleEarnings; use App\Notifications\ModuleEarnings;
use Illuminate\Support\Facades\Cache;
class SendModuleEarnings extends Job class SendModuleEarnings extends Job
{ {

View File

@ -11,7 +11,6 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo as BelongsToAlias; use Illuminate\Database\Eloquent\Relations\BelongsTo as BelongsToAlias;
use Illuminate\Database\Eloquent\Relations\HasMany as HasManyAlias; use Illuminate\Database\Eloquent\Relations\HasMany as HasManyAlias;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
// use Illuminate\Database\Eloquent\SoftDeletes; // use Illuminate\Database\Eloquent\SoftDeletes;
@ -41,6 +40,56 @@ class Host extends Model
// user // user
protected static function boot()
{
parent::boot();
static::created(function ($model) {
broadcast(new UserEvent($model->user_id, 'hosts.created', $model));
});
static::updating(function ($model) {
if ($model->isDirty('status')) {
if ($model->status == 'suspended') {
$model->suspended_at = now();
} else {
$model->suspended_at = null;
}
}
broadcast(new UserEvent($model->user_id, 'hosts.updating', $model));
});
// when Updated
static::updated(function ($model) {
dispatch(new \App\Jobs\Module\Host($model, 'patch'));
Cache::forget('user_hosts_' . $model->user_id);
Cache::forget('user_tasks_' . $model->user_id);
broadcast(new UserEvent($model->user_id, 'hosts.updated', $model));
});
//
// static::deleting(function ($model) {
// broadcast(new UserEvent($model->user_id, 'hosts.deleting', $model));
// });
static::deleting(function ($model) {
Cache::forget('user_tasks_' . $model->user_id);
});
static::deleted(function ($model) {
broadcast(new UserEvent($model->user_id, 'hosts.deleted', $model));
Cache::forget('user_tasks_' . $model->user_id);
Cache::forget('user_hosts_' . $model->user_id);
});
}
// module
public function getUserHosts($user_id = null) public function getUserHosts($user_id = null)
{ {
return $this->where('user_id', $user_id)->with('module', function ($query) { return $this->where('user_id', $user_id)->with('module', function ($query) {
@ -48,35 +97,33 @@ public function getUserHosts($user_id = null)
})->get(); })->get();
} }
// module // workOrders
public function user(): BelongsToAlias public function user(): BelongsToAlias
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
// workOrders // scope
public function module(): BelongsToAlias public function module(): BelongsToAlias
{ {
return $this->belongsTo(Module::class); return $this->belongsTo(Module::class);
} }
// scope
public function workOrders(): HasManyAlias public function workOrders(): HasManyAlias
{ {
return $this->hasMany(WorkOrder::class); return $this->hasMany(WorkOrder::class);
} }
// cost
public function scopeActive($query) public function scopeActive($query)
{ {
return $query->whereIn('status', ['running', 'stopped'])->where('price', '!=', 0)->where('managed_price', '!=', 0); return $query->whereIn('status', ['running', 'stopped'])->where('price', '!=', 0)->where('managed_price', '!=', 0);
} }
// cost
public function scopeThisUser($query, $module = null) public function scopeThisUser($query, $module = null)
{ {
if ($module) { if ($module) {
@ -248,52 +295,4 @@ public function costBalance($amount = 1): bool
return true; return true;
} }
protected static function boot()
{
parent::boot();
static::created(function ($model) {
broadcast(new UserEvent($model->user_id, 'hosts.created', $model));
});
static::updating(function ($model) {
if ($model->isDirty('status')) {
if ($model->status == 'suspended') {
$model->suspended_at = now();
} else {
$model->suspended_at = null;
}
}
broadcast(new UserEvent($model->user_id, 'hosts.updating', $model));
});
// when Updated
static::updated(function ($model) {
dispatch(new \App\Jobs\Module\Host($model, 'patch'));
Cache::forget('user_hosts_' . $model->user_id);
Cache::forget('user_tasks_' . $model->user_id);
broadcast(new UserEvent($model->user_id, 'hosts.updated', $model));
});
//
// static::deleting(function ($model) {
// broadcast(new UserEvent($model->user_id, 'hosts.deleting', $model));
// });
static::deleting(function ($model) {
Cache::forget('user_tasks_' . $model->user_id);
});
static::deleted(function ($model) {
broadcast(new UserEvent($model->user_id, 'hosts.deleted', $model));
Cache::forget('user_tasks_' . $model->user_id);
Cache::forget('user_hosts_' . $model->user_id);
});
}
} }

View File

@ -169,6 +169,32 @@ public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount =
} }
public function addPayoutDrops($user_id, $amount, $description, $host_id, $module_id)
{
$data = [
'type' => 'payout',
'payment' => 'drops',
'description' => $description,
'income' => 0,
'income_drops' => 0,
'outcome' => 0,
'outcome_drops' => (float)$amount,
'host_id' => $host_id,
'module_id' => $module_id,
];
// $amount = (double) $amount;
// Log::debug($amount);
// $month = now()->month;
// Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount);
return $this->addLog($user_id, $data);
}
public function reduceAmount($user_id, $amount = 0, $description = '扣除费用请求。') public function reduceAmount($user_id, $amount = 0, $description = '扣除费用请求。')
{ {
@ -406,30 +432,4 @@ public function reduceDropsWithoutHost($user_id, $amount = 0, $description = nul
$this->addPayoutDrops($user_id, $amount, $description, null, null); $this->addPayoutDrops($user_id, $amount, $description, null, null);
} }
public function addPayoutDrops($user_id, $amount, $description, $host_id, $module_id)
{
$data = [
'type' => 'payout',
'payment' => 'drops',
'description' => $description,
'income' => 0,
'income_drops' => 0,
'outcome' => 0,
'outcome_drops' => (float)$amount,
'host_id' => $host_id,
'module_id' => $module_id,
];
// $amount = (double) $amount;
// Log::debug($amount);
// $month = now()->month;
// Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount);
return $this->addLog($user_id, $data);
}
} }

View File

@ -45,11 +45,6 @@ class User extends Authenticatable
'banned_at' => 'datetime', 'banned_at' => 'datetime',
]; ];
public function hosts(): HasMany
{
return $this->hasMany(Host::class);
}
protected static function boot() protected static function boot()
{ {
parent::boot(); parent::boot();
@ -73,6 +68,11 @@ protected static function boot()
}); });
} }
public function hosts(): HasMany
{
return $this->hasMany(Host::class);
}
/** /**
* @throws CommonException * @throws CommonException
* @throws BalanceNotEnoughException * @throws BalanceNotEnoughException

View File

@ -3,13 +3,13 @@
namespace App\Models\WorkOrder; namespace App\Models\WorkOrder;
use App\Exceptions\CommonException; use App\Exceptions\CommonException;
use App\Jobs\Module\WorkOrder\WorkOrder as WorkOrderJob;
use App\Models\Host; use App\Models\Host;
use App\Models\Module; use App\Models\Module;
use App\Models\User; use App\Models\User;
use GeneaLabs\LaravelModelCaching\Traits\Cachable; use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use App\Jobs\Module\WorkOrder\WorkOrder as WorkOrderJob;
class WorkOrder extends Model class WorkOrder extends Model
{ {
@ -26,17 +26,6 @@ class WorkOrder extends Model
'status', 'status',
]; ];
public function safeDelete(): bool
{
if ($this->status == 'pending') {
throw new CommonException('工单状态是 pending无法删除');
}
dispatch(new WorkOrderJob($this, 'delete'));
return true;
}
protected static function boot() protected static function boot()
{ {
parent::boot(); parent::boot();
@ -81,6 +70,17 @@ protected static function boot()
}); });
} }
public function safeDelete(): bool
{
if ($this->status == 'pending') {
throw new CommonException('工单状态是 pending无法删除');
}
dispatch(new WorkOrderJob($this, 'delete'));
return true;
}
// replies // replies
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo

View File

@ -72,18 +72,18 @@
<br/> <br/>
然后,安装依赖 然后,安装依赖
<code>composer install --no-dev</code> <code>composer install --no-dev</code>
<br /> <br/>
接着,升级数据库(只需要在一台节点上执行) 接着,升级数据库(只需要在一台节点上执行)
<code>art migrate</code> <code>art migrate</code>
<br /> <br/>
最后,清除缓存 最后,清除缓存
<code>art optimize</code> <code>art optimize</code>
<br /> <br/>
如果是非 Web 节点,需要重启全部或者对应服务,比如 队列,计划任务 如果是非 Web 节点,需要重启全部或者对应服务,比如 队列,计划任务
<code>supervisorctl restart all</code> <code>supervisorctl restart all</code>
<br /> <br/>
如果是 Web 节点,需要简单重启即可 如果是 Web 节点,需要简单重启即可
<code>supervisorctl restart lae-web</code> <code>supervisorctl restart lae-web</code>

View File

@ -11,10 +11,10 @@
@csrf @csrf
@method('PUT') @method('PUT')
{{-- <div class="form-group">--}} {{-- <div class="form-group">--}}
{{-- <label for="name" class="col-sm-2 col-form-label">名称</label>--}} {{-- <label for="name" class="col-sm-2 col-form-label">名称</label>--}}
{{-- <input type="text" class="form-control" id="name" name="name" value="{{ $host->name }}">--}} {{-- <input type="text" class="form-control" id="name" name="name" value="{{ $host->name }}">--}}
{{-- </div>--}} {{-- </div>--}}
<div class="form-group"> <div class="form-group">
<label for="managed_price" class="col-sm-2 col-form-label">新的价格 (Drops)</label> <label for="managed_price" class="col-sm-2 col-form-label">新的价格 (Drops)</label>

View File

@ -26,7 +26,7 @@
@foreach ($transactions as $t) @foreach ($transactions as $t)
<tr> <tr>
<td> <td>
@if ($t->type === 'payout') @if ($t->type === 'payout')
<span class="text-danger"> <span class="text-danger">
支出 支出