ServerInformationController.php 5.6 KB
<?php

namespace App\Http\Controllers\Aside\Devops;

use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Devops\ServerInformationLogic;
use App\Http\Requests\Aside\Devops\ServerInformationRequest;
use App\Models\Devops\ServerInformation;
use App\Models\Devops\ServerInformationLog;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ServerInformationController extends BaseController
{
    /**
     * @return JsonResponse
     */
    public function lists($deleted = ServerInformation::DELETED_NORMAL)
    {
        $request = $this->param;
        $search = [];
        $search_array = [
            'ip'    => 'ip',
            'title' => 'title'
        ];
        foreach ($search_array as $key => $item) {
            if (isset($request[$key]) && $request[$key]) {
                $search[$item] = $request[$key];
            }
        }
        $size = request()->input('size', $this->row);
        $query = ServerInformation::query()->select(['id', 'title', 'ip']);
        if ($search) {
            foreach ($search as $key => $item) {
                $query = $query->Where("{$key}", 'like', "%{$item}%");
            }
        }
        $data = $query->where('deleted', $deleted)
            ->orderBy('id', 'desc')
            ->paginate($size);
        return $this->response('success', Code::SUCCESS, $data);

    }

    /**
     * 添加
     * @param ServerInformationRequest $serverInformationRequest
     * @param ServerInformationLogic $serverInformationLogic
     * @return void
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     */
    public function add(ServerInformationRequest $serverInformationRequest, ServerInformationLogic $serverInformationLogic)
    {

        $serverInformationRequest->validated();
        $serverInformationLogic->create();
        $this->response('服务器添加成功!');
    }

    /**
     * 编辑
     * @param ServerInformationRequest $serverInformationRequest
     * @param ServerInformationLogic $serverInformationLogic
     * @return void
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     */
    public function edit(ServerInformationRequest $serverInformationRequest, ServerInformationLogic $serverInformationLogic)
    {
        $serverInformationRequest->validate([
            'id' => 'required|integer',
        ], [
            'id.required' => 'ID不能为空',
            'id.integer'  => 'ID必须为整数',
        ]);
        $serverInformationLogic->update();
        $this->response('服务器修改成功!');
    }

    /**
     * 删除
     * @param ServerInformationLogic $serverInformationLogic
     * @return void
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     */
    public function delete(ServerInformationLogic $serverInformationLogic)
    {
        $serverInformationLogic->get_batch_update();
        $this->response('服务器删除成功!');
    }

    /**
     * 获取软删除的数据
     * @return JsonResponse
     */
    public function delete_list()
    {
        return $this->lists(ServerInformation::DELETED_DELETE);
    }

    /**
     * 恢复数据
     * @param ServerInformationLogic $serverInformationLogic
     * @return void
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     */
    public function restore(ServerInformationLogic $serverInformationLogic)
    {
        $serverInformationLogic->get_batch_update(ServerInformationLog::ACTION_RECOVER, ServerInformation::DELETED_DELETE);
        $this->response('服务器恢复成功!');
    }


    /**
     * 搜索
     * @param Request $request
     * @return JsonResponse
     */
    public function search(Request $request)
    {
        $search = [];
        $ip = $request->input('ip');
        if ($ip) {
            $search['ip'] = $ip;
        }
        $remark = $request->input('title');
        if ($remark) {
            $search['title'] = $remark;
        }
        if (empty($search)) {
            return $this->response('请输入搜索内容', Code::USER_ERROR);
        }
        $query = ServerInformation::query()->select(['id', 'title', 'ip']);
        foreach ($search as $key => $item) {
            $query = $query->Where("{$key}", 'like', "%{$item}%");
        }
        $size = $request->input('size', $this->row);
        $query = $query->orderBy('id', 'desc')->paginate($size);
        return $this->response('success', Code::SUCCESS, $query);
    }

    /**
     * 服务器信息
     * @param int $deleted
     * @return JsonResponse
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getServerInfo(int $deleted = ServerInformation::DELETED_NORMAL)
    {
        $serverInformationLogic = new ServerInformationLogic();
        $data = $serverInformationLogic->serverInfo($deleted);
        if (!$data) {
            return $this->response('服务器信息不存在', Code::USER_ERROR);
        }
        return $this->success($data->toArray());
    }

    /**
     * 获取软删除的服务器信息
     * @return JsonResponse
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getDeleteServerInfo()
    {
        return $this->getServerInfo(ServerInformation::DELETED_DELETE);
    }


}