修复 删除主机的 bug

This commit is contained in:
iVampireSP.com 2023-02-14 13:32:51 +08:00
parent 3999e61a20
commit 38a4d2d3e0
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 31 additions and 25 deletions

View File

@ -5,13 +5,13 @@
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Host; use App\Models\Host;
use App\Models\User; use App\Models\User;
use function auth;
use Illuminate\Contracts\Pagination\Paginator; use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use function auth;
// use App\Models\User; // use App\Models\User;
@ -28,7 +28,8 @@ public function index(Request $request): Paginator
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param Request $request * @param Request $request
*
* @return Response|JsonResponse * @return Response|JsonResponse
* *
* @throws ValidationException * @throws ValidationException
@ -74,7 +75,8 @@ public function store(Request $request): Response|JsonResponse
/** /**
* Display the specified resource. * Display the specified resource.
* *
* @param Host $host * @param Host $host
*
* @return JsonResponse * @return JsonResponse
*/ */
public function show(Host $host): JsonResponse public function show(Host $host): JsonResponse
@ -88,8 +90,9 @@ public function show(Host $host): JsonResponse
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param Request $request * @param Request $request
* @param Host $host * @param Host $host
*
* @return JsonResponse * @return JsonResponse
* *
* @throws ValidationException * @throws ValidationException
@ -112,15 +115,37 @@ public function update(Request $request, Host $host): JsonResponse
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
* @param $host * @param $host
*
* @return JsonResponse * @return JsonResponse
*/ */
public function destroy($host): JsonResponse public function destroy($host): JsonResponse
{ {
// if host not instance of HostJob // if host not instance of HostJob
if (! $host instanceof Host) { if (!$host instanceof Host) {
$host = (new Host)->findOrFail($host); $host = (new Host)->findOrFail($host);
} }
if ($host?->isCycle()) {
$days = $host->next_due_at->diffInDays(now());
// 算出 1 天的价格
$price = bcdiv($host->getPrice(), 31, 4);
// 算出退还的金额
$amount = bcmul($price, $days, 4);
$host->user->charge($amount, 'balance', '删除主机退款。', [
'module_id' => $this->module_id,
'host_id' => $this->id,
'user_id' => $this->user_id,
]);
$host->module->reduce($amount, '删除主机退款。', false, [
'module_id' => $this->module_id,
'host_id' => $this->id,
]);
}
$host?->delete(); $host?->delete();
return $this->deleted(); return $this->deleted();

View File

@ -281,25 +281,6 @@ public function safeDelete(): bool
if ($this->next_due_at === null) { if ($this->next_due_at === null) {
$this->next_due_at = now(); $this->next_due_at = now();
} }
$days = $this->next_due_at->diffInDays(now());
// 算出 1 天的价格
$price = bcdiv($this->getPrice(), 31, 4);
// 算出退还的金额
$amount = bcmul($price, $days, 4);
$this->user->charge($amount, 'balance', '删除主机退款。', [
'module_id' => $this->module_id,
'host_id' => $this->id,
'user_id' => $this->user_id,
]);
$this->module->reduce($amount, '删除主机退款。', false, [
'module_id' => $this->module_id,
'host_id' => $this->id,
]);
} }
// 如果创建时间大于 1 小时 // 如果创建时间大于 1 小时