<?php

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

use App\JsonRpc\WebsiteServiceInterface;
use App\Tools\PublicData;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Tools\Result;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
/**
 * 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__);
    }
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;
    /**
     * @var WebsiteServiceInterface
     */
    #[Inject]
    private $websiteServiceClient;
    /**
     * 创建站点
     * @return array
     */
    public function addWebsite()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make($requireData, ['website_column_id' => 'required', 'website_name' => 'required', 'website_url' => 'required'], ['website_column_id.required' => '栏目id不能为空', 'website_name.required' => '站点名称不能为空', 'website_url.required' => '网站地址不能为空']);
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        if (isset($requireData['website_column_id']) && is_array($requireData['website_column_id'])) {
            $requireData['website_column_arr_id'] = json_encode($requireData['website_column_id']);
            $requireData['website_column_id'] = end($requireData['website_column_id']);
        } else {
            if ($requireData['website_column_id'] == "0") {
                $requireData['website_column_id'] = [0];
            } else {
                $id = [$requireData['website_column_id']];
                $requireData['website_column_arr_id'] = json_encode($id);
                $requireData['website_column_id'] = end($id);
            }
        }
        if (isset($requireData['city_id']) && is_array($requireData['city_id'])) {
            $requireData['city_arr_id'] = json_encode($requireData['city_id']);
            $requireData['city_id'] = end($requireData['city_id']);
        } else {
            $city_id = [0];
            $requireData['city_arr_id'] = json_encode($city_id);
            $requireData['city_id'] = end($city_id);
        }
        $result = $this->websiteServiceClient->createWebsite($requireData);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 获取站点列表
     * @return void
     */
    public function getWebsiteList()
    {
        $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);
        if ($result['data']) {
            foreach ($result['data']['rows'] as $k => $v) {
                $result['data']['rows'][$k]['city_arr_id'] = $v['city_arr_id'] ? json_decode($v['city_arr_id']) : '';
            }
        }
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 更新站点
     * @return array
     */
    public function updateWebsite()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make($requireData, ['id' => 'required', 'website_column_id' => 'required', 'website_name' => 'required', 'website_url' => 'required'], ['id.required' => 'id不能为空', 'website_column_id.required' => '栏目id不能为空', 'website_name.required' => '站点名称不能为空', 'website_url.required' => '网站地址不能为空']);
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        if (isset($requireData['website_column_id']) && is_array($requireData['website_column_id'])) {
            $requireData['website_column_arr_id'] = json_encode($requireData['website_column_id']);
            $requireData['website_column_id'] = end($requireData['website_column_id']);
        } else {
            if ($requireData['website_column_id'] == "0") {
                $requireData['website_column_id'] = [0];
            } else {
                $id = [$requireData['website_column_id']];
                $requireData['website_column_arr_id'] = json_encode($id);
                $requireData['website_column_id'] = end($id);
            }
        }
        if (isset($requireData['city_id']) && is_array($requireData['city_id'])) {
            $requireData['city_arr_id'] = json_encode($requireData['city_id']);
            $requireData['city_id'] = end($requireData['city_id']);
        } else {
            $city_id = [0];
            $requireData['city_arr_id'] = json_encode($city_id);
            $requireData['city_id'] = end($city_id);
        }
        $result = $this->websiteServiceClient->updateWebsite($requireData['id'], $requireData);
        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"]);
        return $result['code'] == 200 ? Result::success($result['data']) : Result::error($result['message']);
    }
    public function getWebsiteColumn()
    {
        $pid = (int) $this->request->input("pid", 0);
        $result = $this->websiteServiceClient->getWebsiteColumn([]);
        $rep = PublicData::buildMenuTree($result['data']);
        return $rep ? Result::success($rep) : Result::error("查询失败");
    }
    /**
     * 获取站点列表
     * @return void
     */
    public function getWebsiteColumnList()
    {
        $keyword = $this->request->input("keyWord", '');
        $page = (int) $this->request->input("page", 1);
        $pageSize = (int) $this->request->input("pageSize", 10);
        $result = $this->websiteServiceClient->getWebsiteColumnList($keyword, $page, $pageSize);
        if ($result['data']) {
            foreach ($result['data']['rows'] as $k => $v) {
                $result['data']['rows'][$k]['column_arr_id'] = $v['column_arr_id'] ? json_decode($v['column_arr_id']) : [0];
            }
        }
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 创建栏目
     * @return array
     */
    public function addWebsiteColumn()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make($requireData, ['column_name' => 'required', 'column_arr_id' => 'required'], ['column_name.required' => '栏目名称不能为空', 'column_arr_id.required' => '栏目ID不能为空']);
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        if (isset($requireData['column_arr_id']) && is_array($requireData['column_arr_id'])) {
            $requireData['pid'] = end($requireData['column_arr_id']);
            $requireData['column_arr_id'] = json_encode($requireData['column_arr_id']);
        } else {
            $column_arr_id = [0];
            $requireData['column_arr_id'] = json_encode($column_arr_id);
            $requireData['pid'] = end($column_arr_id);
        }
        $result = $this->websiteServiceClient->createWebsiteColumn($requireData);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 创建栏目
     * @return array
     */
    public function updateWebsiteColumn()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make($requireData, ['column_name' => 'required', 'column_arr_id' => 'required'], ['column_name.required' => '栏目名称不能为空', 'column_arr_id.required' => '栏目ID不能为空']);
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        if (isset($requireData['column_arr_id']) && is_array($requireData['column_arr_id'])) {
            $requireData['pid'] = end($requireData['column_arr_id']);
            $requireData['column_arr_id'] = json_encode($requireData['column_arr_id']);
        } else {
            $column_arr_id = [0];
            $requireData['column_arr_id'] = json_encode($column_arr_id);
            $requireData['pid'] = end($column_arr_id);
        }
        $result = $this->websiteServiceClient->updateWebsiteColumn($requireData['id'], $requireData);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 删除栏目
     * @return array
     */
    public function delWebsiteColumn()
    {
        $id = (int) $this->request->input("id", '');
        $result = $this->websiteServiceClient->delWebsiteColumn($id);
        return $result['code'] == 200 ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 获取站点角色列表
     * @return void
     */
    public function getWebsiteRoleList()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make($requireData, ['id' => 'required'], ['id.required' => '站点ID不能为空']);
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        $keyword = $this->request->input("keyword", '');
        $page = (int) $this->request->input("page", 1);
        $pageSize = (int) $this->request->input("pageSize", 10);
        $websiteId = (int) $this->request->input("id", '');
        $result = $this->websiteServiceClient->getWebsiteRoleList($keyword, $page, $pageSize, $websiteId);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 创建站点角色
     * @return array
     */
    public function createWebsiteRole()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make($requireData, ['type' => 'required', 'role_id' => 'required', 'website_id' => 'required'], ['type.required' => '类型不能为空', 'type_id.required' => 'ID不能为空', 'website_id.required' => '站点ID不能为空']);
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        $requireData['admin_user_id'] = $this->UserId;
        $result = $this->websiteServiceClient->createWebsiteRole($requireData);
        return $result['code'] != 0 ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 删除站点角色
     * @return array
     */
    public function delWebsiteRole()
    {
        $id = (int) $this->request->input("id", '');
        $result = $this->websiteServiceClient->delWebsiteRole($id);
        return $result['code'] == 200 ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 获取站点角色用户列表
     * @return void
     */
    public function getWebsiteRoleUserList()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make($requireData, ['website_id' => 'required', 'role_id' => 'required'], ['website_id.required' => '站点ID不能为空', 'role_id.required' => '角色ID不能为空']);
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        $keyword = $this->request->input("keyword", '');
        $page = (int) $this->request->input("page", 1);
        $pageSize = (int) $this->request->input("pageSize", 10);
        $roleId = (int) $this->request->input("role_id", '');
        $websiteId = (int) $this->request->input("website_id", '');
        $result = $this->websiteServiceClient->getWebsiteRoleUserList($keyword, $page, $pageSize, $websiteId, $roleId);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 创建站点角色用户
     * @return array
     */
    public function createWebsiteRoleUser()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make($requireData, ['type' => 'required', 'role_id' => 'required', 'website_id' => 'required', 'user_id' => 'required'], ['type.required' => '类型不能为空', 'website_id.required' => '站点ID不能为空', 'role_id.required' => '角色ID不能为空', 'user_id.required' => '用户ID不能为空']);
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        $requireData['admin_user_id'] = $this->UserId;
        $requireData['role_id'] = (int) $requireData['role_id'];
        $requireData['type'] = (int) $requireData['type'];
        $requireData['website_id'] = (int) $requireData['website_id'];
        $requireData['user_id'] = (int) $requireData['user_id'];
        $result = $this->websiteServiceClient->createWebsiteRoleUser($requireData);
        return $result['code'] != 0 ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 删除站点角色用户
     * @return array
     */
    public function delWebsiteRoleUser()
    {
        $id = (int) $this->request->input("id", '');
        $result = $this->websiteServiceClient->delWebsiteRoleUser($id);
        return $result['code'] == 200 ? Result::success($result['data']) : Result::error($result['message']);
    }
}