<?php
namespace App\JsonRpc;
use App\Model\WebsiteRole;
use App\Model\WebsiteRoleUser;
use App\Model\Website;
use App\Model\WebsiteColumn;
use Hyperf\RpcServer\Annotation\RpcService;
use App\Tools\Result;
use App\Model\WebsiteCategory;
use function PHPUnit\Framework\isNull;

#[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
class WebsiteService implements WebsiteServiceInterface
{
    /**
     * @param string $keyword
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public function getWebsitetList(string $keyword,int $page,int $pageSize):array
    {
        $where = [
            ['website.website_name','like','%'.$keyword.'%']
        ];
        $result = Website::where($where)
            ->leftJoin("website_column","website.website_column_id","website_column.id")
            ->leftJoin("district","district.id","website.city_id")
            ->select("website.*","website_column.column_name","district.name")
            ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
        $count = Website::where($where)->count();
        if (empty($result)) {
            return Result::error("没有数据",0);
        }
        $data = [
            'rows'=>$result->toArray(),
            'count'=>$count
        ];
        return Result::success($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function createWebsite(array $data): array
    {
        $insertData = [
            'website_name'=>$data['website_name'],
            'logo'=>$data['logo']??'',
            'website_url'=>$data['website_url']??'',
            'city_id'=>$data['city_id']??0,
            'website_column_id'=>$data['website_column_id'],
            'title'=>$data['title']??'',
            'keywords'=>$data['keywords']??'',
            'description'=>$data['description']??'',
            'status'=>$data['status']??0,
            'website_column_arr_id'=>$data['website_column_arr_id'],
            'city_arr_id'=>$data['city_arr_id']??[0],
        ];
        $result = Website::insertGetId($insertData);
        if(empty($result)){
            return Result::error("创建失败",0);
        }else{
            return Result::success(["id"=>$result]);
        }
    }

    /**
     * @param int $id
     * @param array $data
     * @return array
     */
    public function updateWebsite(int $id,array $data): array
    {
        $insertData = [
            'website_name'=>$data['website_name'],
            'logo'=>$data['logo']??'',
            'website_url'=>$data['website_url']??'',
            'city_id'=>$data['city_id']??0,
            'website_column_id'=>$data['website_column_id'],
            'title'=>$data['title']??'',
            'keywords'=>$data['keywords']??'',
            'description'=>$data['description']??'',
            'status'=>$data['status']??0,
            'website_column_arr_id'=>$data['website_column_arr_id'],
            'city_arr_id'=>$data['city_arr_id']??[0],
        ];
        $result = Website::where('id',$id)->update($insertData);
        var_dump("更新站点",$result);
        if(empty($result)){
            return Result::error("更新失败",0);
        }else{
            return Result::success();
        }
    }

    /**
     * @param int $id
     * @return array
     */
    public function delWebsite(int $id): array
    {
        $result = Website::where('id',$id )->delete();
        var_dump("删除站点",$result);
        if(empty($result)){
            return Result::error("删除失败",0);
        }else{
            return Result::success();
        }
    }

    /**
     * @param int $id
     * @return array
     */
    public function getWebsiteInfo(int $id): array
    {
        $result = Website::where('id',$id )->first();
        if(empty($result)){
            return Result::error("数据不存在",0);
        }else{
            return Result::success($result->toArray());
        }

    }

    /**
     * 查询所有的站点栏目
     * @return array
     */
    public function getWebsiteColumn(array $data): array
    {
        $result = WebsiteColumn::where($data)->get();
        if(empty($result)){
            return Result::error("数据不存在",0);
        }else{
            return Result::success($result->toArray());
        }

    }


    /**
     * @param string $keyword
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public function getWebsiteColumnList(string $keyword,int $page,int $pageSize):array
    {
        $where = [
            ['website_column.column_name','like','%'.$keyword.'%']
        ];
        $result = WebsiteColumn::where($where)
            ->leftJoin("website_column as wc","website_column.pid","wc.id")
            ->select("website_column.*","wc.column_name as parent_column_name")
            ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
        $count = WebsiteColumn::where($where)->count();
        if (empty($result)) {
            return Result::error("没有数据",0);
        }
        $data = [
            'rows'=>$result->toArray(),
            'count'=>$count
        ];
        return Result::success($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function createWebsiteColumn(array $data): array
    {
        $insertData = [
            'column_name'=>$data['column_name'],
            'pid'=>$data['pid']??'',
            'column_arr_id'=>$data['column_arr_id']??[0],
            'sort'=>$data['sort']??0,
            'remark'=>$data['remark']??'',
        ];
        $result = WebsiteColumn::insertGetId($insertData);
        if(empty($result)){
            return Result::error("创建失败",0);
        }else{
            return Result::success(["id"=>$result]);
        }
    }

    /**
     * @param int $id
     * @param array $data
     * @return array
     */
    public function updateWebsiteColumn(int $id,array $data): array
    {
        $insertData = [
            'column_name'=>$data['column_name'],
            'pid'=>$data['pid']??'',
            'column_arr_id'=>$data['column_arr_id']??[0],
            'sort'=>$data['sort']??0,
            'remark'=>$data['remark']??'',
        ];
        $result = WebsiteColumn::where('id',$id)->update($insertData);
        if(empty($result)){
            return Result::error("更新失败",0);
        }else{
            return Result::success();
        }
    }

    /**
     * @param int $id
     * @return array
     */
    public function delWebsiteColumn(int $id): array
    {
        $result = WebsiteColumn::where('id',$id )->delete();
        if(empty($result)){
            return Result::error("删除失败",0);
        }else{
            return Result::success();
        }
    }

