<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\admin\admin;

use app\repository\admin\AdminUserRepository;
use app\repository\admin\RoleRepository;
use app\service\domain\admin\CoreAdminPermissionService;
use app\service\RequestHelper;
use app\service\admin\BaseAdminService;
use app\service\domain\admin\CoreRoleService;
use think\Request;


/**
 * Role Service
 * @package app\service\admin\admin
 */
class RoleService extends BaseAdminService
{

    /**
     * 获取可分配的权限树
     * @param Request $request 请求对象
     * @return array
     */
    public function permissions(Request $request): array
    {
        $actor = $this->adminUserRepository()->findById((int)($request->uid ?? 0));
        if (!$actor || (int)($actor->status ?? 0) !== 1) {
            return ['code' => 403, 'msg' => '', '_http_status' => 403];
        }

        $permissionService = $this->permissionService();
        return [
            'code' => 0,
            'data' => [
                'tree' => $permissionService->getGrantableTreeForUser($actor),
                'flat' => $permissionService->getRegistry()->getFlat(),
            ],
        ];
    }


    /**
     * 角色分页列表
     * @param Request $request 请求对象
     * @return array
     */
    public function list(Request $request): array
    {
        return [
            'code' => 0,
            'data' => $this->roleRepository()->paginate([
                'keyword' => $request->get('keyword'),
                'status' => $request->get('status'),
            ], RequestHelper::parseLimit($request, 'limit', 10, 100)),
        ];
    }


    /**
     * 新增或更新角色
     * @param Request $request 请求对象
     * @return array
     */
    public function save(Request $request): array
    {
        $actor = $this->adminUserRepository()->findById((int)($request->uid ?? 0));
        if (!$actor || (int)($actor->status ?? 0) !== 1) {
            return ['code' => 403, 'msg' => '', '_http_status' => 403];
        }

        return $this->core()->save($actor, [
            'id' => $request->post('id'),
            'name' => $request->post('name', ''),
            'code' => $request->post('code', ''),
            'status' => $request->post('status', 1),
            'permissions' => $request->post('permissions'),
        ]);
    }


    /**
     * 删除角色
     * @param Request $request 请求对象
     * @return array
     */
    public function delete(Request $request): array
    {
        $id = $request->post('id');
        if (!is_numeric($id)) {
            return ['code' => 1, 'msg' => ''];
        }

        return $this->core()->delete((int)$id);
    }

    /**
     * 获取角色核心服务实例
     * @return CoreRoleService
     */
    private function core(): CoreRoleService
    {
        return $this->app->make(CoreRoleService::class);
    }

    /**
     * 获取管理员权限服务实例
     * @return CoreAdminPermissionService
     */
    private function permissionService(): CoreAdminPermissionService
    {
        return $this->app->make(CoreAdminPermissionService::class);
    }

    /**
     * 获取管理员用户仓储实例
     * @return AdminUserRepository
     */
    private function adminUserRepository(): AdminUserRepository
    {
        return $this->app->make(AdminUserRepository::class);
    }

    /**
     * 获取角色仓储实例
     * @return RoleRepository
     */
    private function roleRepository(): RoleRepository
    {
        return $this->app->make(RoleRepository::class);
    }
}