优化 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;
use App\Models\Host;
use App\Models\User;
use Illuminate\Console\Command;

View File

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

View File

@ -34,6 +34,7 @@ public function create()
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
@ -45,6 +46,7 @@ public function store(Request $request)
* Display the specified resource.
*
* @param \App\Models\Module $module
*
* @return \Illuminate\Http\Response
*/
public function show(Module $module)
@ -56,6 +58,7 @@ public function show(Module $module)
* Show the form for editing the specified resource.
*
* @param \App\Models\Module $module
*
* @return \Illuminate\Http\Response
*/
public function edit(Module $module)
@ -68,6 +71,7 @@ public function edit(Module $module)
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Module $module
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Module $module)
@ -79,6 +83,7 @@ public function update(Request $request, Module $module)
* Remove the specified resource from storage.
*
* @param \App\Models\Module $module
*
* @return \Illuminate\Http\Response
*/
public function destroy(Module $module)

View File

@ -32,6 +32,7 @@ public function create()
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
@ -43,6 +44,7 @@ public function store(Request $request)
* Display the specified resource.
*
* @param \App\Models\WorkOrder\Reply $reply
*
* @return \Illuminate\Http\Response
*/
public function show(Reply $reply)
@ -54,6 +56,7 @@ public function show(Reply $reply)
* Show the form for editing the specified resource.
*
* @param \App\Models\WorkOrder\Reply $reply
*
* @return \Illuminate\Http\Response
*/
public function edit(Reply $reply)
@ -66,6 +69,7 @@ public function edit(Reply $reply)
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\WorkOrder\Reply $reply
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Reply $reply)
@ -77,6 +81,7 @@ public function update(Request $request, Reply $reply)
* Remove the specified resource from storage.
*
* @param \App\Models\WorkOrder\Reply $reply
*
* @return \Illuminate\Http\Response
*/
public function destroy(Reply $reply)

View File

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

View File

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

View File

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

View File

@ -11,7 +11,6 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo as BelongsToAlias;
use Illuminate\Database\Eloquent\Relations\HasMany as HasManyAlias;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
// use Illuminate\Database\Eloquent\SoftDeletes;
@ -41,6 +40,56 @@ class Host extends Model
// 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)
{
return $this->where('user_id', $user_id)->with('module', function ($query) {
@ -48,35 +97,33 @@ public function getUserHosts($user_id = null)
})->get();
}
// module
// workOrders
public function user(): BelongsToAlias
{
return $this->belongsTo(User::class);
}
// workOrders
// scope
public function module(): BelongsToAlias
{
return $this->belongsTo(Module::class);
}
// scope
public function workOrders(): HasManyAlias
{
return $this->hasMany(WorkOrder::class);
}
// cost
public function scopeActive($query)
{
return $query->whereIn('status', ['running', 'stopped'])->where('price', '!=', 0)->where('managed_price', '!=', 0);
}
// cost
public function scopeThisUser($query, $module = null)
{
if ($module) {
@ -248,52 +295,4 @@ public function costBalance($amount = 1): bool
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 = '扣除费用请求。')
{
@ -406,30 +432,4 @@ public function reduceDropsWithoutHost($user_id, $amount = 0, $description = nul
$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',
];
public function hosts(): HasMany
{
return $this->hasMany(Host::class);
}
protected static function boot()
{
parent::boot();
@ -73,6 +68,11 @@ protected static function boot()
});
}
public function hosts(): HasMany
{
return $this->hasMany(Host::class);
}
/**
* @throws CommonException
* @throws BalanceNotEnoughException

View File

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