Lae/app/Http/Controllers/Module/HostController.php

166 lines
4.4 KiB
PHP
Raw Normal View History

2022-08-16 10:44:16 +00:00
<?php
2023-01-19 16:11:50 +00:00
namespace App\Http\Controllers\Module;
2022-08-16 10:44:16 +00:00
2022-11-06 11:28:22 +00:00
use App\Http\Controllers\Controller;
2022-08-29 09:31:08 +00:00
use App\Models\Host;
use App\Models\User;
2022-11-20 13:48:33 +00:00
use Illuminate\Contracts\Pagination\Paginator;
2022-11-16 02:29:50 +00:00
use Illuminate\Http\JsonResponse;
2022-08-16 10:44:16 +00:00
use Illuminate\Http\Request;
2022-11-16 02:29:50 +00:00
use Illuminate\Http\Response;
2022-11-06 11:28:22 +00:00
use Illuminate\Support\Str;
2022-12-26 15:19:05 +00:00
use Illuminate\Validation\ValidationException;
2023-02-14 05:32:51 +00:00
use function auth;
2022-11-06 11:28:22 +00:00
2023-01-10 13:49:55 +00:00
// use App\Models\User;
2022-08-16 10:44:16 +00:00
class HostController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): Paginator
2022-08-16 10:44:16 +00:00
{
2023-01-10 13:42:27 +00:00
return (new Host)->where('module_id', $request->user('module')->id)->simplePaginate(100);
2022-08-16 10:44:16 +00:00
}
/**
* Store a newly created resource in storage.
*
2023-02-14 05:32:51 +00:00
* @param Request $request
*
2022-11-16 02:29:50 +00:00
* @return Response|JsonResponse
2023-01-30 16:14:07 +00:00
*
2022-12-26 15:19:05 +00:00
* @throws ValidationException
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function store(Request $request): Response|JsonResponse
2022-08-16 10:44:16 +00:00
{
2022-08-28 17:17:57 +00:00
// 存储计费项目
2022-09-08 16:12:02 +00:00
$this->validate($request, [
2023-02-12 18:22:12 +00:00
'status' => 'required|in:draft,running,stopped,error,suspended,pending',
'billing_cycle' => 'nullable|in:monthly,quarterly,semi-annually,annually,biennially,triennially',
2022-08-29 09:31:08 +00:00
'price' => 'required|numeric',
'user_id' => 'required|integer|exists:users,id',
]);
2023-01-10 13:42:27 +00:00
$user = (new User)->findOrFail($request->input('user_id'));
2023-01-10 13:42:27 +00:00
if ($request->input('price') > 0) {
if ($user->balance < 1) {
return $this->error('此用户余额不足,无法开设计费项目。');
}
}
2022-08-29 09:31:08 +00:00
// 如果没有 name则随机
$name = $request->input('name', Str::random(10));
$data = [
'name' => $name,
'user_id' => $user->id,
2023-01-30 16:14:07 +00:00
'module_id' => auth('module')->id(),
2023-02-12 18:22:12 +00:00
'price' => $request->input('price'),
'managed_price' => $request->input('managed_price'),
'billing_cycle' => $request->input('billing_cycle'),
'status' => $request->input('status'),
2022-08-29 09:31:08 +00:00
];
2023-01-10 13:42:27 +00:00
$host = (new Host)->create($data);
2022-08-29 09:31:08 +00:00
$host['host_id'] = $host->id;
return $this->created($host);
2022-08-16 10:44:16 +00:00
}
/**
* Display the specified resource.
*
2023-02-14 05:32:51 +00:00
* @param Host $host
*
2022-11-16 02:29:50 +00:00
* @return JsonResponse
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function show(Host $host): JsonResponse
2022-08-16 10:44:16 +00:00
{
return $this->success($host);
2022-08-16 10:44:16 +00:00
//
// dd($host->cost());
2022-08-16 10:44:16 +00:00
}
/**
* Update the specified resource in storage.
*
2023-02-14 05:32:51 +00:00
* @param Request $request
* @param Host $host
*
2022-11-16 02:29:50 +00:00
* @return JsonResponse
2023-01-30 16:14:07 +00:00
*
2022-12-26 15:19:05 +00:00
* @throws ValidationException
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function update(Request $request, Host $host): JsonResponse
2022-08-16 10:44:16 +00:00
{
2022-09-08 16:12:02 +00:00
$this->validate($request, [
2022-08-29 09:31:08 +00:00
'status' => 'sometimes|in:running,stopped,error,suspended,pending',
2022-11-19 14:42:47 +00:00
'managed_price' => 'sometimes|numeric|nullable',
]);
$update = $request->except(['module_id', 'user_id']);
2022-08-29 10:59:32 +00:00
$host->update($update);
return $this->updated($host);
2022-08-16 10:44:16 +00:00
}
/**
* Remove the specified resource from storage.
*
2023-01-30 16:14:07 +00:00
* @param $host
2023-02-14 05:32:51 +00:00
*
2022-11-16 02:29:50 +00:00
* @return JsonResponse
2022-08-16 10:44:16 +00:00
*/
2022-11-16 05:22:41 +00:00
public function destroy($host): JsonResponse
2022-08-16 10:44:16 +00:00
{
2022-12-28 13:17:54 +00:00
// if host not instance of HostJob
2023-02-14 05:32:51 +00:00
if (!$host instanceof Host) {
2023-01-10 13:42:27 +00:00
$host = (new Host)->findOrFail($host);
2022-10-16 09:25:04 +00:00
}
2023-02-14 05:32:51 +00:00
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,
]);
}
2022-11-21 03:53:13 +00:00
$host?->delete();
return $this->deleted();
2022-08-16 10:44:16 +00:00
}
2023-02-10 18:18:34 +00:00
public function cost(Request $request, Host $host): JsonResponse
{
$this->validate($request, [
2023-02-11 05:48:03 +00:00
'amount' => 'required|numeric|min:0.0001',
2023-02-10 18:18:34 +00:00
'description' => 'nullable|string:max:255',
]);
2023-02-10 18:20:18 +00:00
$host->cost($request->input('amount'), false, $request->input('description'));
2023-02-10 18:18:34 +00:00
return $this->noContent();
}
2022-08-16 10:44:16 +00:00
}