<?php
namespace app\admin\server\role;

use app\admin\model\SystemRole;
use app\admin\model\SystemMenu;
use support\Redis;

class RoleServer
{
    /**
     * Notes:获取角色列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getRoleList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  SystemRole::getRoleList($page, $limit, $keywords);
        $menus = SystemMenu::getMenus(0);

        if (!empty($list) && $menus){
            for ($i=0;$i<count($menus);$i++){
                if ($menus[$i]['menu_is_show'] == 1){
                    $menus[$i]['menu_is_show'] = true;
                }else{
                    $menus[$i]['menu_is_show'] = false;
                }
                $menus[$i]['children'] = SystemMenu::getMenus($menus[$i]['menu_id']);
                if (!empty($menus[$i]['children'])){
                    for ($s=0;$s<count($menus[$i]['children']);$s++){
                        if ($menus[$i]['children'][$s]['menu_is_show'] == 1){
                            $menus[$i]['children'][$s]['menu_is_show'] = true;
                        }else{
                            $menus[$i]['children'][$s]['menu_is_show'] = false;
                        }
                        $menus[$i]['children'][$s]['children'] = SystemMenu::getMenus($menus[$i]['children'][$s]['menu_id']);
                        if (!empty($$menus[$i]['children'][$s]['children'])){
                            for ($j=0;$j<count($menus[$i]['children'][$s]['children']);$j++){
                                if ($menus[$i]['children'][$s]['children'][$j]['menu_is_show'] == 1){
                                    $menus[$i]['children'][$s]['children'][$j]['menu_is_show'] = true;
                                }else{
                                    $menus[$i]['children'][$s]['children'][$j]['menu_is_show'] = false;
                                }
                            }
                        }
                    }
                }
            }
            foreach ($list as $k => $v){
                if (!empty($v['role_rules'])){
//                    $list[$k]['menus'] = $menus;
                    $rules = explode(',',$v['role_rules']);
                    $mation = SystemMenu::menusMation($rules);
                    $name = [];
                    for ($z=0;$z<count($mation);$z++){
                        $name[] = $mation[$z]['menu_name'];
                    }
                    $list[$k]['menus'] = implode(',',$name);
//                    for ($a=0;$a<count($list[$k]['menus']);$a++){
//                        if (!in_array($list[$k]['menus'][$a]['menu_id'],$rules)){
//                            unset($list[$k]['menus'][$a]);
//                            continue;
//                        }else{
//                            for ($d=0;$d<count($list[$k]['menus'][$a]['children']);$d++){
//                                if (!in_array($list[$k]['menus'][$a]['children'][$d]['menu_id'],$rules)){
//                                    unset($list[$k]['menus'][$a]['children'][$d]);
//                                    continue;
//                                }else{
//                                    for ($f=0;$f<count($list[$k]['menus'][$a]['children'][$d]['children']);$f++){
//                                        if (!in_array($list[$k]['menus'][$a]['children'][$d]['children'][$f]['menu_id'],$rules)){
//                                            unset($list[$k]['menus'][$a]['children'][$d]['children'][$f]);
//                                            continue;
//                                        }
//                                    }
//                                }
//                            }
//                        }
//                    }
                }else{
                    $list[$k]['menus'] = '';
                }
            }
            //$list = array_merge($list);
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取所有角色
     * @return array
     * User: ZQ
     * Date: 2022/9/19
     */
    public static function getRoleAll()
    {
        return SystemRole::getRoleAll();
    }

    /**
     * Notes:修改角色
     * @param string $role_name
     * @param int $role_id
     * @param int $role_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateRole($role_id, $role_name, $role_rules, $role_info, $role_is_show)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['role_id'] = $role_id;
            $data = [];
            $data['role_name'] = $role_name;
            if (!empty($role_rules)){
                $data['role_rules'] = implode(',',$role_rules);
            }
            if (!empty($role_info)){
                $data['role_info'] = $role_info;
            }
            if (!empty($role_is_show)){
                $data['role_is_show'] = $role_is_show;
            }
            $result = SystemRole::where($where)->update($data);
            if ($result !== false){
                Redis::del('roles_' . $role_id);
                SystemRole::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除角色
     * @param int $role_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function delRole($role_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['role_id'] = $role_id;
            $data['role_status'] = 2;
            $result = SystemRole::where($where)->update($data);
            if (!empty($result)){
                Redis::del('roles_' . $role_id);
                SystemRole::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加角色
     * @param string $role_name
     * @param array $role_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function insertRole(string $role_name, $role_rules, $role_info, $role_is_show)
    {

        SystemRole::affairBegin();
        try {
            $data = [];
            $data['role_name'] = $role_name;
            if (!empty($role_rules)){
                $data['role_rules'] = implode(',',$role_rules);
            }else{
                $data['role_rules'] = '';
            }
            $data['role_is_show'] = $role_is_show;
            $data['role_info'] = $role_info;
            $result =  SystemRole::insertGetId($data);
            if (!empty($result)){
                SystemRole::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询角色
     * @param int $role_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function roleInfo($role_id)
    {
        $where = [];
        $where['role_id'] = $role_id;
        $result = SystemRole::where($where)->first();
        if (!empty($result)){
            $result['menu_is_menu'] = (string)$result['menu_is_menu'];
            if (!empty($result['role_rules'])){
                $result['role_rules'] = explode(',',$result['role_rules']);
            }else{
                $result['role_rules'] = [];
            }

        }
        return $result;
    }

    /**
     * Notes:修改角色
     * @param string $role_name
     * @param int $role_is_show
     * @return int
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function updateStatus($role_id, $role_is_show)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['role_id'] = $role_id;
            $data = [];
            $data['role_is_show'] = $role_is_show;
            $result = SystemRole::where($where)->update($data);
            if ($result !== false){
                SystemRole::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