    /**
     * @param string $keyword
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public function getWebsiteRoleList(string $keyword,int $page,int $pageSize,int $websiteId):array
    {
        $where = [
            ['role.role_name','like','%'.$keyword.'%'],
            ['website_role.website_id','=',$websiteId],
        ];
        $result = WebsiteRole::where($where)
            ->leftJoin("role","role.id","website_role.role_id")
            ->select("role.*","website_role.type","website_role.role_id","website_role.id as website_role_id","website_role.website_id")
            ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
        $count = WebsiteRole::where($where) ->leftJoin("role","role.id","website_role.role_id")->count();
        if (empty($result)) {
            return Result::error("没有数据",0);
        }
        $data = [
            'rows'=>$result->toArray(),
            'count'=>$count
        ];
        return Result::success($data);
    }


    /**
     * @param array $data
     * @return array
     */
    public function createWebsiteRole(array $data): array
    {

        $insertData = [
            'website_id'=>$data['website_id'],
            'role_id'=>$data['role_id']??''
        ];
        $info = WebsiteRole::where($insertData)->first();
        if($info){
            return Result::error("不能重复添加角色",0);
        }
        $insertData['admin_user_id'] = $data['admin_user_id']??'';
        $insertData['type'] = $data['type']??'';
        $result = WebsiteRole::insertGetId($insertData);
        if(empty($result)){
            return Result::error("创建失败",0);
        }else{
            return Result::success(["id"=>$result]);
        }
    }

    /**
     * 暂时用不上
     * @param int $id
     * @param array $data
     * @return array
     */
    public function updateWebsiteRole(int $id,array $data): array
    {
        $insertData = [
            'website_id'=>$data['website_id'],
            'type'=>$data['type']??'',
        ];
        $result = WebsiteRole::where('id',$id)->update($insertData);
        if(empty($result)){
            return Result::error("更新失败",0);
        }else{
            return Result::success();
        }
    }

    /**
     * @param int $id
     * @return array
     */
    public function delWebsiteRole(int $id): array
    {
        $result = WebsiteRole::where('id',$id )->delete();
        if(empty($result)){
            return Result::error("删除失败",0);
        }else{
            return Result::success();
        }
    }


    /**
     * @param string $keyword
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public function getWebsiteRoleUserList(string $keyword,int $page,int $pageSize,int $websiteId,int $roleId):array
    {
        $where = [
            ['website_role_user.website_id','=',$websiteId],
            ['website_role_user.role_id','=',$roleId],
        ];
        $count = WebsiteRoleUser::where($where)->count();
       $where[] =   ['u.user_name','like','%'.$keyword.'%'];
        $result = WebsiteRoleUser::where($where)
            ->leftJoin("user as u","website_role_user.user_id","u.id")
            ->leftJoin("website as w","website_role_user.website_id","u.id")
            ->leftJoin("role as r","website_role_user.role_id","r.id")
            ->select("u.*","u.user_name",'w.website_name','r.role_name','website_role_user.id as website_role_user_id','website_role_user.updated_at as user_update_at')
            ->limit($pageSize)->offset(($page-1)*$pageSize)->get();

        if (empty($result)) {
            return Result::error("没有数据",0);
        }
        $data = [
            'rows'=>$result->toArray(),
            'count'=>$count
        ];
        return Result::success($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function createWebsiteRoleUser(array $data): array
    {
        $insertData = [
            'website_id'=>$data['website_id'],
            'user_id'=>$data['user_id']??'',
        ];
        $info = WebsiteRoleUser::where($insertData)->first();
        if($info){
            return Result::error("不能重复添加角色用户",0);
        }
        $insertData['role_id'] = $data['role_id']??'';
        $insertData['admin_user_id'] = $data['admin_user_id']??'';
        $insertData['type'] = $data['type']??'';
        $result = WebsiteRoleUser::insertGetId($insertData);
        if(empty($result)){
            return Result::error("创建失败",0);
        }else{
            return Result::success(["id"=>$result]);
        }
    }

    /**
     * @param int $id
     * @param array $data
     * @return array
     */
    public function updateWebsiteRoleUser(int $id,array $data): array
    {
        $insertData = [
            'website_id'=>$data['website_id'],
            'type'=>$data['type']??'',
            'type_id'=>$data['type_id']??'',
            'role_id'=>$data['role_id']??'',
        ];
        $result = WebsiteRoleUser::where('id',$id)->update($insertData);
        if(empty($result)){
            return Result::error("更新失败",0);
        }else{
            return Result::success();
        }
    }

    /**
     * @param int $id
     * @return array
     */
    public function delWebsiteRoleUser(int $id): array
    {
        $result = WebsiteRoleUser::where('id',$id )->delete();
        if(empty($result)){
            return Result::error("删除失败",0);
        }else{
            return Result::success();
        }
    }

    /**
     * 根据域名获取网站 站点id
     * @param array $data
     * @return array
     */
    public function getWebsiteId(array $data): array
    {
        $where = [
            'website_url'=>$data['website_url']
        ];
        $result = Website::where($where)->first();
        if(empty($result)){
            return Result::error("查询站点失败",0);
        }else{
            return Result::success($result);
        }
    }

    /**
     * 查询网站下面的导航
     * @param array $data
     * @return array
     */
    public function getWebsiteCategory(array $data): array
    {
        $where = [
            'website_id'=>$data['website_id'],
            'pid'=>0
        ];
        $result = WebsiteCategory::where($where)->orderBy('sort','asc')->get();
        if(empty($result)){
            return Result::error("查询站点栏目失败",0);
        }else{
            return Result::success($result);
        }
    }

    /**
     * 网站首页数据统计， 管理员
     * @return void
     */
    public function getAdminIndex()
    {

    }


}