支持 Managed Price

This commit is contained in:
iVampireSP.com 2022-11-18 19:39:30 +08:00
parent 59729277bb
commit 4b9d6213a3
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
10 changed files with 233 additions and 79 deletions

View File

@ -5,6 +5,7 @@
use App\Http\Controllers\Controller;
use App\Models\Host;
use Illuminate\Http\Request;
use Illuminate\View\View;
class HostController extends Controller
{
@ -58,9 +59,11 @@ public function show(Host $host)
* @param \App\Models\Host $host
* @return \Illuminate\Http\Response
*/
public function edit(Host $host)
public function edit(Host $host): View
{
//
return view('admin.hosts.edit', compact('host'));
}
/**
@ -73,6 +76,15 @@ public function edit(Host $host)
public function update(Request $request, Host $host)
{
//
$request->validate([
'name' => 'required|string|max:255',
'managed_price' => 'nullable|numeric',
]);
$host->update($request->all());
return back()->with('success', '此主机已更新。');
}
/**

View File

@ -101,6 +101,11 @@ public function edit(User $user)
public function update(Request $request, User $user)
{
//
$request->validate([
'balance' => 'nullable|numeric|min:0.01|max:1000',
'drops' => 'nullable|numeric|min:1|max:10000',
]);
$transaction = new Transaction();

View File

@ -4,24 +4,27 @@
use App\Http\Controllers\Controller;
use App\Models\WorkOrder\WorkOrder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;
class WorkOrderController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
* @return View
*/
public function index()
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.
*
* @return \Illuminate\Http\Response
* @return Response
*/
public function create()
{
@ -32,7 +35,7 @@ public function create()
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @return Response
*/
public function store(Request $request)
{
@ -43,41 +46,54 @@ public function store(Request $request)
* Display the specified resource.
*
* @param \App\Models\WorkOrder\WorkOrder $workOrder
* @return \Illuminate\Http\Response
* @return Response
*/
public function show(WorkOrder $workOrder)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\WorkOrder\WorkOrder $workOrder
* @return \Illuminate\Http\Response
* @param \App\Models\WorkOrder\WorkOrder $workOrder
*
* @return View
*/
public function edit(WorkOrder $workOrder)
public function edit(WorkOrder $workOrder): View
{
//
return view('admin.work-orders.edit', compact('workOrder'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\WorkOrder\WorkOrder $workOrder
* @return \Illuminate\Http\Response
* @param \Illuminate\Http\Request $request
* @param \App\Models\WorkOrder\WorkOrder $workOrder
*
* @return RedirectResponse
*/
public function update(Request $request, WorkOrder $workOrder)
public function update(Request $request, WorkOrder $workOrder): RedirectResponse
{
//
$request->validate([
'title' => 'required|string|max:255',
]);
$workOrder->update($request->all());
return back()->with('success', '工单更新成功');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\WorkOrder\WorkOrder $workOrder
* @return \Illuminate\Http\Response
* @return Response
*/
public function destroy(WorkOrder $workOrder)
{

View File

@ -38,57 +38,6 @@ class Host extends Model
];
// get user hosts
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);
});
}
// user
public function getUserHosts($user_id = null)
@ -121,7 +70,7 @@ public function workOrders(): HasManyAlias
public function scopeActive($query)
{
return $query->where('status', 'running')->where('price', '!=', 0);
return $query->whereIn('status', ['running', 'stopped'])->where('price', '!=', 0)->where('managed_price', '!=', 0);
}
@ -147,7 +96,12 @@ public function cost($price = null, $auto = true): bool
if ($price !== null) {
$real_price = $price;
} else {
$real_price = $this->price;
if ($this->managed_price === null) {
$real_price = $this->price;
} else {
$real_price = $this->managed_price;
}
}
if ($real_price == 0) {
@ -239,4 +193,52 @@ 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

@ -0,0 +1,33 @@
@extends('layouts.admin')
@section('title', '主机:' . $host->name)
@section('content')
<h3>{{ $host->name }}</h3>
{{-- 修改主机 --}}
<form method="post" action="{{ route('admin.hosts.update', $host) }}">
@csrf
@method('PUT')
<div class="form-group">
<label for="name" class="col-sm-2 col-form-label">名称</label>
<input type="text" class="form-control" id="name" name="name" value="{{ $host->name }}">
</div>
<div class="form-group">
<label for="managed_price" class="col-sm-2 col-form-label">新的价格 (Drops)</label>
<input type="text" class="form-control" id="managed_price" name="managed_price"
value="{{ $host->managed_price }}" placeholder="{{ $host->price }}">
留空以使用默认价格
</div>
<button type="submit" class="btn btn-primary mt-3">修改</button>
</form>
@endsection

View File

@ -19,12 +19,12 @@
@foreach ($hosts as $host)
<tr>
<td>
<a href="{{ route('admin.hosts.show', $host) }}">
<a href="{{ route('admin.hosts.edit', $host) }}">
{{ $host->id }}
</a>
</td>
<td>
<span class="module_name" module="{{ $host->module_id }}">{{ $host->module_id }}</span>
<a href="{{ route('admin.hosts.edit', $host) }}" class="module_name" module="{{ $host->module_id }}">{{ $host->module_id }}</a>
</td>
<td>
{{ $host->name }}

View File

@ -4,7 +4,7 @@
@section('content')
<h3>{{ $user->name }}</h3>
<a href="{{ route('admin.users.show', $user) }}">切换到 {{ $user->name }}</a>
<a href="{{ route('admin.users.show', $user) }}">作为 {{ $user->name }} 登录</a>
@if ($user->banned_at)
<p class="text-danger">已被封禁,原因: {{ $user->banned_reason }}</p>
@ -44,13 +44,13 @@
</td>
<td>{{ $host->name }}</td>
<td>
<span>{{ $host->price }} Drops</span>
<span>{{ $host->managed_price ?? $host->price }} Drops</span>
<span>{{ round($host->price / $drops_rage * (30 * 24 * 60 / 5), 2) }} / </span>
<span>{{ round($host->managed_price ?? $host->price / $drops_rage * (30 * 24 * 60 / 5), 2) }} / </span>
</td>
<td>
<a href="{{ route('admin.hosts.show', $host) }}" class="btn btn-primary btn-sm">查看</a>
<a href="{{ route('admin.hosts.edit', $host) }}" class="btn btn-primary btn-sm">查看</a>
</td>
</tr>
</tbody>
@ -79,7 +79,7 @@
<x-work-order-status :status="$workOrder->status"/>
</td>
<td>
<a href="{{ route('admin.work-orders.show', $host) }}" class="btn btn-primary btn-sm">查看</a>
<a href="{{ route('admin.work-orders.show', $host) }}" class="btn btn-primary btn-sm">编辑</a>
</td>
</tr>
@endforeach
@ -136,12 +136,12 @@
<div class="form-group">
<label for="balance">充值余额()</label>
<input type="number" class="form-control" id="balance" name="balance" placeholder="充值金额">
<input type="text" class="form-control" id="balance" name="balance" placeholder="充值金额">
</div>
<div class="form-group">
<label for="drops">充值 Drops</label>
<input type="number" class="form-control" id="drops" name="drops" placeholder="充值 Drops">
<input type="text" class="form-control" id="drops" name="drops" placeholder="充值 Drops">
</div>
{{-- 封禁 --}}

View File

@ -11,7 +11,7 @@
<th>ID</th>
<th>用户名</th>
<th>邮箱</th>
<th></th>
<th></th>
<th>注册时间</th>
<th>操作</th>
</thead>

View File

@ -0,0 +1,28 @@
@extends('layouts.admin')
@section('title', '工单: ' . $workOrder->title)
@section('content')
<form method="post" action="{{ route('admin.work-orders.update', $workOrder) }}">
@csrf
@method('PUT')
<div class="form-group">
<label for="title" class="col-sm-2 col-form-label">标题</label>
<input type="text" class="form-control" id="title" name="title" value="{{ $workOrder->title }}">
</div>
{{-- <div class="form-group">--}}
{{-- <textaera name="title">--}}
{{-- {{ $workOrder->title }}--}}
{{-- </textaera>--}}
{{-- </div>--}}
<button type="submit" class="btn btn-primary mt-3">修改</button>
</form>
@endsection

View File

@ -0,0 +1,58 @@
@extends('layouts.admin')
@section('title', '工单')
@section('content')
<div class="overflow-auto">
<table class="table table-hover">
<thead>
<th>ID</th>
<th>标题</th>
<th>模块</th>
<th>发起者</th>
<th>操作</th>
</thead>
<tbody>
@foreach ($workOrders as $workOrder)
<tr>
<td>
<a href="{{ route('admin.work-orders.show', $workOrder) }}">
{{ $workOrder->id }}
</a>
</td>
<td>
<a href="{{ route('admin.work-orders.show', $workOrder) }}">
{{ $workOrder->title }}
</a>
</td>
<td>
<a
href="{{ route('admin.modules.show', $workOrder->module_id) }}"
class="module_name"
module="{{ $workOrder->module_id }}">{{ $workOrder->module_id }}
</a>
</td>
<td>
<a href="{{ route('admin.users.edit', $workOrder->user_id) }}">{{ $workOrder->user->name }}</a>
</td>
<td>
<a href="{{ route('admin.work-orders.edit', $workOrder) }}"
class="btn btn-primary btn-sm">编辑</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
{{-- 分页 --}}
{{ $workOrders->links() }}
@endsection