<?php

namespace app\admin\controller\User;

use app\BaseController;
use app\common\model\Role\Role as ModelRole;
use app\common\model\User\User as ModelUser;
use app\common\model\User\UserRole as ModelUserRole;
use app\Request;

class UserRole extends BaseController
{
    /**
     * 获取未绑定角色用户列表
     *
     * @param Request $request
     * @return void
     * @date 2022-03-02
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getUnbindUserList(Request $request): array
    {
        $role_guid = $request->param('role_guid');
        $user = $request->param('user');
        $this->validate($request->param(), [
            'role_guid' => 'require'
        ]);
        $user_guids = [];
        $con = [];
        $users =  ModelRole::getByRoleGuid($role_guid)->users;
        if ($users) {
            $user_guids = $users->column('user_guid');
        }
        if ($user) {
            $con['user_name'] = $user;
        }
        $query = ModelUser::withSearch(['user_name'], $con)->where($con)->where('user_guid', 'not in', $user_guids);
        $select =  self::pageWrapper($query)->field([
            'user_name',
            'user_guid',
        ])->order([
            'user_id' => 'desc'
        ])->select();
        $count =  $query->count();
        return [
            'code' => 0,
            'data' => $select,
            'count' => $count,
            'msg' => 'ok'
        ];
    }
    /**
     * 获取绑定角色用户列表
     *
     * @param Request $request
     * @return void
     * @date 2022-03-02
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getBindUserList(Request $request): array
    {
        $role_guid = $request->param('role_guid');
        $user = $request->param('user');
        $this->validate($request->param(), [
            'role_guid' => 'require'
        ]);
        $user_ids = [];
        $con = [];
        $users = ModelRole::getByRoleGuid($role_guid)->users;
        if ($users) {
            $user_ids = $users->column('user_id');
        }
        if ($user) {
            $con['user_name'] = $user;
        }
        $query = ModelUser::withSearch(['user_name'], $con)->where($con)->where('user_id', 'in', $user_ids);
        $select =  self::pageWrapper($query)->field([
            'user_name',
            'user_guid',
        ])->order([
            'user_id' => 'desc'
        ])->select();
        $count =  $query->count();
        return [
            'code' => 0,
            'data' => $select,
            'count' => $count,
            'msg' => 'ok'
        ];
    }

    /**
     * 绑定用户角色
     *
     * @param Request $request
     * @date 2022-03-02
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function bindUserRole(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'user_guid' => 'require',
            'role_guid' => 'require',
        ]);
        $users = array_map(function ($v) {
            return ModelUser::getByUserGuid($v);
        }, explode(',', $params['user_guid']));
        $roles = array_map(function ($v) {
            return ModelRole::getByRoleGuid($v);
        }, explode(',', $params['role_guid']));
        ModelUserRole::bindUserRole($users, $roles);
        return [
            'code' => 0,
            'msg' => '绑定成功'
        ];
    }

    /**
     * 绑定用户角色
     *
     * @param Request $request
     * @date 2022-03-02
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function unBindUserRole(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'user_guid' => 'require',
            'role_guid' => 'require',
        ]);
        $users = array_map(function ($v) {
            return ModelUser::getByUserGuid($v);
        }, explode(',', $params['user_guid']));
        $roles = array_map(function ($v) {
            return ModelRole::getByRoleGuid($v);
        }, explode(',', $params['role_guid']));
        ModelUserRole::unbindUserRole($users, $roles);
        return [
            'code' => 0,
            'msg' => '解绑成功'
        ];
    }
}
