<?php

declare (strict_types=1);
namespace App\Controller;

use App\JsonRpc\WebsiteServiceInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Tools\Result;
use App\Constants\ErrorCode;
use Psr\Log\LoggerInterface;
/**
 * Class WebsiteController
 * @package App\Controller
 */
#[AutoController]
class WebsiteController extends AbstractController
{
    use \Hyperf\Di\Aop\ProxyTrait;
    use \Hyperf\Di\Aop\PropertyHandlerTrait;
    function __construct(\Hyperf\Logger\LoggerFactory $loggerFactory)
    {
        if (method_exists(parent::class, '__construct')) {
            parent::__construct(...func_get_args());
        }
        $this->__handlePropertyHandler(__CLASS__);
    }
    /**
     * @var WebsiteServiceInterface
     */
    #[Inject]
    private $websiteServiceClient;
    /**
     * 创建站点
     * @return array
     */
    public function createWebsite()
    {
        $data = $this->request->all();
        $result = $this->websiteServiceClient->createWebsite($data);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 获取站点列表
     * @return void
     */
    public function getWebsitetList()
    {
        $keyword = $this->request->input("keyword", '');
        $page = (int) $this->request->input("page", 1);
        $pageSize = (int) $this->request->input("pageSize", 10);
        $result = $this->websiteServiceClient->getWebsitetList($keyword, $page, $pageSize);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 更新站点
     * @return array
     */
    public function updateWebsite()
    {
        $id = (int) $this->request->input("id", '');
        $req = $this->request->all();
        unset($req['id']);
        $result = $this->websiteServiceClient->updateWebsite($id, $req);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 删除站点
     * @return array
     */
    public function delWebsite()
    {
        $id = (int) $this->request->input("id", '');
        $result = $this->websiteServiceClient->delWebsite($id);
        return $result['code'] == 200 ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 获取站点信息
     * @return array
     */
    public function getWebsiteInfo()
    {
        $id = (int) $this->request->input("id", '');
        $result = $this->websiteServiceClient->getWebsiteInfo($id);
        $this->logger->info('hello world', [1 => "ceshi"], ["a" => "11"]);
        //        $this->logger->error("====@@");
        return $result['code'] == 200 ? Result::success($result['data']) : Result::error($result['message']);
    }
}