<?php
namespace App\JsonRpc;
use App\Model\Role;
use App\Model\Menu;
use App\Model\Website;
use App\Model\WebsiteRoleUser;
use App\Tools\Result;
use Hyperf\RpcServer\Annotation\RpcService;

#[RpcService(name: "AuthorityService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
class AuthorityService implements AuthorityServiceInterface
{

    /**
     * @param array $data
     * @return array
     */
    public function getMenuList(array $data): array
    {
        // TODO: Implement getMenuList() method.
        $where = [
            "pid"=>$data['id']
        ];
        $data['pageSize'] = $data['pageSize']?$data['pageSize']:10;
        $data['page'] = $data['page']?$data['page']:1;
        $result = Menu::where($where)->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->get();
        $count =  Menu::where($where)->count();
        if (empty($result)) {
            return Result::error("没有菜单",0,[]);
        }
        foreach($result as $k=>$v){
            $result[$k]['is_links'] = (int)$result[$k]['is_links'];
            $result[$k]['hidden'] = (int)$result[$k]['hidden'];
        }
        $data['rows'] = $result;
        $data['count'] = $count;
        return Result::success($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function getMenuInfo(array $data): array
    {
        // TODO: Implement getMenuInfo() method.
        return  [];
    }

    /**
     * @param array $data
     * @return array
     */
    public function updateMenu(array $data): array
    {
        $where = [
            'id'=>$data['id']
        ];
        unset($data['id']);
        $result = Menu::where($where)->update($data);
        if($result){
            return Result::success($data);
        }else{
            return Result::error($data);
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function delMenu(array $data): array
    {
        $result = Menu::where(['id'=>$data['id']])->delete();
        if($result){
            return Result::success($data);
        }else{
            return Result::error($data);
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function addMenu(array $data): array
    {
        $result = Menu::insertGetId($data);
        if($result){
            return Result::success($data);
        }else{
            return Result::error($data);
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function getRecursionMenu(array $data): array
    {
        //先查询站点ID
        $websiteData = [
            'website_url' => $data['logindevice']
        ];
        $websiteInfo = Website::where($websiteData)->first();
//        var_dump($websiteInfo,$data['logindevice']);
        if(empty($websiteInfo)){
            return Result::error("网站不存在",0);
        }
        //根据网站和用户ID 查询出角色
        $whereData = [
            'website_id'=>$websiteInfo['id'],
            'user_id'=>$data['user_id']??''
        ];
        $WebsiteRoleUserInfo = WebsiteRoleUser::where($whereData)->first();
//        var_dump("++++:",$WebsiteRoleUserInfo['role_id']);
        if(empty($WebsiteRoleUserInfo)){
            return Result::error("角色不存在",0);
        }

        //根据角色查询权限信息
        $roleWhere = [
            'id'=>$WebsiteRoleUserInfo['role_id']
        ];
        $roleInfo = Role::where($roleWhere)->first();
//        var_dump("+++++++++++",$roleInfo);
        if(empty($roleInfo)){
            return Result::error("没有权限",0);
        }
        //查询
        var_dump("=========:",$roleInfo['rule']);
        $roleArr = json_decode($roleInfo['rule']);
//        var_dump($roleArr);
        $result = Menu::whereIn('id',$roleArr)->get();
//        var_dump("+++++++++++",$result);
        if (empty($result)) {
            return Result::error("没有菜单",0,[]);
        }
        return Result::success($result);
    }
}